Showing the changes to interface/src/Util.cpp (1 of 2 files). Show all
Unified · Split
| @@ -25,6 +25,12 @@ · 24 unmodified lines | |||
| 25 | 25 | ||
| 26 | #include "Util.h" | 26 | #include "Util.h" |
| 27 | 27 | ||
| 28 | #ifdef _WIN32 | ||
| 29 | bool isnan(double value) { return _isnan(value); } | ||
| 30 | #else | ||
| 31 | bool isnan(double value) { return std::isnan(value); } | ||
| 32 | #endif | ||
| 33 | |||
| 28 | using namespace std; | 34 | using namespace std; |
| 29 | 35 | ||
| 30 | // no clue which versions are affected... | 36 | // no clue which versions are affected... |
| @@ -37,14 +43,14 @@ void eulerToOrthonormals(glm::vec3 * angles, glm::vec3 * front, glm::vec3 * righ · 6 unmodified lines | |||
| 37 | // | 43 | // |
| 38 | // Angles contains (pitch, yaw, roll) in radians | 44 | // Angles contains (pitch, yaw, roll) in radians |
| 39 | // | 45 | // |
| 40 | 46 | ||
| 41 | // First, create the quaternion associated with these euler angles | 47 | // First, create the quaternion associated with these euler angles |
| 42 | glm::quat q(glm::vec3(angles->x, -(angles->y), angles->z)); | 48 | glm::quat q(glm::vec3(angles->x, -(angles->y), angles->z)); |
| 43 | 49 | ||
| 44 | // Next, create a rotation matrix from that quaternion | 50 | // Next, create a rotation matrix from that quaternion |
| 45 | glm::mat4 rotation; | 51 | glm::mat4 rotation; |
| 46 | rotation = glm::mat4_cast(q); | 52 | rotation = glm::mat4_cast(q); |
| 47 | 53 | ||
| 48 | // Transform the original vectors by the rotation matrix to get the new vectors | 54 | // Transform the original vectors by the rotation matrix to get the new vectors |
| 49 | glm::vec4 qup(0,1,0,0); | 55 | glm::vec4 qup(0,1,0,0); |
| 50 | glm::vec4 qright(-1,0,0,0); | 56 | glm::vec4 qright(-1,0,0,0); |
| @@ -52,7 +58,7 @@ void eulerToOrthonormals(glm::vec3 * angles, glm::vec3 * front, glm::vec3 * righ · 1 unmodified line | |||
| 52 | glm::vec4 upNew = qup*rotation; | 58 | glm::vec4 upNew = qup*rotation; |
| 53 | glm::vec4 rightNew = qright*rotation; | 59 | glm::vec4 rightNew = qright*rotation; |
| 54 | glm::vec4 frontNew = qfront*rotation; | 60 | glm::vec4 frontNew = qfront*rotation; |
| 55 | 61 | ||
| 56 | // Copy the answers to output vectors | 62 | // Copy the answers to output vectors |
| 57 | up->x = upNew.x; up->y = upNew.y; up->z = upNew.z; | 63 | up->x = upNew.x; up->y = upNew.y; up->z = upNew.z; |
| 58 | right->x = rightNew.x; right->y = rightNew.y; right->z = rightNew.z; | 64 | right->x = rightNew.x; right->y = rightNew.y; right->z = rightNew.z; |
| @@ -68,12 +74,12 @@ float azimuth_to(glm::vec3 head_pos, glm::vec3 source_pos) { · 9 unmodified lines | |||
| 68 | return atan2(head_pos.x - source_pos.x, head_pos.z - source_pos.z) * 180.0f / PIf; | 74 | return atan2(head_pos.x - source_pos.x, head_pos.z - source_pos.z) * 180.0f / PIf; |
| 69 | } | 75 | } |
| 70 | 76 | ||
| 71 | // Return the angle in degrees between the head and an object in the scene. The value is zero if you are looking right at it. The angle is negative if the object is to your right. | 77 | // Return the angle in degrees between the head and an object in the scene. The value is zero if you are looking right at it. The angle is negative if the object is to your right. |
| 72 | float angle_to(glm::vec3 head_pos, glm::vec3 source_pos, float render_yaw, float head_yaw) { | 78 | float angle_to(glm::vec3 head_pos, glm::vec3 source_pos, float render_yaw, float head_yaw) { |
| 73 | return atan2(head_pos.x - source_pos.x, head_pos.z - source_pos.z) * 180.0f / PIf + render_yaw + head_yaw; | 79 | return atan2(head_pos.x - source_pos.x, head_pos.z - source_pos.z) * 180.0f / PIf + render_yaw + head_yaw; |
| 74 | } | 80 | } |
| 75 | 81 | ||
| 76 | // Helper function returns the positive angle in degrees between two 3D vectors | 82 | // Helper function returns the positive angle in degrees between two 3D vectors |
| 77 | float angleBetween(const glm::vec3& v1, const glm::vec3& v2) { | 83 | float angleBetween(const glm::vec3& v1, const glm::vec3& v2) { |
| 78 | return acos((glm::dot(v1, v2)) / (glm::length(v1) * glm::length(v2))) * 180.f / PIf; | 84 | return acos((glm::dot(v1, v2)) / (glm::length(v1) * glm::length(v2))) * 180.f / PIf; |
| 79 | } | 85 | } |
| @@ -92,7 +98,7 @@ glm::quat rotationBetween(const glm::vec3& v1, const glm::vec3& v2) { · 12 unmodified lines | |||
| 92 | axis = glm::normalize(glm::cross(v1, glm::vec3(0.0f, 1.0f, 0.0f))); | 98 | axis = glm::normalize(glm::cross(v1, glm::vec3(0.0f, 1.0f, 0.0f))); |
| 93 | } else { | 99 | } else { |
| 94 | axis /= axisLength; | 100 | axis /= axisLength; |
| 95 | } | 101 | } |
| 96 | } else { | 102 | } else { |
| 97 | axis = glm::normalize(glm::cross(v1, v2)); | 103 | axis = glm::normalize(glm::cross(v1, v2)); |
| 98 | } | 104 | } |
| @@ -110,7 +116,7 @@ glm::vec3 safeEulerAngles(const glm::quat& q) { · 11 unmodified lines | |||
| 110 | atan2f(q.y * q.z + q.x * q.w, 0.5f - (q.x * q.x + q.y * q.y)), | 116 | atan2f(q.y * q.z + q.x * q.w, 0.5f - (q.x * q.x + q.y * q.y)), |
| 111 | asinf(sy), | 117 | asinf(sy), |
| 112 | atan2f(q.x * q.y + q.z * q.w, 0.5f - (q.y * q.y + q.z * q.z)))); | 118 | atan2f(q.x * q.y + q.z * q.w, 0.5f - (q.y * q.y + q.z * q.z)))); |
| 113 | 119 | ||
| 114 | } else { | 120 | } else { |
| 115 | // not a unique solution; x + z = atan2(-m21, m11) | 121 | // not a unique solution; x + z = atan2(-m21, m11) |
| 116 | return glm::degrees(glm::vec3( | 122 | return glm::degrees(glm::vec3( |
| @@ -149,7 +155,7 @@ glm::quat safeMix(const glm::quat& q1, const glm::quat& q2, float proportion) { · 32 unmodified lines | |||
| 149 | float angle = acosf(cosa), sina = sinf(angle); | 155 | float angle = acosf(cosa), sina = sinf(angle); |
| 150 | s0 = sinf((1.0f - proportion) * angle) / sina; | 156 | s0 = sinf((1.0f - proportion) * angle) / sina; |
| 151 | s1 = sinf(proportion * angle) / sina; | 157 | s1 = sinf(proportion * angle) / sina; |
| 152 | 158 | ||
| 153 | } else { | 159 | } else { |
| 154 | s0 = 1.0f - proportion; | 160 | s0 = 1.0f - proportion; |
| 155 | s1 = proportion; | 161 | s1 = proportion; |
| @@ -179,7 +185,7 @@ glm::quat extractRotation(const glm::mat4& matrix, bool assumeOrthogonal) { · 23 unmodified lines | |||
| 179 | for (int i = 0; i < 10; i++) { | 185 | for (int i = 0; i < 10; i++) { |
| 180 | // store the results of the previous iteration | 186 | // store the results of the previous iteration |
| 181 | glm::mat3 previous = upper; | 187 | glm::mat3 previous = upper; |
| 182 | 188 | ||
| 183 | // compute average of the matrix with its inverse transpose | 189 | // compute average of the matrix with its inverse transpose |
| 184 | float sd00 = previous[1][1] * previous[2][2] - previous[2][1] * previous[1][2]; | 190 | float sd00 = previous[1][1] * previous[2][2] - previous[2][1] * previous[1][2]; |
| 185 | float sd10 = previous[0][1] * previous[2][2] - previous[2][1] * previous[0][2]; | 191 | float sd10 = previous[0][1] * previous[2][2] - previous[2][1] * previous[0][2]; |
| @@ -211,13 +217,13 @@ glm::quat extractRotation(const glm::mat4& matrix, bool assumeOrthogonal) { · 25 unmodified lines | |||
| 211 | } | 217 | } |
| 212 | } | 218 | } |
| 213 | } | 219 | } |
| 214 | 220 | ||
| 215 | // now that we have a nice orthogonal matrix, we can extract the rotation quaternion | 221 | // now that we have a nice orthogonal matrix, we can extract the rotation quaternion |
| 216 | // using the method described in http://en.wikipedia.org/wiki/Rotation_matrix#Conversions | 222 | // using the method described in http://en.wikipedia.org/wiki/Rotation_matrix#Conversions |
| 217 | float x2 = fabs(1.0f + upper[0][0] - upper[1][1] - upper[2][2]); | 223 | float x2 = fabs(1.0f + upper[0][0] - upper[1][1] - upper[2][2]); |
| 218 | float y2 = fabs(1.0f - upper[0][0] + upper[1][1] - upper[2][2]); | 224 | float y2 = fabs(1.0f - upper[0][0] + upper[1][1] - upper[2][2]); |
| 219 | float z2 = fabs(1.0f - upper[0][0] - upper[1][1] + upper[2][2]); | 225 | float z2 = fabs(1.0f - upper[0][0] - upper[1][1] + upper[2][2]); |
| 220 | float w2 = fabs(1.0f + upper[0][0] + upper[1][1] + upper[2][2]); | 226 | float w2 = fabs(1.0f + upper[0][0] + upper[1][1] + upper[2][2]); |
| 221 | return glm::normalize(glm::quat(0.5f * sqrtf(w2), | 227 | return glm::normalize(glm::quat(0.5f * sqrtf(w2), |
| 222 | 0.5f * sqrtf(x2) * (upper[1][2] >= upper[2][1] ? 1.0f : -1.0f), | 228 | 0.5f * sqrtf(x2) * (upper[1][2] >= upper[2][1] ? 1.0f : -1.0f), |
| 223 | 0.5f * sqrtf(y2) * (upper[2][0] >= upper[0][2] ? 1.0f : -1.0f), | 229 | 0.5f * sqrtf(y2) * (upper[2][0] >= upper[0][2] ? 1.0f : -1.0f), |
| @@ -255,14 +261,14 @@ void drawVector(glm::vec3 * vector) { · 31 unmodified lines | |||
| 255 | glVertex3f(0,0,0); | 261 | glVertex3f(0,0,0); |
| 256 | glVertex3f(0, 0, 1); | 262 | glVertex3f(0, 0, 1); |
| 257 | glEnd(); | 263 | glEnd(); |
| 258 | 264 | ||
| 259 | // Draw the vector itself | 265 | // Draw the vector itself |
| 260 | glBegin(GL_LINES); | 266 | glBegin(GL_LINES); |
| 261 | glColor3f(1,1,1); | 267 | glColor3f(1,1,1); |
| 262 | glVertex3f(0,0,0); | 268 | glVertex3f(0,0,0); |
| 263 | glVertex3f(vector->x, vector->y, vector->z); | 269 | glVertex3f(vector->x, vector->y, vector->z); |
| 264 | glEnd(); | 270 | glEnd(); |
| 265 | 271 | ||
| 266 | // Draw spheres for magnitude | 272 | // Draw spheres for magnitude |
| 267 | glPushMatrix(); | 273 | glPushMatrix(); |
| 268 | glColor3f(1,0,0); | 274 | glColor3f(1,0,0); |
| @@ -284,10 +290,10 @@ void noiseTest(int w, int h) { · 15 unmodified lines | |||
| 284 | const float NOISE_SCALE = 10.0; | 290 | const float NOISE_SCALE = 10.0; |
| 285 | float xStep = (float) w / CELLS; | 291 | float xStep = (float) w / CELLS; |
| 286 | float yStep = (float) h / CELLS; | 292 | float yStep = (float) h / CELLS; |
| 287 | glBegin(GL_QUADS); | 293 | glBegin(GL_QUADS); |
| 288 | for (float x = 0; x < (float)w; x += xStep) { | 294 | for (float x = 0; x < (float)w; x += xStep) { |
| 289 | for (float y = 0; y < (float)h; y += yStep) { | 295 | for (float y = 0; y < (float)h; y += yStep) { |
| 290 | // Generate a vector varying between 0-1 corresponding to the screen location | 296 | // Generate a vector varying between 0-1 corresponding to the screen location |
| 291 | glm::vec2 position(NOISE_SCALE * x / (float) w, NOISE_SCALE * y / (float) h); | 297 | glm::vec2 position(NOISE_SCALE * x / (float) w, NOISE_SCALE * y / (float) h); |
| 292 | // Set the cell color using the noise value at that location | 298 | // Set the cell color using the noise value at that location |
| 293 | float color = glm::perlin(position); | 299 | float color = glm::perlin(position); |
| @@ -300,14 +306,14 @@ void noiseTest(int w, int h) { · 6 unmodified lines | |||
| 300 | } | 306 | } |
| 301 | glEnd(); | 307 | glEnd(); |
| 302 | } | 308 | } |
| 303 | 309 | ||
| 304 | void renderWorldBox() { | 310 | void renderWorldBox() { |
| 305 | // Show edge of world | 311 | // Show edge of world |
| 306 | float red[] = {1, 0, 0}; | 312 | float red[] = {1, 0, 0}; |
| 307 | float green[] = {0, 1, 0}; | 313 | float green[] = {0, 1, 0}; |
| 308 | float blue[] = {0, 0, 1}; | 314 | float blue[] = {0, 0, 1}; |
| 309 | float gray[] = {0.5, 0.5, 0.5}; | 315 | float gray[] = {0.5, 0.5, 0.5}; |
| 310 | 316 | ||
| 311 | glDisable(GL_LIGHTING); | 317 | glDisable(GL_LIGHTING); |
| 312 | glLineWidth(1.0); | 318 | glLineWidth(1.0); |
| 313 | glBegin(GL_LINES); | 319 | glBegin(GL_LINES); |
| @@ -355,7 +361,7 @@ double diffclock(timeval *clock1,timeval *clock2) · 41 unmodified lines | |||
| 355 | { | 361 | { |
| 356 | double diffms = (clock2->tv_sec - clock1->tv_sec) * 1000.0; | 362 | double diffms = (clock2->tv_sec - clock1->tv_sec) * 1000.0; |
| 357 | diffms += (clock2->tv_usec - clock1->tv_usec) / 1000.0; // us to ms | 363 | diffms += (clock2->tv_usec - clock1->tv_usec) / 1000.0; // us to ms |
| 358 | 364 | ||
| 359 | return diffms; | 365 | return diffms; |
| 360 | } | 366 | } |
| 361 | 367 | ||
| @@ -389,9 +395,9 @@ void drawtext(int x, int y, float scale, float rotate, float thick, int mono, · 27 unmodified lines | |||
| 389 | glRotated(rotate,0,0,1); | 395 | glRotated(rotate,0,0,1); |
| 390 | // glLineWidth(thick); | 396 | // glLineWidth(thick); |
| 391 | glScalef(scale / 0.10, scale / 0.10, 1.0); | 397 | glScalef(scale / 0.10, scale / 0.10, 1.0); |
| 392 | 398 | ||
| 393 | textRenderer(mono)->draw(0, 0, string); | 399 | textRenderer(mono)->draw(0, 0, string); |
| 394 | 400 | ||
| 395 | glPopMatrix(); | 401 | glPopMatrix(); |
| 396 | 402 | ||
| 397 | } | 403 | } |
| @@ -416,7 +422,7 @@ void drawvec3(int x, int y, float scale, float rotate, float thick, int mono, gl · 18 unmodified lines | |||
| 416 | else glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, int(vectext[i])); | 422 | else glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, int(vectext[i])); |
| 417 | } | 423 | } |
| 418 | glPopMatrix(); | 424 | glPopMatrix(); |
| 419 | } | 425 | } |
| 420 | 426 | ||
| 421 | void renderCollisionOverlay(int width, int height, float magnitude) { | 427 | void renderCollisionOverlay(int width, int height, float magnitude) { |
| 422 | const float MIN_VISIBLE_COLLISION = 0.01f; | 428 | const float MIN_VISIBLE_COLLISION = 0.01f; |
| @@ -435,7 +441,7 @@ void renderMouseVoxelGrid(const float& mouseVoxelX, const float& mouseVoxelY, co · 12 unmodified lines | |||
| 435 | glm::vec3 origin = glm::vec3(mouseVoxelX, mouseVoxelY, mouseVoxelZ); | 441 | glm::vec3 origin = glm::vec3(mouseVoxelX, mouseVoxelY, mouseVoxelZ); |
| 436 | 442 | ||
| 437 | glLineWidth(3.0); | 443 | glLineWidth(3.0); |
| 438 | 444 | ||
| 439 | const int HALF_GRID_DIMENSIONS = 4; | 445 | const int HALF_GRID_DIMENSIONS = 4; |
| 440 | glBegin(GL_LINES); | 446 | glBegin(GL_LINES); |
| 441 | 447 | ||
| @@ -463,7 +469,7 @@ void renderNudgeGrid(float voxelX, float voxelY, float voxelZ, float voxelS, flo · 21 unmodified lines | |||
| 463 | glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ); | 469 | glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ); |
| 464 | 470 | ||
| 465 | glLineWidth(1.0); | 471 | glLineWidth(1.0); |
| 466 | 472 | ||
| 467 | const int GRID_DIMENSIONS = 4; | 473 | const int GRID_DIMENSIONS = 4; |
| 468 | const int GRID_SCALER = voxelS / voxelPrecision; | 474 | const int GRID_SCALER = voxelS / voxelPrecision; |
| 469 | const int GRID_SEGMENTS = GRID_DIMENSIONS * GRID_SCALER; | 475 | const int GRID_SEGMENTS = GRID_DIMENSIONS * GRID_SCALER; |
| @@ -485,7 +491,7 @@ void renderNudgeGrid(float voxelX, float voxelY, float voxelZ, float voxelS, flo · 15 unmodified lines | |||
| 485 | glEnd(); | 491 | glEnd(); |
| 486 | 492 | ||
| 487 | glColor3f(1.0f,1.0f,1.0f); | 493 | glColor3f(1.0f,1.0f,1.0f); |
| 488 | 494 | ||
| 489 | glBegin(GL_POLYGON);//begin drawing of square | 495 | glBegin(GL_POLYGON);//begin drawing of square |
| 490 | glVertex3f(voxelX, 0.0f, voxelZ);//first vertex | 496 | glVertex3f(voxelX, 0.0f, voxelZ);//first vertex |
| 491 | glVertex3f(voxelX + voxelS, 0.0f, voxelZ);//second vertex | 497 | glVertex3f(voxelX + voxelS, 0.0f, voxelZ);//second vertex |
| @@ -498,7 +504,7 @@ void renderNudgeGuide(float voxelX, float voxelY, float voxelZ, float voxelS) { · 6 unmodified lines | |||
| 498 | glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ); | 504 | glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ); |
| 499 | 505 | ||
| 500 | glLineWidth(3.0); | 506 | glLineWidth(3.0); |
| 501 | 507 | ||
| 502 | glBegin(GL_LINES); | 508 | glBegin(GL_LINES); |
| 503 | 509 | ||
| 504 | glm::vec3 guideColor(1.0, 1.0, 1.0); | 510 | glm::vec3 guideColor(1.0, 1.0, 1.0); |
| @@ -526,21 +532,21 @@ void renderSphereOutline(glm::vec3 position, float radius, int numSides, glm::ve · 21 unmodified lines | |||
| 526 | glm::vec3 vectorToPosition(glm::normalize(position - cameraPosition)); | 532 | glm::vec3 vectorToPosition(glm::normalize(position - cameraPosition)); |
| 527 | glm::vec3 right = glm::cross(vectorToPosition, glm::vec3(0.0f, 1.0f, 0.0f)); | 533 | glm::vec3 right = glm::cross(vectorToPosition, glm::vec3(0.0f, 1.0f, 0.0f)); |
| 528 | glm::vec3 up = glm::cross(right, vectorToPosition); | 534 | glm::vec3 up = glm::cross(right, vectorToPosition); |
| 529 | 535 | ||
| 530 | glBegin(GL_LINE_STRIP); | 536 | glBegin(GL_LINE_STRIP); |
| 531 | for (int i=0; i<numSides+1; i++) { | 537 | for (int i=0; i<numSides+1; i++) { |
| 532 | float r = ((float)i / (float)numSides) * PIf * 2.0; | 538 | float r = ((float)i / (float)numSides) * PIf * 2.0; |
| 533 | float s = radius * sin(r); | 539 | float s = radius * sin(r); |
| 534 | float c = radius * cos(r); | 540 | float c = radius * cos(r); |
| 535 | 541 | ||
| 536 | glVertex3f | 542 | glVertex3f |
| 537 | ( | 543 | ( |
| 538 | position.x + right.x * s + up.x * c, | 544 | position.x + right.x * s + up.x * c, |
| 539 | position.y + right.y * s + up.y * c, | 545 | position.y + right.y * s + up.y * c, |
| 540 | position.z + right.z * s + up.z * c | 546 | position.z + right.z * s + up.z * c |
| 541 | ); | 547 | ); |
| 542 | } | 548 | } |
| 543 | 549 | ||
| 544 | glEnd(); | 550 | glEnd(); |
| 545 | } | 551 | } |
| 546 | 552 | ||
| @@ -548,8 +554,8 @@ void renderSphereOutline(glm::vec3 position, float radius, int numSides, glm::ve · 1 unmodified line | |||
| 548 | void renderCircle(glm::vec3 position, float radius, glm::vec3 surfaceNormal, int numSides) { | 554 | void renderCircle(glm::vec3 position, float radius, glm::vec3 surfaceNormal, int numSides) { |
| 549 | glm::vec3 perp1 = glm::vec3(surfaceNormal.y, surfaceNormal.z, surfaceNormal.x); | 555 | glm::vec3 perp1 = glm::vec3(surfaceNormal.y, surfaceNormal.z, surfaceNormal.x); |
| 550 | glm::vec3 perp2 = glm::vec3(surfaceNormal.z, surfaceNormal.x, surfaceNormal.y); | 556 | glm::vec3 perp2 = glm::vec3(surfaceNormal.z, surfaceNormal.x, surfaceNormal.y); |
| 551 | 557 | ||
| 552 | glBegin(GL_LINE_STRIP); | 558 | glBegin(GL_LINE_STRIP); |
| 553 | 559 | ||
| 554 | for (int i=0; i<numSides+1; i++) { | 560 | for (int i=0; i<numSides+1; i++) { |
| 555 | float r = ((float)i / (float)numSides) * PIf * 2.0; | 561 | float r = ((float)i / (float)numSides) * PIf * 2.0; |
| @@ -557,10 +563,10 @@ void renderCircle(glm::vec3 position, float radius, glm::vec3 surfaceNormal, int · 1 unmodified line | |||
| 557 | float c = radius * cos(r); | 563 | float c = radius * cos(r); |
| 558 | glVertex3f | 564 | glVertex3f |
| 559 | ( | 565 | ( |
| 560 | position.x + perp1.x * s + perp2.x * c, | 566 | position.x + perp1.x * s + perp2.x * c, |
| 561 | position.y + perp1.y * s + perp2.y * c, | 567 | position.y + perp1.y * s + perp2.y * c, |
| 562 | position.z + perp1.z * s + perp2.z * c | 568 | position.z + perp1.z * s + perp2.z * c |
| 563 | ); | 569 | ); |
| 564 | } | 570 | } |
| 565 | glEnd(); | 571 | glEnd(); |
| 566 | } | 572 | } |
| @@ -570,7 +576,7 @@ void renderOrientationDirections(glm::vec3 position, const glm::quat& orientatio · 3 unmodified lines | |||
| 570 | glm::vec3 pRight = position + orientation * IDENTITY_RIGHT * size; | 576 | glm::vec3 pRight = position + orientation * IDENTITY_RIGHT * size; |
| 571 | glm::vec3 pUp = position + orientation * IDENTITY_UP * size; | 577 | glm::vec3 pUp = position + orientation * IDENTITY_UP * size; |
| 572 | glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size; | 578 | glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size; |
| 573 | 579 | ||
| 574 | glColor3f(1.0f, 0.0f, 0.0f); | 580 | glColor3f(1.0f, 0.0f, 0.0f); |
| 575 | glBegin(GL_LINE_STRIP); | 581 | glBegin(GL_LINE_STRIP); |
| 576 | glVertex3f(position.x, position.y, position.z); | 582 | glVertex3f(position.x, position.y, position.z); |
| @@ -611,7 +617,7 @@ void runTimingTests() { · 34 unmodified lines | |||
| 611 | } | 617 | } |
| 612 | elapsedMsecs = diffclock(&startTime, &endTime); | 618 | elapsedMsecs = diffclock(&startTime, &endTime); |
| 613 | qDebug("gettimeofday() usecs: %f\n", 1000.0f * elapsedMsecs / (float) numTests); | 619 | qDebug("gettimeofday() usecs: %f\n", 1000.0f * elapsedMsecs / (float) numTests); |
| 614 | 620 | ||
| 615 | // Random number generation | 621 | // Random number generation |
| 616 | gettimeofday(&startTime, NULL); | 622 | gettimeofday(&startTime, NULL); |
| 617 | for (int i = 1; i < numTests; i++) { | 623 | for (int i = 1; i < numTests; i++) { |
| @@ -651,13 +657,13 @@ void runTimingTests() { · 33 unmodified lines | |||
| 651 | } | 657 | } |
| 652 | gettimeofday(&endTime, NULL); | 658 | gettimeofday(&endTime, NULL); |
| 653 | elapsedMsecs = diffclock(&startTime, &endTime); | 659 | elapsedMsecs = diffclock(&startTime, &endTime); |
| 654 | qDebug("vector math usecs: %f [%f msecs total for %d tests]\n", | 660 | qDebug("vector math usecs: %f [%f msecs total for %d tests]\n", |
| 655 | 1000.0f * elapsedMsecs / (float) numTests, elapsedMsecs, numTests); | 661 | 1000.0f * elapsedMsecs / (float) numTests, elapsedMsecs, numTests); |
| 656 | 662 | ||
| 657 | // Vec3 test | 663 | // Vec3 test |
| 658 | glm::vec3 vecA(randVector()), vecB(randVector()); | 664 | glm::vec3 vecA(randVector()), vecB(randVector()); |
| 659 | float result; | 665 | float result; |
| 660 | 666 | ||
| 661 | gettimeofday(&startTime, NULL); | 667 | gettimeofday(&startTime, NULL); |
| 662 | for (int i = 1; i < numTests; i++) { | 668 | for (int i = 1; i < numTests; i++) { |
| 663 | glm::vec3 temp = vecA-vecB; | 669 | glm::vec3 temp = vecA-vecB; |
| @@ -679,32 +685,32 @@ float loadSetting(QSettings* settings, const char* name, float defaultValue) { · 15 unmodified lines | |||
| 679 | bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, | 685 | bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, |
| 680 | const glm::vec3& sphereCenter, float sphereRadius, float& distance) { | 686 | const glm::vec3& sphereCenter, float sphereRadius, float& distance) { |
| 681 | glm::vec3 relativeOrigin = rayStarting - sphereCenter; | 687 | glm::vec3 relativeOrigin = rayStarting - sphereCenter; |
| 682 | 688 | ||
| 683 | // compute the b, c terms of the quadratic equation (a is dot(direction, direction), which is one) | 689 | // compute the b, c terms of the quadratic equation (a is dot(direction, direction), which is one) |
| 684 | float b = 2.0f * glm::dot(rayNormalizedDirection, relativeOrigin); | 690 | float b = 2.0f * glm::dot(rayNormalizedDirection, relativeOrigin); |
| 685 | float c = glm::dot(relativeOrigin, relativeOrigin) - sphereRadius * sphereRadius; | 691 | float c = glm::dot(relativeOrigin, relativeOrigin) - sphereRadius * sphereRadius; |
| 686 | 692 | ||
| 687 | // compute the radicand of the quadratic. if less than zero, there's no intersection | 693 | // compute the radicand of the quadratic. if less than zero, there's no intersection |
| 688 | float radicand = b * b - 4.0f * c; | 694 | float radicand = b * b - 4.0f * c; |
| 689 | if (radicand < 0.0f) { | 695 | if (radicand < 0.0f) { |
| 690 | return false; | 696 | return false; |
| 691 | } | 697 | } |
| 692 | 698 | ||
| 693 | // compute the first solution of the quadratic | 699 | // compute the first solution of the quadratic |
| 694 | float root = sqrtf(radicand); | 700 | float root = sqrtf(radicand); |
| 695 | float firstSolution = -b - root; | 701 | float firstSolution = -b - root; |
| 696 | if (firstSolution > 0.0f) { | 702 | if (firstSolution > 0.0f) { |
| 697 | distance = firstSolution / 2.0f; | 703 | distance = firstSolution / 2.0f; |
| 698 | return true; // origin is outside the sphere | 704 | return true; // origin is outside the sphere |
| 699 | } | 705 | } |
| 700 | 706 | ||
| 701 | // now try the second solution | 707 | // now try the second solution |
| 702 | float secondSolution = -b + root; | 708 | float secondSolution = -b + root; |
| 703 | if (secondSolution > 0.0f) { | 709 | if (secondSolution > 0.0f) { |
| 704 | distance = 0.0f; | 710 | distance = 0.0f; |
| 705 | return true; // origin is inside the sphere | 711 | return true; // origin is inside the sphere |
| 706 | } | 712 | } |
| 707 | 713 | ||
| 708 | return false; | 714 | return false; |
| 709 | } | 715 | } |
| 710 | 716 | ||