OBJECTIVE: This lab introduces you to more complex shading techniques, interactive control, and animation. Modify Lab 4 to include:
|
PPM files will start with either a 'P3' (having values in ASCII) or 'P6' (having values after the header lines in binary).
You don't need to write in a robust file parse - just write something that will read in the file that you're using. You need to read it into a byte array (e.g., char *data). For example, for an ASCII .ppm color file that contains no comment line,
open(file) read in an ASCII line ('P3') read in two ASCII values, xres and yres (resolution, e.g., 256, 256) read in another ASCII value (e.g. max value = 255) k = 0; for i=0 to yres for j=0 to xres Read three ASCII integers from file: r,g,b data[k++] = r; data[k++] = g; data[k++] = b; close(file)
glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,xres,yres,GL_RGB,GL_UNSIGNED_BYTE,data);
glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBindTexture(GL_TEXTURE_2D,texture1);
glTexCoord2d(0.0,0.0); glVertex3f(-100.0,0.0,-100.0); ...
glDisable(GL_TEXTURE_2D);
getModelCabPostion(&cx,&cy,&cz) getModelDirection(&dx,&dy,&dz) glLookAt(cx,cy,cz,cx+dx,cy+dy-0.05,cz+dz,0.0,1.0,0.0);This has the first person view looking down at the road a little bit.
The direction vector is just the sine and cosine of the y-axis rotation of the vehicle for the x and z coordinates with y=0. The cab position would be computed using the model's position in the scene, the dimensions of the vehicle, and possibly the direction vector to calculate the position for the cab (the location of the operator of the vehicle) in the scene.
In first person mode, the initial view direction in x and z is the direction the car is going. If you place the location of the view on top of the car, You might make the y direction a bit negative so the view sees some of the ground. In this mode, the left button mouse movement is to rotate this view direction left-right and up-down at reasonable rates.
k = 0; for i=0 to yres for j=0 to xres Read three ASCII integers from file: r,g,b data[k++] = r; data[k++] = g; data[k++] = b; data[k++] = ((r+g+b) > 600) ? 0 : 255; close(file)
gluBuild2DMipmaps(GL_TEXTURE_2D,4,xres,yres,GL_RGBA,GL_UNSIGNED_BYTE,data);
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, treeTexture ); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); ...draw quadrilateral with texture coordinates glDisable( GL_TEXTURE_2D ); glDisable(GL_BLEND);
GLubytehalftone[] = { 0xAA,0xAA,0xAA,0xAA,0x55,0x55,0x55,0x55, ...
glEnable(GL_POLYGON_STIPPLE); glPolygonStipple(halftone); <draw polygon> glDisable(GL_POLYGON_STIPPLE);
if (status == INFLIGHT) v' = v + (0.0,-9.8,0.0)*dt p = p + (1/2)*(v'+v)*dt v = v' draw cube at p if (py < 0) status = EXPLODING explode_i = 0 else if (status == EXPLODING) if (explode_i < explode_n) s = explode_i/explode_n draw exploding textured quad at p, facing car, scaled by s explode_i++ else status = NONEXISTENT
NOTES: