Changeset 4a9416a in opengl-game
- Timestamp:
- Apr 26, 2020, 9:50:00 PM (4 years ago)
- Branches:
- feature/imgui-sdl, master
- Children:
- b8d4456
- Parents:
- 52a02e6
- git-author:
- Dmitry Portnoy <dmitry.portnoy@…> (04/26/20 19:00:18)
- git-committer:
- Dmitry Portnoy <dmitry.portnoy@…> (04/26/20 21:50:00)
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r52a02e6 r4a9416a 47 47 }; 48 48 49 /*** START OF REFACTORED CODE ***/ 49 50 enum ObjectType { 50 51 TYPE_SHIP, … … 67 68 UNIFORM_3F, 68 69 }; 70 /*** END OF REFACTORED CODE ***/ 69 71 70 72 enum UIValueType { … … 73 75 }; 74 76 77 /*** START OF REFACTORED CODE ***/ 75 78 struct SceneObject { 76 79 unsigned int id; … … 153 156 unsigned int vboCapacity; 154 157 }; 158 /*** END OF REFACTORED CODE ***/ 155 159 156 160 struct UIValue { … … 193 197 map<ObjectType, ShaderModelGroup>& modelGroups, 194 198 GLuint ubo); 195 /*** END OF REFACTORED CODE ***/196 199 void removeObjectFromScene(SceneObject& obj, GLuint ubo); 197 200 … … 200 203 void addModelToGroup(ShaderModelGroup& modelGroup, SceneObject& model); 201 204 202 /*** START OF REFACTORED CODE ***/203 205 void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset); 204 206 void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, UniformType type, GLfloat* data); … … 206 208 void bindUniformData(AttribInfo& attrib); 207 209 void bindUniformData(AttribInfo& attrib, GLfloat* data); 208 /*** END OF REFACTORED CODE ***/209 210 210 211 size_t GLsizeof(GLenum); … … 214 215 void calculateObjectBoundingBox(SceneObject* obj); 215 216 216 /*** START OF REFACTORED CODE ***/217 217 void populateBuffers(vector<SceneObject*>& objects, 218 218 map<GLuint, BufferInfo>& shaderBufferInfo, … … 231 231 Asteroid* createAsteroid(vec3 pos); 232 232 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width); 233 /*** END OF REFACTORED CODE ***/234 233 ParticleEffect* createExplosion(mat4 model_mat); 235 234 236 /*** START OF REFACTORED CODE ***/237 235 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo); 238 236 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, ShaderModelGroup& laserSmg, GLuint asteroid_sp); … … 254 252 255 253 const int KEY_STATE_UNCHANGED = -1; 254 /*** START OF REFACTORED CODE ***/ 256 255 const bool FULLSCREEN = false; 257 256 const int EXPLOSION_PARTICLE_COUNT = 300; 258 257 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const 258 /*** END OF REFACTORED CODE ***/ 259 259 260 260 int key_state[NUM_KEYS]; … … 381 381 cout << "OpenGL debug message callback is not supported" << endl; 382 382 } 383 /*** END OF REFACTORED CODE ***/384 383 385 384 srand(time(0)); … … 409 408 cout << "MAX_UNIFORMS: " << MAX_UNIFORMS << endl; 410 409 411 /*** START OF REFACTORED CODE ***/412 410 // Setup Dear ImGui binding 413 411 IMGUI_CHECKVERSION(); … … 489 487 */ 490 488 489 /*** START OF REFACTORED CODE ***/ 491 490 GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f}; 492 491 GLfloat curTime, prevTime, elapsedTime; … … 498 497 map<ObjectType, ShaderModelGroup> modelGroups; 499 498 500 /*** START OF REFACTORED CODE ***/501 499 modelGroups[TYPE_SHIP] = createModelGroup( 502 500 loadShaderProgram("gl-shaders/ship.vert", "gl-shaders/ship.frag")); … … 558 556 559 557 initModelGroupAttribs(modelGroups[TYPE_LASER]); 560 /*** END OF REFACTORED CODE ***/561 558 562 559 modelGroups[TYPE_EXPLOSION] = createModelGroup( … … 580 577 initModelGroupAttribs(modelGroups[TYPE_EXPLOSION]); 581 578 582 /*** START OF REFACTORED CODE ***/583 579 cam_pos = vec3(0.0f, 0.0f, 2.0f); 584 580 float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f; … … 666 662 667 663 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models"); 668 /*** END OF REFACTORED CODE ***/669 664 670 665 GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models"); 671 666 672 667 673 /*** START OF REFACTORED CODE ***/674 668 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); 675 669 bindUniformData(modelGroups[TYPE_SHIP].attribs["view"]); … … 698 692 glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point); 699 693 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 700 /*** END OF REFACTORED CODE ***/701 694 702 695 … … 707 700 glUniformBlockBinding(modelGroups[TYPE_EXPLOSION].shaderProgram, explosion_sp_models_ub_index, ub_binding_point); 708 701 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 702 /*** END OF REFACTORED CODE ***/ 709 703 710 704 … … 881 875 score++; 882 876 877 /*** START OF REFACTORED CODE ***/ 883 878 addObjectToScene(createExplosion(model_mat), shaderBufferInfo, modelGroups, ubo); 884 879 } … … 889 884 } 890 885 } 891 /*** START OF REFACTORED CODE ***/892 886 } 893 887 } … … 977 971 cam_moved = false; 978 972 } 979 /*** END OF REFACTORED CODE ***/980 973 981 974 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram); 982 975 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["cur_time"]); 983 976 984 /*** START OF REFACTORED CODE ***/985 977 // Render scene 986 978 … … 1200 1192 1201 1193 1194 /*** START OF REFACTORED CODE ***/ 1202 1195 GLuint loadShader(GLenum type, string file) { 1203 1196 cout << "Loading shader from file " << file << endl; … … 1279 1272 return image_data; 1280 1273 } 1274 /*** END OF REFACTORED CODE ***/ 1281 1275 1282 1276 bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point) { … … 1324 1318 } 1325 1319 1320 /*** START OF REFACTORED CODE ***/ 1326 1321 // TODO: Pass a reference, not a pointer 1327 1322 void initObject(SceneObject* obj) { … … 1446 1441 } 1447 1442 1448 /*** START OF REFACTORED CODE ***/1449 1443 SceneObject* createShip() { 1450 1444 SceneObject* ship = new SceneObject(); … … 1917 1911 return obj; 1918 1912 } 1919 /*** END OF REFACTORED CODE ***/1920 1913 1921 1914 ShaderModelGroup createModelGroup(GLuint shaderProgram) { … … 1942 1935 } 1943 1936 1944 /*** START OF REFACTORED CODE ***/1945 1937 void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, 1946 1938 GLint size, GLenum type, size_t fieldOffset) { … … 2031 2023 } 2032 2024 } 2033 /*** END OF REFACTORED CODE ***/2034 2025 2035 2026 /* The purpose of this function is to replace the use of sizeof() when calling … … 2403 2394 glDisable(GL_BLEND); 2404 2395 } 2396 /*** END OF REFACTORED CODE ***/ 2405 2397 2406 2398 void renderSceneGui(map<string, vector<UIValue>> valueLists) { … … 2671 2663 return obj; 2672 2664 } 2673 /*** END OF REFACTORED CODE ***/2674 2665 2675 2666 // TODO: Maybe pass in startTime instead of calling glfwGetTime() here … … 2703 2694 return obj; 2704 2695 } 2696 /*** END OF REFACTORED CODE ***/ -
vulkan-game.cpp
r52a02e6 r4a9416a 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 … … 564 566 laserPipeline.createDescriptorSets(swapChainImages); 565 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 566 584 cout << "Created all the graphics pipelines" << endl; 567 585 … … 591 609 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 592 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); 593 616 } 594 617 … … 621 644 laser_VP_mats.view = viewMat; 622 645 laser_VP_mats.proj = projMat; 646 647 explosion_UBO.view = viewMat; 648 explosion_UBO.proj = projMat; 623 649 } 624 650 … … 691 717 vec3(-0.21f, -1.19f, 1.76f) + offset, 692 718 vec3(-0.21f, -1.19f, -3.0f) + offset, 693 vec3(1.0f, 0.0f, 0.0f), 0.03f);719 LASER_COLOR, 0.03f); 694 720 695 721 leftLaserIdx = laserObjects.size() - 1; … … 700 726 vec3(0.21f, -1.19f, 1.76f) + offset, 701 727 vec3(0.21f, -1.19f, -3.0f) + offset, 702 vec3(0.0f, 1.0f, 0.0f), 0.03f);728 LASER_COLOR, 0.03f); 703 729 704 730 rightLaserIdx = laserObjects.size() - 1; … … 818 844 vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f)); 819 845 820 if ( (objCenter.z - asteroid.radius) > -NEAR_CLIP ||asteroid.ssbo.hp <= 0.0f) {846 if (asteroid.ssbo.hp <= 0.0f) { 821 847 asteroid.ssbo.deleted = true; 822 848 823 // 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; 824 860 } else { 825 861 asteroid.model_transform = … … 914 950 } 915 951 952 for (SceneObject<ExplosionVertex, SSBO_Explosion>& explosion : this->explosionObjects) { 953 if (!explosion.ssbo.deleted) { 954 if (curTime > (explosion.ssbo.explosionStartTime + explosion.ssbo.explosionDuration)) { 955 explosion.ssbo.deleted = true; 956 explosion.modified = true; 957 } 958 } 959 } 960 916 961 for (size_t i = 0; i < shipObjects.size(); i++) { 917 962 if (shipObjects[i].modified) { … … 938 983 } 939 984 985 for (size_t i = 0; i < explosionObjects.size(); i++) { 986 if (explosionObjects[i].modified) { 987 updateObject(explosionObjects, explosionPipeline, i); 988 } 989 } 990 991 explosion_UBO.cur_time = curTime; 992 940 993 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats); 941 994 … … 945 998 946 999 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats); 1000 1001 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO); 947 1002 } 948 1003 … … 1051 1106 asteroidPipeline.cleanupBuffers(); 1052 1107 laserPipeline.cleanupBuffers(); 1108 explosionPipeline.cleanupBuffers(); 1053 1109 1054 1110 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { … … 1543 1599 asteroidPipeline.createRenderCommands(commandBuffers[i], i); 1544 1600 laserPipeline.createRenderCommands(commandBuffers[i], i); 1601 explosionPipeline.createRenderCommands(commandBuffers[i], i); 1545 1602 1546 1603 // Always render this pipeline last … … 1797 1854 } 1798 1855 1856 void VulkanGame::addExplosion(mat4 model_mat, float duration, float cur_time) { 1857 vector<ExplosionVertex> vertices; 1858 vertices.reserve(EXPLOSION_PARTICLE_COUNT); 1859 1860 float particle_start_time = 0.0f; 1861 1862 for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) { 1863 float randx = ((float)rand() / (float)RAND_MAX) - 0.5f; 1864 float randy = ((float)rand() / (float)RAND_MAX) - 0.5f; 1865 1866 vertices.push_back({ vec3(randx, randy, 0.0f), particle_start_time}); 1867 1868 particle_start_time += .01f; 1869 // TODO: Get this working 1870 // particle_start_time += 1.0f * EXPLOSION_PARTICLE_COUNT / duration 1871 } 1872 1873 // Fill the indices with the the first EXPLOSION_PARTICLE_COUNT ints 1874 vector<uint16_t> indices(EXPLOSION_PARTICLE_COUNT); 1875 iota(indices.begin(), indices.end(), 0); 1876 1877 SceneObject<ExplosionVertex, SSBO_Explosion>& explosion = addObject( 1878 explosionObjects, explosionPipeline, 1879 addObjectIndex(explosionObjects.size(), vertices), 1880 indices, { 1881 mat4(1.0f), 1882 cur_time, 1883 duration, 1884 false 1885 }, true); 1886 1887 explosion.model_base = model_mat; 1888 explosion.model_transform = mat4(1.0f); 1889 1890 explosion.modified = true; 1891 } 1892 1799 1893 // TODO: Fix the crash that happens when alt-tabbing 1800 1894 void VulkanGame::recreateSwapChain() { … … 1858 1952 laserPipeline.createDescriptorPool(swapChainImages); 1859 1953 laserPipeline.createDescriptorSets(swapChainImages); 1954 1955 createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1956 uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline); 1957 1958 explosionPipeline.updateRenderPass(renderPass); 1959 explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv"); 1960 explosionPipeline.createDescriptorPool(swapChainImages); 1961 explosionPipeline.createDescriptorSets(swapChainImages); 1860 1962 1861 1963 createCommandBuffers(); … … 1876 1978 asteroidPipeline.cleanup(); 1877 1979 laserPipeline.cleanup(); 1980 explosionPipeline.cleanup(); 1878 1981 1879 1982 for (size_t i = 0; i < uniformBuffers_modelPipeline.size(); i++) { … … 1897 2000 } 1898 2001 2002 for (size_t i = 0; i < uniformBuffers_explosionPipeline.size(); i++) { 2003 vkDestroyBuffer(device, uniformBuffers_explosionPipeline[i], nullptr); 2004 vkFreeMemory(device, uniformBuffersMemory_explosionPipeline[i], nullptr); 2005 } 2006 1899 2007 vkDestroyRenderPass(device, renderPass, nullptr); 1900 2008 -
vulkan-game.hpp
r52a02e6 r4a9416a 57 57 }; 58 58 59 struct ExplosionVertex { 60 vec3 particleStartVelocity; 61 float particleStartTime; 62 unsigned int objIndex; 63 }; 64 59 65 struct SSBO_ModelObject { 60 66 alignas(16) mat4 model; … … 73 79 }; 74 80 81 struct SSBO_Explosion { 82 alignas(16) mat4 model; 83 alignas(4) float explosionStartTime; 84 alignas(4) float explosionDuration; 85 alignas(4) unsigned int deleted; 86 }; 87 75 88 struct UBO_VP_mats { 76 89 alignas(16) mat4 view; 77 90 alignas(16) mat4 proj; 91 }; 92 93 struct UBO_Explosion { 94 alignas(16) mat4 view; 95 alignas(16) mat4 proj; 96 alignas(4) float cur_time; 78 97 }; 79 98 … … 183 202 const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def 184 203 204 const int EXPLOSION_PARTICLE_COUNT = 300; 205 const vec3 LASER_COLOR = vec3(0.2f, 1.0f, 0.2f); 206 185 207 vec3 cam_pos; 186 208 … … 287 309 288 310 UBO_VP_mats laser_VP_mats; 311 312 GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline; 313 vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects; 314 315 vector<VkBuffer> uniformBuffers_explosionPipeline; 316 vector<VkDeviceMemory> uniformBuffersMemory_explosionPipeline; 317 vector<VkDescriptorBufferInfo> uniformBufferInfoList_explosionPipeline; 318 319 UBO_Explosion explosion_UBO; 289 320 290 321 vector<BaseEffectOverTime*> effects; … … 368 399 vec3& start, vec3& end, vec3& intersection); 369 400 401 void addExplosion(mat4 model_mat, float duration, float cur_time); 402 370 403 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 371 404 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, … … 382 415 void* pUserData); 383 416 }; 417 418 // Start of specialized no-op functions 419 420 template<> 421 inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) { 422 } 423 424 // End of specialized no-op functions 384 425 385 426 // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model … … 409 450 SceneObject<VertexType, SSBOType>& obj = objects.back(); 410 451 411 if (!is_same_v<VertexType, LaserVertex> ) {452 if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) { 412 453 centerObject(obj); 413 454 } -
vulkan-utils.cpp
r52a02e6 r4a9416a 257 257 void VulkanUtils::createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice, 258 258 VkCommandPool commandPool, string filename, VulkanImage& image, VkQueue graphicsQueue) { 259 // TODO: Since the image loaded here will be used as a texture, display a warning if it has 260 // non power-of-two dimensions 259 261 int texWidth, texHeight, texChannels; 260 262
Note:
See TracChangeset
for help on using the changeset viewer.