Game Development Reference
In-Depth Information
Gl.glColor3d(0.0, 0.0, 1.0);
Gl.glVertex3d(0, 0.5, 0);
}
Gl.glEnd();
Running this code will produce a triangle with a red, green, and blue corner.
Along the surface of the triangle, the colors mix and fade into each other.
By default, OpenGL will interpolate colors between vertices. In practice, this
means if you give each vertex of a triangle a different color, then each pixel of
the rendered triangle will be colored according to the distance from each of the
vertices. Basic lighting systems get the lighting information for each vertex and
then use this interpolation to decide how the surface should be shaded.
All that remains now is to spin the triangle. There are two ways to spin the tri-
angle: move all the vertices or move the entire scene. It's simpler to move the
entire scene as OpenGL has a helpful function for rotation.
Gl.glRotated(10 * elapsedTime, 0, 1, 0);
Gl.glBegin(Gl.GL_TRIANGLE_STRIP);
{
Gl.glColor4d(1.0, 0.0, 0.0, 0.5);
Gl.glVertex3d(-0.5, 0, 0);
Gl.glColor3d(0.0, 1.0, 0.0);
Gl.glVertex3d(0.5, 0, 0);
Gl.glColor3d(0.0, 0.0, 1.0);
Gl.glVertex3d(0, 0.5, 0);
}
Gl.glEnd();
There are a few subtle points to touch on here. glRotate takes four arguments.
The first argument is the angle to rotate in degrees and the last three are the axis
to rotate around. The rotation is cumulative, which means if glRotated
was called twice, those rotations will both be applied—rotating the object twice
as much. In our case, the update call is being called as often as possible so
the degree of rotation continually increases, which appears to make the triangle
rotate. The rotate function automatically wraps at 360 so a rotation of 361 is
equivalent to a rotation to 1.
The next three arguments describe the axis. These three arguments are actually a
normalized vector. Vectors will be described later in the topic. For now, just
 
Search WWH ::




Custom Search