Changeset 39ac76d in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jan 31, 2019, 5:04:01 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
ae0c7f4
Parents:
646f3f2
Message:

Remove some unused code and rename some variables to more descriptive names

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r646f3f2 r39ac76d  
    7070   vector<GLfloat> texcoords;
    7171   vector<GLfloat> normals;
    72    vector<GLfloat> selected_colors;
    7372   bool deleted;
    7473   vec3 bounding_center;
     
    9291   SceneObject* effectedObject;
    9392
     93   // TODO: Why not just use an initializer list for all the instance variables
    9494   EffectOverTime(float& effectedValue, float changePerSecond, SceneObject* object)
    9595      : effectedValue(effectedValue), changePerSecond(changePerSecond), effectedObject(object) {
     
    139139                  GLuint points_vbo,
    140140                  GLuint colors_vbo,
    141                   GLuint selected_colors_vbo,
    142141                  GLuint texcoords_vbo,
    143142                  GLuint normals_vbo,
     
    152151                  GLuint* points_vbo,
    153152                  GLuint* colors_vbo,
    154                   GLuint* selected_colors_vbo,
    155153                  GLuint* texcoords_vbo,
    156154                  GLuint* normals_vbo,
     
    162160                  GLuint points_vbo,
    163161                  GLuint colors_vbo,
    164                   GLuint selected_colors_vbo,
    165162                  GLuint texcoords_vbo,
    166163                  GLuint normals_vbo,
     
    172169                  GLuint points_vbo,
    173170                  GLuint colors_vbo,
    174                   GLuint selected_colors_vbo,
    175171                  GLuint texcoords_vbo,
    176172                  GLuint normals_vbo,
     
    183179                  GLuint points_vbo,
    184180                  GLuint colors_vbo,
    185                   GLuint selected_colors_vbo,
    186181                  GLuint texcoords_vbo,
    187182                  GLuint normals_vbo,
     
    206201
    207202void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    208                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
    209                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    210                   GLuint colors_vbo, GLuint selected_colors_vbo, GLuint ubo,
    211                   SceneObject* selectedObject);
     203                  GLuint ship_sp, GLuint asteroid_sp, GLuint laser_sp, GLuint explosion_sp,
     204                  GLuint ship_vao, GLuint asteroid_vao, GLuint laser_vao, GLuint explosion_vao,
     205                  GLuint colors_vbo, GLuint ubo, SceneObject* selectedObject);
    212206
    213207void renderSceneGui();
     
    451445   map<GLuint, BufferInfo> shaderBufferInfo;
    452446
    453    // TODO: Rename color_sp to ship_sp and comment out texture_sp)
    454 
    455    GLuint color_sp = loadShaderProgram("./ship.vert", "./ship.frag");
     447   GLuint ship_sp = loadShaderProgram("./ship.vert", "./ship.frag");
    456448   GLuint asteroid_sp = loadShaderProgram("./asteroid.vert", "./asteroid.frag");
    457    GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
    458449   GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
    459450   GLuint explosion_sp = loadShaderProgram("./explosion.vert", "./explosion.frag");
    460451
    461    shaderBufferInfo[color_sp] = BufferInfo();
     452   shaderBufferInfo[ship_sp] = BufferInfo();
    462453   shaderBufferInfo[asteroid_sp] = BufferInfo();
    463    shaderBufferInfo[texture_sp] = BufferInfo();
    464454   shaderBufferInfo[laser_sp] = BufferInfo();
    465455   shaderBufferInfo[explosion_sp] = BufferInfo();
     
    470460
    471461   // player ship
    472    SceneObject* ship = createShip(color_sp);
     462   SceneObject* ship = createShip(ship_sp);
    473463   objects.push_back(ship);
    474464
    475465   vector<SceneObject>::iterator obj_it;
    476466
    477    GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
    478       normals_vbo, ubo, model_mat_idx_vbo;
     467   GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo;
    479468
    480469   initializeBuffers(
    481470      &points_vbo,
    482471      &colors_vbo,
    483       &selected_colors_vbo,
    484472      &texcoords_vbo,
    485473      &normals_vbo,
     
    491479      points_vbo,
    492480      colors_vbo,
    493       selected_colors_vbo,
    494481      texcoords_vbo,
    495482      normals_vbo,
     
    498485      asteroid_sp);
    499486
    500    GLuint color_vao = 0;
    501    glGenVertexArrays(1, &color_vao);
    502    glBindVertexArray(color_vao);
     487   GLuint ship_vao = 0;
     488   glGenVertexArrays(1, &ship_vao);
     489   glBindVertexArray(ship_vao);
    503490
    504491   glEnableVertexAttribArray(0);
     
    534521   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    535522   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    536 
    537    glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    538    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    539 
    540    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    541    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    542 
    543    GLuint texture_vao = 0;
    544    glGenVertexArrays(1, &texture_vao);
    545    glBindVertexArray(texture_vao);
    546 
    547    glEnableVertexAttribArray(0);
    548    glEnableVertexAttribArray(1);
    549    glEnableVertexAttribArray(2);
    550    glEnableVertexAttribArray(3);
    551 
    552    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    553    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    554 
    555    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    556    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    557523
    558524   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     
    616582      points_vbo,
    617583      colors_vbo,
    618       selected_colors_vbo,
    619584      texcoords_vbo,
    620585      normals_vbo,
     
    624589   GLuint ub_binding_point = 0;
    625590
    626    // TODO: Replace test_loc and mat_loc with more descriptive names
    627    GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
    628    GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
    629    GLuint color_sp_models_ub_index = glGetUniformBlockIndex(color_sp, "models");
     591   GLuint ship_view_mat_loc = glGetUniformLocation(ship_sp, "view");
     592   GLuint ship_proj_mat_loc = glGetUniformLocation(ship_sp, "proj");
     593   GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(ship_sp, "models");
    630594
    631595   GLuint asteroid_view_mat_loc = glGetUniformLocation(asteroid_sp, "view");
    632596   GLuint asteroid_proj_mat_loc = glGetUniformLocation(asteroid_sp, "proj");
    633597   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(asteroid_sp, "models");
    634 
    635    GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
    636    GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
    637    GLuint texture_sp_models_ub_index = glGetUniformBlockIndex(texture_sp, "models");
    638598
    639599   GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
     
    647607
    648608
    649    glUseProgram(color_sp);
    650    glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    651    glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    652 
    653    glUniformBlockBinding(color_sp, color_sp_models_ub_index, ub_binding_point);
     609   glUseProgram(ship_sp);
     610   glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     611   glUniformMatrix4fv(ship_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     612
     613   glUniformBlockBinding(ship_sp, ship_sp_models_ub_index, ub_binding_point);
    654614   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    655615
     
    660620
    661621   glUniformBlockBinding(asteroid_sp, asteroid_sp_models_ub_index, ub_binding_point);
    662    glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    663 
    664 
    665    glUseProgram(texture_sp);
    666    glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    667    glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    668 
    669    glUniformBlockBinding(texture_sp, texture_sp_models_ub_index, ub_binding_point);
    670622   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    671623
     
    756708               points_vbo,
    757709               colors_vbo,
    758                selected_colors_vbo,
    759710               texcoords_vbo,
    760711               normals_vbo,
     
    816767               points_vbo,
    817768               colors_vbo,
    818                selected_colors_vbo,
    819769               texcoords_vbo,
    820770               normals_vbo,
     
    834784               points_vbo,
    835785               colors_vbo,
    836                selected_colors_vbo,
    837786               texcoords_vbo,
    838787               normals_vbo,
     
    955904         //printVector("cam pos", cam_pos);
    956905
    957          glUseProgram(color_sp);
    958          glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    959 
    960          glUseProgram(texture_sp);
    961          glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     906         glUseProgram(ship_sp);
     907         glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    962908
    963909         glUseProgram(laser_sp);
     
    981927         case STATE_GAME:
    982928            renderScene(shaderBufferInfo,
    983                color_sp, asteroid_sp, texture_sp, laser_sp, explosion_sp,
    984                color_vao, asteroid_vao, texture_vao, laser_vao, explosion_vao,
    985                colors_vbo, selected_colors_vbo, ubo,
    986                selectedObject);
     929               ship_sp, asteroid_sp, laser_sp, explosion_sp,
     930               ship_vao, asteroid_vao, laser_vao, explosion_vao,
     931               colors_vbo, ubo, selectedObject);
    987932            renderSceneGui();
    988933            break;
     
    13381283   GLuint points_vbo,
    13391284   GLuint colors_vbo,
    1340    GLuint selected_colors_vbo,
    13411285   GLuint texcoords_vbo,
    13421286   GLuint normals_vbo,
     
    13631307         points_vbo,
    13641308         colors_vbo,
    1365          selected_colors_vbo,
    13661309         texcoords_vbo,
    13671310         normals_vbo,
     
    13731316         points_vbo,
    13741317         colors_vbo,
    1375          selected_colors_vbo,
    13761318         texcoords_vbo,
    13771319         normals_vbo,
     
    18181760   };
    18191761   ship->texcoords = { 0.0f };
    1820    ship->selected_colors = { 0.0f };
    18211762
    18221763   mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
     
    19231864                  GLuint* points_vbo,
    19241865                  GLuint* colors_vbo,
    1925                   GLuint* selected_colors_vbo,
    19261866                  GLuint* texcoords_vbo,
    19271867                  GLuint* normals_vbo,
     
    19341874   glGenBuffers(1, colors_vbo);
    19351875
    1936    *selected_colors_vbo = 0;
    1937    glGenBuffers(1, selected_colors_vbo);
    1938 
    19391876   *texcoords_vbo = 0;
    19401877   glGenBuffers(1, texcoords_vbo);
     
    19541891                  GLuint points_vbo,
    19551892                  GLuint colors_vbo,
    1956                   GLuint selected_colors_vbo,
    19571893                  GLuint texcoords_vbo,
    19581894                  GLuint normals_vbo,
     
    20151951      points_vbo,
    20161952      colors_vbo,
    2017       selected_colors_vbo,
    20181953      texcoords_vbo,
    20191954      normals_vbo,
     
    20291964                  GLuint points_vbo,
    20301965                  GLuint colors_vbo,
    2031                   GLuint selected_colors_vbo,
    20321966                  GLuint texcoords_vbo,
    20331967                  GLuint normals_vbo,
     
    21102044   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    21112045
    2112    glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2113    glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    2114 
    21152046   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    21162047   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 2, NULL, GL_DYNAMIC_DRAW);
     
    21292060         points_vbo,
    21302061         colors_vbo,
    2131          selected_colors_vbo,
    21322062         texcoords_vbo,
    21332063         normals_vbo,
     
    21422072                  GLuint points_vbo,
    21432073                  GLuint colors_vbo,
    2144                   GLuint selected_colors_vbo,
    21452074                  GLuint texcoords_vbo,
    21462075                  GLuint normals_vbo,
     
    21722101         glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    21732102         glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
    2174 
    2175          glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2176          glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
    21772103
    21782104         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     
    23582284
    23592285void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    2360                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
    2361                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    2362                   GLuint colors_vbo, GLuint selected_colors_vbo, GLuint ubo,
    2363                   SceneObject* selectedObject) {
    2364 
    2365    glUseProgram(color_sp);
    2366    glBindVertexArray(color_vao);
    2367 
    2368    /*
    2369    if (selectedObject != NULL) {
    2370       glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2371       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    2372 
    2373       glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points);
    2374    }
    2375    */
    2376 
    2377    // Uncomment this code when I want to use selected colors again
    2378    // glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2379    // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    2380 
    2381    glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset);
     2286                  GLuint ship_sp, GLuint asteroid_sp, GLuint laser_sp, GLuint explosion_sp,
     2287                  GLuint ship_vao, GLuint asteroid_vao, GLuint laser_vao, GLuint explosion_vao,
     2288                  GLuint colors_vbo, GLuint ubo, SceneObject* selectedObject) {
     2289
     2290   glUseProgram(ship_sp);
     2291   glBindVertexArray(ship_vao);
     2292
     2293   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[ship_sp].vbo_base, shaderBufferInfo[ship_sp].vbo_offset);
    23822294
    23832295   glUseProgram(asteroid_sp);
     
    23862298   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[asteroid_sp].vbo_base, shaderBufferInfo[asteroid_sp].vbo_offset);
    23872299
    2388    glUseProgram(texture_sp);
    2389    glBindVertexArray(texture_vao);
    2390 
    2391    glDrawArrays(GL_TRIANGLES, shaderBufferInfo[texture_sp].vbo_base, shaderBufferInfo[texture_sp].vbo_offset);
    2392 
    23932300   glEnable(GL_BLEND);
    23942301
     
    23972304
    23982305   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
     2306
     2307   // To render explosions, my new shader descriptor object needs to have a refernece to the shader and the vao
     2308   // and know the number of points to render.
    23992309
    24002310   glUseProgram(explosion_sp);
     
    26162526   };
    26172527   obj->texcoords = { 0.0f };
    2618    obj->selected_colors = { 0.0f };
    26192528
    26202529   mat4 T = translate(mat4(1.0f), pos);
Note: See TracChangeset for help on using the changeset viewer.