Changeset b7fc3c2 in opengl-game for vulkan-game.cpp
- Timestamp:
- Jun 10, 2021, 2:58:54 PM (3 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- c1ec4f6
- Parents:
- bb76950
- git-author:
- Dmitry Portnoy <dportnoy@…> (06/10/21 14:53:52)
- git-committer:
- Dmitry Portnoy <dportnoy@…> (06/10/21 14:58:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rbb76950 rb7fc3c2 110 110 vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); 111 111 112 objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.minUniformBufferOffsetAlignment); 113 objects_shipPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.minUniformBufferOffsetAlignment); 114 objects_asteroidPipeline = VulkanBuffer<SSBO_Asteroid>(10, deviceProperties.limits.minUniformBufferOffsetAlignment); 115 objects_laserPipeline = VulkanBuffer<SSBO_Laser>(2, deviceProperties.limits.minUniformBufferOffsetAlignment); 116 objects_explosionPipeline = VulkanBuffer<SSBO_Explosion>(2, deviceProperties.limits.minUniformBufferOffsetAlignment); 112 objects_modelPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange, 113 deviceProperties.limits.minStorageBufferOffsetAlignment); 114 objects_shipPipeline = VulkanBuffer<SSBO_ModelObject>(10, deviceProperties.limits.maxStorageBufferRange, 115 deviceProperties.limits.minStorageBufferOffsetAlignment); 116 objects_asteroidPipeline = VulkanBuffer<SSBO_Asteroid>(10, deviceProperties.limits.maxStorageBufferRange, 117 deviceProperties.limits.minStorageBufferOffsetAlignment); 118 objects_laserPipeline = VulkanBuffer<SSBO_Laser>(2, deviceProperties.limits.maxStorageBufferRange, 119 deviceProperties.limits.minStorageBufferOffsetAlignment); 120 objects_explosionPipeline = VulkanBuffer<SSBO_Explosion>(2, deviceProperties.limits.maxStorageBufferRange, 121 deviceProperties.limits.minStorageBufferOffsetAlignment); 117 122 118 123 initImGuiOverlay(); … … 941 946 // where it will run just once per frame 942 947 void VulkanGame::updateScene() { 943 // Rotate the textured squares944 for (SceneObject<ModelVertex, SSBO_ModelObject>& model : this->modelObjects) {945 model.model_transform =946 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *947 rotate(mat4(1.0f), curTime * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));948 model.modified = true;949 }950 951 if (leftLaserIdx != -1) {952 updateLaserTarget(leftLaserIdx);953 }954 if (rightLaserIdx != -1) {955 updateLaserTarget(rightLaserIdx);956 }957 958 for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) {959 if ((*it)->deleted) {960 delete *it;961 it = effects.erase(it);962 } else {963 BaseEffectOverTime* eot = *it;964 965 eot->applyEffect(curTime);966 967 it++;968 }969 }970 971 for (SceneObject<ModelVertex, SSBO_Asteroid>& asteroid : this->asteroidObjects) {972 if (!asteroid.ssbo.deleted) {973 vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f));974 975 if (asteroid.ssbo.hp <= 0.0f) {976 asteroid.ssbo.deleted = true;977 978 // TODO: Optimize this so I don't recalculate the camera rotation every time979 // TODO: Also, avoid re-declaring cam_pitch980 float cam_pitch = -50.0f;981 mat4 pitch_mat = rotate(mat4(1.0f), radians(cam_pitch), vec3(1.0f, 0.0f, 0.0f));982 mat4 model_mat = translate(mat4(1.0f), asteroid.center) * pitch_mat;983 984 addExplosion(model_mat, 0.5f, curTime);985 986 this->score++;987 } else if ((objCenter.z - asteroid.radius) > -NEAR_CLIP) {988 asteroid.ssbo.deleted = true;989 } else {990 asteroid.model_transform =991 translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) *992 asteroid.model_transform;993 }994 995 asteroid.modified = true;996 }997 }998 999 948 if (curTime - this->lastSpawn_asteroid > this->spawnRate_asteroid) { 1000 949 this->lastSpawn_asteroid = curTime; … … 1080 1029 } 1081 1030 1082 for (SceneObject<ExplosionVertex, SSBO_Explosion>& explosion : this->explosionObjects) { 1083 if (!explosion.ssbo.deleted) { 1084 if (curTime > (explosion.ssbo.explosionStartTime + explosion.ssbo.explosionDuration)) { 1085 explosion.ssbo.deleted = true; 1086 explosion.modified = true; 1031 // TODO: Probably move the resizing to the VulkanBuffer class 1032 1033 // TODO: Replace updateBufferSet to one call to copyDataToMemory, using VulkanBuffer to provide the data source 1034 // TODO: Figure out a way to make updateDescriptorInfo easier to use, maybe store the binding index in the buffer set 1035 1036 if (objects_modelPipeline.resized) { 1037 resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline.memorySize(), resourceCommandPool, 1038 graphicsQueue, true); 1039 1040 objects_modelPipeline.resize(); 1041 1042 modelPipeline.updateDescriptorInfo(1, &storageBuffers_modelPipeline.infoSet, swapChainImages.size()); 1043 } 1044 1045 for (size_t i = 0; i < modelObjects.size(); i++) { 1046 SceneObject<ModelVertex, SSBO_ModelObject>& obj = modelObjects[i]; 1047 SSBO_ModelObject& objData = obj.ssbo; 1048 1049 // Rotate the textured squares 1050 obj.model_transform = 1051 translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) * 1052 rotate(mat4(1.0f), curTime * radians(90.0f), vec3(0.0f, 0.0f, 1.0f)); 1053 obj.modified = true; 1054 1055 if (obj.modified) { 1056 objData.model = obj.model_transform * obj.model_base; 1057 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1058 1059 obj.modified = false; 1060 1061 updateBufferSet(storageBuffers_modelPipeline, i, objData); 1062 } 1063 } 1064 1065 if (objects_shipPipeline.resized) { 1066 resizeBufferSet(storageBuffers_shipPipeline, objects_shipPipeline.memorySize(), resourceCommandPool, 1067 graphicsQueue, true); 1068 1069 objects_shipPipeline.resize(); 1070 1071 shipPipeline.updateDescriptorInfo(1, &storageBuffers_shipPipeline.infoSet, swapChainImages.size()); 1072 } 1073 1074 // TODO: Move ship position updates from the ui event handling code into this function 1075 for (size_t i = 0; i < shipObjects.size(); i++) { 1076 SceneObject<ModelVertex, SSBO_ModelObject>& obj = shipObjects[i]; 1077 SSBO_ModelObject& objData = obj.ssbo; 1078 1079 if (obj.modified) { 1080 objData.model = obj.model_transform * obj.model_base; 1081 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1082 1083 obj.modified = false; 1084 1085 updateBufferSet(storageBuffers_shipPipeline, i, objData); 1086 } 1087 } 1088 1089 if (objects_asteroidPipeline.resized) { 1090 resizeBufferSet(storageBuffers_asteroidPipeline, objects_asteroidPipeline.memorySize(), resourceCommandPool, 1091 graphicsQueue, true); 1092 1093 objects_asteroidPipeline.resize(); 1094 1095 asteroidPipeline.updateDescriptorInfo(1, &storageBuffers_asteroidPipeline.infoSet, swapChainImages.size()); 1096 } 1097 1098 for (size_t i = 0; i < asteroidObjects.size(); i++) { 1099 SceneObject<ModelVertex, SSBO_Asteroid>& obj = asteroidObjects[i]; 1100 SSBO_Asteroid& objData = obj.ssbo; 1101 1102 if (!objData.deleted) { 1103 vec3 objCenter = vec3(viewMat * vec4(obj.center, 1.0f)); 1104 1105 if (objData.hp <= 0.0f) { 1106 objData.deleted = true; 1107 1108 // TODO: Optimize this so I don't recalculate the camera rotation every time 1109 // TODO: Also, avoid re-declaring cam_pitch 1110 float cam_pitch = -50.0f; 1111 mat4 pitch_mat = rotate(mat4(1.0f), radians(cam_pitch), vec3(1.0f, 0.0f, 0.0f)); 1112 mat4 model_mat = translate(mat4(1.0f), obj.center) * pitch_mat; 1113 1114 addExplosion(model_mat, 0.5f, curTime); 1115 1116 this->score++; 1117 } else if ((objCenter.z - obj.radius) > -NEAR_CLIP) { 1118 objData.deleted = true; 1119 } else { 1120 obj.model_transform = 1121 translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) * 1122 obj.model_transform; 1123 } 1124 1125 obj.modified = true; 1126 } 1127 1128 if (obj.modified) { 1129 objData.model = obj.model_transform * obj.model_base; 1130 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1131 1132 obj.modified = false; 1133 1134 updateBufferSet(storageBuffers_asteroidPipeline, i, objData); 1135 } 1136 } 1137 1138 if (objects_laserPipeline.resized) { 1139 resizeBufferSet(storageBuffers_laserPipeline, objects_laserPipeline.memorySize(), resourceCommandPool, 1140 graphicsQueue, true); 1141 1142 objects_laserPipeline.resize(); 1143 1144 laserPipeline.updateDescriptorInfo(1, &storageBuffers_laserPipeline.infoSet, swapChainImages.size()); 1145 } 1146 1147 if (leftLaserIdx != -1) { 1148 updateLaserTarget(leftLaserIdx); 1149 } 1150 if (rightLaserIdx != -1) { 1151 updateLaserTarget(rightLaserIdx); 1152 } 1153 1154 for (size_t i = 0; i < laserObjects.size(); i++) { 1155 SceneObject<LaserVertex, SSBO_Laser>& obj = laserObjects[i]; 1156 SSBO_Laser& objData = obj.ssbo; 1157 1158 if (obj.modified) { 1159 objData.model = obj.model_transform * obj.model_base; 1160 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1161 1162 obj.modified = false; 1163 1164 updateBufferSet(storageBuffers_laserPipeline, i, objData); 1165 } 1166 } 1167 1168 if (objects_explosionPipeline.resized) { 1169 resizeBufferSet(storageBuffers_explosionPipeline, objects_explosionPipeline.memorySize(), resourceCommandPool, 1170 graphicsQueue, true); 1171 1172 objects_explosionPipeline.resize(); 1173 1174 explosionPipeline.updateDescriptorInfo(1, &storageBuffers_explosionPipeline.infoSet, swapChainImages.size()); 1175 } 1176 1177 for (size_t i = 0; i < explosionObjects.size(); i++) { 1178 SceneObject<ExplosionVertex, SSBO_Explosion>& obj = explosionObjects[i]; 1179 SSBO_Explosion& objData = obj.ssbo; 1180 1181 if (!objData.deleted) { 1182 if (curTime > (objData.explosionStartTime + objData.explosionDuration)) { 1183 objData.deleted = true; 1184 obj.modified = true; 1087 1185 } 1088 1186 } 1089 } 1090 1091 // TODO: Replace updateBufferSet to one call to copyDataToMemory, using VulkanBuffer to provide the data source 1092 // TODO: Figure out a way to make updateDescriptorInfo easier to use, maybe store the binding index in the buffer set 1093 // TODO: Probably move the resizing to the VulkanBuffer class 1094 1095 if (objects_modelPipeline.resized) { 1096 resizeBufferSet(storageBuffers_modelPipeline, objects_modelPipeline.capacity * sizeof(SSBO_ModelObject), 1097 resourceCommandPool, graphicsQueue, true); 1098 1099 objects_modelPipeline.resize(); 1100 1101 modelPipeline.updateDescriptorInfo(1, &storageBuffers_modelPipeline.infoSet, swapChainImages.size()); 1102 } 1103 1104 for (size_t i = 0; i < modelObjects.size(); i++) { 1105 if (modelObjects[i].modified) { 1106 updateObject(modelObjects[i]); 1107 updateBufferSet(storageBuffers_modelPipeline, i, modelObjects[i].ssbo); 1187 1188 if (obj.modified) { 1189 objData.model = obj.model_transform * obj.model_base; 1190 obj.center = vec3(objData.model * vec4(0.0f, 0.0f, 0.0f, 1.0f)); 1191 1192 obj.modified = false; 1193 1194 updateBufferSet(storageBuffers_explosionPipeline, i, objData); 1108 1195 } 1109 1196 } 1110 1197 1111 // TODO: Probably move the resizing to the VulkanBuffer class 1112 if (objects_shipPipeline.resized) { 1113 resizeBufferSet(storageBuffers_shipPipeline, objects_shipPipeline.capacity * sizeof(SSBO_ModelObject), 1114 resourceCommandPool, graphicsQueue, true); 1115 1116 objects_shipPipeline.resize(); 1117 1118 shipPipeline.updateDescriptorInfo(1, &storageBuffers_shipPipeline.infoSet, swapChainImages.size()); 1119 } 1120 1121 for (size_t i = 0; i < shipObjects.size(); i++) { 1122 if (shipObjects[i].modified) { 1123 updateObject(shipObjects[i]); 1124 updateBufferSet(storageBuffers_shipPipeline, i, shipObjects[i].ssbo); 1198 explosion_UBO.cur_time = curTime; 1199 1200 for (vector<BaseEffectOverTime*>::iterator it = effects.begin(); it != effects.end(); ) { 1201 if ((*it)->deleted) { 1202 delete *it; 1203 it = effects.erase(it); 1204 } else { 1205 BaseEffectOverTime* eot = *it; 1206 1207 eot->applyEffect(curTime); 1208 1209 it++; 1125 1210 } 1126 1211 } 1127 1128 // TODO: Probably move the resizing to the VulkanBuffer class1129 if (objects_asteroidPipeline.resized) {1130 resizeBufferSet(storageBuffers_asteroidPipeline, objects_asteroidPipeline.capacity * sizeof(SSBO_Asteroid),1131 resourceCommandPool, graphicsQueue, true);1132 1133 objects_asteroidPipeline.resize();1134 1135 asteroidPipeline.updateDescriptorInfo(1, &storageBuffers_asteroidPipeline.infoSet, swapChainImages.size());1136 }1137 1138 for (size_t i = 0; i < asteroidObjects.size(); i++) {1139 if (asteroidObjects[i].modified) {1140 updateObject(asteroidObjects[i]);1141 updateBufferSet(storageBuffers_asteroidPipeline, i, asteroidObjects[i].ssbo);1142 }1143 }1144 1145 // TODO: Probably move the resizing to the VulkanBuffer class1146 if (objects_laserPipeline.resized) {1147 resizeBufferSet(storageBuffers_laserPipeline, objects_laserPipeline.capacity * sizeof(SSBO_Laser),1148 resourceCommandPool, graphicsQueue, true);1149 1150 objects_laserPipeline.resize();1151 1152 laserPipeline.updateDescriptorInfo(1, &storageBuffers_laserPipeline.infoSet, swapChainImages.size());1153 }1154 1155 for (size_t i = 0; i < laserObjects.size(); i++) {1156 if (laserObjects[i].modified) {1157 updateObject(laserObjects[i]);1158 updateBufferSet(storageBuffers_laserPipeline, i, laserObjects[i].ssbo);1159 }1160 }1161 1162 // TODO: Probably move the resizing to the VulkanBuffer class1163 if (objects_explosionPipeline.resized) {1164 resizeBufferSet(storageBuffers_explosionPipeline, objects_explosionPipeline.capacity * sizeof(SSBO_Explosion),1165 resourceCommandPool, graphicsQueue, true);1166 1167 objects_explosionPipeline.resize();1168 1169 explosionPipeline.updateDescriptorInfo(1, &storageBuffers_explosionPipeline.infoSet, swapChainImages.size());1170 }1171 1172 for (size_t i = 0; i < explosionObjects.size(); i++) {1173 if (explosionObjects[i].modified) {1174 updateObject(explosionObjects[i]);1175 updateBufferSet(storageBuffers_explosionPipeline, i, explosionObjects[i].ssbo);1176 }1177 }1178 1179 explosion_UBO.cur_time = curTime;1180 1212 1181 1213 VulkanUtils::copyDataToMemory(device, &object_VP_mats, uniformBuffers_modelPipeline.memory[imageIndex], 0,
Note:
See TracChangeset
for help on using the changeset viewer.