Homework 9, due Monday, Dec 8.

This is going to be a fairly easy one. All you need to do is put texture mapping into your renderer. Choose a texture image that you particularly like, such as a jpeg or gif file, and map it onto one of your z-buffered parametric meshes. You don't need to do any form of antialiasing yet (we'll get to that next week).

The basic idea is that you'll be adding two pieces of info to each vertex: the i and j coordinates of the texture image. When you linearly interpolate from vertices down to pixels, you will need to interpolate i and j in addition to r, g, b and pz.

To get the texture coordinates at each vertex, you should start with the parametric coordinates (u,v) on the surface at that vertex. Then first do a linear 2D transformation (ie: buil and apply a 3×3 matrix) to reposition your texture within the parametric space. This 2D transformation lets you translate, rotate and scale the texture image within the surface until the texture is just where you want it.

After that, do a texture viewport transform to convert your (u',v') to pixels within the texture source image. The purpose of the texture viewport transform is to scale up from the small values (between 0 and 1) of the (u',v') coordinates to the much larger texture pixel values of the (i,j) coordinates.

For now you can do the texture mapping by just doing a lookup at each pixel into an ImageBuffer object, and use the resulting r,g,b value to modulate surface color in your per-pixel Phong shader.

To load your texture image into your applet, you'll need to declare, within your HTML file, a parameter between the applet begin and end tags that points to the URL of your texture source image:

  <applet code=MyZbufferApplet.class width=500 height=500>
  <param name=texture value=http://www.nyu.edu/joe_student/cool_image.jpg>
  </applet>
In your java applet, you'll need to fetch this image and use it to create an ImageBuffer:
  import java.net.*;
  ...

  Image textureImage;
  try {
     textureImage = getImage(new URL(getParameter("texture")));
  } catch MalformedURLException e) { }
  ImageBuffer imageBuffer = new ImageBuffer(textureImage, this);
Then you can fetch individual packed pixel RGB values from your image buffer by:
  int rgb = imageBuffer.get(i,j);
which you can, not surprisingly, unpack into red, green and blue values:
  int red   = rgb >> 16 & 255;
  int green = rgb >>  8 & 255;
  int blue  = rgb       & 255;