source: GTP/trunk/App/Demos/Vis/CHC_revisited/Geometry.cpp @ 2751

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