Changeset dc19a39 in opengl-game


Ignore:
Timestamp:
Apr 26, 2019, 4:20:37 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
7e10667
Parents:
f97e638
Message:

Make explosion objects use the global model mat uniform buffer and a varying ubo index attribute to access it

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • explosion.vert

    rf97e638 rdc19a39  
    44
    55uniform mat4 view, proj;
    6 uniform mat4 model_mat;
    76layout (std140) uniform models {
    87  mat4 model_mats[MAX_NUM_OBJECTS];
     
    1413layout (location = 0) in vec3 v_i; // initial velocity
    1514layout (location = 1) in float start_time;
    16 //layout(location = 2) in uint ubo_index;
     15layout(location = 2) in uint ubo_index;
    1716
    1817out float opacity;
     
    4039   p += normalize(v_i) * mod(t, duration) / duration * 0.3; // allow time to loop around so particle emitter keeps going
    4140
    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);
    4442   gl_PointSize = 15.0; // size in pixels
    4543}
  • new-game.cpp

    rf97e638 rdc19a39  
    103103};
    104104
     105struct ParticleEffect : SceneObject {
     106   vector<GLfloat> particleVelocities;
     107   vector<GLfloat> particleTimes;
     108};
     109
    105110struct EffectOverTime {
    106111   float& effectedValue;
     
    203208void calculateObjectBoundingBox(SceneObject* obj);
    204209
    205 void initializeParticleEffectBuffers(vec3 origin,
    206                   map<GLuint, BufferInfo>& shaderBufferInfo,
     210void initializeParticleEffectBuffers(map<GLuint, BufferInfo>& shaderBufferInfo,
    207211                  map<ObjectType, ShaderModelGroup>& modelGroups,
    208212                  GLuint ubo);
     
    224228Asteroid* createAsteroid(vec3 pos);
    225229Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width);
    226 SceneObject* createExplosion();
     230ParticleEffect* createExplosion();
    227231
    228232void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
     
    285289
    286290SceneObject* objExplosion;
    287 SceneObject* objFirst;
    288291
    289292map<string, vector<UIValue>> valueLists;
     
    551554   defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING,
    552555      1, GL_FLOAT, 0);
     556   defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING,
     557      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
    553558
    554559   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "explosion_start_time", ATTRIB_UNIFORM,
     
    556561   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM,
    557562      1, UNIFORM_1F, &curTime);
    558    defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "model_mat", ATTRIB_UNIFORM,
    559       1, UNIFORM_MATRIX_4F, NULL);
    560563   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "view", ATTRIB_UNIFORM,
    561564      1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
     
    610613   proj_mat = make_mat4(proj_arr);
    611614
    612    initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f),
    613       shaderBufferInfo,
    614       modelGroups,
    615       ubo);
     615   initializeParticleEffectBuffers(shaderBufferInfo, modelGroups, ubo);
    616616
    617617   /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos):
     
    866866
    867867                  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));
    869871               }
    870872            }
     
    20232025}
    20242026
    2025 void initializeParticleEffectBuffers(vec3 origin,
    2026                   map<GLuint, BufferInfo>& shaderBufferInfo,
     2027void initializeParticleEffectBuffers(map<GLuint, BufferInfo>& shaderBufferInfo,
    20272028                  map<ObjectType, ShaderModelGroup>& modelGroups,
    20282029                  GLuint ubo) {
     
    20422043   }
    20432044
    2044    mat4 model_mat = translate(mat4(1.0f), origin);
    2045 
    20462045   glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
    20472046
    20482047   bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["proj"]);
    20492048   bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["view"]);
    2050    bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(model_mat));
    20512049
    20522050   // the glBufferData calls need to stay here while the corresponding arrays
     
    20602058   glBufferData(GL_ARRAY_BUFFER, sizeof(vt), vt, GL_STATIC_DRAW);
    20612059
     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
    20622062   objExplosion = createExplosion();
    20632063   addObjectToScene(objExplosion, shaderBufferInfo, modelGroups, ubo);
     
    21872187         glBindBuffer(GL_ARRAY_BUFFER, attrib->buffer);
    21882188         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)
    21902202   }
    21912203
     
    22062218   BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj.type].shaderProgram];
    22072219
    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;
    22132221   obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
    22142222
    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") {
    22252229         switch (it->second.attribType) {
    22262230            case ATTRIB_POINT_VARYING:
     
    22382242         }
    22392243      }
    2240 
     2244   }
     2245
     2246   if (obj.type != TYPE_EXPLOSION) {
    22412247      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);
    22522259   }
    22532260
     
    24432450   glEnable(GL_PROGRAM_POINT_SIZE);
    24442451
    2445    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    2446    glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(objExplosion->model_mat));
    2447 
    24482452   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));
    24522453
    24532454   glDisable(GL_PROGRAM_POINT_SIZE);
     
    27212722}
    27222723
    2723 SceneObject* createExplosion() {
    2724    SceneObject* obj = new SceneObject();
     2724ParticleEffect* createExplosion() {
     2725   ParticleEffect* obj = new ParticleEffect();
    27252726   obj->type = TYPE_EXPLOSION;
    27262727
Note: See TracChangeset for help on using the changeset viewer.