| 1 | #include "3dStructs.h" | 
|---|
| 2 | #include <GL/glut.h> | 
|---|
| 3 |  | 
|---|
| 4 | /* Debugging*/ | 
|---|
| 5 | #include <iostream.h> | 
|---|
| 6 |  | 
|---|
| 7 |  | 
|---|
| 8 | void C3dModel::Draw(int g_ViewMode) | 
|---|
| 9 | { | 
|---|
| 10 |    /* This function draws the models, currently not all functions  | 
|---|
| 11 |       supported */    | 
|---|
| 12 |  | 
|---|
| 13 |         for(int i = 0; i < numOfObjects; i++) | 
|---|
| 14 |         { | 
|---|
| 15 |                 // Make sure we have valid objects just in case. (size() is in the vector class) | 
|---|
| 16 |                 if(pObject.size() <= 0) break; | 
|---|
| 17 |  | 
|---|
| 18 |                 // Get the current object that we are displaying | 
|---|
| 19 |                          | 
|---|
| 20 |                 // Check to see if this object has a texture map, if so bind the texture to it. | 
|---|
| 21 |                 if(pObject[i].bHasTexture) { | 
|---|
| 22 |  | 
|---|
| 23 |                         // Turn on texture mapping and turn off color | 
|---|
| 24 |                         glEnable(GL_TEXTURE_2D); | 
|---|
| 25 |  | 
|---|
| 26 |                         // Reset the color to normal again | 
|---|
| 27 |                         glColor3ub(255, 255, 255); | 
|---|
| 28 |  | 
|---|
| 29 |                         // Bind the texture map to the object by it's materialID | 
|---|
| 30 |                         //glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]); | 
|---|
| 31 |                 } else { | 
|---|
| 32 |  | 
|---|
| 33 |                         // Turn off texture mapping and turn on color | 
|---|
| 34 |                         glDisable(GL_TEXTURE_2D); | 
|---|
| 35 |  | 
|---|
| 36 |                         // Reset the color to normal again | 
|---|
| 37 |                         glColor3ub(255, 255, 255); | 
|---|
| 38 |                 } | 
|---|
| 39 |  | 
|---|
| 40 |                 // This determines if we are in wireframe or normal mode | 
|---|
| 41 |                 glBegin(g_ViewMode);                                    // Begin drawing with our selected mode (triangles or lines) | 
|---|
| 42 |  | 
|---|
| 43 |                         // Go through all of the faces (polygons) of the object and draw them | 
|---|
| 44 |                         for(int j = 0; j < pObject[i].iNumOfFaces; j++) | 
|---|
| 45 |                         { | 
|---|
| 46 |                                 // Go through each corner of the triangle and draw it. | 
|---|
| 47 |                                 for(int whichVertex = 0; whichVertex < 3; whichVertex++) | 
|---|
| 48 |                                 { | 
|---|
| 49 |                                         // Get the index for each point of the face | 
|---|
| 50 |                                         int index = pObject[i].pFaces[j].vertIndex[whichVertex]; | 
|---|
| 51 |                          | 
|---|
| 52 |                                         // Give OpenGL the normal for this vertex. | 
|---|
| 53 |                                         glNormal3f(pObject[i].pNormals[ index ].x, pObject[i].pNormals[ index ].y, pObject[i].pNormals[ index ].z); | 
|---|
| 54 |                                  | 
|---|
| 55 |                                         // If the object has a texture associated with it, give it a texture coordinate. | 
|---|
| 56 |                                         if(pObject[i].bHasTexture) { | 
|---|
| 57 |  | 
|---|
| 58 |                                                 // Make sure there was a UVW map applied to the object or else it won't have tex coords. | 
|---|
| 59 |                                                 if(pObject[i].pTexVerts) { | 
|---|
| 60 |                                                         glTexCoord2f(pObject[i].pTexVerts[ index ].x, pObject[i].pTexVerts[ index ].y); | 
|---|
| 61 |                                                 } | 
|---|
| 62 |                                         } else { | 
|---|
| 63 |  | 
|---|
| 64 |                                                 // Make sure there is a valid material/color assigned to this object. | 
|---|
| 65 |                                                 // You should always at least assign a material color to an object,  | 
|---|
| 66 |                                                 // but just in case we want to check the size of the material list. | 
|---|
| 67 |                                                 // if the size is at least one, and the material ID != -1, | 
|---|
| 68 |                                                 // then we have a valid material. | 
|---|
| 69 |                                                 if(pMaterials.size() && pObject[i].materialID >= 0)  | 
|---|
| 70 |                                                 { | 
|---|
| 71 |                                                         // Get and set the color that the object is, since it must not have a texture | 
|---|
| 72 |                                                         char *pColor = pMaterials[pObject[i].materialID].color; | 
|---|
| 73 |  | 
|---|
| 74 |                                                         // Assign the current color to this model | 
|---|
| 75 |                                                         glColor3ub(pColor[0], pColor[1], pColor[2]); | 
|---|
| 76 |                                                 } | 
|---|
| 77 |                                         } | 
|---|
| 78 |  | 
|---|
| 79 |                                         // Pass in the current vertex of the object (Corner of current face) | 
|---|
| 80 |                                         glVertex3f(pObject[i].pVerts[ index ].x, pObject[i].pVerts[ index ].y, pObject[i].pVerts[ index ].z); | 
|---|
| 81 |                                 } | 
|---|
| 82 |                         } | 
|---|
| 83 |  | 
|---|
| 84 |                 glEnd();                                                                // End the drawing | 
|---|
| 85 |         } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | /* Just a debugging function */ | 
|---|
| 89 | void C3dModel::PrintProperties() | 
|---|
| 90 | { | 
|---|
| 91 |    printf( "Number of objects: %i\n", numOfObjects); | 
|---|
| 92 |    printf( "Number of materials: %i\n", numOfMaterials); | 
|---|
| 93 |    for( int i; i < numOfObjects; i++ ) | 
|---|
| 94 |    { | 
|---|
| 95 |       printf( "Object Number %i:\n", i );    | 
|---|
| 96 |       printf( "Number of Vertices: %i\n", pObject[i].iNumOfVerts); | 
|---|
| 97 |       printf( "Number of Faces: %i\n", pObject[i].iNumOfFaces); | 
|---|
| 98 |       printf( "Number of Texture Materials: %i\n", pObject[i].iNumTexVertex); | 
|---|
| 99 |       printf( "Has a texture: %i\n", pObject[i].bHasTexture);    | 
|---|
| 100 |    } | 
|---|
| 101 | } | 
|---|