Changeset b62c109 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Feb 11, 2019, 4:01:08 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
dd9771c
Parents:
4c7cd57
Message:

Use a ShaderModelGroup for the lasers amd remove BufferInfo.vbo_offset since it's no longer being used

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r4c7cd57 rb62c109  
    106106struct BufferInfo {
    107107   unsigned int vbo_base;
    108    unsigned int vbo_offset;
    109108   unsigned int vbo_capacity;
    110109   unsigned int ubo_base;
     
    216215
    217216void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    218                   map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo,
    219                   GLuint laser_sp, GLuint laser_vao);
     217                  map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo);
    220218
    221219void renderSceneGui();
     
    460458   map<ObjectType, ShaderModelGroup> modelGroups;
    461459
    462    GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
    463 
    464    shaderBufferInfo[laser_sp] = BufferInfo();
    465 
    466460   modelGroups[TYPE_SHIP] = createModelGroup(
    467461      loadShaderProgram("./ship.vert", "./ship.frag"));
     
    471465      loadShaderProgram("./asteroid.vert", "./asteroid.frag"));
    472466   shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary
     467
     468   modelGroups[TYPE_LASER] = createModelGroup(
     469      loadShaderProgram("./laser.vert", "./laser.frag"));
     470   shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary
    473471
    474472   modelGroups[TYPE_EXPLOSION] = createModelGroup(
     
    544542   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    545543
    546    GLuint laser_vao = 0;
    547    glGenVertexArrays(1, &laser_vao);
    548    glBindVertexArray(laser_vao);
     544   glBindVertexArray(modelGroups[TYPE_LASER].vao);
    549545
    550546   glEnableVertexAttribArray(0);
     
    647643   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models");
    648644
    649    GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
    650    GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj");
    651    GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
    652    GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(laser_sp, "models");
     645   GLuint laser_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "view");
     646   GLuint laser_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "proj");
     647   GLuint laser_color_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "laser_color");
     648   GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models");
    653649
    654650   GLuint explosion_start_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "explosion_start_time");
     
    673669
    674670
    675    glUseProgram(laser_sp);
     671   glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
    676672   glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    677673   glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    678674   glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
    679675
    680    glUniformBlockBinding(laser_sp, laser_sp_models_ub_index, ub_binding_point);
     676   glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point);
    681677   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    682678
     
    812808
    813809            leftLaser = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset,
    814                vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);
     810               vec3(0.0f, 1.0f, 0.0f), 0.03f, modelGroups[TYPE_LASER].shaderProgram);
    815811            addObjectToScene(leftLaser, shaderBufferInfo, modelGroups,
    816812               points_vbo,
     
    828824
    829825            rightLaser = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset,
    830                vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);
     826               vec3(0.0f, 1.0f, 0.0f), 0.03f, modelGroups[TYPE_LASER].shaderProgram);
    831827            addObjectToScene(rightLaser, shaderBufferInfo, modelGroups,
    832828               points_vbo,
     
    953949         glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    954950
    955          glUseProgram(laser_sp);
     951         glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
    956952         glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    957953
     
    972968            break;
    973969         case STATE_GAME:
    974             renderScene(shaderBufferInfo, modelGroups, ubo,
    975                laser_sp, laser_vao);
     970            renderScene(shaderBufferInfo, modelGroups, ubo);
    976971            renderSceneGui();
    977972            break;
     
    13361331   BufferInfo* bufferInfo = &shaderBufferInfo[obj->shader_program];
    13371332
    1338    unsigned int numPoints =
    1339       obj->type == TYPE_SHIP ||
    1340       obj->type == TYPE_ASTEROID ||
    1341       obj->type == TYPE_EXPLOSION ?
    1342       modelGroups[obj->type].numPoints :
    1343       bufferInfo->vbo_offset;
    1344 
    13451333   // Check if the buffers aren't large enough to fit the new object and, if so, call
    13461334   // populateBuffers() to resize and repopupulate them
    1347    if (bufferInfo->vbo_capacity < (numPoints + obj->num_points) ||
     1335   if (bufferInfo->vbo_capacity < (modelGroups[obj->type].numPoints + obj->num_points) ||
    13481336      bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) {
    13491337
     
    20892077      shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2;
    20902078
    2091       if (shaderCountIt->first != modelGroups[TYPE_SHIP].shaderProgram &&
    2092          shaderCountIt->first != modelGroups[TYPE_ASTEROID].shaderProgram &&
    2093          shaderCountIt->first != modelGroups[TYPE_EXPLOSION].shaderProgram) {
    2094          shaderBufferInfo[shaderCountIt->first].vbo_offset = 0;
    2095       }
    20962079      shaderBufferInfo[shaderCountIt->first].ubo_offset = 0;
    20972080
     
    21052088   map<ObjectType, ShaderModelGroup>::iterator modelGroupIt;
    21062089   for (modelGroupIt = modelGroups.begin(); modelGroupIt != modelGroups.end(); modelGroupIt++) {
    2107       if (modelGroupIt->first == TYPE_SHIP ||
    2108          modelGroupIt->first == TYPE_ASTEROID ||
    2109          modelGroupIt->first == TYPE_EXPLOSION) {
    2110          modelGroups[modelGroupIt->first].numPoints = 0;
    2111       }
     2090      modelGroups[modelGroupIt->first].numPoints = 0;
    21122091   }
    21132092
     
    21542133   BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
    21552134
    2156    if (obj.type == TYPE_SHIP ||
    2157       obj.type == TYPE_ASTEROID ||
    2158       obj.type == TYPE_EXPLOSION) {
    2159       obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints;
    2160    } else {
    2161       obj.vertex_vbo_offset = bufferInfo->vbo_base + bufferInfo->vbo_offset;
    2162    }
    2163    
     2135   obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints;
    21642136   obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
    21652137
     
    22012173   }
    22022174
    2203    if (obj.type == TYPE_SHIP ||
    2204       obj.type == TYPE_ASTEROID ||
    2205       obj.type == TYPE_EXPLOSION) {
    2206       modelGroups[obj.type].numPoints += obj.num_points;
    2207    } else {
    2208       bufferInfo->vbo_offset += obj.num_points;
    2209    }
     2175   modelGroups[obj.type].numPoints += obj.num_points;
    22102176   bufferInfo->ubo_offset++;
    22112177}
     
    23722338
    23732339void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    2374                   map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo,
    2375                   GLuint laser_sp, GLuint laser_vao) {
     2340                  map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) {
    23762341
    23772342   glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
     
    23872352   glEnable(GL_BLEND);
    23882353
    2389    glUseProgram(laser_sp);
    2390    glBindVertexArray(laser_vao);
    2391 
    2392    glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
     2354   glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
     2355   glBindVertexArray(modelGroups[TYPE_LASER].vao);
     2356
     2357   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram].vbo_base, modelGroups[TYPE_LASER].numPoints);
    23932358
    23942359   glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
Note: See TracChangeset for help on using the changeset viewer.