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