Homework 9, due Wednesday, Dec 10.

For this assignment, you need to 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 do not need to do any form of antialiasing for this assignment.

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.

You then need to transform that (u,v) position to a location within your texture image. You do this via a texture viewport transform. This is very similar to the viewport transform you're already familiar with. 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;