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 | {
|
---|
85 | Vector3 tangent = Normalize(mTangents[i]);
|
---|
86 | Vector3 normal = Normalize(mNormals[i]);
|
---|
87 |
|
---|
88 | float dotProd = DotProd(tangent,normal);
|
---|
89 |
|
---|
90 | tangent -= normal * dotProd;
|
---|
91 | tangent = Normalize(tangent);
|
---|
92 |
|
---|
93 | //mTangents[i] = tangent;
|
---|
94 | mTangents[i] = Vector3(0,1,0);//tangent * 0.5f + Vector3(0.5f);
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
98 | ((Vector3 *)data)[mNumVertices * 2 + i] = mTangents[i];
|
---|
99 |
|
---|
100 | for (int i = 0; i < 3; ++ i)
|
---|
101 | cout << ((Vector3 *)data)[mNumVertices * 2 + i] << " ";
|
---|
102 |
|
---|
103 | currentPData += mNumVertices * 3;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (mTexCoords)
|
---|
107 | {
|
---|
108 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
109 | ((Texcoord2 *)currentPData)[i] = mTexCoords[i];
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | glGenBuffersARB(1, &mVboId);
|
---|
114 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
115 |
|
---|
116 | int currentPVbo = 0;
|
---|
117 |
|
---|
118 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
119 |
|
---|
120 | currentPVbo += mNumVertices * sizeof(Vector3);
|
---|
121 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
122 |
|
---|
123 | currentPVbo += mNumVertices * 2 * sizeof(Vector3);
|
---|
124 |
|
---|
125 | if (mTangents)
|
---|
126 | {
|
---|
127 | cout << "dotprods: " << endl;
|
---|
128 |
|
---|
129 | for (int i = 0; i < 1000; ++ i)
|
---|
130 | {
|
---|
131 | //cout << mTangents[i] << " " << mNormals[i] << " " << DotProd(Normalize(mTangents[i]), Normalize(mNormals[i])) << endl;
|
---|
132 | float d = fabs(DotProd(Normalize(mTangents[i]), Normalize(mNormals[i])));
|
---|
133 | if (d > 0.01) cout << d << " ";
|
---|
134 | //cout << d << " ";
|
---|
135 | }
|
---|
136 | cout << endl;
|
---|
137 |
|
---|
138 | glColorPointer(3, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
139 | currentPVbo += mNumVertices * sizeof(Vector3);
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (mTexCoords)
|
---|
143 | {
|
---|
144 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
145 | }
|
---|
146 |
|
---|
147 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize * sizeof(float),
|
---|
148 | (float *)data, GL_STATIC_DRAW_ARB);
|
---|
149 |
|
---|
150 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
151 |
|
---|
152 | // data handled by graphics driver from now on
|
---|
153 | delete [] data;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void Geometry::Render(RenderState *state)
|
---|
158 | {
|
---|
159 | if (mHasTangents)
|
---|
160 | {
|
---|
161 | cout<<"x";
|
---|
162 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
163 | }
|
---|
164 | if (state->GetCurrentVboId() != mVboId)
|
---|
165 | {
|
---|
166 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
167 |
|
---|
168 | int currentPointer;
|
---|
169 |
|
---|
170 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
171 |
|
---|
172 | if (mHasTangents)
|
---|
173 | {
|
---|
174 | //glClientActiveTextureARB(GL_TEXTURE1_ARB);
|
---|
175 | //glTexCoordPointer(2, GL_FLOAT, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
176 |
|
---|
177 | glColorPointer(3, GL_FLOAT, 0, (char *)NULL + mNumVertices * 2 * sizeof(Vector3));
|
---|
178 | currentPointer = mNumVertices * 3 * sizeof(Vector3);
|
---|
179 | //glClientActiveTextureARB(GL_TEXTURE0_ARB);
|
---|
180 |
|
---|
181 | }
|
---|
182 | else
|
---|
183 | currentPointer = mNumVertices * 2 * sizeof(Vector3);
|
---|
184 |
|
---|
185 | if (mHasTexture)
|
---|
186 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPointer);
|
---|
187 |
|
---|
188 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
189 |
|
---|
190 | state->SetCurrentVboId(mVboId);
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 | // don't render first degenerate index
|
---|
195 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
196 |
|
---|
197 | if (mHasTangents)
|
---|
198 | glDisableClientState(GL_COLOR_ARRAY);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void Geometry::CalcBoundingBox()
|
---|
203 | {
|
---|
204 | mBoundingBox.Initialize();
|
---|
205 |
|
---|
206 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
207 | {
|
---|
208 | mBoundingBox.Include(mVertices[i]);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
214 | {
|
---|
215 | return mBoundingBox;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | } |
---|