#include "3dStructs.h" #include /* Debugging*/ #include void C3dModel::Draw(int g_ViewMode) { /* This function draws the models, currently not all functions supported */ for(int i = 0; i < numOfObjects; i++) { // Make sure we have valid objects just in case. (size() is in the vector class) if(pObject.size() <= 0) break; // Get the current object that we are displaying // Check to see if this object has a texture map, if so bind the texture to it. if(pObject[i].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[i].iNumOfFaces; 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[i].pFaces[j].vertIndex[whichVertex]; // Give OpenGL the normal for this vertex. glNormal3f(pObject[i].pNormals[ index ].x, pObject[i].pNormals[ index ].y, pObject[i].pNormals[ index ].z); // If the object has a texture associated with it, give it a texture coordinate. if(pObject[i].bHasTexture) { // Make sure there was a UVW map applied to the object or else it won't have tex coords. if(pObject[i].pTexVerts) { glTexCoord2f(pObject[i].pTexVerts[ index ].x, pObject[i].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(pMaterials.size() && pObject[i].materialID >= 0) { // Get and set the color that the object is, since it must not have a texture char *pColor = pMaterials[pObject[i].materialID].color; // Assign the current color to this model glColor3ub(pColor[0], pColor[1], pColor[2]); } } // Pass in the current vertex of the object (Corner of current face) glVertex3f(pObject[i].pVerts[ index ].x, pObject[i].pVerts[ index ].y, pObject[i].pVerts[ index ].z); } } glEnd(); // End the drawing } } /* Just a debugging function */ void C3dModel::PrintProperties() { printf( "Number of objects: %i\n", numOfObjects); printf( "Number of materials: %i\n", numOfMaterials); for( int i; i < numOfObjects; i++ ) { printf( "Object Number %i:\n", i ); printf( "Number of Vertices: %i\n", pObject[i].iNumOfVerts); printf( "Number of Faces: %i\n", pObject[i].iNumOfFaces); printf( "Number of Texture Materials: %i\n", pObject[i].iNumTexVertex); printf( "Has a texture: %i\n", pObject[i].bHasTexture); } }