1 | #include "Geometry.h"
|
---|
2 | #include "Triangle3.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 | #include "RenderState.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | #ifdef _CRT_SET
|
---|
8 | #define _CRTDBG_MAP_ALLOC
|
---|
9 | #include <stdlib.h>
|
---|
10 | #include <crtdbg.h>
|
---|
11 |
|
---|
12 | // redefine new operator
|
---|
13 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
14 | #define new DEBUG_NEW
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | namespace CHCDemoEngine
|
---|
20 | {
|
---|
21 |
|
---|
22 | Geometry::Geometry(Vector3 *vertices,
|
---|
23 | Vector3 *normals,
|
---|
24 | Texcoord2 *texcoords,
|
---|
25 | int numVertices,
|
---|
26 | bool delData,
|
---|
27 | Vector3 *tangents):
|
---|
28 | mVertices(vertices),
|
---|
29 | mNormals(normals),
|
---|
30 | mTexCoords(texcoords),
|
---|
31 | mTangents(tangents),
|
---|
32 | mNumVertices(numVertices),
|
---|
33 | mVboId(-1)
|
---|
34 | {
|
---|
35 | mHasTexture = (mTexCoords != NULL);
|
---|
36 | mHasTangents = (mTangents != NULL);
|
---|
37 |
|
---|
38 | Prepare();
|
---|
39 |
|
---|
40 | if (delData)
|
---|
41 | {
|
---|
42 | DEL_ARRAY_PTR(mVertices);
|
---|
43 | DEL_ARRAY_PTR(mNormals);
|
---|
44 | DEL_ARRAY_PTR(mTexCoords);
|
---|
45 | DEL_ARRAY_PTR(mTangents);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | Geometry::~Geometry()
|
---|
51 | {
|
---|
52 | DEL_ARRAY_PTR(mVertices);
|
---|
53 | DEL_ARRAY_PTR(mNormals);
|
---|
54 | DEL_ARRAY_PTR(mTexCoords);
|
---|
55 | DEL_ARRAY_PTR(mTangents);
|
---|
56 |
|
---|
57 | // delete vbo
|
---|
58 | glDeleteBuffersARB(1, &mVboId);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | void Geometry::Prepare()
|
---|
63 | {
|
---|
64 | CalcBoundingBox();
|
---|
65 |
|
---|
66 | int dataSize = mNumVertices * 6;
|
---|
67 |
|
---|
68 | if (mTexCoords) dataSize += mNumVertices * 2;
|
---|
69 | if (mTangents) dataSize += mNumVertices * 3;
|
---|
70 |
|
---|
71 | float *data = new float[dataSize];
|
---|
72 |
|
---|
73 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
74 | {
|
---|
75 | ((Vector3 *)data)[i] = mVertices[i];
|
---|
76 | ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
|
---|
77 | }
|
---|
78 |
|
---|
79 | float *currentPData = data + mNumVertices * 6;
|
---|
80 |
|
---|
81 | if (mTangents)
|
---|
82 | {
|
---|
83 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
84 | ((Vector3 *)data)[mNumVertices * 2 + i] = mTangents[i];
|
---|
85 |
|
---|
86 | for (int i = 0; i < 3; ++ i)
|
---|
87 | cout << ((Vector3 *)data)[mNumVertices * 2 + i] << " ";
|
---|
88 |
|
---|
89 | currentPData += mNumVertices * 3;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (mTexCoords)
|
---|
93 | {
|
---|
94 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
95 | ((Texcoord2 *)currentPData)[i] = mTexCoords[i];
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | glGenBuffersARB(1, &mVboId);
|
---|
100 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
101 |
|
---|
102 | int currentPVbo = 0;
|
---|
103 |
|
---|
104 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
105 |
|
---|
106 | currentPVbo += mNumVertices * sizeof(Vector3);
|
---|
107 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
108 |
|
---|
109 | currentPVbo += mNumVertices * 2 * sizeof(Vector3);
|
---|
110 |
|
---|
111 | if (mTangents)
|
---|
112 | {
|
---|
113 | glColorPointer(3, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
114 | currentPVbo += mNumVertices * sizeof(Vector3);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (mTexCoords)
|
---|
118 | {
|
---|
119 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
120 | }
|
---|
121 |
|
---|
122 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize * sizeof(float),
|
---|
123 | (float *)data, GL_STATIC_DRAW_ARB);
|
---|
124 |
|
---|
125 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
126 |
|
---|
127 | // data handled by graphics driver from now on
|
---|
128 | delete [] data;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void Geometry::Render(RenderState *state)
|
---|
133 | {
|
---|
134 | if (mHasTangents)
|
---|
135 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
136 |
|
---|
137 | if (state->GetCurrentVboId() != mVboId)
|
---|
138 | {
|
---|
139 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
140 |
|
---|
141 | int currentPointer;
|
---|
142 |
|
---|
143 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
144 |
|
---|
145 | if (mHasTangents)
|
---|
146 | {
|
---|
147 | glColorPointer(3, GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
148 | currentPointer = mNumVertices * 3 * sizeof(Vector3);
|
---|
149 | }
|
---|
150 | else
|
---|
151 | currentPointer = mNumVertices * 2 * sizeof(Vector3);
|
---|
152 |
|
---|
153 | if (mHasTexture)
|
---|
154 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPointer);
|
---|
155 |
|
---|
156 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
157 |
|
---|
158 | state->SetCurrentVboId(mVboId);
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | // don't render first degenerate index
|
---|
163 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
164 |
|
---|
165 | if (mHasTangents)
|
---|
166 | glDisableClientState(GL_COLOR_ARRAY);
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | void Geometry::CalcBoundingBox()
|
---|
171 | {
|
---|
172 | mBoundingBox.Initialize();
|
---|
173 |
|
---|
174 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
175 | {
|
---|
176 | mBoundingBox.Include(mVertices[i]);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
182 | {
|
---|
183 | return mBoundingBox;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | } |
---|