1 | #include "Geometry.h"
|
---|
2 | #include "Triangle3.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 | #include "RenderState.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 | Geometry::Geometry(Vector3 *vertices,
|
---|
11 | Vector3 *normals,
|
---|
12 | float *texcoords,
|
---|
13 | int numVertices,
|
---|
14 | bool delData):
|
---|
15 | mVertices(vertices),
|
---|
16 | mNormals(normals),
|
---|
17 | mTexCoords(texcoords),
|
---|
18 | mNumVertices(numVertices),
|
---|
19 | mVboId(-1)
|
---|
20 | {
|
---|
21 | Prepare();
|
---|
22 |
|
---|
23 | if (delData)
|
---|
24 | {
|
---|
25 | delete [] mVertices; mVertices = NULL;
|
---|
26 | delete [] mNormals; mNormals = NULL;
|
---|
27 | if (!mTexCoords) delete [] mTexCoords; mTexCoords = NULL;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | Geometry::~Geometry()
|
---|
32 | {
|
---|
33 | if (!mVertices) delete [] mVertices;
|
---|
34 | if (!mNormals) delete [] mNormals;
|
---|
35 | if (!mTexCoords) delete [] mTexCoords;
|
---|
36 |
|
---|
37 | // delete vbo
|
---|
38 | glDeleteBuffersARB(1, &mVboId);
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | void Geometry::Prepare()
|
---|
43 | {
|
---|
44 | CalcBoundingBox();
|
---|
45 |
|
---|
46 | int dataSize = mNumVertices * 6;
|
---|
47 |
|
---|
48 | if (mTexCoords)
|
---|
49 | dataSize += mNumVertices * 2;
|
---|
50 |
|
---|
51 | float *data = new float[dataSize];
|
---|
52 |
|
---|
53 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
54 | {
|
---|
55 | ((Vector3 *)data)[i] = mVertices[i];
|
---|
56 | ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (mTexCoords)
|
---|
60 | {
|
---|
61 | for (int i = 0; i < mNumVertices * 2; ++ i)
|
---|
62 | data[mNumVertices * 6 + i] = mTexCoords[i];
|
---|
63 | }
|
---|
64 |
|
---|
65 | glGenBuffersARB(1, &mVboId);
|
---|
66 |
|
---|
67 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
68 |
|
---|
69 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
70 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
71 |
|
---|
72 | if (mTexCoords)
|
---|
73 | {
|
---|
74 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
|
---|
75 | }
|
---|
76 | glBufferDataARB(GL_ARRAY_BUFFER_ARB,
|
---|
77 | dataSize * sizeof(float),
|
---|
78 | (float *)data,
|
---|
79 | GL_STATIC_DRAW_ARB);
|
---|
80 |
|
---|
81 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
82 |
|
---|
83 | // data handled by graphics driver from now on
|
---|
84 | delete [] data;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void Geometry::Render(RenderState *state)
|
---|
89 | {
|
---|
90 | if (state->GetCurrentVboId() != mVboId)
|
---|
91 | {
|
---|
92 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
93 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
94 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
95 |
|
---|
96 | if (mTexCoords)
|
---|
97 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
|
---|
98 |
|
---|
99 | state->SetCurrentVboId(mVboId);
|
---|
100 | }
|
---|
101 |
|
---|
102 | // don't render first degenerate index
|
---|
103 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | void Geometry::CalcBoundingBox()
|
---|
108 | {
|
---|
109 | mBoundingBox.Initialize();
|
---|
110 |
|
---|
111 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
112 | {
|
---|
113 | mBoundingBox.Include(mVertices[i]);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
119 | {
|
---|
120 | return mBoundingBox;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | } |
---|