Changes in new-game.cpp [bebfd5c:39ac76d] in opengl-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rbebfd5c 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,
    142                   GLuint texcoords_vbo,
    143                   GLuint normals_vbo,
    144                   GLuint ubo,
    145                   GLuint model_mat_idx_vbo,
    146                   GLuint asteroid_sp,
    147                   GLuint explosion_sp);
    148 void removeObjectFromScene(SceneObject& obj, GLuint ubo);
    149 
    150 void calculateObjectBoundingBox(SceneObject* obj);
    151 
    152 void initializeBuffers(
    153                   GLuint* points_vbo,
    154                   GLuint* colors_vbo,
    155                   GLuint* selected_colors_vbo,
    156                   GLuint* texcoords_vbo,
    157                   GLuint* normals_vbo,
    158                   GLuint* ubo,
    159                   GLuint* model_mat_idx_vbo);
    160 
    161 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp,
    162                   map<GLuint, BufferInfo>& shaderBufferInfo,
    163                   GLuint points_vbo,
    164                   GLuint colors_vbo,
    165                   GLuint selected_colors_vbo,
    166                   GLuint texcoords_vbo,
    167                   GLuint normals_vbo,
    168                   GLuint ubo,
    169                   GLuint model_mat_idx_vbo);
    170 
    171 void populateBuffers(vector<SceneObject*>& objects,
    172                   map<GLuint, BufferInfo>& shaderBufferInfo,
    173                   GLuint points_vbo,
    174                   GLuint colors_vbo,
    175                   GLuint selected_colors_vbo,
    176                   GLuint texcoords_vbo,
    177                   GLuint normals_vbo,
    178                   GLuint ubo,
    179                   GLuint model_mat_idx_vbo,
    180                   GLuint asteroid_sp,
    181                   GLuint explosion_sp);
    182 
    183 void copyObjectDataToBuffers(SceneObject& obj,
    184                   map<GLuint, BufferInfo>& shaderBufferInfo,
    185                   GLuint points_vbo,
    186                   GLuint colors_vbo,
    187                   GLuint selected_colors_vbo,
    188141                  GLuint texcoords_vbo,
    189142                  GLuint normals_vbo,
     
    191144                  GLuint model_mat_idx_vbo,
    192145                  GLuint asteroid_sp);
     146void removeObjectFromScene(SceneObject& obj, GLuint ubo);
     147
     148void calculateObjectBoundingBox(SceneObject* obj);
     149
     150void initializeBuffers(
     151                  GLuint* points_vbo,
     152                  GLuint* colors_vbo,
     153                  GLuint* texcoords_vbo,
     154                  GLuint* normals_vbo,
     155                  GLuint* ubo,
     156                  GLuint* model_mat_idx_vbo);
     157
     158GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp,
     159                  map<GLuint, BufferInfo>& shaderBufferInfo,
     160                  GLuint points_vbo,
     161                  GLuint colors_vbo,
     162                  GLuint texcoords_vbo,
     163                  GLuint normals_vbo,
     164                  GLuint ubo,
     165                  GLuint model_mat_idx_vbo);
     166
     167void populateBuffers(vector<SceneObject*>& objects,
     168                  map<GLuint, BufferInfo>& shaderBufferInfo,
     169                  GLuint points_vbo,
     170                  GLuint colors_vbo,
     171                  GLuint texcoords_vbo,
     172                  GLuint normals_vbo,
     173                  GLuint ubo,
     174                  GLuint model_mat_idx_vbo,
     175                  GLuint asteroid_sp);
     176
     177void copyObjectDataToBuffers(SceneObject& obj,
     178                  map<GLuint, BufferInfo>& shaderBufferInfo,
     179                  GLuint points_vbo,
     180                  GLuint colors_vbo,
     181                  GLuint texcoords_vbo,
     182                  GLuint normals_vbo,
     183                  GLuint ubo,
     184                  GLuint model_mat_idx_vbo,
     185                  GLuint asteroid_sp);
    193186
    194187void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
    195188
    196 // instead of using these methods, create constructors for these
     189// TODO: instead of using these methods, create constructors for these
    197190SceneObject* createShip(GLuint shader);
    198191Asteroid* createAsteroid(vec3 pos, GLuint shader);
     
    208201
    209202void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    210                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
    211                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    212                   GLuint colors_vbo, GLuint selected_colors_vbo,
    213                   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);
    214206
    215207void renderSceneGui();
     
    262254
    263255SceneObject* objExplosion;
    264 SceneObject* objFirst; // TODO: Set this, probably in populateBuffers and wherever else is appropriate.
     256SceneObject* objFirst;
    265257
    266258/*
     
    453445   map<GLuint, BufferInfo> shaderBufferInfo;
    454446
    455    // TODO: Rename color_sp to ship_sp and comment out texture_sp)
    456 
    457    GLuint color_sp = loadShaderProgram("./ship.vert", "./ship.frag");
     447   GLuint ship_sp = loadShaderProgram("./ship.vert", "./ship.frag");
    458448   GLuint asteroid_sp = loadShaderProgram("./asteroid.vert", "./asteroid.frag");
    459    GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
    460449   GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
    461450   GLuint explosion_sp = loadShaderProgram("./explosion.vert", "./explosion.frag");
    462451
    463    shaderBufferInfo[color_sp] = BufferInfo();
     452   shaderBufferInfo[ship_sp] = BufferInfo();
    464453   shaderBufferInfo[asteroid_sp] = BufferInfo();
    465    shaderBufferInfo[texture_sp] = BufferInfo();
    466454   shaderBufferInfo[laser_sp] = BufferInfo();
    467455   shaderBufferInfo[explosion_sp] = BufferInfo();
     
    472460
    473461   // player ship
    474    SceneObject* ship = createShip(color_sp);
     462   SceneObject* ship = createShip(ship_sp);
    475463   objects.push_back(ship);
    476464
    477465   vector<SceneObject>::iterator obj_it;
    478466
    479    GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
    480       normals_vbo, ubo, model_mat_idx_vbo;
     467   GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo;
    481468
    482469   initializeBuffers(
    483470      &points_vbo,
    484471      &colors_vbo,
    485       &selected_colors_vbo,
    486472      &texcoords_vbo,
    487473      &normals_vbo,
     
    493479      points_vbo,
    494480      colors_vbo,
    495       selected_colors_vbo,
    496481      texcoords_vbo,
    497482      normals_vbo,
    498483      ubo,
    499484      model_mat_idx_vbo,
    500       asteroid_sp,
    501       explosion_sp);
    502 
    503    GLuint color_vao = 0;
    504    glGenVertexArrays(1, &color_vao);
    505    glBindVertexArray(color_vao);
     485      asteroid_sp);
     486
     487   GLuint ship_vao = 0;
     488   glGenVertexArrays(1, &ship_vao);
     489   glBindVertexArray(ship_vao);
    506490
    507491   glEnableVertexAttribArray(0);
     
    537521   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    538522   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    539 
    540    glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    541    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    542 
    543    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    544    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    545 
    546    GLuint texture_vao = 0;
    547    glGenVertexArrays(1, &texture_vao);
    548    glBindVertexArray(texture_vao);
    549 
    550    glEnableVertexAttribArray(0);
    551    glEnableVertexAttribArray(1);
    552    glEnableVertexAttribArray(2);
    553    glEnableVertexAttribArray(3);
    554 
    555    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    556    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    557 
    558    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    559    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    560523
    561524   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     
    619582      points_vbo,
    620583      colors_vbo,
    621       selected_colors_vbo,
    622584      texcoords_vbo,
    623585      normals_vbo,
     
    625587      model_mat_idx_vbo);
    626588
    627    /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos):
    628 
    629       No, you're misunderstanding how this works. UBO binding works exactly like texture object binding.
    630 
    631       The OpenGL context has a number of slots for binding UBOs. There are GL_MAX_UNIFORM_BUFFER_BINDINGS number of
    632       slots for UBO binding.
    633 
    634       Uniform Blocks in a program can be set to use one of the slots in the context. You do this by first querying
    635       the block index using the block name (glGetUniformBlockIndex). This is similar to how you need to use
    636       glGetUniformLocation in order to set a uniform's value with glUniform. Block indices, like uniform locations,
    637       are specific to a program.
    638 
    639       Once you have the block index, you use glUniformBlockBinding to set that specific program to use a particular
    640       uniform buffer slot in the context.
    641 
    642       Let's say you have a global UBO that you want to use for every program. To make using it easier, you want to
    643       bind it just once.
    644 
    645       So first, you pick a uniform buffer slot in the context, one that always will refer to this UBO. Let's say
    646       you pick slot 8.
    647 
    648       When you build a program object that may use this global uniform buffer, what you do is quite simple. First,
    649       after linking the program, call glGetUniformBlockIndex(program, "NameOfGlobalUniformBlock"). If you get back
    650       GL_INVALID_INDEX, then you know that the global uniform block isn't used in that program. Otherwise you get
    651       back a block index.
    652 
    653       If you got a valid block index, then you call glUniformBlockBinding(program, uniformBlockIndex, 8). Remember
    654       that 8 is the uniform buffer context slot that we selected earlier. This causes this particular program to
    655       use uniform buffer slot #8 to find the buffer for "NameOfGlobalUniformBlock".
    656 
    657       Finally, to set the actual buffer in the context, call glBindBufferRange(GL_UNIFORM_BUFFER, 8,
    658       bufferObjectName, offset, size);
    659    */
    660 
    661589   GLuint ub_binding_point = 0;
    662590
    663    // TODO: Replace test_loc and mat_loc with more descriptive names
    664    GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
    665    GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
    666    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");
    667594
    668595   GLuint asteroid_view_mat_loc = glGetUniformLocation(asteroid_sp, "view");
    669596   GLuint asteroid_proj_mat_loc = glGetUniformLocation(asteroid_sp, "proj");
    670597   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(asteroid_sp, "models");
    671 
    672    GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
    673    GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
    674    GLuint texture_sp_models_ub_index = glGetUniformBlockIndex(texture_sp, "models");
    675598
    676599   GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
     
    684607
    685608
    686    glUseProgram(color_sp);
    687    glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    688    glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    689 
    690    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);
    691614   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    692615
     
    697620
    698621   glUniformBlockBinding(asteroid_sp, asteroid_sp_models_ub_index, ub_binding_point);
    699    glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    700 
    701 
    702    glUseProgram(texture_sp);
    703    glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    704    glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    705 
    706    glUniformBlockBinding(texture_sp, texture_sp_models_ub_index, ub_binding_point);
    707622   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    708623
     
    793708               points_vbo,
    794709               colors_vbo,
    795                selected_colors_vbo,
    796710               texcoords_vbo,
    797711               normals_vbo,
    798712               ubo,
    799713               model_mat_idx_vbo,
    800                asteroid_sp,
    801                explosion_sp);
     714               asteroid_sp);
    802715
    803716            elapsed_seconds_spawn -= 0.5f;
     
    854767               points_vbo,
    855768               colors_vbo,
    856                selected_colors_vbo,
    857769               texcoords_vbo,
    858770               normals_vbo,
    859771               ubo,
    860772               model_mat_idx_vbo,
    861                asteroid_sp,
    862                explosion_sp);
     773               asteroid_sp);
    863774         } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
    864775            removeObjectFromScene(*leftLaser, ubo);
     
    873784               points_vbo,
    874785               colors_vbo,
    875                selected_colors_vbo,
    876786               texcoords_vbo,
    877787               normals_vbo,
    878788               ubo,
    879789               model_mat_idx_vbo,
    880                asteroid_sp,
    881                explosion_sp);
     790               asteroid_sp);
    882791         } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
    883792            removeObjectFromScene(*rightLaser, ubo);
     
    894803                  removeObjectFromScene(*objects[i], ubo);
    895804               }
     805               // MARKER: Continue code review from here
    896806               if (((Asteroid*)objects[i])->hp <= 0) {
    897                   printVector("center", objects[i]->bounding_center);
    898 
    899807                  // TODO: Optimize this so I don't recalculate the camera rotation every time
    900808                  float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
     
    909817                  // initiate an explosion
    910818                  glUseProgram(explosion_sp);
    911                  
     819
    912820                  GLuint model_mat_loc = glGetUniformLocation(explosion_sp, "model_mat");
    913821                  glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objExplosion->model_mat));
    914822
    915                   // TODO: To get ubos working for the explosion,
    916                   // I need to calculate the correct ubo index for the model matrix
    917                   // Then I need to copy the matrix into the right place in the ubo and
    918                   // copy the index into the correct uniform
    919 
    920                   // STEP 1: Make sure the UBO offset for the explosion shader is correct
    921 
    922                   cout << "UBO OFFSET: " << objExplosion->ubo_offset << endl;
    923                   //glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    924                   //glBufferSubData(GL_UNIFORM_BUFFER, objExplosion->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(objExplosion->model_mat));
    925 
    926823                  glUniform1f(explosion_start_time_loc, (GLfloat)glfwGetTime());
    927 
    928                   // generic code used to change a matrix in the ubo
    929                   // we need to know the matrix of the first object in the ubo so we can save it and move it back
    930                   // after the explosion is rendered
    931                   // However, that should be done in the render loop, not here
    932                   // Here, we are already saving the explosion model mat in objExplosion->model_mat, so that is all we need
    933                   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    934                   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    935824               }
    936825            }
     
    1015904         //printVector("cam pos", cam_pos);
    1016905
    1017          glUseProgram(color_sp);
    1018          glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    1019 
    1020          glUseProgram(texture_sp);
    1021          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));
    1022908
    1023909         glUseProgram(laser_sp);
     
    1041927         case STATE_GAME:
    1042928            renderScene(shaderBufferInfo,
    1043                color_sp, asteroid_sp, texture_sp, laser_sp, explosion_sp,
    1044                color_vao, asteroid_vao, texture_vao, laser_vao, explosion_vao,
    1045                colors_vbo, selected_colors_vbo,
    1046                selectedObject);
     929               ship_sp, asteroid_sp, laser_sp, explosion_sp,
     930               ship_vao, asteroid_vao, laser_vao, explosion_vao,
     931               colors_vbo, ubo, selectedObject);
    1047932            renderSceneGui();
    1048933            break;
     
    13981283   GLuint points_vbo,
    13991284   GLuint colors_vbo,
    1400    GLuint selected_colors_vbo,
    14011285   GLuint texcoords_vbo,
    14021286   GLuint normals_vbo,
    14031287   GLuint ubo,
    14041288   GLuint model_mat_idx_vbo,
    1405    GLuint asteroid_sp,
    1406    GLuint explosion_sp) {
     1289   GLuint asteroid_sp) {
    14071290   objects.push_back(obj);
    14081291
     
    14241307         points_vbo,
    14251308         colors_vbo,
    1426          selected_colors_vbo,
    14271309         texcoords_vbo,
    14281310         normals_vbo,
    14291311         ubo,
    14301312         model_mat_idx_vbo,
    1431          asteroid_sp,
    1432          explosion_sp);
     1313         asteroid_sp);
    14331314   } else {
    14341315      copyObjectDataToBuffers(*objects.back(), shaderBufferInfo,
    14351316         points_vbo,
    14361317         colors_vbo,
    1437          selected_colors_vbo,
    14381318         texcoords_vbo,
    14391319         normals_vbo,
     
    18801760   };
    18811761   ship->texcoords = { 0.0f };
    1882    ship->selected_colors = { 0.0f };
    18831762
    18841763   mat4 T_model = translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f));
     
    19851864                  GLuint* points_vbo,
    19861865                  GLuint* colors_vbo,
    1987                   GLuint* selected_colors_vbo,
    19881866                  GLuint* texcoords_vbo,
    19891867                  GLuint* normals_vbo,
     
    19961874   glGenBuffers(1, colors_vbo);
    19971875
    1998    *selected_colors_vbo = 0;
    1999    glGenBuffers(1, selected_colors_vbo);
    2000 
    20011876   *texcoords_vbo = 0;
    20021877   glGenBuffers(1, texcoords_vbo);
     
    20161891                  GLuint points_vbo,
    20171892                  GLuint colors_vbo,
    2018                   GLuint selected_colors_vbo,
    20191893                  GLuint texcoords_vbo,
    20201894                  GLuint normals_vbo,
    20211895                  GLuint ubo,
    20221896                  GLuint model_mat_idx_vbo) {
    2023    //unsigned int num_explosions = 1;
    2024 
    20251897   float vv[EXPLOSION_PARTICLE_COUNT * 3]; // initial velocities vec3
    20261898   float vt[EXPLOSION_PARTICLE_COUNT]; // initial times
     
    20521924   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat));
    20531925
    2054    /*
    2055    GLuint model_ubo = 0;
    2056    glGenBuffers(1, &model_ubo);
    2057 
    2058    glBindBuffer(GL_UNIFORM_BUFFER, model_ubo);
    2059    glBufferData(GL_UNIFORM_BUFFER, num_explosions * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
    2060 
    2061    //glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    2062    glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(model_mat));
    2063    */
    2064 
    20651926   GLuint velocity_vbo;
    20661927   glGenBuffers(1, &velocity_vbo);
     
    20791940   glEnableVertexAttribArray(0);
    20801941   glEnableVertexAttribArray(1);
    2081    glEnableVertexAttribArray(2);
    20821942
    20831943   glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
     
    20871947   glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
    20881948
    2089    // TODO: I think I can call glVertexAttribIPointer whenever the vbo_base changes
    2090    // to rebind the ubo index vbo
    2091    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    2092    glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL);
    2093 
    20941949   objExplosion = createExplosion(explosion_sp);
    2095    objExplosion->num_points = EXPLOSION_PARTICLE_COUNT;
    20961950   addObjectToScene(objExplosion, shaderBufferInfo,
    20971951      points_vbo,
    20981952      colors_vbo,
    2099       selected_colors_vbo,
    21001953      texcoords_vbo,
    21011954      normals_vbo,
    21021955      ubo,
    21031956      model_mat_idx_vbo,
    2104       0,
    2105       explosion_sp);
     1957      0);
    21061958
    21071959   return vao;
     
    21121964                  GLuint points_vbo,
    21131965                  GLuint colors_vbo,
    2114                   GLuint selected_colors_vbo,
    21151966                  GLuint texcoords_vbo,
    21161967                  GLuint normals_vbo,
    21171968                  GLuint ubo,
    21181969                  GLuint ubo_idx_vbo,
    2119                   GLuint asteroid_sp,
    2120                   GLuint explosion_sp) {
     1970                  GLuint asteroid_sp) {
    21211971   GLsizeiptr num_points = 0;
    21221972   GLsizeiptr num_objects = 0;
     
    21762026      shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2;
    21772027
    2178       if (shaderCountIt->first == explosion_sp) {
    2179          cout << "EXPLOSION VBO_BASE: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl;
    2180       }
    2181 
    2182       /*
    2183       cout << "shader: " << shaderCountIt->first << endl;
    2184       cout << "point counts: " << shaderCounts[shaderCountIt->first] << endl;
    2185       cout << "object counts: " << shaderUboCounts[shaderCountIt->first] << endl;
    2186       cout << "vbo_base: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl;
    2187       cout << "ubo_base: " << shaderBufferInfo[shaderCountIt->first].ubo_base << endl;
    2188       */
    2189 
    21902028      shaderBufferInfo[shaderCountIt->first].vbo_offset = 0;
    21912029      shaderBufferInfo[shaderCountIt->first].ubo_offset = 0;
     
    22042042
    22052043   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2206    glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    2207 
    2208    glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    22092044   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    22102045
     
    22252060         points_vbo,
    22262061         colors_vbo,
    2227          selected_colors_vbo,
    22282062         texcoords_vbo,
    22292063         normals_vbo,
     
    22382072                  GLuint points_vbo,
    22392073                  GLuint colors_vbo,
    2240                   GLuint selected_colors_vbo,
    22412074                  GLuint texcoords_vbo,
    22422075                  GLuint normals_vbo,
     
    22492082   obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
    22502083
    2251    if (obj.type == TYPE_EXPLOSION) {
    2252       cout << "UBO OFFSET: " << obj.ubo_offset << endl;
    2253    }
    2254    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    2255    for (unsigned int i = 0; i < obj.num_points; i++) {
    2256       glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
     2084   if (obj.ubo_offset == 0) {
     2085      objFirst = &obj;
    22572086   }
    22582087
    22592088   if (obj.type != TYPE_EXPLOSION) {
     2089      glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     2090      for (unsigned int i = 0; i < obj.num_points; i++) {
     2091         glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
     2092      }
     2093
    22602094      glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    22612095      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
     
    22672101         glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    22682102         glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
    2269 
    2270          glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2271          glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
    22722103
    22732104         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     
    24532284
    24542285void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    2455                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
    2456                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    2457                   GLuint colors_vbo, GLuint selected_colors_vbo,
    2458                   SceneObject* selectedObject) {
    2459 
    2460    glUseProgram(color_sp);
    2461    glBindVertexArray(color_vao);
    2462 
    2463    /*
    2464    if (selectedObject != NULL) {
    2465       glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2466       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    2467 
    2468       glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points);
    2469    }
    2470    */
    2471 
    2472    // Uncomment this code when I want to use selected colors again
    2473    // glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2474    // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    2475 
    2476    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);
    24772294
    24782295   glUseProgram(asteroid_sp);
     
    24812298   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[asteroid_sp].vbo_base, shaderBufferInfo[asteroid_sp].vbo_offset);
    24822299
    2483    glUseProgram(texture_sp);
    2484    glBindVertexArray(texture_vao);
    2485 
    2486    glDrawArrays(GL_TRIANGLES, shaderBufferInfo[texture_sp].vbo_base, shaderBufferInfo[texture_sp].vbo_offset);
    2487 
    24882300   glEnable(GL_BLEND);
    24892301
     
    24932305   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
    24942306
     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.
     2309
    24952310   glUseProgram(explosion_sp);
    2496 
    2497    //glActiveTexture(GL_TEXTURE1);
    2498    //glBindTexture(GL_TEXTURE_2D, tex); // tex is 0 here
    24992311
    25002312   glEnable(GL_PROGRAM_POINT_SIZE);
    25012313   glBindVertexArray(explosion_vao);
    25022314
     2315   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     2316   glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(objExplosion->model_mat));
     2317
    25032318   glDrawArrays(GL_POINTS, 0, shaderBufferInfo[explosion_sp].vbo_offset);
    2504    //glDrawArrays(GL_POINTS, shaderBufferInfo[explosion_sp].vbo_base, shaderBufferInfo[explosion_sp].vbo_offset);
    2505    //cout << shaderBufferInfo[explosion_sp].vbo_base << ", " << shaderBufferInfo[explosion_sp].vbo_offset << endl;
     2319
     2320   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     2321   glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mat4), value_ptr(objFirst->model_mat));
    25062322
    25072323   glDisable(GL_PROGRAM_POINT_SIZE);
     
    27102526   };
    27112527   obj->texcoords = { 0.0f };
    2712    obj->selected_colors = { 0.0f };
    27132528
    27142529   mat4 T = translate(mat4(1.0f), pos);
     
    27382553
    27392554   initObject(obj);
     2555   obj->num_points = EXPLOSION_PARTICLE_COUNT;
    27402556
    27412557   return obj;
Note: See TracChangeset for help on using the changeset viewer.