Example #1
0
void CEntity::OnDraw(CCamera *camera)
{
	glTranslatef(position.x, position.y, position.z);
	glRotatef(-direction, 0.0, 1.0, 0.0);
	glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
	glScalef(0.25, 0.25, 0.25);
		
	AnimateModel(stateStart, stateEnd, deltaT*animSpeed);
}
void RenderScene() 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The matrix

	gluLookAt(0, 35, 75,		0, 25, 0,			0, 1, 0);
	
	// We want the model to rotate around the axis so we give it a rotation
	// value, then increase/decrease it. You can rotate right of left with the arrow keys.

	glRotatef(g_RotateX, 0, 1.0f, 0);						// Rotate the object around the Y-Axis
	g_RotateX += g_RotationSpeed;							// Increase the speed of rotation


//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

	// Here we draw a grid to give us a better representation of the model and it's size
	Draw3DSGrid();								

	// Below, we need to increase the frame, but first we put a delay between
	// each increase so that it doesn't go super fast.  I believe the average frame speed 
	// is ~~ 16 milliseconds in 3D Studio Max.  You can change this to what ever you want.
	// When ever Delay() returns true it is time to increase to the next frame of animation.

	// Check if we can increase to the next frame of animation yet
	if(Delay(16))
	{
		// Increase the frame count but mod it by the total amount of frames
		g_3DModel.currentFrame = (g_3DModel.currentFrame) % (g_3DModel.numberOfFrames) + 1;
	}

	// * IMPORTANT *
	//
	// We do NOT want to animation frame 0 of the animation because it screws
	// up the animation sometimes.  If you have rotations in your model in frame 0
	// it will store that rotation in the animation, which messes it up because
	// when you save the model, the vertices already reflect that rotation.  SO, we just
	// ignore frame 0.  If you need frame 0 for some reason, refer to the Quick Notes
	// for more information on how to make it work (Hint, Reset The Transforms for the object).

	// Make sure we skip frame 0 of the animation
	if(g_3DModel.currentFrame == 0) g_3DModel.currentFrame = 1;

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

	
	// Since we know how many objects our model has, go through each of them.
	for(int i = 0; i < g_3DModel.numOfObjects; i++)
	{
		// Make sure we have valid objects just in case. (size() is in the vector class)
		if(g_3DModel.pObject.size() <= 0) break;

		// Get the current object that we are displaying
		t3DObject *pObject = &g_3DModel.pObject[i];


//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

		// Push on a new matrix for every object so we don't translate/rotate/scale for another
		glPushMatrix();

		// Animated the model with the scale, rotation and translation
		AnimateModel(&g_3DModel, pObject);

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////


		// Check to see if this object has a texture map, if so bind the texture to it.
		if(pObject->bHasTexture) {

			// Turn on texture mapping and turn off color
			glEnable(GL_TEXTURE_2D);

			// Reset the color to normal again
			glColor3ub(255, 255, 255);

			// Bind the texture map to the object by it's materialID
			glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
		} else {

			// Turn off texture mapping and turn on color
			glDisable(GL_TEXTURE_2D);

			// Reset the color to normal again
			glColor3ub(255, 255, 255);
		}

		// This determines if we are in wireframe or normal mode
		glBegin(g_ViewMode);					// Begin drawing with our selected mode (triangles or lines)

			// Go through all of the faces (polygons) of the object and draw them
			for(int j = 0; j < pObject->numOfFaces; j++)
			{
				// Go through each corner of the triangle and draw it.
				for(int whichVertex = 0; whichVertex < 3; whichVertex++)
				{
					// Get the index for each point of the face
					int index = pObject->pFaces[j].vertIndex[whichVertex];
			
					// Make sure the normals were calculated
					if(pObject->pNormals) 
					{
						// Give OpenGL the normal for this vertex.
						glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
					}
				
					// If the object has a texture associated with it, give it a texture coordinate.
					if(pObject->bHasTexture) {

						// Make sure there was a UVW map applied to the object or else it won't have tex coords.
						if(pObject->pTexVerts) {
							glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
						}
					} else {

						// Make sure there is a valid material/color assigned to this object.
						// You should always at least assign a material color to an object, 
						// but just in case we want to check the size of the material list.
						// if the size is at least one, and the material ID != -1,
						// then we have a valid material.
						if(g_3DModel.pMaterials.size() && pObject->materialID >= 0) 
						{
							// Get and set the color that the object is, since it must not have a texture
							BYTE *pColor = g_3DModel.pMaterials[pObject->materialID].color;

							// Assign the current color to this model
							glColor3ub(pColor[0], pColor[1], pColor[2]);
						}
					}

					// Make sure we have valid vertices
					if(pObject->pVerts) 
					{
						// Pass in the current vertex of the object (Corner of current face)
						glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
					}
				}
			}

		glEnd();								// End the drawing


//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

		// Pop the current matrix and start fresh for another object
		glPopMatrix();

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

	}

	SwapBuffers(g_hDC);									// Swap the backbuffers to the foreground
}