|
|
Subdivide(x,y,z,x1,y1,z1,x2,y2,z2,n,m)
pi = (x,y,z)
dvi = v1/n where v1 = (x1,y1,z1)
dvj = v2/m where v2 = (x2,y2,z2)
glBegin Quads
for (i=0 to n)
pj = pi
for (j=0 to m)
glVertex(pj)
glVertex(pj+dvj)
glVertex(pj+dvj+dvi)
glVertex(pj+dvi)
pj += dv2
pi += dv1
glEnd
Replace any quad with a call to 'subdivide'.
For example, if you had a groundplane quad that went from -200 to +200 in x and z, then to subdivide it into 20x20 mini-quads, call:
Subdivide(-200,0,-200, 400.0,0.0,0.0, 0.0,0.0,400.0, 20,20);Be careful with how you write the routine and call it so that you maintain the correct orientation of the quads (CW v. CCW).
// define top flat-shaded polygon, y = 1; all vertices get the same normal glNormal(0,1,0) for theta = 0 to 2*Pi by increments of -Pi/8 glVertex(cos(theta),1.0,sin(theta)) // define bottom flat-shaded polygon, y = -1; all vertices get the same normal glNormal(0,-1,0) for theta = 2*Pi to 2*Pi by increments of Pi/8 glVertex(cos(theta),-1.0,sin(theta)) // define side quadrilaterals // each vertex normal of a quadrilateral is vector from the origin to the center of the quad // alternate the color of each side panel color[0][4] = one color color[1][4] = the other color i = 0; for theta = 0 to 2*Pi by increments of Pi/8 assign color[i] i = 1-i glNormal3f(cos(theta+Pi/16), 0.0, sin(theta+Pi/16)) glVertex(cos(theta), 1.0, sin(theta)) glVertex(cos(theta+Pi/8), 1.0, sin(theta+Pi/8)) glVertex(cos(theta+Pi/8), -1.0, sin(theta+Pi/8)) glVertex(cos(theta), -1.0, sin(theta))
Because each vertex appears in 3 polygons, You can save a little computation time by precomputing all the vertices first, storying them into arrays, and then indexing into the arrays as you define the polygons. However, because this is one-time code (when it's put in the display list), the savings is not that significant.
The cylinder should be scaled, rotated, and positioned appropriately relative to the vehicle body to form the wheels.
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS,1,1);
glStencilOp(GL_KEEP,GL_ZERO,GL_REPLACE);
<draw base polygon>
glDisable(GL_DEPTH_TEST);
glStencilFunc(GL_EQUAL,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
<draw decal>
glEnable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
mode = GLUT_RGB | .... | GLUT_STENCIL;
..
glutInitDisplayMode(mode);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);