Changeset 1f3d32b in opengl-game for new-game.cpp
- Timestamp:
- Oct 5, 2018, 12:35:21 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 2b0214c
- Parents:
- e9347b4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
re9347b4 r1f3d32b 24 24 #include <iostream> 25 25 #include <fstream> 26 #include <sstream> 26 27 #include <cmath> 27 28 #include <string> … … 74 75 }; 75 76 77 struct Asteroid : SceneObject { 78 double hp; 79 }; 80 81 struct Laser : SceneObject { 82 Asteroid* targetAsteroid; 83 }; 84 85 struct EffectOverTime { 86 double& effectedValue; 87 double startValue; 88 double startTime; 89 double changePerSecond; 90 bool deleted; 91 SceneObject* effectedObject; 92 93 EffectOverTime(double& effectedValue, double changePerSecond, SceneObject* object) 94 : effectedValue(effectedValue), effectedObject(object) { 95 startValue = effectedValue; 96 startTime = glfwGetTime(); 97 this->changePerSecond = changePerSecond; 98 deleted = false; 99 } 100 }; 101 76 102 struct BufferInfo { 77 103 unsigned int vbo_base; … … 98 124 void print4DVector(string label, vec4& v); 99 125 100 void initObject(SceneObject &obj);101 void addObjectToScene(SceneObject &obj,126 void initObject(SceneObject* obj); 127 void addObjectToScene(SceneObject* obj, 102 128 map<GLuint, BufferInfo>& shaderBufferInfo, 103 129 GLuint points_vbo, … … 110 136 void removeObjectFromScene(SceneObject& obj, GLuint ubo); 111 137 112 void calculateObjectBoundingBox(SceneObject& obj); 113 114 SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp); 138 void calculateObjectBoundingBox(SceneObject* obj); 115 139 116 140 void initializeBuffers( … … 123 147 GLuint* model_mat_idx_vbo); 124 148 125 void populateBuffers(vector<SceneObject >& objects,149 void populateBuffers(vector<SceneObject*>& objects, 126 150 map<GLuint, BufferInfo>& shaderBufferInfo, 127 151 GLuint points_vbo, … … 145 169 void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo); 146 170 147 void translateLaser(SceneObject& laser, const vec3& translation, GLuint ubo); 148 void updateLaserTarget(SceneObject& laser, vector<SceneObject>& objects, GLuint points_vbo); 171 SceneObject* createShip(GLuint shader); 172 Asteroid* createAsteroid(vec3 pos, GLuint shader); 173 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp); 174 175 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo); 176 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo); 149 177 bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection); 150 178 … … 159 187 void renderSceneGui(); 160 188 161 void spawnAsteroid(vec3 pos, GLuint shader,162 map<GLuint, BufferInfo>& shaderBufferInfo,163 GLuint points_vbo,164 GLuint colors_vbo,165 GLuint selected_colors_vbo,166 GLuint texcoords_vbo,167 GLuint normals_vbo,168 GLuint ubo,169 GLuint model_mat_idx_vbo);170 171 189 float getRandomNum(float low, float high); 172 190 … … 176 194 const int KEY_STATE_UNCHANGED = -1; 177 195 const bool FULLSCREEN = false; 178 const bool SHOW_FPS = false;179 const bool DISABLE_VSYNC = false; // disable vsync to see real framerate180 196 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const 181 197 … … 193 209 mat4 proj_mat; 194 210 195 // TODO: Consider using a list instead since it will make element deletion more efficient 196 vector<SceneObject> objects; 211 vector<SceneObject*> objects; 197 212 queue<Event> events; 213 vector<EffectOverTime*> effects; 198 214 199 215 SceneObject* clickedObject = NULL; … … 208 224 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 209 225 210 // TODO: Come up with a better way to keep references to specific objects, 211 // which remain valid (or are recreated) when the object vector or VBOs are resized. 212 // I could store SceneObject*, but I have to keep track of when the vector is about to 213 // resize and re-init the pointers when that happens since they'll be invalidated 214 // I would prefer the above apporach, but I should write some helper functions to make it less error-prone 215 int leftLaserIdx = -1; 216 int rightLaserIdx = -1; 226 Laser* leftLaser = NULL; 227 EffectOverTime* leftLaserEffect = NULL; 228 229 Laser* rightLaser = NULL; 230 EffectOverTime* rightLaserEffect = NULL; 217 231 218 232 /* … … 404 418 float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f; 405 419 406 SceneObject obj;407 mat4 T_model, R_model;408 409 420 // player ship 410 obj = SceneObject(); 411 obj.type = TYPE_SHIP; 412 obj.shader_program = color_sp; 413 obj.points = { 414 //back 415 -0.5f, 0.3f, 0.0f, 416 -0.5f, 0.0f, 0.0f, 417 0.5f, 0.0f, 0.0f, 418 -0.5f, 0.3f, 0.0f, 419 0.5f, 0.0f, 0.0f, 420 0.5f, 0.3f, 0.0f, 421 422 // left back 423 -0.5f, 0.3f, -2.0f, 424 -0.5f, 0.0f, -2.0f, 425 -0.5f, 0.0f, 0.0f, 426 -0.5f, 0.3f, -2.0f, 427 -0.5f, 0.0f, 0.0f, 428 -0.5f, 0.3f, 0.0f, 429 430 // right back 431 0.5f, 0.3f, 0.0f, 432 0.5f, 0.0f, 0.0f, 433 0.5f, 0.0f, -2.0f, 434 0.5f, 0.3f, 0.0f, 435 0.5f, 0.0f, -2.0f, 436 0.5f, 0.3f, -2.0f, 437 438 // left mid 439 -0.25f, 0.3f, -3.0f, 440 -0.25f, 0.0f, -3.0f, 441 -0.5f, 0.0f, -2.0f, 442 -0.25f, 0.3f, -3.0f, 443 -0.5f, 0.0f, -2.0f, 444 -0.5f, 0.3f, -2.0f, 445 446 // right mid 447 0.5f, 0.3f, -2.0f, 448 0.5f, 0.0f, -2.0f, 449 0.25f, 0.0f, -3.0f, 450 0.5f, 0.3f, -2.0f, 451 0.25f, 0.0f, -3.0f, 452 0.25f, 0.3f, -3.0f, 453 454 // left front 455 0.0f, 0.0f, -3.5f, 456 -0.25f, 0.0f, -3.0f, 457 -0.25f, 0.3f, -3.0f, 458 459 // right front 460 0.25f, 0.3f, -3.0f, 461 0.25f, 0.0f, -3.0f, 462 0.0f, 0.0f, -3.5f, 463 464 // top back 465 -0.5f, 0.3f, -2.0f, 466 -0.5f, 0.3f, 0.0f, 467 0.5f, 0.3f, 0.0f, 468 -0.5f, 0.3f, -2.0f, 469 0.5f, 0.3f, 0.0f, 470 0.5f, 0.3f, -2.0f, 471 472 // bottom back 473 -0.5f, 0.0f, 0.0f, 474 -0.5f, 0.0f, -2.0f, 475 0.5f, 0.0f, 0.0f, 476 0.5f, 0.0f, 0.0f, 477 -0.5f, 0.0f, -2.0f, 478 0.5f, 0.0f, -2.0f, 479 480 // top mid 481 -0.25f, 0.3f, -3.0f, 482 -0.5f, 0.3f, -2.0f, 483 0.5f, 0.3f, -2.0f, 484 -0.25f, 0.3f, -3.0f, 485 0.5f, 0.3f, -2.0f, 486 0.25f, 0.3f, -3.0f, 487 488 // bottom mid 489 -0.5f, 0.0f, -2.0f, 490 -0.25f, 0.0f, -3.0f, 491 0.5f, 0.0f, -2.0f, 492 0.5f, 0.0f, -2.0f, 493 -0.25f, 0.0f, -3.0f, 494 0.25f, 0.0f, -3.0f, 495 496 // top front 497 -0.25f, 0.3f, -3.0f, 498 0.25f, 0.3f, -3.0f, 499 0.0f, 0.0f, -3.5f, 500 501 // bottom front 502 0.25f, 0.0f, -3.0f, 503 -0.25f, 0.0f, -3.0f, 504 0.0f, 0.0f, -3.5f, 505 506 // left wing start back 507 -1.5f, 0.3f, 0.0f, 508 -1.5f, 0.0f, 0.0f, 509 -0.5f, 0.0f, 0.0f, 510 -1.5f, 0.3f, 0.0f, 511 -0.5f, 0.0f, 0.0f, 512 -0.5f, 0.3f, 0.0f, 513 514 // left wing start top 515 -0.5f, 0.3f, -0.3f, 516 -1.3f, 0.3f, -0.3f, 517 -1.5f, 0.3f, 0.0f, 518 -0.5f, 0.3f, -0.3f, 519 -1.5f, 0.3f, 0.0f, 520 -0.5f, 0.3f, 0.0f, 521 522 // left wing start front 523 -0.5f, 0.3f, -0.3f, 524 -0.5f, 0.0f, -0.3f, 525 -1.3f, 0.0f, -0.3f, 526 -0.5f, 0.3f, -0.3f, 527 -1.3f, 0.0f, -0.3f, 528 -1.3f, 0.3f, -0.3f, 529 530 // left wing start bottom 531 -0.5f, 0.0f, 0.0f, 532 -1.5f, 0.0f, 0.0f, 533 -1.3f, 0.0f, -0.3f, 534 -0.5f, 0.0f, 0.0f, 535 -1.3f, 0.0f, -0.3f, 536 -0.5f, 0.0f, -0.3f, 537 538 // left wing end outside 539 -1.5f, 0.3f, 0.0f, 540 -2.2f, 0.15f, -0.8f, 541 -1.5f, 0.0f, 0.0f, 542 543 // left wing end top 544 -1.3f, 0.3f, -0.3f, 545 -2.2f, 0.15f, -0.8f, 546 -1.5f, 0.3f, 0.0f, 547 548 // left wing end front 549 -1.3f, 0.0f, -0.3f, 550 -2.2f, 0.15f, -0.8f, 551 -1.3f, 0.3f, -0.3f, 552 553 // left wing end bottom 554 -1.5f, 0.0f, 0.0f, 555 -2.2f, 0.15f, -0.8f, 556 -1.3f, 0.0f, -0.3f, 557 558 // right wing start back 559 1.5f, 0.0f, 0.0f, 560 1.5f, 0.3f, 0.0f, 561 0.5f, 0.0f, 0.0f, 562 0.5f, 0.0f, 0.0f, 563 1.5f, 0.3f, 0.0f, 564 0.5f, 0.3f, 0.0f, 565 566 // right wing start top 567 1.3f, 0.3f, -0.3f, 568 0.5f, 0.3f, -0.3f, 569 1.5f, 0.3f, 0.0f, 570 1.5f, 0.3f, 0.0f, 571 0.5f, 0.3f, -0.3f, 572 0.5f, 0.3f, 0.0f, 573 574 // right wing start front 575 0.5f, 0.0f, -0.3f, 576 0.5f, 0.3f, -0.3f, 577 1.3f, 0.0f, -0.3f, 578 1.3f, 0.0f, -0.3f, 579 0.5f, 0.3f, -0.3f, 580 1.3f, 0.3f, -0.3f, 581 582 // right wing start bottom 583 1.5f, 0.0f, 0.0f, 584 0.5f, 0.0f, 0.0f, 585 1.3f, 0.0f, -0.3f, 586 1.3f, 0.0f, -0.3f, 587 0.5f, 0.0f, 0.0f, 588 0.5f, 0.0f, -0.3f, 589 590 // right wing end outside 591 2.2f, 0.15f, -0.8f, 592 1.5f, 0.3f, 0.0f, 593 1.5f, 0.0f, 0.0f, 594 595 // right wing end top 596 2.2f, 0.15f, -0.8f, 597 1.3f, 0.3f, -0.3f, 598 1.5f, 0.3f, 0.0f, 599 600 // right wing end front 601 2.2f, 0.15f, -0.8f, 602 1.3f, 0.0f, -0.3f, 603 1.3f, 0.3f, -0.3f, 604 605 // right wing end bottom 606 2.2f, 0.15f, -0.8f, 607 1.5f, 0.0f, 0.0f, 608 1.3f, 0.0f, -0.3f, 609 }; 610 obj.colors = { 611 0.0f, 0.0f, 0.3f, 612 0.0f, 0.0f, 0.3f, 613 0.0f, 0.0f, 0.3f, 614 0.0f, 0.0f, 0.3f, 615 0.0f, 0.0f, 0.3f, 616 0.0f, 0.0f, 0.3f, 617 618 0.0f, 0.0f, 0.3f, 619 0.0f, 0.0f, 0.3f, 620 0.0f, 0.0f, 0.3f, 621 0.0f, 0.0f, 0.3f, 622 0.0f, 0.0f, 0.3f, 623 0.0f, 0.0f, 0.3f, 624 625 0.0f, 0.0f, 0.3f, 626 0.0f, 0.0f, 0.3f, 627 0.0f, 0.0f, 0.3f, 628 0.0f, 0.0f, 0.3f, 629 0.0f, 0.0f, 0.3f, 630 0.0f, 0.0f, 0.3f, 631 632 0.0f, 0.0f, 0.3f, 633 0.0f, 0.0f, 0.3f, 634 0.0f, 0.0f, 0.3f, 635 0.0f, 0.0f, 0.3f, 636 0.0f, 0.0f, 0.3f, 637 0.0f, 0.0f, 0.3f, 638 639 0.0f, 0.0f, 0.3f, 640 0.0f, 0.0f, 0.3f, 641 0.0f, 0.0f, 0.3f, 642 0.0f, 0.0f, 0.3f, 643 0.0f, 0.0f, 0.3f, 644 0.0f, 0.0f, 0.3f, 645 646 0.0f, 0.0f, 1.0f, 647 0.0f, 0.0f, 1.0f, 648 0.0f, 0.0f, 1.0f, 649 650 0.0f, 0.0f, 1.0f, 651 0.0f, 0.0f, 1.0f, 652 0.0f, 0.0f, 1.0f, 653 654 0.0f, 0.0f, 1.0f, 655 0.0f, 0.0f, 1.0f, 656 0.0f, 0.0f, 1.0f, 657 0.0f, 0.0f, 1.0f, 658 0.0f, 0.0f, 1.0f, 659 0.0f, 0.0f, 1.0f, 660 661 0.0f, 0.0f, 1.0f, 662 0.0f, 0.0f, 1.0f, 663 0.0f, 0.0f, 1.0f, 664 0.0f, 0.0f, 1.0f, 665 0.0f, 0.0f, 1.0f, 666 0.0f, 0.0f, 1.0f, 667 668 0.0f, 0.0f, 1.0f, 669 0.0f, 0.0f, 1.0f, 670 0.0f, 0.0f, 1.0f, 671 0.0f, 0.0f, 1.0f, 672 0.0f, 0.0f, 1.0f, 673 0.0f, 0.0f, 1.0f, 674 675 0.0f, 0.0f, 1.0f, 676 0.0f, 0.0f, 1.0f, 677 0.0f, 0.0f, 1.0f, 678 0.0f, 0.0f, 1.0f, 679 0.0f, 0.0f, 1.0f, 680 0.0f, 0.0f, 1.0f, 681 682 0.0f, 0.0f, 0.3f, 683 0.0f, 0.0f, 0.3f, 684 0.0f, 0.0f, 0.3f, 685 686 0.0f, 0.0f, 0.3f, 687 0.0f, 0.0f, 0.3f, 688 0.0f, 0.0f, 0.3f, 689 690 0.0f, 0.0f, 0.3f, 691 0.0f, 0.0f, 0.3f, 692 0.0f, 0.0f, 0.3f, 693 0.0f, 0.0f, 0.3f, 694 0.0f, 0.0f, 0.3f, 695 0.0f, 0.0f, 0.3f, 696 697 0.0f, 0.0f, 0.3f, 698 0.0f, 0.0f, 0.3f, 699 0.0f, 0.0f, 0.3f, 700 0.0f, 0.0f, 0.3f, 701 0.0f, 0.0f, 0.3f, 702 0.0f, 0.0f, 0.3f, 703 704 0.0f, 0.0f, 0.3f, 705 0.0f, 0.0f, 0.3f, 706 0.0f, 0.0f, 0.3f, 707 0.0f, 0.0f, 0.3f, 708 0.0f, 0.0f, 0.3f, 709 0.0f, 0.0f, 0.3f, 710 711 0.0f, 0.0f, 0.3f, 712 0.0f, 0.0f, 0.3f, 713 0.0f, 0.0f, 0.3f, 714 0.0f, 0.0f, 0.3f, 715 0.0f, 0.0f, 0.3f, 716 0.0f, 0.0f, 0.3f, 717 718 0.0f, 0.0f, 0.3f, 719 0.0f, 0.0f, 0.3f, 720 0.0f, 0.0f, 0.3f, 721 722 0.0f, 0.0f, 0.3f, 723 0.0f, 0.0f, 0.3f, 724 0.0f, 0.0f, 0.3f, 725 726 0.0f, 0.0f, 0.3f, 727 0.0f, 0.0f, 0.3f, 728 0.0f, 0.0f, 0.3f, 729 730 0.0f, 0.0f, 0.3f, 731 0.0f, 0.0f, 0.3f, 732 0.0f, 0.0f, 0.3f, 733 734 0.0f, 0.0f, 0.3f, 735 0.0f, 0.0f, 0.3f, 736 0.0f, 0.0f, 0.3f, 737 0.0f, 0.0f, 0.3f, 738 0.0f, 0.0f, 0.3f, 739 0.0f, 0.0f, 0.3f, 740 741 0.0f, 0.0f, 0.3f, 742 0.0f, 0.0f, 0.3f, 743 0.0f, 0.0f, 0.3f, 744 0.0f, 0.0f, 0.3f, 745 0.0f, 0.0f, 0.3f, 746 0.0f, 0.0f, 0.3f, 747 748 0.0f, 0.0f, 0.3f, 749 0.0f, 0.0f, 0.3f, 750 0.0f, 0.0f, 0.3f, 751 0.0f, 0.0f, 0.3f, 752 0.0f, 0.0f, 0.3f, 753 0.0f, 0.0f, 0.3f, 754 755 0.0f, 0.0f, 0.3f, 756 0.0f, 0.0f, 0.3f, 757 0.0f, 0.0f, 0.3f, 758 0.0f, 0.0f, 0.3f, 759 0.0f, 0.0f, 0.3f, 760 0.0f, 0.0f, 0.3f, 761 762 0.0f, 0.0f, 0.3f, 763 0.0f, 0.0f, 0.3f, 764 0.0f, 0.0f, 0.3f, 765 766 0.0f, 0.0f, 0.3f, 767 0.0f, 0.0f, 0.3f, 768 0.0f, 0.0f, 0.3f, 769 770 0.0f, 0.0f, 0.3f, 771 0.0f, 0.0f, 0.3f, 772 0.0f, 0.0f, 0.3f, 773 774 0.0f, 0.0f, 0.3f, 775 0.0f, 0.0f, 0.3f, 776 0.0f, 0.0f, 0.3f, 777 }; 778 obj.texcoords = { 0.0f }; 779 obj.selected_colors = { 0.0f }; 780 781 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)); 782 //R_model = rotate(mat4(1.0f), 20.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 0.0f, 0.0f)); 783 R_model = mat4(1.0f); 784 obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 785 786 obj.translate_mat = T_model; 787 788 initObject(obj); 789 objects.push_back(obj); 421 SceneObject* ship = createShip(color_sp); 422 objects.push_back(ship); 790 423 791 424 vector<SceneObject>::iterator obj_it; … … 953 586 //glPolygonMode(GL_FRONT, GL_LINE); 954 587 955 if (DISABLE_VSYNC && SHOW_FPS) { 956 glfwSwapInterval(0); 957 } 588 // disable vsync to see real framerate 589 //glfwSwapInterval(0); 958 590 959 591 State curState = STATE_MAIN_MENU; … … 964 596 previous_seconds = current_seconds; 965 597 966 if (SHOW_FPS) { 967 elapsed_seconds_fps += elapsed_seconds; 968 if (elapsed_seconds_fps > 0.25f) { 969 fps = (double)frame_count / elapsed_seconds_fps; 970 cout << "FPS: " << fps << endl; 971 972 frame_count = 0; 973 elapsed_seconds_fps = 0.0f; 974 } 975 976 frame_count++; 977 } 598 elapsed_seconds_fps += elapsed_seconds; 599 if (elapsed_seconds_fps > 0.25f) { 600 fps = (double)frame_count / elapsed_seconds_fps; 601 602 frame_count = 0; 603 elapsed_seconds_fps = 0.0f; 604 } 605 606 frame_count++; 978 607 979 608 // Handle events … … 1008 637 elapsed_seconds_spawn += elapsed_seconds; 1009 638 if (elapsed_seconds_spawn > 0.5f) { 1010 //spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp, 1011 spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)), color_sp, 1012 shaderBufferInfo, 639 SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)), color_sp); 640 addObjectToScene(obj, shaderBufferInfo, 1013 641 points_vbo, 1014 642 colors_vbo, … … 1044 672 1045 673 if (key_down[GLFW_KEY_RIGHT]) { 1046 transformObject( objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo);1047 1048 if (leftLaser Idx != -1 && !objects[leftLaserIdx].deleted) {1049 translateLaser( objects[leftLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo);674 transformObject(*objects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)), ubo); 675 676 if (leftLaser != NULL && !leftLaser->deleted) { 677 translateLaser(leftLaser, vec3(0.01f, 0.0f, 0.0f), ubo); 1050 678 } 1051 if (rightLaser Idx != -1 && !objects[rightLaserIdx].deleted) {1052 translateLaser( objects[rightLaserIdx], vec3(0.01f, 0.0f, 0.0f), ubo);679 if (rightLaser != NULL && !rightLaser->deleted) { 680 translateLaser(rightLaser, vec3(0.01f, 0.0f, 0.0f), ubo); 1053 681 } 1054 682 } 1055 683 if (key_down[GLFW_KEY_LEFT]) { 1056 transformObject( objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo);1057 1058 if (leftLaser Idx != -1 && !objects[leftLaserIdx].deleted) {1059 translateLaser( objects[leftLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo);684 transformObject(*objects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)), ubo); 685 686 if (leftLaser != NULL && !leftLaser->deleted) { 687 translateLaser(leftLaser, vec3(-0.01f, 0.0f, 0.0f), ubo); 1060 688 } 1061 if (rightLaser Idx != -1 && !objects[rightLaserIdx].deleted) {1062 translateLaser( objects[rightLaserIdx], vec3(-0.01f, 0.0f, 0.0f), ubo);689 if (rightLaser != NULL && !rightLaser->deleted) { 690 translateLaser(rightLaser, vec3(-0.01f, 0.0f, 0.0f), ubo); 1063 691 } 1064 692 } 1065 693 1066 694 if (key_state[GLFW_KEY_Z] == GLFW_PRESS) { 1067 vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1068 leftLaserIdx = objects.size(); 1069 1070 SceneObject obj = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset, 695 vec3 offset(objects[0]->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 696 697 leftLaser = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset, 1071 698 vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp); 1072 addObjectToScene( obj, shaderBufferInfo,699 addObjectToScene(leftLaser, shaderBufferInfo, 1073 700 points_vbo, 1074 701 colors_vbo, … … 1079 706 model_mat_idx_vbo); 1080 707 } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) { 1081 removeObjectFromScene( objects[leftLaserIdx], ubo);708 removeObjectFromScene(*leftLaser, ubo); 1082 709 } 1083 710 1084 711 if (key_state[GLFW_KEY_X] == GLFW_PRESS) { 1085 vec3 offset(objects[0].model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1086 rightLaserIdx = objects.size(); 1087 1088 SceneObject obj = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset, 712 vec3 offset(objects[0]->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 713 714 rightLaser = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset, 1089 715 vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp); 1090 addObjectToScene( obj, shaderBufferInfo,716 addObjectToScene(rightLaser, shaderBufferInfo, 1091 717 points_vbo, 1092 718 colors_vbo, … … 1097 723 model_mat_idx_vbo); 1098 724 } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) { 1099 removeObjectFromScene( objects[rightLaserIdx], ubo);725 removeObjectFromScene(*rightLaser, ubo); 1100 726 } 1101 727 1102 728 // this code moves the asteroids 1103 729 for (int i = 0; i < objects.size(); i++) { 1104 if (objects[i] .type == TYPE_ASTEROID && !objects[i].deleted) {1105 transformObject( objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo);1106 1107 vec3 obj_center = vec3(view_mat * vec4(objects[i] .bounding_center, 1.0f));1108 1109 if ((obj_center.z - objects[i] .bounding_radius) > -NEAR_CLIP) {1110 removeObjectFromScene( objects[i], ubo);730 if (objects[i]->type == TYPE_ASTEROID && !objects[i]->deleted) { 731 transformObject(*objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo); 732 733 vec3 obj_center = vec3(view_mat * vec4(objects[i]->bounding_center, 1.0f)); 734 735 if ((obj_center.z - objects[i]->bounding_radius) > -NEAR_CLIP) { 736 removeObjectFromScene(*objects[i], ubo); 1111 737 } 1112 738 } 1113 739 } 1114 740 1115 if (leftLaser Idx != -1 && !objects[leftLaserIdx].deleted) {1116 updateLaserTarget( objects[leftLaserIdx], objects, points_vbo);741 if (leftLaser != NULL && !leftLaser->deleted) { 742 updateLaserTarget(leftLaser, objects, points_vbo); 1117 743 } 1118 if (rightLaserIdx != -1 && !objects[rightLaserIdx].deleted) { 1119 updateLaserTarget(objects[rightLaserIdx], objects, points_vbo); 744 if (rightLaser != NULL && !rightLaser->deleted) { 745 updateLaserTarget(rightLaser, objects, points_vbo); 746 } 747 } 748 749 for (vector<EffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) { 750 if ((*it)->deleted || (*it)->effectedObject->deleted) { 751 delete *it; 752 it = effects.erase(it); 753 } else { 754 EffectOverTime* eot = *it; 755 eot->effectedValue = eot->startValue + (current_seconds - eot->startTime) * eot->changePerSecond; 756 757 it++; 1120 758 } 1121 759 } … … 1168 806 } 1169 807 */ 1170 if (cam_moved ) {808 if (cam_moved && false) { // disable camera movement 1171 809 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 1172 810 … … 1219 857 glfwTerminate(); 1220 858 859 // free memory 860 861 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 862 delete *it; 863 } 864 1221 865 return 0; 1222 866 } … … 1248 892 SceneObject* closest_object = NULL; 1249 893 1250 for (vector<SceneObject >::iterator it = objects.begin(); it != objects.end(); it++) {1251 if ( it->type == TYPE_LASER) continue;1252 for (unsigned int p_idx = 0; p_idx < it->points.size(); p_idx += 9) {894 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 895 if ((*it)->type == TYPE_LASER) continue; 896 for (unsigned int p_idx = 0; p_idx < (*it)->points.size(); p_idx += 9) { 1253 897 if (faceClicked( 1254 898 { 1255 vec3( it->points[p_idx], it->points[p_idx + 1], it->points[p_idx + 2]),1256 vec3( it->points[p_idx + 3], it->points[p_idx + 4], it->points[p_idx + 5]),1257 vec3( it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),899 vec3((*it)->points[p_idx], (*it)->points[p_idx + 1], (*it)->points[p_idx + 2]), 900 vec3((*it)->points[p_idx + 3], (*it)->points[p_idx + 4], (*it)->points[p_idx + 5]), 901 vec3((*it)->points[p_idx + 6], (*it)->points[p_idx + 7], (*it)->points[p_idx + 8]), 1258 902 }, 1259 &*it, ray_world, vec4(cam_pos, 1.0f), click_point903 *it, ray_world, vec4(cam_pos, 1.0f), click_point 1260 904 )) { 1261 905 click_point = view_mat * click_point; … … 1263 907 if (-NEAR_CLIP >= click_point.z && click_point.z > -FAR_CLIP && click_point.z > closest_point.z) { 1264 908 closest_point = vec3(click_point); 1265 closest_object = &*it;909 closest_object = *it; 1266 910 } 1267 911 } … … 1414 1058 } 1415 1059 1416 void initObject(SceneObject &obj) {1060 void initObject(SceneObject* obj) { 1417 1061 // Each objects must have at least 3 points, so the size of 1418 1062 // the points array must be a positive multiple of 9 1419 if (obj.points.size() == 0 || (obj.points.size() % 9) != 0) { 1063 if (obj->points.size() == 0 || (obj->points.size() % 9) != 0) { 1064 // TODO: Maybe throw some kind of error here instead 1420 1065 return; 1421 1066 } 1422 1067 1423 obj .id = objects.size(); // currently unused1424 obj .num_points = obj.points.size() / 3;1425 obj .model_transform = mat4(1.0f);1426 obj .deleted = false;1427 1428 obj .normals.reserve(obj.points.size());1429 for (int i = 0; i < obj .points.size(); i += 9) {1430 vec3 point1 = vec3(obj .points[i], obj.points[i + 1], obj.points[i + 2]);1431 vec3 point2 = vec3(obj .points[i + 3], obj.points[i + 4], obj.points[i + 5]);1432 vec3 point3 = vec3(obj .points[i + 6], obj.points[i + 7], obj.points[i + 8]);1068 obj->id = objects.size(); // currently unused 1069 obj->num_points = obj->points.size() / 3; 1070 obj->model_transform = mat4(1.0f); 1071 obj->deleted = false; 1072 1073 obj->normals.reserve(obj->points.size()); 1074 for (int i = 0; i < obj->points.size(); i += 9) { 1075 vec3 point1 = vec3(obj->points[i], obj->points[i + 1], obj->points[i + 2]); 1076 vec3 point2 = vec3(obj->points[i + 3], obj->points[i + 4], obj->points[i + 5]); 1077 vec3 point3 = vec3(obj->points[i + 6], obj->points[i + 7], obj->points[i + 8]); 1433 1078 1434 1079 vec3 normal = normalize(cross(point2 - point1, point3 - point1)); … … 1436 1081 // Add the same normal for all 3 points 1437 1082 for (int j = 0; j < 3; j++) { 1438 obj .normals.push_back(normal.x);1439 obj .normals.push_back(normal.y);1440 obj .normals.push_back(normal.z);1441 } 1442 } 1443 1444 if (obj .type != TYPE_LASER) {1083 obj->normals.push_back(normal.x); 1084 obj->normals.push_back(normal.y); 1085 obj->normals.push_back(normal.z); 1086 } 1087 } 1088 1089 if (obj->type != TYPE_LASER) { 1445 1090 calculateObjectBoundingBox(obj); 1446 1091 1447 obj .bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));1448 } 1449 } 1450 1451 void addObjectToScene(SceneObject &obj,1092 obj->bounding_center = vec3(obj->translate_mat * vec4(obj->bounding_center, 1.0f)); 1093 } 1094 } 1095 1096 void addObjectToScene(SceneObject* obj, 1452 1097 map<GLuint, BufferInfo>& shaderBufferInfo, 1453 1098 GLuint points_vbo, … … 1460 1105 objects.push_back(obj); 1461 1106 1462 BufferInfo* bufferInfo = &shaderBufferInfo[obj .shader_program];1107 BufferInfo* bufferInfo = &shaderBufferInfo[obj->shader_program]; 1463 1108 1464 1109 // Check if the buffers aren't large enough to fit the new object and, if so, call 1465 1110 // populateBuffers() to resize and repopupulate them 1466 if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj .num_points) ||1111 if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj->num_points) || 1467 1112 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1468 1113 1469 if (leftLaser Idx != -1 && objects[leftLaserIdx].deleted) {1470 leftLaser Idx = -1;1471 } 1472 if (rightLaser Idx != -1 && objects[rightLaserIdx].deleted) {1473 rightLaser Idx = -1;1114 if (leftLaser != NULL && leftLaser->deleted) { 1115 leftLaser = NULL; 1116 } 1117 if (rightLaser != NULL && rightLaser->deleted) { 1118 rightLaser = NULL; 1474 1119 } 1475 1120 … … 1483 1128 model_mat_idx_vbo); 1484 1129 } else { 1485 copyObjectDataToBuffers( objects.back(), shaderBufferInfo,1130 copyObjectDataToBuffers(*objects.back(), shaderBufferInfo, 1486 1131 points_vbo, 1487 1132 colors_vbo, … … 1503 1148 } 1504 1149 1505 void calculateObjectBoundingBox(SceneObject &obj) {1506 GLfloat min_x = obj .points[0];1507 GLfloat max_x = obj .points[0];1508 GLfloat min_y = obj .points[1];1509 GLfloat max_y = obj .points[1];1510 GLfloat min_z = obj .points[2];1511 GLfloat max_z = obj .points[2];1150 void calculateObjectBoundingBox(SceneObject* obj) { 1151 GLfloat min_x = obj->points[0]; 1152 GLfloat max_x = obj->points[0]; 1153 GLfloat min_y = obj->points[1]; 1154 GLfloat max_y = obj->points[1]; 1155 GLfloat min_z = obj->points[2]; 1156 GLfloat max_z = obj->points[2]; 1512 1157 1513 1158 // start from the second point 1514 for (int i = 3; i < obj .points.size(); i += 3) {1515 if (min_x > obj .points[i]) {1516 min_x = obj .points[i];1517 } 1518 else if (max_x < obj .points[i]) {1519 max_x = obj .points[i];1520 } 1521 1522 if (min_y > obj .points[i + 1]) {1523 min_y = obj .points[i + 1];1524 } 1525 else if (max_y < obj .points[i + 1]) {1526 max_y = obj .points[i + 1];1527 } 1528 1529 if (min_z > obj .points[i + 2]) {1530 min_z = obj .points[i + 2];1531 } 1532 else if (max_z < obj .points[i + 2]) {1533 max_z = obj .points[i + 2];1534 } 1535 } 1536 1537 obj .bounding_center = vec3((min_x + max_x) / 2.0f, (min_y + max_y) / 2.0f, (min_z + max_z) / 2.0f);1538 1539 GLfloat radius_x = max_x - obj .bounding_center.x;1540 GLfloat radius_y = max_y - obj .bounding_center.y;1541 GLfloat radius_z = max_z - obj .bounding_center.z;1159 for (int i = 3; i < obj->points.size(); i += 3) { 1160 if (min_x > obj->points[i]) { 1161 min_x = obj->points[i]; 1162 } 1163 else if (max_x < obj->points[i]) { 1164 max_x = obj->points[i]; 1165 } 1166 1167 if (min_y > obj->points[i + 1]) { 1168 min_y = obj->points[i + 1]; 1169 } 1170 else if (max_y < obj->points[i + 1]) { 1171 max_y = obj->points[i + 1]; 1172 } 1173 1174 if (min_z > obj->points[i + 2]) { 1175 min_z = obj->points[i + 2]; 1176 } 1177 else if (max_z < obj->points[i + 2]) { 1178 max_z = obj->points[i + 2]; 1179 } 1180 } 1181 1182 obj->bounding_center = vec3((min_x + max_x) / 2.0f, (min_y + max_y) / 2.0f, (min_z + max_z) / 2.0f); 1183 1184 GLfloat radius_x = max_x - obj->bounding_center.x; 1185 GLfloat radius_y = max_y - obj->bounding_center.y; 1186 GLfloat radius_z = max_z - obj->bounding_center.z; 1542 1187 1543 1188 // TODO: This actually underestimates the radius. Might need to be fixed at some point. 1544 1189 // TODO: Does not take into account any scaling in the model matrix 1545 obj.bounding_radius = radius_x; 1546 if (obj.bounding_radius < radius_y) 1547 obj.bounding_radius = radius_y; 1548 if (obj.bounding_radius < radius_z) 1549 obj.bounding_radius = radius_z; 1550 1551 for (int i = 0; i < obj.points.size(); i += 3) { 1552 obj.points[i] -= obj.bounding_center.x; 1553 obj.points[i + 1] -= obj.bounding_center.y; 1554 obj.points[i + 2] -= obj.bounding_center.z; 1555 } 1556 1557 obj.bounding_center = vec3(0.0f, 0.0f, 0.0f); 1190 obj->bounding_radius = radius_x; 1191 if (obj->bounding_radius < radius_y) 1192 obj->bounding_radius = radius_y; 1193 if (obj->bounding_radius < radius_z) 1194 obj->bounding_radius = radius_z; 1195 1196 for (int i = 0; i < obj->points.size(); i += 3) { 1197 obj->points[i] -= obj->bounding_center.x; 1198 obj->points[i + 1] -= obj->bounding_center.y; 1199 obj->points[i + 2] -= obj->bounding_center.z; 1200 } 1201 1202 obj->bounding_center = vec3(0.0f, 0.0f, 0.0f); 1203 } 1204 1205 SceneObject* createShip(GLuint shader) { 1206 SceneObject* ship = new SceneObject(); 1207 1208 ship->type = TYPE_SHIP; 1209 ship->shader_program = shader; 1210 1211 ship->points = { 1212 //back 1213 -0.5f, 0.3f, 0.0f, 1214 -0.5f, 0.0f, 0.0f, 1215 0.5f, 0.0f, 0.0f, 1216 -0.5f, 0.3f, 0.0f, 1217 0.5f, 0.0f, 0.0f, 1218 0.5f, 0.3f, 0.0f, 1219 1220 // left back 1221 -0.5f, 0.3f, -2.0f, 1222 -0.5f, 0.0f, -2.0f, 1223 -0.5f, 0.0f, 0.0f, 1224 -0.5f, 0.3f, -2.0f, 1225 -0.5f, 0.0f, 0.0f, 1226 -0.5f, 0.3f, 0.0f, 1227 1228 // right back 1229 0.5f, 0.3f, 0.0f, 1230 0.5f, 0.0f, 0.0f, 1231 0.5f, 0.0f, -2.0f, 1232 0.5f, 0.3f, 0.0f, 1233 0.5f, 0.0f, -2.0f, 1234 0.5f, 0.3f, -2.0f, 1235 1236 // left mid 1237 -0.25f, 0.3f, -3.0f, 1238 -0.25f, 0.0f, -3.0f, 1239 -0.5f, 0.0f, -2.0f, 1240 -0.25f, 0.3f, -3.0f, 1241 -0.5f, 0.0f, -2.0f, 1242 -0.5f, 0.3f, -2.0f, 1243 1244 // right mid 1245 0.5f, 0.3f, -2.0f, 1246 0.5f, 0.0f, -2.0f, 1247 0.25f, 0.0f, -3.0f, 1248 0.5f, 0.3f, -2.0f, 1249 0.25f, 0.0f, -3.0f, 1250 0.25f, 0.3f, -3.0f, 1251 1252 // left front 1253 0.0f, 0.0f, -3.5f, 1254 -0.25f, 0.0f, -3.0f, 1255 -0.25f, 0.3f, -3.0f, 1256 1257 // right front 1258 0.25f, 0.3f, -3.0f, 1259 0.25f, 0.0f, -3.0f, 1260 0.0f, 0.0f, -3.5f, 1261 1262 // top back 1263 -0.5f, 0.3f, -2.0f, 1264 -0.5f, 0.3f, 0.0f, 1265 0.5f, 0.3f, 0.0f, 1266 -0.5f, 0.3f, -2.0f, 1267 0.5f, 0.3f, 0.0f, 1268 0.5f, 0.3f, -2.0f, 1269 1270 // bottom back 1271 -0.5f, 0.0f, 0.0f, 1272 -0.5f, 0.0f, -2.0f, 1273 0.5f, 0.0f, 0.0f, 1274 0.5f, 0.0f, 0.0f, 1275 -0.5f, 0.0f, -2.0f, 1276 0.5f, 0.0f, -2.0f, 1277 1278 // top mid 1279 -0.25f, 0.3f, -3.0f, 1280 -0.5f, 0.3f, -2.0f, 1281 0.5f, 0.3f, -2.0f, 1282 -0.25f, 0.3f, -3.0f, 1283 0.5f, 0.3f, -2.0f, 1284 0.25f, 0.3f, -3.0f, 1285 1286 // bottom mid 1287 -0.5f, 0.0f, -2.0f, 1288 -0.25f, 0.0f, -3.0f, 1289 0.5f, 0.0f, -2.0f, 1290 0.5f, 0.0f, -2.0f, 1291 -0.25f, 0.0f, -3.0f, 1292 0.25f, 0.0f, -3.0f, 1293 1294 // top front 1295 -0.25f, 0.3f, -3.0f, 1296 0.25f, 0.3f, -3.0f, 1297 0.0f, 0.0f, -3.5f, 1298 1299 // bottom front 1300 0.25f, 0.0f, -3.0f, 1301 -0.25f, 0.0f, -3.0f, 1302 0.0f, 0.0f, -3.5f, 1303 1304 // left wing start back 1305 -1.5f, 0.3f, 0.0f, 1306 -1.5f, 0.0f, 0.0f, 1307 -0.5f, 0.0f, 0.0f, 1308 -1.5f, 0.3f, 0.0f, 1309 -0.5f, 0.0f, 0.0f, 1310 -0.5f, 0.3f, 0.0f, 1311 1312 // left wing start top 1313 -0.5f, 0.3f, -0.3f, 1314 -1.3f, 0.3f, -0.3f, 1315 -1.5f, 0.3f, 0.0f, 1316 -0.5f, 0.3f, -0.3f, 1317 -1.5f, 0.3f, 0.0f, 1318 -0.5f, 0.3f, 0.0f, 1319 1320 // left wing start front 1321 -0.5f, 0.3f, -0.3f, 1322 -0.5f, 0.0f, -0.3f, 1323 -1.3f, 0.0f, -0.3f, 1324 -0.5f, 0.3f, -0.3f, 1325 -1.3f, 0.0f, -0.3f, 1326 -1.3f, 0.3f, -0.3f, 1327 1328 // left wing start bottom 1329 -0.5f, 0.0f, 0.0f, 1330 -1.5f, 0.0f, 0.0f, 1331 -1.3f, 0.0f, -0.3f, 1332 -0.5f, 0.0f, 0.0f, 1333 -1.3f, 0.0f, -0.3f, 1334 -0.5f, 0.0f, -0.3f, 1335 1336 // left wing end outside 1337 -1.5f, 0.3f, 0.0f, 1338 -2.2f, 0.15f, -0.8f, 1339 -1.5f, 0.0f, 0.0f, 1340 1341 // left wing end top 1342 -1.3f, 0.3f, -0.3f, 1343 -2.2f, 0.15f, -0.8f, 1344 -1.5f, 0.3f, 0.0f, 1345 1346 // left wing end front 1347 -1.3f, 0.0f, -0.3f, 1348 -2.2f, 0.15f, -0.8f, 1349 -1.3f, 0.3f, -0.3f, 1350 1351 // left wing end bottom 1352 -1.5f, 0.0f, 0.0f, 1353 -2.2f, 0.15f, -0.8f, 1354 -1.3f, 0.0f, -0.3f, 1355 1356 // right wing start back 1357 1.5f, 0.0f, 0.0f, 1358 1.5f, 0.3f, 0.0f, 1359 0.5f, 0.0f, 0.0f, 1360 0.5f, 0.0f, 0.0f, 1361 1.5f, 0.3f, 0.0f, 1362 0.5f, 0.3f, 0.0f, 1363 1364 // right wing start top 1365 1.3f, 0.3f, -0.3f, 1366 0.5f, 0.3f, -0.3f, 1367 1.5f, 0.3f, 0.0f, 1368 1.5f, 0.3f, 0.0f, 1369 0.5f, 0.3f, -0.3f, 1370 0.5f, 0.3f, 0.0f, 1371 1372 // right wing start front 1373 0.5f, 0.0f, -0.3f, 1374 0.5f, 0.3f, -0.3f, 1375 1.3f, 0.0f, -0.3f, 1376 1.3f, 0.0f, -0.3f, 1377 0.5f, 0.3f, -0.3f, 1378 1.3f, 0.3f, -0.3f, 1379 1380 // right wing start bottom 1381 1.5f, 0.0f, 0.0f, 1382 0.5f, 0.0f, 0.0f, 1383 1.3f, 0.0f, -0.3f, 1384 1.3f, 0.0f, -0.3f, 1385 0.5f, 0.0f, 0.0f, 1386 0.5f, 0.0f, -0.3f, 1387 1388 // right wing end outside 1389 2.2f, 0.15f, -0.8f, 1390 1.5f, 0.3f, 0.0f, 1391 1.5f, 0.0f, 0.0f, 1392 1393 // right wing end top 1394 2.2f, 0.15f, -0.8f, 1395 1.3f, 0.3f, -0.3f, 1396 1.5f, 0.3f, 0.0f, 1397 1398 // right wing end front 1399 2.2f, 0.15f, -0.8f, 1400 1.3f, 0.0f, -0.3f, 1401 1.3f, 0.3f, -0.3f, 1402 1403 // right wing end bottom 1404 2.2f, 0.15f, -0.8f, 1405 1.5f, 0.0f, 0.0f, 1406 1.3f, 0.0f, -0.3f, 1407 }; 1408 ship->colors = { 1409 0.0f, 0.0f, 0.3f, 1410 0.0f, 0.0f, 0.3f, 1411 0.0f, 0.0f, 0.3f, 1412 0.0f, 0.0f, 0.3f, 1413 0.0f, 0.0f, 0.3f, 1414 0.0f, 0.0f, 0.3f, 1415 1416 0.0f, 0.0f, 0.3f, 1417 0.0f, 0.0f, 0.3f, 1418 0.0f, 0.0f, 0.3f, 1419 0.0f, 0.0f, 0.3f, 1420 0.0f, 0.0f, 0.3f, 1421 0.0f, 0.0f, 0.3f, 1422 1423 0.0f, 0.0f, 0.3f, 1424 0.0f, 0.0f, 0.3f, 1425 0.0f, 0.0f, 0.3f, 1426 0.0f, 0.0f, 0.3f, 1427 0.0f, 0.0f, 0.3f, 1428 0.0f, 0.0f, 0.3f, 1429 1430 0.0f, 0.0f, 0.3f, 1431 0.0f, 0.0f, 0.3f, 1432 0.0f, 0.0f, 0.3f, 1433 0.0f, 0.0f, 0.3f, 1434 0.0f, 0.0f, 0.3f, 1435 0.0f, 0.0f, 0.3f, 1436 1437 0.0f, 0.0f, 0.3f, 1438 0.0f, 0.0f, 0.3f, 1439 0.0f, 0.0f, 0.3f, 1440 0.0f, 0.0f, 0.3f, 1441 0.0f, 0.0f, 0.3f, 1442 0.0f, 0.0f, 0.3f, 1443 1444 0.0f, 0.0f, 1.0f, 1445 0.0f, 0.0f, 1.0f, 1446 0.0f, 0.0f, 1.0f, 1447 1448 0.0f, 0.0f, 1.0f, 1449 0.0f, 0.0f, 1.0f, 1450 0.0f, 0.0f, 1.0f, 1451 1452 0.0f, 0.0f, 1.0f, 1453 0.0f, 0.0f, 1.0f, 1454 0.0f, 0.0f, 1.0f, 1455 0.0f, 0.0f, 1.0f, 1456 0.0f, 0.0f, 1.0f, 1457 0.0f, 0.0f, 1.0f, 1458 1459 0.0f, 0.0f, 1.0f, 1460 0.0f, 0.0f, 1.0f, 1461 0.0f, 0.0f, 1.0f, 1462 0.0f, 0.0f, 1.0f, 1463 0.0f, 0.0f, 1.0f, 1464 0.0f, 0.0f, 1.0f, 1465 1466 0.0f, 0.0f, 1.0f, 1467 0.0f, 0.0f, 1.0f, 1468 0.0f, 0.0f, 1.0f, 1469 0.0f, 0.0f, 1.0f, 1470 0.0f, 0.0f, 1.0f, 1471 0.0f, 0.0f, 1.0f, 1472 1473 0.0f, 0.0f, 1.0f, 1474 0.0f, 0.0f, 1.0f, 1475 0.0f, 0.0f, 1.0f, 1476 0.0f, 0.0f, 1.0f, 1477 0.0f, 0.0f, 1.0f, 1478 0.0f, 0.0f, 1.0f, 1479 1480 0.0f, 0.0f, 0.3f, 1481 0.0f, 0.0f, 0.3f, 1482 0.0f, 0.0f, 0.3f, 1483 1484 0.0f, 0.0f, 0.3f, 1485 0.0f, 0.0f, 0.3f, 1486 0.0f, 0.0f, 0.3f, 1487 1488 0.0f, 0.0f, 0.3f, 1489 0.0f, 0.0f, 0.3f, 1490 0.0f, 0.0f, 0.3f, 1491 0.0f, 0.0f, 0.3f, 1492 0.0f, 0.0f, 0.3f, 1493 0.0f, 0.0f, 0.3f, 1494 1495 0.0f, 0.0f, 0.3f, 1496 0.0f, 0.0f, 0.3f, 1497 0.0f, 0.0f, 0.3f, 1498 0.0f, 0.0f, 0.3f, 1499 0.0f, 0.0f, 0.3f, 1500 0.0f, 0.0f, 0.3f, 1501 1502 0.0f, 0.0f, 0.3f, 1503 0.0f, 0.0f, 0.3f, 1504 0.0f, 0.0f, 0.3f, 1505 0.0f, 0.0f, 0.3f, 1506 0.0f, 0.0f, 0.3f, 1507 0.0f, 0.0f, 0.3f, 1508 1509 0.0f, 0.0f, 0.3f, 1510 0.0f, 0.0f, 0.3f, 1511 0.0f, 0.0f, 0.3f, 1512 0.0f, 0.0f, 0.3f, 1513 0.0f, 0.0f, 0.3f, 1514 0.0f, 0.0f, 0.3f, 1515 1516 0.0f, 0.0f, 0.3f, 1517 0.0f, 0.0f, 0.3f, 1518 0.0f, 0.0f, 0.3f, 1519 1520 0.0f, 0.0f, 0.3f, 1521 0.0f, 0.0f, 0.3f, 1522 0.0f, 0.0f, 0.3f, 1523 1524 0.0f, 0.0f, 0.3f, 1525 0.0f, 0.0f, 0.3f, 1526 0.0f, 0.0f, 0.3f, 1527 1528 0.0f, 0.0f, 0.3f, 1529 0.0f, 0.0f, 0.3f, 1530 0.0f, 0.0f, 0.3f, 1531 1532 0.0f, 0.0f, 0.3f, 1533 0.0f, 0.0f, 0.3f, 1534 0.0f, 0.0f, 0.3f, 1535 0.0f, 0.0f, 0.3f, 1536 0.0f, 0.0f, 0.3f, 1537 0.0f, 0.0f, 0.3f, 1538 1539 0.0f, 0.0f, 0.3f, 1540 0.0f, 0.0f, 0.3f, 1541 0.0f, 0.0f, 0.3f, 1542 0.0f, 0.0f, 0.3f, 1543 0.0f, 0.0f, 0.3f, 1544 0.0f, 0.0f, 0.3f, 1545 1546 0.0f, 0.0f, 0.3f, 1547 0.0f, 0.0f, 0.3f, 1548 0.0f, 0.0f, 0.3f, 1549 0.0f, 0.0f, 0.3f, 1550 0.0f, 0.0f, 0.3f, 1551 0.0f, 0.0f, 0.3f, 1552 1553 0.0f, 0.0f, 0.3f, 1554 0.0f, 0.0f, 0.3f, 1555 0.0f, 0.0f, 0.3f, 1556 0.0f, 0.0f, 0.3f, 1557 0.0f, 0.0f, 0.3f, 1558 0.0f, 0.0f, 0.3f, 1559 1560 0.0f, 0.0f, 0.3f, 1561 0.0f, 0.0f, 0.3f, 1562 0.0f, 0.0f, 0.3f, 1563 1564 0.0f, 0.0f, 0.3f, 1565 0.0f, 0.0f, 0.3f, 1566 0.0f, 0.0f, 0.3f, 1567 1568 0.0f, 0.0f, 0.3f, 1569 0.0f, 0.0f, 0.3f, 1570 0.0f, 0.0f, 0.3f, 1571 1572 0.0f, 0.0f, 0.3f, 1573 0.0f, 0.0f, 0.3f, 1574 0.0f, 0.0f, 0.3f, 1575 }; 1576 ship->texcoords = { 0.0f }; 1577 ship->selected_colors = { 0.0f }; 1578 1579 mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)); 1580 mat4 R_model(1.0f); 1581 ship->model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 1582 1583 ship->translate_mat = T_model; 1584 1585 initObject(ship); 1586 1587 return ship; 1558 1588 } 1559 1589 … … 1572 1602 // TODO: Make the color parameter have an effect 1573 1603 // TODO: Come up with a better way of passing the object back than copying it 1574 SceneObject createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1575 SceneObject obj = SceneObject(); 1576 obj.type = TYPE_LASER; 1577 obj.shader_program = laser_sp; 1604 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1605 Laser* obj = new Laser(); 1606 obj->type = TYPE_LASER; 1607 obj->targetAsteroid = NULL; 1608 obj->shader_program = laser_sp; 1578 1609 1579 1610 vec3 ray = end - start; 1580 1611 float length = glm::length(ray); 1581 1612 1582 obj .points = {1613 obj->points = { 1583 1614 width / 2, 0.0f, -width / 2, 1584 1615 -width / 2, 0.0f, -width / 2, … … 1601 1632 }; 1602 1633 1603 obj .texcoords = {1634 obj->texcoords = { 1604 1635 1.0f, 0.5f, 1605 1636 0.0f, 0.5f, … … 1636 1667 float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam)); 1637 1668 1638 obj .model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));1669 obj->model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)); 1639 1670 1640 1671 initObject(obj); 1641 1672 1642 obj .model_transform = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj.model_transform;1643 obj .model_transform = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_transform;1644 obj .model_transform = translate(mat4(1.0f), start) * obj.model_transform;1673 obj->model_transform = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj->model_transform; 1674 obj->model_transform = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj->model_transform; 1675 obj->model_transform = translate(mat4(1.0f), start) * obj->model_transform; 1645 1676 1646 1677 return obj; … … 1677 1708 } 1678 1709 1679 void populateBuffers(vector<SceneObject >& objects,1710 void populateBuffers(vector<SceneObject*>& objects, 1680 1711 map<GLuint, BufferInfo>& shaderBufferInfo, 1681 1712 GLuint points_vbo, … … 1694 1725 map<GLuint, unsigned int> shaderUboCounts; 1695 1726 1696 vector<SceneObject >::iterator it;1727 vector<SceneObject*>::iterator it; 1697 1728 1698 1729 /* Find all shaders that need to be used and the number of objects and … … 1702 1733 * vertex buffer for each shader. 1703 1734 */ 1704 for (it = objects.begin(); it != objects.end();) { 1705 if (it->deleted) { 1706 // terrible way of keeping track of the laser scene objects 1707 // TODO: Find a more robust way of doing this, one that would work well 1708 // for any objects in the scene 1709 if (leftLaserIdx > it - objects.begin()) { 1710 leftLaserIdx--; 1711 } 1712 if (rightLaserIdx > it - objects.begin()) { 1713 rightLaserIdx--; 1714 } 1715 1735 for (it = objects.begin(); it != objects.end(); ) { 1736 if ((*it)->deleted) { 1737 delete *it; 1716 1738 it = objects.erase(it); 1717 1739 } else { 1718 points_buffer_size += it->num_points * sizeof(GLfloat) * 3;1719 textures_buffer_size += it->num_points * sizeof(GLfloat) * 2;1740 points_buffer_size += (*it)->num_points * sizeof(GLfloat) * 3; 1741 textures_buffer_size += (*it)->num_points * sizeof(GLfloat) * 2; 1720 1742 ubo_buffer_size += 16 * sizeof(GLfloat); 1721 model_mat_idx_buffer_size += it->num_points * sizeof(GLuint);1722 1723 if (shaderCounts.count( it->shader_program) == 0) {1724 shaderCounts[ it->shader_program] = it->num_points;1725 shaderUboCounts[ it->shader_program] = 1;1743 model_mat_idx_buffer_size += (*it)->num_points * sizeof(GLuint); 1744 1745 if (shaderCounts.count((*it)->shader_program) == 0) { 1746 shaderCounts[(*it)->shader_program] = (*it)->num_points; 1747 shaderUboCounts[(*it)->shader_program] = 1; 1726 1748 } else { 1727 shaderCounts[ it->shader_program] += it->num_points;1728 shaderUboCounts[ it->shader_program]++;1749 shaderCounts[(*it)->shader_program] += (*it)->num_points; 1750 shaderUboCounts[(*it)->shader_program]++; 1729 1751 } 1730 1752 … … 1796 1818 1797 1819 for (it = objects.begin(); it != objects.end(); it++) { 1798 copyObjectDataToBuffers(* it, shaderBufferInfo,1820 copyObjectDataToBuffers(**it, shaderBufferInfo, 1799 1821 points_vbo, 1800 1822 colors_vbo, … … 1863 1885 } 1864 1886 1865 void translateLaser( SceneObject&laser, const vec3& translation, GLuint ubo) {1887 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo) { 1866 1888 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 1867 1889 // and then re-used here 1868 1890 1869 mat4 new_model_transform = translate(mat4(1.0f), translation) * laser .model_transform;1870 1871 vec3 start = vec3(laser .model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));1872 vec3 end = vec3(laser .model_transform * vec4(0.0f, 0.0f, laser.points[38], 1.0f));1891 mat4 new_model_transform = translate(mat4(1.0f), translation) * laser->model_transform; 1892 1893 vec3 start = vec3(laser->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1894 vec3 end = vec3(laser->model_transform * vec4(0.0f, 0.0f, laser->points[38], 1.0f)); 1873 1895 1874 1896 vec3 ray = end - start; … … 1889 1911 float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam)); 1890 1912 1891 laser .model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f));1892 1893 transformObject( laser, translate(mat4(1.0f), translation), ubo);1894 } 1895 1896 void updateLaserTarget( SceneObject& laser, vector<SceneObject>& objects, GLuint points_vbo) {1913 laser->model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)); 1914 1915 transformObject(*laser, translate(mat4(1.0f), translation), ubo); 1916 } 1917 1918 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo) { 1897 1919 // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created, 1898 1920 // and then re-used here 1899 1921 1900 vec3 start = vec3(laser .model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f));1901 vec3 end = vec3(laser .model_transform * vec4(0.0f, 0.0f, laser.points[2] + laser.points[20], 1.0f));1922 vec3 start = vec3(laser->model_transform * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1923 vec3 end = vec3(laser->model_transform * vec4(0.0f, 0.0f, laser->points[2] + laser->points[20], 1.0f)); 1902 1924 1903 1925 vec3 intersection(0.0f), closestIntersection(0.0f); 1904 SceneObject* closestAsteroid = NULL;1905 1906 for (vector<SceneObject >::iterator it = objects.begin(); it != objects.end(); it++) {1907 if ( it->type == TYPE_ASTEROID && !it->deleted && getLaserAndAsteroidIntersection(start, end,*it, intersection)) {1926 Asteroid* closestAsteroid = NULL; 1927 1928 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 1929 if ((*it)->type == TYPE_ASTEROID && !(*it)->deleted && getLaserAndAsteroidIntersection(start, end, **it, intersection)) { 1908 1930 // TODO: Implement a more generic algorithm for testing the closest object by getting the distance between the points 1909 1931 if (closestAsteroid == NULL || intersection.z > closestIntersection.z) { 1910 1932 // TODO: At this point, find the real intersection of the laser with one of the asteroid's sides 1911 closestAsteroid = &*it;1933 closestAsteroid = (Asteroid*)*it; 1912 1934 closestIntersection = intersection; 1913 1935 } … … 1915 1937 } 1916 1938 1917 float width = laser.points[0] - laser.points[2]; 1918 1919 float length = 5.24f; 1939 float width = laser->points[0] - laser->points[2]; 1940 1941 if (laser->targetAsteroid != closestAsteroid) { 1942 if (laser->targetAsteroid != NULL) { 1943 if (laser == leftLaser) { 1944 leftLaserEffect->deleted = true; 1945 } else if (laser == rightLaser) { 1946 rightLaserEffect->deleted = true; 1947 } 1948 } 1949 1950 EffectOverTime* eot = NULL; 1951 1952 if (closestAsteroid != NULL) { 1953 eot = new EffectOverTime(closestAsteroid->hp, -5, closestAsteroid); 1954 effects.push_back(eot); 1955 } 1956 1957 if (laser == leftLaser) { 1958 leftLaserEffect = eot; 1959 } else if (laser == rightLaser) { 1960 rightLaserEffect = eot; 1961 } 1962 } 1963 laser->targetAsteroid = closestAsteroid; 1964 1965 float length = 5.24f; // I think this was to make sure the laser went past the end of the screen 1920 1966 if (closestAsteroid != NULL) { 1921 1967 length = glm::length(closestIntersection - start); 1922 1968 } 1923 1969 1924 laser .points[20] = -length + width / 2;1925 laser .points[23] = -length + width / 2;1926 laser .points[29] = -length + width / 2;1927 laser .points[38] = -length;1928 laser .points[41] = -length;1929 laser .points[44] = -length + width / 2;1930 laser .points[47] = -length;1931 laser .points[50] = -length + width / 2;1932 laser .points[53] = -length + width / 2;1970 laser->points[20] = -length + width / 2; 1971 laser->points[23] = -length + width / 2; 1972 laser->points[29] = -length + width / 2; 1973 laser->points[38] = -length; 1974 laser->points[41] = -length; 1975 laser->points[44] = -length + width / 2; 1976 laser->points[47] = -length; 1977 laser->points[50] = -length + width / 2; 1978 laser->points[53] = -length + width / 2; 1933 1979 1934 1980 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 1935 glBufferSubData(GL_ARRAY_BUFFER, laser .vertex_vbo_offset * sizeof(GLfloat) * 3, laser.points.size() * sizeof(GLfloat), &laser.points[0]);1981 glBufferSubData(GL_ARRAY_BUFFER, laser->vertex_vbo_offset * sizeof(GLfloat) * 3, laser->points.size() * sizeof(GLfloat), &laser->points[0]); 1936 1982 } 1937 1983 … … 2042 2088 */ 2043 2089 2090 stringstream ss; 2091 ss << "FPS: " << fps; 2092 2044 2093 { 2045 ImGui::SetNextWindowSize(ImVec2( 85, 22), ImGuiCond_Once);2094 ImGui::SetNextWindowSize(ImVec2(95, 46), ImGuiCond_Once); 2046 2095 ImGui::SetNextWindowPos(ImVec2(10, 50), ImGuiCond_Once); 2047 2096 ImGui::Begin("WndStats", NULL, … … 2050 2099 ImGuiWindowFlags_NoMove); 2051 2100 ImGui::Text("Score: ???"); 2101 ImGui::Text(ss.str().c_str()); 2052 2102 ImGui::End(); 2053 2103 } … … 2108 2158 } 2109 2159 2110 void spawnAsteroid(vec3 pos, GLuint shader, 2111 map<GLuint, BufferInfo>& shaderBufferInfo, 2112 GLuint points_vbo, 2113 GLuint colors_vbo, 2114 GLuint selected_colors_vbo, 2115 GLuint texcoords_vbo, 2116 GLuint normals_vbo, 2117 GLuint ubo, 2118 GLuint model_mat_idx_vbo) { 2119 SceneObject obj = SceneObject(); 2120 obj.type = TYPE_ASTEROID; 2121 obj.shader_program = shader; 2122 2123 obj.points = { 2160 Asteroid* createAsteroid(vec3 pos, GLuint shader) { 2161 Asteroid* obj = new Asteroid(); 2162 obj->type = TYPE_ASTEROID; 2163 obj->hp = 10.0f; 2164 obj->shader_program = shader; 2165 2166 obj->points = { 2124 2167 // front 2125 2168 1.0f, 1.0f, 1.0f, … … 2170 2213 -1.0f, -1.0f, 1.0f, 2171 2214 }; 2172 obj .colors = {2215 obj->colors = { 2173 2216 // front 2174 2217 0.8f, 0.0f, 0.0f, … … 2219 2262 0.8f, 0.0f, 0.0f, 2220 2263 }; 2221 obj .texcoords = { 0.0f };2222 obj .selected_colors = { 0.0f };2264 obj->texcoords = { 0.0f }; 2265 obj->selected_colors = { 0.0f }; 2223 2266 2224 2267 mat4 T = translate(mat4(1.0f), pos); 2225 2268 mat4 R = rotate(mat4(1.0f), 60.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 1.0f, -1.0f)); 2226 obj .model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));2227 2228 obj .translate_mat = T;2269 obj->model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 2270 2271 obj->translate_mat = T; 2229 2272 2230 2273 initObject(obj); … … 2234 2277 // TODO: Once the intersection check with the sides of the asteroid is done, 2235 2278 // this can be removed. 2236 obj.bounding_radius /= 8.0f; 2237 2238 addObjectToScene(obj, shaderBufferInfo, 2239 points_vbo, 2240 colors_vbo, 2241 selected_colors_vbo, 2242 texcoords_vbo, 2243 normals_vbo, 2244 ubo, 2245 model_mat_idx_vbo); 2279 obj->bounding_radius /= 8.0f; 2280 2281 return obj; 2246 2282 } 2247 2283
Note:
See TracChangeset
for help on using the changeset viewer.