Changeset dc19a39 in opengl-game
- Timestamp:
- Apr 26, 2019, 4:20:37 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 7e10667
- Parents:
- f97e638
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
explosion.vert
rf97e638 rdc19a39 4 4 5 5 uniform mat4 view, proj; 6 uniform mat4 model_mat;7 6 layout (std140) uniform models { 8 7 mat4 model_mats[MAX_NUM_OBJECTS]; … … 14 13 layout (location = 0) in vec3 v_i; // initial velocity 15 14 layout (location = 1) in float start_time; 16 //layout(location = 2) in uint ubo_index;15 layout(location = 2) in uint ubo_index; 17 16 18 17 out float opacity; … … 40 39 p += normalize(v_i) * mod(t, duration) / duration * 0.3; // allow time to loop around so particle emitter keeps going 41 40 42 //gl_Position = proj * view * model_mats[ubo_index] * vec4(p, 1.0); 43 gl_Position = proj * view * model_mats[0] * vec4(p, 1.0); 41 gl_Position = proj * view * model_mats[ubo_index] * vec4(p, 1.0); 44 42 gl_PointSize = 15.0; // size in pixels 45 43 } -
new-game.cpp
rf97e638 rdc19a39 103 103 }; 104 104 105 struct ParticleEffect : SceneObject { 106 vector<GLfloat> particleVelocities; 107 vector<GLfloat> particleTimes; 108 }; 109 105 110 struct EffectOverTime { 106 111 float& effectedValue; … … 203 208 void calculateObjectBoundingBox(SceneObject* obj); 204 209 205 void initializeParticleEffectBuffers(vec3 origin, 206 map<GLuint, BufferInfo>& shaderBufferInfo, 210 void initializeParticleEffectBuffers(map<GLuint, BufferInfo>& shaderBufferInfo, 207 211 map<ObjectType, ShaderModelGroup>& modelGroups, 208 212 GLuint ubo); … … 224 228 Asteroid* createAsteroid(vec3 pos); 225 229 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width); 226 SceneObject* createExplosion();230 ParticleEffect* createExplosion(); 227 231 228 232 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo); … … 285 289 286 290 SceneObject* objExplosion; 287 SceneObject* objFirst;288 291 289 292 map<string, vector<UIValue>> valueLists; … … 551 554 defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING, 552 555 1, GL_FLOAT, 0); 556 defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING, 557 1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset)); 553 558 554 559 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "explosion_start_time", ATTRIB_UNIFORM, … … 556 561 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM, 557 562 1, UNIFORM_1F, &curTime); 558 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "model_mat", ATTRIB_UNIFORM,559 1, UNIFORM_MATRIX_4F, NULL);560 563 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "view", ATTRIB_UNIFORM, 561 564 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); … … 610 613 proj_mat = make_mat4(proj_arr); 611 614 612 initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), 613 shaderBufferInfo, 614 modelGroups, 615 ubo); 615 initializeParticleEffectBuffers(shaderBufferInfo, modelGroups, ubo); 616 616 617 617 /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos): … … 866 866 867 867 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["explosion_start_time"]); 868 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(objExplosion->model_mat)); 868 869 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 870 glBufferSubData(GL_UNIFORM_BUFFER, objExplosion->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(objExplosion->model_mat)); 869 871 } 870 872 } … … 2023 2025 } 2024 2026 2025 void initializeParticleEffectBuffers(vec3 origin, 2026 map<GLuint, BufferInfo>& shaderBufferInfo, 2027 void initializeParticleEffectBuffers(map<GLuint, BufferInfo>& shaderBufferInfo, 2027 2028 map<ObjectType, ShaderModelGroup>& modelGroups, 2028 2029 GLuint ubo) { … … 2042 2043 } 2043 2044 2044 mat4 model_mat = translate(mat4(1.0f), origin);2045 2046 2045 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram); 2047 2046 2048 2047 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["proj"]); 2049 2048 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["view"]); 2050 bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(model_mat));2051 2049 2052 2050 // the glBufferData calls need to stay here while the corresponding arrays … … 2060 2058 glBufferData(GL_ARRAY_BUFFER, sizeof(vt), vt, GL_STATIC_DRAW); 2061 2059 2060 // TODO: createExplosion should take the velocities and times as arguments and copy them to the explosion object 2061 // That way, this function could maybe be called every time a new explosion is created 2062 2062 objExplosion = createExplosion(); 2063 2063 addObjectToScene(objExplosion, shaderBufferInfo, modelGroups, ubo); … … 2187 2187 glBindBuffer(GL_ARRAY_BUFFER, attrib->buffer); 2188 2188 glBufferData(GL_ARRAY_BUFFER, smg->vboCapacity * GLsizeof(attrib->type) * attrib->size, NULL, GL_DYNAMIC_DRAW); 2189 } 2189 } else if (modelGroupIt->first == TYPE_EXPLOSION) { 2190 attrib = &smg->attribs["ubo_index"]; 2191 glBindBuffer(GL_ARRAY_BUFFER, attrib->buffer); 2192 glBufferData(GL_ARRAY_BUFFER, smg->vboCapacity * GLsizeof(attrib->type) * attrib->size, NULL, GL_DYNAMIC_DRAW); 2193 } 2194 2195 // TODO: I could move the glBufferData calls for explosions here and call glBufferSubData in copyObjectDataToBuffers 2196 // To make this work, I would need to store the explosion points and times in the explosion object so I could then copy 2197 // them to the buffers. 2198 // Also, confirm that rendering multiple exxplosions would be feasible. They might need different curTimes 2199 // Also, store the explosion model mat(s) in the global ubo, just like all other objects 2200 // This should just require setting their ubo_offset correctly 2201 // (This already seems to be happening. I think I just need to add a ubo index buffer to explosions) 2190 2202 } 2191 2203 … … 2206 2218 BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj.type].shaderProgram]; 2207 2219 2208 if (obj.type == TYPE_SHIP || obj.type == TYPE_ASTEROID || obj.type == TYPE_LASER) { 2209 obj.vertex_vbo_offset = modelGroups[obj.type].numPoints; 2210 } else { 2211 obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints; 2212 } 2220 obj.vertex_vbo_offset = modelGroups[obj.type].numPoints; 2213 2221 obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset; 2214 2222 2215 if (obj.ubo_offset == 0) { 2216 objFirst = &obj; 2217 } 2218 2219 if (obj.type != TYPE_EXPLOSION) { 2220 ShaderModelGroup& smg = modelGroups[obj.type]; 2221 2222 for (map<string, AttribInfo>::iterator it = smg.attribs.begin(); it != smg.attribs.end(); it++) { 2223 glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer); 2224 2223 ShaderModelGroup& smg = modelGroups[obj.type]; 2224 2225 for (map<string, AttribInfo>::iterator it = smg.attribs.begin(); it != smg.attribs.end(); it++) { 2226 glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer); 2227 2228 if (obj.type != TYPE_EXPLOSION || it->first == "ubo_index") { 2225 2229 switch (it->second.attribType) { 2226 2230 case ATTRIB_POINT_VARYING: … … 2238 2242 } 2239 2243 } 2240 2244 } 2245 2246 if (obj.type != TYPE_EXPLOSION) { 2241 2247 obj.model_mat = obj.model_transform * obj.model_base; 2242 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 2243 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 2244 2245 if (obj.type == TYPE_ASTEROID) { 2246 glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram); 2247 2248 ostringstream oss; 2249 oss << "hp[" << obj.ubo_offset << "]"; 2250 glUniform1f(glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, oss.str().c_str()), ((Asteroid*)&obj)->hp); 2251 } 2248 } 2249 2250 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 2251 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 2252 2253 if (obj.type == TYPE_ASTEROID) { 2254 glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram); 2255 2256 ostringstream oss; 2257 oss << "hp[" << obj.ubo_offset << "]"; 2258 glUniform1f(glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, oss.str().c_str()), ((Asteroid*)&obj)->hp); 2252 2259 } 2253 2260 … … 2443 2450 glEnable(GL_PROGRAM_POINT_SIZE); 2444 2451 2445 glBindBuffer(GL_UNIFORM_BUFFER, ubo);2446 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(objExplosion->model_mat));2447 2448 2452 glDrawArrays(GL_POINTS, 0, modelGroups[TYPE_EXPLOSION].numPoints); 2449 2450 glBindBuffer(GL_UNIFORM_BUFFER, ubo);2451 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(objFirst->model_mat));2452 2453 2453 2454 glDisable(GL_PROGRAM_POINT_SIZE); … … 2721 2722 } 2722 2723 2723 SceneObject* createExplosion() {2724 SceneObject* obj = new SceneObject();2724 ParticleEffect* createExplosion() { 2725 ParticleEffect* obj = new ParticleEffect(); 2725 2726 obj->type = TYPE_EXPLOSION; 2726 2727
Note:
See TracChangeset
for help on using the changeset viewer.