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