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