Changeset 845a2cb in opengl-game for vulkan-game.cpp
- Timestamp:
- Apr 22, 2020, 12:58:07 AM (5 years ago)
- Branches:
- points-test
- Parents:
- 73a10ca
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
r73a10ca r845a2cb 3 3 #include <array> 4 4 #include <iostream> 5 #include <numeric> 5 6 #include <set> 6 7 … … 28 29 this->asteroid_VP_mats = {}; 29 30 this->laser_VP_mats = {}; 31 this->explosion_UBO = {}; 30 32 } 31 33 … … 519 521 }, false); 520 522 523 ship.model_base = 524 translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) * 525 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 526 ship.modified = true; 527 521 528 shipPipeline.createDescriptorSetLayout(); 522 529 shipPipeline.createPipeline("shaders/ship-vert.spv", "shaders/ship-frag.spv"); … … 559 566 laserPipeline.createDescriptorSets(swapChainImages); 560 567 568 explosionPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ExplosionVertex::particleStartVelocity)); 569 explosionPipeline.addAttribute(VK_FORMAT_R32_SFLOAT, offset_of(&ExplosionVertex::particleStartTime)); 570 explosionPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ExplosionVertex::objIndex)); 571 572 createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 573 uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline); 574 575 explosionPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 576 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_explosionPipeline); 577 explosionPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT); 578 579 explosionPipeline.createDescriptorSetLayout(); 580 explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv"); 581 explosionPipeline.createDescriptorPool(swapChainImages); 582 explosionPipeline.createDescriptorSets(swapChainImages); 583 561 584 cout << "Created all the graphics pipelines" << endl; 562 585 … … 564 587 565 588 createSyncObjects(); 566 567 ship.model_base =568 translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *569 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));570 ship.modified = true;571 589 } 572 590 573 591 void VulkanGame::initGraphicsPipelines() { 574 overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>(physicalDevice, device, renderPass, 592 overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>( 593 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 575 594 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 4, 6, 0); 576 595 577 modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>(physicalDevice, device, renderPass, 596 modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>( 597 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 578 598 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 16, 24, 10); 579 599 580 shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>(physicalDevice, device, renderPass, 600 shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>( 601 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 581 602 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 138, 138, 10); 582 603 583 asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>(physicalDevice, device, renderPass, 604 asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>( 605 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 584 606 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 24, 36, 10); 585 607 586 laserPipeline = GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser>(physicalDevice, device, renderPass, 608 laserPipeline = GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser>( 609 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 587 610 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 8, 18, 2); 611 612 explosionPipeline = GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion>( 613 VK_PRIMITIVE_TOPOLOGY_POINT_LIST, physicalDevice, device, renderPass, 614 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, 615 swapChainImages, EXPLOSION_PARTICLE_COUNT, EXPLOSION_PARTICLE_COUNT, 2); 588 616 } 589 617 … … 616 644 laser_VP_mats.view = viewMat; 617 645 laser_VP_mats.proj = projMat; 646 647 explosion_UBO.view = viewMat; 648 explosion_UBO.proj = projMat; 618 649 } 619 650 … … 732 763 break; 733 764 case UI_EVENT_UNKNOWN: 734 cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;765 //cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl; 735 766 break; 736 767 default: … … 813 844 vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f)); 814 845 815 if ( (objCenter.z - asteroid.radius) > -NEAR_CLIP ||asteroid.ssbo.hp <= 0.0f) {846 if (asteroid.ssbo.hp <= 0.0f) { 816 847 asteroid.ssbo.deleted = true; 817 848 818 // TODO: Create explosion here 849 // TODO: Optimize this so I don't recalculate the camera rotation every time 850 // TODO: Also, avoid re-declaring cam_pitch 851 float cam_pitch = -50.0f; 852 mat4 pitch_mat = rotate(mat4(1.0f), radians(cam_pitch), vec3(1.0f, 0.0f, 0.0f)); 853 mat4 model_mat = translate(mat4(1.0f), asteroid.center) * pitch_mat; 854 855 addExplosion(model_mat, 0.5f, curTime); 856 857 // TODO: Increment player's score here 858 } else if ((objCenter.z - asteroid.radius) > -NEAR_CLIP) { 859 asteroid.ssbo.deleted = true; 819 860 } else { 820 861 asteroid.model_transform = … … 933 974 } 934 975 976 for (size_t i = 0; i < explosionObjects.size(); i++) { 977 if (explosionObjects[i].modified) { 978 updateObject(explosionObjects, explosionPipeline, i); 979 } 980 } 981 982 explosion_UBO.cur_time = curTime; 983 935 984 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats); 936 985 … … 940 989 941 990 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats); 991 992 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO); 942 993 } 943 994 … … 1046 1097 asteroidPipeline.cleanupBuffers(); 1047 1098 laserPipeline.cleanupBuffers(); 1099 explosionPipeline.cleanupBuffers(); 1048 1100 1049 1101 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { … … 1538 1590 asteroidPipeline.createRenderCommands(commandBuffers[i], i); 1539 1591 laserPipeline.createRenderCommands(commandBuffers[i], i); 1592 explosionPipeline.createRenderCommands(commandBuffers[i], i); 1540 1593 1541 1594 // Always render this pipeline last … … 1571 1624 } 1572 1625 1573 void VulkanGame::addLaser( 1626 void VulkanGame::addLaser(vec3 start, vec3 end, vec3 color, float width) { 1574 1627 vec3 ray = end - start; 1575 1628 float length = glm::length(ray); … … 1792 1845 } 1793 1846 1847 void VulkanGame::addExplosion(mat4 model_mat, float duration, float cur_time) { 1848 cout << "Adding explosion..." << endl; 1849 1850 vector<ExplosionVertex> vertices; 1851 vertices.reserve(EXPLOSION_PARTICLE_COUNT); 1852 1853 float particlestart_time = 0.0f; 1854 1855 for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) { 1856 float randx = ((float)rand() / (float)RAND_MAX) - 0.5f; 1857 float randy = ((float)rand() / (float)RAND_MAX) - 0.5f; 1858 1859 vertices.push_back({ vec3(randx, randy, 0.0f), particlestart_time}); 1860 1861 particlestart_time += 0.01f; 1862 } 1863 1864 // Fill the indices with the the first EXPLOSION_PARTICLE_COUNT ints 1865 vector<uint16_t> indices(EXPLOSION_PARTICLE_COUNT); 1866 iota(indices.begin(), indices.end(), 0); 1867 1868 SceneObject<ExplosionVertex, SSBO_Explosion>& explosion = addObject( 1869 explosionObjects, explosionPipeline, 1870 addObjectIndex(explosionObjects.size(), vertices), 1871 indices, { 1872 mat4(1.0f), 1873 cur_time, 1874 duration, 1875 false 1876 }, true); 1877 1878 explosion.model_base = model_mat; 1879 explosion.model_transform = mat4(1.0f); 1880 1881 explosion.modified = true; 1882 } 1883 1794 1884 // TODO: Fix the crash that happens when alt-tabbing 1795 1885 void VulkanGame::recreateSwapChain() { … … 1853 1943 laserPipeline.createDescriptorPool(swapChainImages); 1854 1944 laserPipeline.createDescriptorSets(swapChainImages); 1945 1946 createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1947 uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline); 1948 1949 explosionPipeline.updateRenderPass(renderPass); 1950 explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv"); 1951 explosionPipeline.createDescriptorPool(swapChainImages); 1952 explosionPipeline.createDescriptorSets(swapChainImages); 1855 1953 1856 1954 createCommandBuffers(); … … 1871 1969 asteroidPipeline.cleanup(); 1872 1970 laserPipeline.cleanup(); 1971 explosionPipeline.cleanup(); 1873 1972 1874 1973 for (size_t i = 0; i < uniformBuffers_modelPipeline.size(); i++) { … … 1892 1991 } 1893 1992 1993 for (size_t i = 0; i < uniformBuffers_explosionPipeline.size(); i++) { 1994 vkDestroyBuffer(device, uniformBuffers_explosionPipeline[i], nullptr); 1995 vkFreeMemory(device, uniformBuffersMemory_explosionPipeline[i], nullptr); 1996 } 1997 1894 1998 vkDestroyRenderPass(device, renderPass, nullptr); 1895 1999
Note:
See TracChangeset
for help on using the changeset viewer.