Changeset 0807aeb in opengl-game
- Timestamp:
- Mar 10, 2020, 2:53:34 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 22217d4
- Parents:
- 44f23af
- git-author:
- Dmitry Portnoy <dmp1488@…> (03/10/20 00:43:57)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (03/10/20 02:53:34)
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
VulkanGame.vcxproj
r44f23af r0807aeb 159 159 <ItemGroup> 160 160 <None Include="assets\laser.xcf" /> 161 <None Include="shaders\asteroid.frag" /> 162 <None Include="shaders\asteroid.vert" /> 161 163 <None Include="shaders\overlay.frag" /> 162 164 <None Include="shaders\overlay.vert" /> -
utils.cpp
r44f23af r0807aeb 1 1 #include "utils.hpp" 2 2 3 #include <ctime> 3 4 #include <iostream> 4 5 6 #include "compiler.hpp" 7 8 #ifdef WINDOWS 9 #include <process.h> 10 #else 11 #include <unistd.h> 12 #endif 13 14 // TODO: Use a more modern method of generating random numbers 15 16 void seedRandomNums() { 17 #ifdef WINDOWS 18 srand(_getpid() ^ time(nullptr)); 19 #else 20 srand(getpid() ^ time(nullptr)); 21 #endif 22 } 23 5 24 float getRandomNum(float low, float high) { 6 return low + ((float)rand() / RAND_MAX) * (high -low);25 return low + ((float)rand() / RAND_MAX) * (high - low); 7 26 } 8 27 -
utils.hpp
r44f23af r0807aeb 10 10 using namespace glm; 11 11 12 void seedRandomNums(); 12 13 float getRandomNum(float low, float high); 13 14 -
vulkan-game.cpp
r44f23af r0807aeb 2 2 3 3 #include <array> 4 #include <chrono>5 4 #include <iostream> 6 5 #include <set> … … 32 31 33 32 void VulkanGame::run(int width, int height, unsigned char guiFlags) { 33 seedRandomNums(); 34 34 35 cout << "DEBUGGING IS " << (ENABLE_VALIDATION_LAYERS ? "ON" : "OFF") << endl; 35 36 … … 586 587 bool quit = false; 587 588 588 curTime = SDL_GetPerformanceCounter(); 589 this->startTime = high_resolution_clock::now(); 590 this->curTime = duration<float, seconds::period>(high_resolution_clock::now() - this->startTime).count(); 591 592 lastSpawn_asteroid = this->curTime; 589 593 590 594 while (!quit) { 591 595 592 prevTime = curTime; 593 curTime = SDL_GetPerformanceCounter(); 594 595 elapsedTime = (double)((curTime - prevTime) * 1000 / (double)SDL_GetPerformanceFrequency()); 596 this->prevTime = this->curTime; 597 this->curTime = duration<float, seconds::period>(high_resolution_clock::now() - this->startTime).count(); 598 this->elapsedTime = this->curTime - this->prevTime; 596 599 597 600 gui->processEvents(); … … 629 632 mat4(1.0f) 630 633 }, true); 631 } else if (e.key.keycode == SDL_SCANCODE_Z) {632 addObject(asteroidObjects, asteroidPipeline,633 addObjectIndex<AsteroidVertex>(asteroidObjects.size(),634 addVertexNormals<AsteroidVertex>({635 636 // front637 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},638 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},639 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},640 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},641 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},642 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},643 644 // top645 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},646 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},647 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},648 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},649 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},650 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},651 652 // bottom653 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},654 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},655 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},656 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},657 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},658 {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}},659 660 // back661 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},662 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},663 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},664 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},665 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},666 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},667 668 // right669 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},670 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},671 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},672 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},673 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},674 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},675 676 // left677 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},678 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},679 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},680 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},681 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},682 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},683 })), {684 0, 1, 2, 3, 4, 5,685 6, 7, 8, 9, 10, 11,686 12, 13, 14, 15, 16, 17,687 18, 19, 20, 21, 22, 23,688 24, 25, 26, 27, 28, 29,689 30, 31, 32, 33, 34, 35,690 }, {691 mat4(1.0f),692 10.0f,693 0694 }, true);695 696 // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *697 asteroidObjects.back().model_base =698 translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f)) *699 rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *700 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));701 702 updateObject(asteroidObjects, asteroidPipeline, asteroidObjects.size() - 1);703 634 } else { 704 635 cout << "Key event detected" << endl; … … 725 656 // Check which keys are held down 726 657 727 float shipSpeed = 0.0005f;728 float asteroidSpeed = 0.002f;729 730 658 if (gui->keyPressed(SDL_SCANCODE_LEFT)) { 731 shipObjects[0].model_transform = translate(mat4(1.0f), vec3(- shipSpeed *elapsedTime, 0.0f, 0.0f))659 shipObjects[0].model_transform = translate(mat4(1.0f), vec3(-this->shipSpeed * this->elapsedTime, 0.0f, 0.0f)) 732 660 * shipObjects[0].model_transform; 733 661 734 662 updateObject(shipObjects, shipPipeline, 0); 735 663 } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) { 736 shipObjects[0].model_transform = translate(mat4(1.0f), vec3( shipSpeed *elapsedTime, 0.0f, 0.0f))664 shipObjects[0].model_transform = translate(mat4(1.0f), vec3(this->shipSpeed * this->elapsedTime, 0.0f, 0.0f)) 737 665 * shipObjects[0].model_transform; 738 666 … … 740 668 } 741 669 670 // TODO: Remove this block of code and correctly update the center of all objects when they are transformed 742 671 if (gui->keyPressed(SDL_SCANCODE_X)) { 743 672 if (asteroidObjects.size() > 0 && !asteroidObjects[0].ssbo.deleted) { 744 asteroidObjects[0].model_transform = translate(mat4(1.0f), vec3(0.0f, 0.0f, asteroidSpeed *elapsedTime))673 asteroidObjects[0].model_transform = translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) 745 674 * asteroidObjects[0].model_transform; 746 675 … … 765 694 // where it will run just once per frame 766 695 void VulkanGame::updateScene(uint32_t currentImage) { 767 static auto startTime = chrono::high_resolution_clock::now();768 769 auto currentTime = chrono::high_resolution_clock::now();770 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();771 772 696 for (int i = 0; i < modelObjects.size(); i++) { 773 697 modelObjects[i].model_transform = 774 698 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 775 rotate(mat4(1.0f), t ime * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));699 rotate(mat4(1.0f), this->curTime * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 776 700 777 701 updateObject(modelObjects, modelPipeline, i); 702 } 703 704 for (int i = 0; i < asteroidObjects.size(); i++) { 705 if (!asteroidObjects[i].ssbo.deleted) { 706 asteroidObjects[i].model_transform = 707 translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) * 708 asteroidObjects[i].model_transform; 709 710 updateObject(asteroidObjects, asteroidPipeline, i); 711 } 712 } 713 714 if (this->curTime - this->lastSpawn_asteroid > this->spawnRate_asteroid) { 715 this->lastSpawn_asteroid = this->curTime; 716 717 addObject(asteroidObjects, asteroidPipeline, 718 addObjectIndex<AsteroidVertex>(asteroidObjects.size(), 719 addVertexNormals<AsteroidVertex>({ 720 721 // front 722 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 723 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 724 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 725 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 726 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 727 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 728 729 // top 730 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 731 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 732 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 733 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 734 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 735 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 736 737 // bottom 738 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 739 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 740 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 741 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 742 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 743 {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}}, 744 745 // back 746 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 747 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 748 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 749 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 750 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 751 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 752 753 // right 754 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 755 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 756 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 757 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 758 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 759 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 760 761 // left 762 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 763 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 764 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 765 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 766 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 767 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 768 })), { 769 0, 1, 2, 3, 4, 5, 770 6, 7, 8, 9, 10, 11, 771 12, 13, 14, 15, 16, 17, 772 18, 19, 20, 21, 22, 23, 773 24, 25, 26, 27, 28, 29, 774 30, 31, 32, 33, 34, 35, 775 }, { 776 mat4(1.0f), 777 10.0f, 778 0 779 }, true); 780 781 // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) * 782 // translate(mat4(1.0f), vec3(0.0504826f, -1.2f, 1.0f)) * 783 asteroidObjects.back().model_base = 784 translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f)) * 785 rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) * 786 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 787 788 updateObject(asteroidObjects, asteroidPipeline, asteroidObjects.size() - 1); 778 789 } 779 790 -
vulkan-game.hpp
r44f23af r0807aeb 1 1 #ifndef _VULKAN_GAME_H 2 2 #define _VULKAN_GAME_H 3 4 #include <chrono> 3 5 4 6 #define GLM_FORCE_RADIANS … … 15 17 16 18 using namespace glm; 19 using namespace std::chrono; 17 20 18 21 #ifdef NDEBUG … … 182 185 UBO_VP_mats asteroid_VP_mats; 183 186 184 Uint64 curTime, prevTime; 185 double elapsedTime; 187 time_point<steady_clock> startTime; 188 float curTime, prevTime, elapsedTime; 189 190 float shipSpeed = 0.5f; 191 float asteroidSpeed = 2.0f; 192 193 float spawnRate_asteroid = 0.5; 194 float lastSpawn_asteroid; 186 195 187 196 bool initWindow(int width, int height, unsigned char guiFlags); … … 286 295 } 287 296 297 // TODO: Just pass in the single object instead of a list of all of them 288 298 template<class VertexType, class SSBOType> 289 299 void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
Note:
See TracChangeset
for help on using the changeset viewer.