Changeset 2da64ef in opengl-game
- Timestamp:
- Feb 19, 2020, 4:47:41 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 3b84bb6
- Parents:
- 860a0da
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.hpp
r860a0da r2da64ef 74 74 VkQueue graphicsQueue); 75 75 76 void updateObject(size_t objIndex, SSBOType& ssbo); 77 76 78 void cleanup(); 77 79 void cleanupBuffers(); 78 79 StorageBufferSet storageBufferSet;80 80 81 81 private: … … 108 108 size_t numObjects; 109 109 size_t objectCapacity; 110 111 StorageBufferSet storageBufferSet; 110 112 111 113 VkShaderModule createShaderModule(const vector<char>& code); … … 491 493 492 494 template<class VertexType, class SSBOType> 495 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::updateObject(size_t objIndex, SSBOType& ssbo) { 496 if (!is_same_v<SSBOType, void*>) { 497 for (size_t i = 0; i < storageBufferSet.memory.size(); i++) { 498 VulkanUtils::copyDataToMemory(device, storageBufferSet.memory[i], objIndex, ssbo); 499 } 500 } 501 } 502 503 template<class VertexType, class SSBOType> 493 504 void GraphicsPipeline_Vulkan<VertexType, SSBOType>::cleanup() { 494 505 vkDestroyPipeline(device, pipeline, nullptr); -
vulkan-game.cpp
r860a0da r2da64ef 524 524 asteroidPipeline.addStorageDescriptor(); 525 525 526 addObject(asteroidObjects, asteroidPipeline,527 centerObject<AsteroidVertex>(528 addObjectIndex<AsteroidVertex>(asteroidObjects.size(),529 addVertexNormals<AsteroidVertex>({530 531 // front532 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},533 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},534 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},535 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},536 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},537 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},538 539 // top540 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},541 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},542 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},543 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},544 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},545 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},546 547 // bottom548 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},549 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},550 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},551 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},552 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},553 {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}},554 555 // back556 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},557 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},558 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},559 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},560 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},561 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},562 563 // right564 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},565 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},566 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},567 {{ 1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},568 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},569 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},570 571 // left572 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},573 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},574 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},575 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},576 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}},577 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}},578 }))), {579 0, 1, 2, 3, 4, 5,580 6, 7, 8, 9, 10, 11,581 12, 13, 14, 15, 16, 17,582 18, 19, 20, 21, 22, 23,583 24, 25, 26, 27, 28, 29,584 30, 31, 32, 33, 34, 35,585 }, {586 mat4(1.0f),587 10.0f,588 0589 });590 591 526 asteroidPipeline.createDescriptorSetLayout(); 592 527 asteroidPipeline.createPipeline("shaders/asteroid-vert.spv", "shaders/asteroid-frag.spv"); … … 604 539 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 605 540 606 // vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)); 607 // vec3 pos = vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f); 608 // vec3 pos = vec3(1.0f, -1.2f, -1.0f); 609 vec3 pos = vec3(0.0504826f, -1.2f, 1.0f); 610 611 asteroidObjects[0].model_base = 612 translate(mat4(1.0f), pos) * 613 rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) * 614 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 615 asteroidObjects[0].model_transform = mat4(1.0); // Might not be needed 616 617 so_Asteroid.hp = 10.0f; 618 so_Asteroid.deleted = 0; 541 updateObject(shipObjects, shipPipeline, 0); 619 542 } 620 543 … … 713 636 createCommandBuffers(); 714 637 } else if (e.key.keycode == SDL_SCANCODE_Z) { 715 cout << "Deleting asteroid..." << endl; 716 so_Asteroid.deleted = so_Asteroid.deleted == 0 ? 1 : 0; 638 vkDeviceWaitIdle(device); 639 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 640 641 addObject(asteroidObjects, asteroidPipeline, 642 addObjectIndex<AsteroidVertex>(asteroidObjects.size(), 643 addVertexNormals<AsteroidVertex>({ 644 645 // front 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 {{ 1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 652 653 // top 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.0f}, {0.4f, 0.4f, 0.4f}}, 659 {{ 1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 660 661 // bottom 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 {{ 1.0f, -1.0f, -1.0}, {0.4f, 0.4f, 0.4f}}, 668 669 // back 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 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 676 677 // right 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 {{ 1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 684 685 // left 686 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 687 {{-1.0f, 1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 688 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 689 {{-1.0f, 1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 690 {{-1.0f, -1.0f, -1.0f}, {0.4f, 0.4f, 0.4f}}, 691 {{-1.0f, -1.0f, 1.0f}, {0.4f, 0.4f, 0.4f}}, 692 })), { 693 0, 1, 2, 3, 4, 5, 694 6, 7, 8, 9, 10, 11, 695 12, 13, 14, 15, 16, 17, 696 18, 19, 20, 21, 22, 23, 697 24, 25, 26, 27, 28, 29, 698 30, 31, 32, 33, 34, 35, 699 }, { 700 mat4(1.0f), 701 10.0f, 702 0 703 }); 704 705 // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) * 706 asteroidObjects.back().model_base = 707 translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f)) * 708 rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) * 709 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 710 711 updateObject(asteroidObjects, asteroidPipeline, asteroidObjects.size() - 1); 712 createCommandBuffers(); 717 713 } else { 718 714 cout << "Key event detected" << endl; … … 743 739 744 740 if (gui->keyPressed(SDL_SCANCODE_LEFT)) { 745 transformObject(shipObjects[0], translate(mat4(1.0f), vec3(-shipSpeed * elapsedTime, 0.0f, 0.0f))); 741 shipObjects[0].model_transform = translate(mat4(1.0f), vec3(-shipSpeed * elapsedTime, 0.0f, 0.0f)) 742 * shipObjects[0].model_transform; 743 744 updateObject(shipObjects, shipPipeline, 0); 746 745 } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) { 747 transformObject(shipObjects[0], translate(mat4(1.0f), vec3(shipSpeed * elapsedTime, 0.0f, 0.0f))); 748 } 749 750 if (gui->keyPressed(SDL_SCANCODE_DOWN)) { 751 transformObject(asteroidObjects[0], translate(mat4(1.0f), vec3(0.0f, 0.0f, asteroidSpeed * elapsedTime))); 746 shipObjects[0].model_transform = translate(mat4(1.0f), vec3(shipSpeed * elapsedTime, 0.0f, 0.0f)) 747 * shipObjects[0].model_transform; 748 749 updateObject(shipObjects, shipPipeline, 0); 752 750 } 753 751 … … 759 757 } 760 758 761 // TODO: The view and projection mats only need to be updated once. 762 // Create a separate function that can do this once per Vulkan image at the beginning 759 // TODO: The only updates that need to happen once per Vulkan image are the SSBO ones, 760 // which are already handled by updateObject(). Move this code to a different place, 761 // where it will run just once per frame 763 762 void VulkanGame::updateScene(uint32_t currentImage) { 764 763 static auto startTime = chrono::high_resolution_clock::now(); … … 767 766 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count(); 768 767 769 // TODO: Will need to change this to go through all objects of all pipelines and update their model mats 770 771 so_Object.model = 772 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 773 rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 774 775 so_Ship.model = shipObjects[0].model_transform * shipObjects[0].model_base; 776 777 so_Asteroid.model = asteroidObjects[0].model_transform * asteroidObjects[0].model_base; 768 for (int i = 0; i < modelObjects.size(); i++) { 769 modelObjects[i].model_transform = 770 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 771 rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 772 773 updateObject(modelObjects, modelPipeline, i); 774 } 778 775 779 776 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats); 780 777 781 for (size_t i = 0; i < modelObjects.size(); i++) {782 VulkanUtils::copyDataToMemory(device, modelPipeline.storageBufferSet.memory[currentImage],783 i, so_Object);784 }785 786 778 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[currentImage], 0, ship_VP_mats); 787 779 788 VulkanUtils::copyDataToMemory(device, shipPipeline.storageBufferSet.memory[currentImage], 0, so_Ship);789 790 780 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[currentImage], 0, asteroid_VP_mats); 791 792 VulkanUtils::copyDataToMemory(device, asteroidPipeline.storageBufferSet.memory[currentImage], 0, so_Asteroid);793 781 } 794 782 … … 1449 1437 createFramebuffers(); 1450 1438 1439 // TODO: Move UBO creation/management into GraphicsPipeline_Vulkan, like I did with SSBOs 1440 1451 1441 createBufferSet(sizeof(UBO_VP_mats), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1452 1442 uniformBuffers_modelPipeline, uniformBuffersMemory_modelPipeline, uniformBufferInfoList_modelPipeline); -
vulkan-game.hpp
r860a0da r2da64ef 75 75 }; 76 76 77 // Have to figure out how to include an optional ssbo parameter for each object 78 // Could probably use the same approach to make indices optional 79 77 80 class VulkanGame { 78 81 public: … … 158 161 159 162 UBO_VP_mats object_VP_mats; 160 SSBO_ModelObject so_Object;161 163 162 164 GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject> shipPipeline; … … 168 170 169 171 UBO_VP_mats ship_VP_mats; 170 SSBO_ModelObject so_Ship;171 172 172 173 GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid> asteroidPipeline; … … 178 179 179 180 UBO_VP_mats asteroid_VP_mats; 180 SSBO_Asteroid so_Asteroid;181 181 182 182 Uint64 curTime, prevTime; … … 219 219 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo); 220 220 221 template<class VertexType, class SSBOType> 222 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 223 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index); 224 221 225 template<class VertexType> 222 226 vector<VertexType> addVertexNormals(vector<VertexType> vertices); … … 227 231 template<class VertexType> 228 232 vector<VertexType> centerObject(vector<VertexType> vertices); 229 230 template<class VertexType, class SSBOType>231 void transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat);232 233 233 234 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, … … 258 259 259 260 pipeline.addVertices(vertices, indices, commandPool, graphicsQueue); 261 } 262 263 template<class VertexType, class SSBOType> 264 void VulkanGame::updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 265 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index) { 266 SceneObject<VertexType, SSBOType>& obj = objects[index]; 267 268 obj.ssbo.model = obj.model_transform * obj.model_base; 269 270 // could probably re-calculate the object center here based on model 271 // I think the center should be calculated by using model * vec3(0, 0, 0) 272 // model_base is currently only used to set the original location of the ship and asteroids 273 274 pipeline.updateObject(index, obj.ssbo); 260 275 } 261 276 … … 326 341 } 327 342 328 template<class VertexType, class SSBOType>329 void VulkanGame::transformObject(SceneObject<VertexType, SSBOType>& obj, mat4 mat) {330 obj.model_transform = mat * obj.model_transform;331 }332 333 343 #endif // _VULKAN_GAME_H
Note:
See TracChangeset
for help on using the changeset viewer.