1 | #include "Geometry.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "stdio.h"
|
---|
4 | #include "teapot.h"
|
---|
5 |
|
---|
6 | #include <math.h>
|
---|
7 |
|
---|
8 | #define NO_LIST -1
|
---|
9 | #define STRIP_END -1
|
---|
10 |
|
---|
11 | int Geometry::num_torus_indices;
|
---|
12 | int Geometry::num_torus_vertices;
|
---|
13 | int Geometry::num_torus_normals;
|
---|
14 |
|
---|
15 | const int Geometry::torus_precision = 24;
|
---|
16 | const int Geometry::sphere_precision = 24;
|
---|
17 |
|
---|
18 | float *Geometry::torus_vertices;
|
---|
19 | float *Geometry::torus_normals;
|
---|
20 | int *Geometry::torus_indices;
|
---|
21 |
|
---|
22 | // choose size so the glut-rendered sphere is approximately
|
---|
23 | // as big as the other objects
|
---|
24 | const float Geometry::sphere_radius = 1.0 / 9.0f;
|
---|
25 | const float Geometry::torus_inner_radius = 0.05;
|
---|
26 | const float Geometry::torus_outer_radius = 0.07;
|
---|
27 |
|
---|
28 | int Geometry::sDisplayList[NUM_OBJECTS];
|
---|
29 |
|
---|
30 | bool Geometry::sIsInitialised = Init();
|
---|
31 |
|
---|
32 | Geometry::Geometry():
|
---|
33 | mXRotation(0), mYRotation(0), mZRotation(0), mScale(1.0), mObjectType(TEAPOT)
|
---|
34 | {
|
---|
35 | copyVector3Values(mTranslation, 0, 0, 0);
|
---|
36 |
|
---|
37 | SetAmbientColor(0.1745, 0.01175, 0.01175);
|
---|
38 | SetDiffuseColor(0.61424, 0.04136, 0.04136);
|
---|
39 | SetSpecularColor(0.727811, 0.626959, 0.626959);
|
---|
40 |
|
---|
41 | CalcBoundingVolume();
|
---|
42 | CalcTransform();
|
---|
43 | GenerateList();
|
---|
44 | }
|
---|
45 |
|
---|
46 | Geometry::Geometry(Vector3 translation, float xRot, float yRot, float zRot, float scale, int objectType):
|
---|
47 | mXRotation(xRot), mYRotation(yRot), mZRotation(zRot), mScale(scale), mObjectType(objectType)
|
---|
48 | {
|
---|
49 | copyVector3(mTranslation, translation);
|
---|
50 |
|
---|
51 | SetAmbientColor(0.1745, 0.01175, 0.01175);
|
---|
52 | SetDiffuseColor(0.61424, 0.04136, 0.04136);
|
---|
53 | SetSpecularColor(0.727811, 0.626959, 0.626959);
|
---|
54 |
|
---|
55 | CalcBoundingVolume();
|
---|
56 | CalcTransform();
|
---|
57 | GenerateList();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Geometry::ResetLists()
|
---|
61 | {
|
---|
62 | for(int i = 0; i < NUM_OBJECTS; i++)
|
---|
63 | {
|
---|
64 | if(sDisplayList[i] == NO_LIST)
|
---|
65 | glDeleteLists(sDisplayList[i], 1);
|
---|
66 |
|
---|
67 | sDisplayList[i] = NO_LIST;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void Geometry::CleanUp()
|
---|
72 | {
|
---|
73 | ResetLists();
|
---|
74 |
|
---|
75 | delete [] torus_vertices;
|
---|
76 | delete [] torus_normals;
|
---|
77 | delete [] torus_indices;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | bool Geometry::Init()
|
---|
82 | {
|
---|
83 | for(int i=0; i < NUM_OBJECTS; i++)
|
---|
84 | sDisplayList[i] = NO_LIST;
|
---|
85 |
|
---|
86 | // parameters chosen so torus is approximately as big as teapot
|
---|
87 | CreateTorus(torus_inner_radius, torus_outer_radius, torus_precision);
|
---|
88 |
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void Geometry::GenerateList()
|
---|
93 | {
|
---|
94 | if(sDisplayList[mObjectType] == NO_LIST)
|
---|
95 | {
|
---|
96 | sDisplayList[mObjectType] = glGenLists(1);
|
---|
97 | glNewList(sDisplayList[mObjectType], GL_COMPILE);
|
---|
98 |
|
---|
99 | switch(mObjectType)
|
---|
100 | {
|
---|
101 | case TEAPOT:
|
---|
102 | RenderTeapot();
|
---|
103 | break;
|
---|
104 | case TORUS:
|
---|
105 | RenderTorus();
|
---|
106 | break;
|
---|
107 | case SPHERE:
|
---|
108 | RenderSphere();
|
---|
109 | break;
|
---|
110 |
|
---|
111 | default:
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | glEndList();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Geometry::Render()
|
---|
119 | {
|
---|
120 | glMaterialfv(GL_FRONT, GL_AMBIENT, mAmbientColor);
|
---|
121 | glMaterialfv(GL_FRONT, GL_DIFFUSE, mDiffuseColor);
|
---|
122 | glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecularColor);
|
---|
123 |
|
---|
124 | glPushMatrix();
|
---|
125 |
|
---|
126 | Transform();
|
---|
127 |
|
---|
128 | glCallList(sDisplayList[mObjectType]);
|
---|
129 |
|
---|
130 | glPopMatrix();
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | //! sets rotations around the three axis: executed in the order specified here
|
---|
135 | void Geometry::SetRotations(float xRot, float yRot, float zRot)
|
---|
136 | {
|
---|
137 | mXRotation = xRot;
|
---|
138 | mYRotation = yRot;
|
---|
139 | mZRotation = zRot;
|
---|
140 |
|
---|
141 | CalcBoundingVolume();
|
---|
142 | CalcTransform();
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | void Geometry::SetTranslation(Vector3 translation)
|
---|
147 | {
|
---|
148 | copyVector3(mTranslation, translation);
|
---|
149 |
|
---|
150 | CalcBoundingVolume();
|
---|
151 | CalcTransform();
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void Geometry::SetScale(float scale)
|
---|
156 | {
|
---|
157 | mScale = scale;
|
---|
158 | CalcTransform();
|
---|
159 |
|
---|
160 | CalcBoundingVolume();
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | void Geometry::SetAmbientColor(float ambientR, float ambientG, float ambientB)
|
---|
165 | {
|
---|
166 | mAmbientColor[0] = ambientR;
|
---|
167 | mAmbientColor[1] = ambientG;
|
---|
168 | mAmbientColor[2] = ambientB;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | void Geometry::SetDiffuseColor(float diffuseR, float diffuseG, float diffuseB)
|
---|
173 | {
|
---|
174 | mDiffuseColor[0] = diffuseR;
|
---|
175 | mDiffuseColor[1] = diffuseG;
|
---|
176 | mDiffuseColor[2] = diffuseB;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void Geometry::SetSpecularColor(float specularR, float specularG, float specularB)
|
---|
181 | {
|
---|
182 | mSpecularColor[0] = specularR;
|
---|
183 | mSpecularColor[1] = specularG;
|
---|
184 | mSpecularColor[2] = specularB;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | float Geometry::GetScale()
|
---|
189 | {
|
---|
190 | return mScale;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | void Geometry::GetTranslation(Vector3 translation)
|
---|
195 | {
|
---|
196 | copyVector3(translation, mTranslation);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | void Geometry::GetRotations(float &xRot, float &yRot, float &zRot)
|
---|
201 | {
|
---|
202 | xRot = mXRotation;
|
---|
203 | yRot = mYRotation;
|
---|
204 | zRot = mZRotation;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | const AABox &Geometry::GetBoundingVolume()
|
---|
209 | {
|
---|
210 | return mBoundingBox;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | void Geometry::SetLastVisited(int lastVisited)
|
---|
215 | {
|
---|
216 | mLastVisited = lastVisited;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | int Geometry::GetLastVisited()
|
---|
221 | {
|
---|
222 | return mLastVisited;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | void Geometry::SetObjectType(int type)
|
---|
227 | {
|
---|
228 | mObjectType = type;
|
---|
229 | GenerateList();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | void Geometry::RenderTeapot()
|
---|
234 | {
|
---|
235 | int i = 0;
|
---|
236 |
|
---|
237 | while(i < num_teapot_indices)
|
---|
238 | {
|
---|
239 | glBegin(GL_TRIANGLE_STRIP);
|
---|
240 | while(teapot_indices[i] != STRIP_END)
|
---|
241 | {
|
---|
242 | int index = teapot_indices[i] * 3;
|
---|
243 |
|
---|
244 | glNormal3fv(teapot_normals + index);
|
---|
245 | glVertex3fv(teapot_vertices + index);
|
---|
246 |
|
---|
247 | i++;
|
---|
248 | }
|
---|
249 | glEnd();
|
---|
250 |
|
---|
251 | i++; // skip strip end flag
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | void Geometry::CalcBoundingVolume()
|
---|
256 | {
|
---|
257 | switch(mObjectType)
|
---|
258 | {
|
---|
259 | case TORUS:
|
---|
260 | CalcBoundingVolume(torus_vertices, num_torus_vertices);
|
---|
261 | break;
|
---|
262 | case SPHERE:
|
---|
263 | CalcSphereBoundingVolume();
|
---|
264 | break;
|
---|
265 | case TEAPOT:
|
---|
266 | CalcBoundingVolume(teapot_vertices, num_teapot_vertices);
|
---|
267 | break;
|
---|
268 | default:
|
---|
269 | break;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | void Geometry::CalcBoundingVolume(float *vertices, const int num_vertices)
|
---|
275 | {
|
---|
276 | Vector3 *rotatedPoints = new Vector3[num_vertices];
|
---|
277 |
|
---|
278 | for(int i = 0; i < num_vertices; i++)
|
---|
279 | {
|
---|
280 | copyVector3Values(rotatedPoints[i], vertices[i * 3],
|
---|
281 | vertices[i * 3 + 1], vertices[i * 3 + 2]);
|
---|
282 |
|
---|
283 | rotateVectorZ(rotatedPoints[i], mZRotation * PI_180);
|
---|
284 | rotateVectorY(rotatedPoints[i], mYRotation * PI_180);
|
---|
285 | rotateVectorX(rotatedPoints[i], mXRotation * PI_180);
|
---|
286 | }
|
---|
287 |
|
---|
288 | calcCubicHull(mBoundingBox.min, mBoundingBox.max, rotatedPoints, num_vertices);
|
---|
289 |
|
---|
290 | for(int i=0; i< 3; i++)
|
---|
291 | {
|
---|
292 | mBoundingBox.min[i] *= mScale;
|
---|
293 | mBoundingBox.max[i] *= mScale;
|
---|
294 | }
|
---|
295 |
|
---|
296 | addVector3(mBoundingBox.min, mTranslation, mBoundingBox.min);
|
---|
297 | addVector3(mBoundingBox.max, mTranslation, mBoundingBox.max);
|
---|
298 |
|
---|
299 | delete [] rotatedPoints;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | void Geometry::CalcSphereBoundingVolume()
|
---|
304 | {
|
---|
305 | float len = mScale * sphere_radius;
|
---|
306 | Vector3 size = {len, len, len};
|
---|
307 |
|
---|
308 | diffVector3(mBoundingBox.min, mTranslation, size);
|
---|
309 | addVector3(mBoundingBox.max, mTranslation, size);
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | void Geometry::RenderTorus()
|
---|
314 | {
|
---|
315 | glVertexPointer(3, GL_FLOAT, sizeof(float), torus_vertices);
|
---|
316 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
317 |
|
---|
318 | glNormalPointer(GL_FLOAT, sizeof(float), torus_normals);
|
---|
319 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
320 |
|
---|
321 | glDrawElements(GL_TRIANGLES, num_torus_indices, GL_UNSIGNED_INT, torus_indices);
|
---|
322 |
|
---|
323 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
324 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*
|
---|
328 | creates a torus with specified radii, with number of rings and ring
|
---|
329 | subdivision = precision.
|
---|
330 | */
|
---|
331 | void Geometry::CreateTorus(float innerRadius, float outerRadius, int precision)
|
---|
332 | {
|
---|
333 | num_torus_vertices = num_torus_normals = (precision + 1) * (precision + 1);
|
---|
334 | num_torus_indices = 2 * precision * precision * 3;
|
---|
335 |
|
---|
336 | torus_vertices = new float[num_torus_vertices * 3];
|
---|
337 | torus_normals = new float[num_torus_normals * 3];
|
---|
338 | torus_indices = new int[num_torus_indices];
|
---|
339 |
|
---|
340 | for(int i = 0; i < precision + 1; ++ i)
|
---|
341 | {
|
---|
342 | int index = i * 3;
|
---|
343 |
|
---|
344 | Vector3 currentVertex = {innerRadius, 0.0f, 0.0f};
|
---|
345 | rotateVectorZ(currentVertex, ((float)i * 2.0 * PI) / (float)precision);
|
---|
346 |
|
---|
347 | currentVertex[0] += outerRadius;
|
---|
348 |
|
---|
349 | Vector3 sTangent = {0.0f, 0.0f, -1.0f};
|
---|
350 | Vector3 tTangent = {0, -1, 0};
|
---|
351 | rotateVectorZ(tTangent, ((float)i * 2.0 * PI) / (float)precision);
|
---|
352 |
|
---|
353 | Vector3 currentNormal;
|
---|
354 |
|
---|
355 | cross(currentNormal, tTangent, sTangent);
|
---|
356 |
|
---|
357 | torus_normals[index + 0] = currentNormal[0];
|
---|
358 | torus_normals[index + 1] = currentNormal[1];
|
---|
359 | torus_normals[index + 2] = currentNormal[2];
|
---|
360 |
|
---|
361 | torus_vertices[index + 0] = currentVertex[0];
|
---|
362 | torus_vertices[index + 1] = currentVertex[1];
|
---|
363 | torus_vertices[index + 2] = currentVertex[2];
|
---|
364 | }
|
---|
365 |
|
---|
366 | for(int i_rng = 1; i_rng < precision + 1; ++ i_rng)
|
---|
367 | {
|
---|
368 | for(int i=0; i<precision+1; ++i)
|
---|
369 | {
|
---|
370 | int index = 3 * (i_rng * (precision + 1) + i);
|
---|
371 |
|
---|
372 | Vector3 currentVertex = {torus_vertices[i*3], torus_vertices[i*3+1], torus_vertices[i*3+2]};
|
---|
373 |
|
---|
374 | rotateVectorY(currentVertex, ((float)i_rng * 2.0 * PI) / (float)precision);
|
---|
375 |
|
---|
376 | Vector3 currentNormal = {torus_normals[i*3], torus_normals[i*3+1], torus_normals[i*3+2]};
|
---|
377 |
|
---|
378 | rotateVectorY(currentNormal, ((float)i_rng * 2.0 * PI) / (float)precision);
|
---|
379 |
|
---|
380 | torus_normals[index + 0] = currentNormal[0];
|
---|
381 | torus_normals[index + 1] = currentNormal[1];
|
---|
382 | torus_normals[index + 2] = currentNormal[2];
|
---|
383 |
|
---|
384 | torus_vertices[index + 0] = currentVertex[0];
|
---|
385 | torus_vertices[index + 1] = currentVertex[1];
|
---|
386 | torus_vertices[index + 2] = currentVertex[2];
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | for(int i_rng = 0; i_rng < precision; ++ i_rng)
|
---|
391 | {
|
---|
392 | for(int i = 0; i < precision; ++ i)
|
---|
393 | {
|
---|
394 | int index = ((i_rng * precision + i) * 2) * 3;
|
---|
395 |
|
---|
396 | torus_indices[index+0] = (i_rng * (precision+1) + i)*3;
|
---|
397 | torus_indices[index+1] = ((i_rng+1) * (precision+1) + i)*3;
|
---|
398 | torus_indices[index+2] = (i_rng * (precision+1) + i + 1)*3;
|
---|
399 |
|
---|
400 | index = ((i_rng * precision + i) * 2 + 1) * 3;
|
---|
401 |
|
---|
402 | torus_indices[index+0] = (i_rng * (precision+1) + i + 1)*3;
|
---|
403 | torus_indices[index+1] = ((i_rng+1) * (precision+1) + i)*3;
|
---|
404 | torus_indices[index+2] = ((i_rng+1) * (precision+1) + i + 1)*3;
|
---|
405 | }
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | counts the triangles of the teapot.
|
---|
411 | traverses through all the triangle strips.
|
---|
412 | */
|
---|
413 | int Geometry::CountTeapotTriangles()
|
---|
414 | {
|
---|
415 | int result = 0;
|
---|
416 | int i=0;
|
---|
417 | // n - 2 triangles are drawn for a strip
|
---|
418 | while(i < num_teapot_indices)
|
---|
419 | {
|
---|
420 | while(teapot_indices[i] != STRIP_END)
|
---|
421 | {
|
---|
422 | result ++;;
|
---|
423 | i++;
|
---|
424 | }
|
---|
425 | result -= 2;
|
---|
426 | i++; // skip STRIP_END
|
---|
427 | }
|
---|
428 | return result;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | counts the triangles of the torus
|
---|
433 | */
|
---|
434 | int Geometry::CountTorusTriangles()
|
---|
435 | {
|
---|
436 | // every 3 indices specify one triangle
|
---|
437 | return num_torus_indices / 3;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | counts the triangles of the sphere.
|
---|
442 | sphere_precision / 2 + 1 strips with sphere_precision * 2 - 2 triangles each
|
---|
443 | */
|
---|
444 | int Geometry::CountSphereTriangles()
|
---|
445 | {
|
---|
446 | return sphere_precision * sphere_precision;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | int Geometry::CountTriangles(int objectType)
|
---|
451 | {
|
---|
452 | int result = 0;
|
---|
453 |
|
---|
454 | switch(objectType)
|
---|
455 | {
|
---|
456 | case TEAPOT:
|
---|
457 | result = CountTeapotTriangles();
|
---|
458 | break;
|
---|
459 | case TORUS:
|
---|
460 | result = CountTorusTriangles();
|
---|
461 | break;
|
---|
462 | case SPHERE:
|
---|
463 | result = CountSphereTriangles();
|
---|
464 | break;
|
---|
465 | default:
|
---|
466 | break;
|
---|
467 | }
|
---|
468 | return result;
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | void Geometry::Transform()
|
---|
473 | {
|
---|
474 | glMultMatrixd(mTransform);
|
---|
475 | }
|
---|
476 |
|
---|
477 | void Geometry::CalcTransform()
|
---|
478 | {
|
---|
479 | Matrix4x4 scale;
|
---|
480 | Matrix4x4 xrot;
|
---|
481 | Matrix4x4 yrot;
|
---|
482 | Matrix4x4 zrot;
|
---|
483 | Matrix4x4 transl;
|
---|
484 |
|
---|
485 | makeScaleMtx(scale, mScale, mScale, mScale);
|
---|
486 | makeTranslationMtx(transl, mTranslation);
|
---|
487 | makeRotationXMtx(xrot, mXRotation * PI_180);
|
---|
488 | makeRotationYMtx(yrot, mYRotation * PI_180);
|
---|
489 | makeRotationZMtx(zrot, mZRotation * PI_180);
|
---|
490 |
|
---|
491 | copyMatrix(mTransform, IDENTITY);
|
---|
492 | mult(mTransform, mTransform, transl);
|
---|
493 | mult(mTransform, mTransform, xrot);
|
---|
494 | mult(mTransform, mTransform, yrot);
|
---|
495 | mult(mTransform, mTransform, zrot);
|
---|
496 | mult(mTransform, mTransform, scale);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /**
|
---|
500 | renders a sphere with sphere_precision subdivisions.
|
---|
501 | note: works only for even sphere_precision
|
---|
502 | */
|
---|
503 | void Geometry::RenderSphere()
|
---|
504 | {
|
---|
505 | Vector3 vertex;
|
---|
506 |
|
---|
507 | // this algorithm renders the triangles clockwise
|
---|
508 | glFrontFace(GL_CW);
|
---|
509 |
|
---|
510 | for (int j = 0; j < sphere_precision/2; ++j)
|
---|
511 | {
|
---|
512 | double alpha = j * PI * 2.0 / (double)sphere_precision - PI_2;
|
---|
513 | double beta = (j + 1) * PI * 2.0 / (double)sphere_precision - PI_2;
|
---|
514 |
|
---|
515 | glBegin(GL_TRIANGLE_STRIP);
|
---|
516 |
|
---|
517 | for (int i = 0; i <= sphere_precision; ++i)
|
---|
518 | {
|
---|
519 | double gamma = i * PI * 2.0 / (double)sphere_precision;
|
---|
520 |
|
---|
521 | vertex[0] = sphere_radius * cos(beta) * cos(gamma);
|
---|
522 | vertex[1] = sphere_radius * sin(beta);
|
---|
523 | vertex[2] = sphere_radius * cos(beta) * sin(gamma);
|
---|
524 |
|
---|
525 | glNormal3dv(vertex);
|
---|
526 | glVertex3dv(vertex);
|
---|
527 |
|
---|
528 | vertex[0] = sphere_radius * cos(alpha) * cos(gamma);
|
---|
529 | vertex[1] = sphere_radius * sin(alpha);
|
---|
530 | vertex[2] = sphere_radius * cos(alpha) * sin(gamma);
|
---|
531 |
|
---|
532 | glNormal3dv(vertex);
|
---|
533 | glVertex3dv(vertex);
|
---|
534 |
|
---|
535 | }
|
---|
536 | glEnd();
|
---|
537 | }
|
---|
538 | glFrontFace(GL_CCW);
|
---|
539 | } |
---|