1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
/*
* TriangleMesh.h
* OpenGL SuperBible
*
* Copyright 2007 Richard S. Wright Jr.. All rights reserved.
* This class allows you to simply add triangles as if this class were a
* container. The AddTriangle() function searches the current list of triangles
* and determines if the vertex/normal/texcoord is a duplicate. If so, it addes
* an entry to the index array instead of the list of vertices.
* When finished, call EndMesh() to free up extra unneeded memory that is reserved
* as workspace when you call BeginMesh().
*
* This class can easily be extended to contain other vertex attributes, and to
* save itself and load itself from disk (thus forming the beginnings of a custom
* model file format).
*/
#include "gltools.h"
#include "math3d.h"
class CTriangleMesh
{
public:
CTriangleMesh(void);
~CTriangleMesh(void);
// Use these three functions to add triangles
void BeginMesh(GLuint nMaxVerts);
void AddTriangle(M3DVector3f verts[3], M3DVector3f vNorms[3], M3DVector2f vTexCoords[3]);
void EndMesh(void);
// Useful for statistics
inline GLuint GetIndexCount(void) { return nNumIndexes; }
inline GLuint GetVertexCount(void) { return nNumVerts; }
// In place scale of the vertices
void Scale(GLfloat fScaleValue) {
for(int i = 0; i < nNumVerts; i++)
m3dScaleVector3(pVerts[i], fScaleValue);
}
// Draw - make sure you call glEnableClientState for these arrays
void Draw(void) {
// Here's where the data is now
glVertexPointer(3, GL_FLOAT,0, pVerts);
glNormalPointer(GL_FLOAT, 0, pNorms);
glTexCoordPointer(2, GL_FLOAT, 0, pTexCoords);
// Draw them
glDrawElements(GL_TRIANGLES, nNumIndexes, GL_UNSIGNED_SHORT, pIndexes);
}
protected:
GLushort *pIndexes; // Array of indexes
M3DVector3f *pVerts; // Array of vertices
M3DVector3f *pNorms; // Array of normals
M3DVector2f *pTexCoords; // Array of texture coordinates
GLuint nMaxIndexes; // Maximum workspace
GLuint nNumIndexes; // Number of indexes currently used
GLuint nNumVerts; //
};
|