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


Ignore:
Timestamp:
Jan 18, 2019, 2:35:11 PM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
bebfd5c
Parents:
fe5e3ca
Message:

WIP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rfe5e3ca rc5fb958  
    5050   TYPE_ASTEROID,
    5151   TYPE_LASER,
     52   TYPE_EXPLOSION,
    5253};
    5354
     
    143144                  GLuint ubo,
    144145                  GLuint model_mat_idx_vbo,
    145                   GLuint asteroid_sp);
     146                  GLuint asteroid_sp,
     147                  GLuint explosion_sp);
    146148void removeObjectFromScene(SceneObject& obj, GLuint ubo);
    147149
     
    157159                  GLuint* model_mat_idx_vbo);
    158160
    159 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp);
     161GLuint 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);
    160170
    161171void populateBuffers(vector<SceneObject*>& objects,
     
    168178                  GLuint ubo,
    169179                  GLuint model_mat_idx_vbo,
    170                   GLuint asteroid_sp);
     180                  GLuint asteroid_sp,
     181                  GLuint explosion_sp);
    171182
    172183void copyObjectDataToBuffers(SceneObject& obj,
     
    183194void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
    184195
     196// instead of using these methods, create constructors for these
    185197SceneObject* createShip(GLuint shader);
    186198Asteroid* createAsteroid(vec3 pos, GLuint shader);
    187199Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp);
     200SceneObject* createExplosion(GLuint shader);
    188201
    189202void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
     
    199212                  GLuint colors_vbo, GLuint selected_colors_vbo,
    200213                  SceneObject* selectedObject);
    201 void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex);
    202214
    203215void renderSceneGui();
     
    248260Laser* rightLaser = NULL;
    249261EffectOverTime* rightLaserEffect = NULL;
     262
     263SceneObject* objExplosion;
    250264
    251265/*
     
    450464   shaderBufferInfo[texture_sp] = BufferInfo();
    451465   shaderBufferInfo[laser_sp] = BufferInfo();
     466   shaderBufferInfo[explosion_sp] = BufferInfo();
    452467
    453468   cam_pos = vec3(0.0f, 0.0f, 2.0f);
     
    482497      ubo,
    483498      model_mat_idx_vbo,
    484       asteroid_sp);
     499      asteroid_sp,
     500      explosion_sp);
    485501
    486502   GLuint color_vao = 0;
     
    494510
    495511   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    496    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     512   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    497513
    498514   // Comment these two lines out when I want to use selected colors
    499515   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    500    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     516   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    501517
    502518   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    503    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
     519   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    504520
    505521   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    506    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
     522   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    507523
    508524   GLuint asteroid_vao = 0;
     
    516532
    517533   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    518    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     534   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    519535
    520536   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    521    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     537   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    522538
    523539   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    524    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
     540   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    525541
    526542   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    527    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
     543   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    528544
    529545   GLuint texture_vao = 0;
     
    537553
    538554   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    539    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     555   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    540556
    541557   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    542    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
     558   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    543559
    544560   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    545    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
     561   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    546562
    547563   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    548    glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
     564   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL);
    549565
    550566   GLuint laser_vao = 0;
     
    557573
    558574   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    559    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     575   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    560576
    561577   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    562    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
     578   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    563579
    564580   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    565    glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, 0);
     581   glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL);
    566582
    567583   float cam_speed = 1.0f;
     
    597613   proj_mat = make_mat4(proj_arr);
    598614
    599    GLuint explosion_vao = initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat, explosion_sp);
     615   GLuint explosion_vao = initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat,
     616      explosion_sp,
     617      shaderBufferInfo,
     618      points_vbo,
     619      colors_vbo,
     620      selected_colors_vbo,
     621      texcoords_vbo,
     622      normals_vbo,
     623      ubo,
     624      model_mat_idx_vbo);
     625
     626   /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos):
     627
     628      No, you're misunderstanding how this works. UBO binding works exactly like texture object binding.
     629
     630      The OpenGL context has a number of slots for binding UBOs. There are GL_MAX_UNIFORM_BUFFER_BINDINGS number of
     631      slots for UBO binding.
     632
     633      Uniform Blocks in a program can be set to use one of the slots in the context. You do this by first querying
     634      the block index using the block name (glGetUniformBlockIndex). This is similar to how you need to use
     635      glGetUniformLocation in order to set a uniform's value with glUniform. Block indices, like uniform locations,
     636      are specific to a program.
     637
     638      Once you have the block index, you use glUniformBlockBinding to set that specific program to use a particular
     639      uniform buffer slot in the context.
     640
     641      Let's say you have a global UBO that you want to use for every program. To make using it easier, you want to
     642      bind it just once.
     643
     644      So first, you pick a uniform buffer slot in the context, one that always will refer to this UBO. Let's say
     645      you pick slot 8.
     646
     647      When you build a program object that may use this global uniform buffer, what you do is quite simple. First,
     648      after linking the program, call glGetUniformBlockIndex(program, "NameOfGlobalUniformBlock"). If you get back
     649      GL_INVALID_INDEX, then you know that the global uniform block isn't used in that program. Otherwise you get
     650      back a block index.
     651
     652      If you got a valid block index, then you call glUniformBlockBinding(program, uniformBlockIndex, 8). Remember
     653      that 8 is the uniform buffer context slot that we selected earlier. This causes this particular program to
     654      use uniform buffer slot #8 to find the buffer for "NameOfGlobalUniformBlock".
     655
     656      Finally, to set the actual buffer in the context, call glBindBufferRange(GL_UNIFORM_BUFFER, 8,
     657      bufferObjectName, offset, size);
     658   */
    600659
    601660   GLuint ub_binding_point = 0;
     
    621680   GLuint explosion_start_time_loc = glGetUniformLocation(explosion_sp, "explosion_start_time");
    622681   GLuint cur_time_loc = glGetUniformLocation(explosion_sp, "cur_time");
     682   //GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(explosion_sp, "models");
    623683
    624684
     
    654714   glUniformBlockBinding(laser_sp, laser_sp_models_ub_index, ub_binding_point);
    655715   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     716
     717
     718   /*
     719   glUseProgram(explosion_sp);
     720   glUniformBlockBinding(explosion_sp, explosion_sp_models_ub_index, ub_binding_point);
     721   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     722   */
    656723
    657724
     
    732799               ubo,
    733800               model_mat_idx_vbo,
    734                asteroid_sp);
     801               asteroid_sp,
     802               explosion_sp);
    735803
    736804            elapsed_seconds_spawn -= 0.5f;
     
    792860               ubo,
    793861               model_mat_idx_vbo,
    794                asteroid_sp);
     862               asteroid_sp,
     863               explosion_sp);
    795864         } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
    796865            removeObjectFromScene(*leftLaser, ubo);
     
    810879               ubo,
    811880               model_mat_idx_vbo,
    812                asteroid_sp);
     881               asteroid_sp,
     882               explosion_sp);
    813883         } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
    814884            removeObjectFromScene(*rightLaser, ubo);
     
    826896               }
    827897               if (((Asteroid*)objects[i])->hp <= 0) {
     898                  printVector("center", objects[i]->bounding_center);
     899
     900                  // TODO: Optimize this so I don't recalculate the camera rotation every time
     901                  float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
     902                  mat4 pitch_mat = rotate(mat4(1.0f), cam_pitch, vec3(1.0f, 0.0f, 0.0f));
     903                  mat4 model_mat = translate(mat4(1.0f), objects[i]->bounding_center + vec3(0.0f, 0.0f, 0.0f)) * pitch_mat;
     904
    828905                  removeObjectFromScene(*objects[i], ubo);
    829906                  score++;
    830907
    831                   // render an explosion
     908                  objExplosion->model_mat = model_mat;
     909
     910                  // initiate an explosion
    832911                  glUseProgram(explosion_sp);
     912                 
     913                  GLuint model_mat_loc = glGetUniformLocation(explosion_sp, "model_mat");
     914                  glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objExplosion->model_mat));
     915
     916                  // TODO: To get ubos working for the explosion,
     917                  // I need to calculate the correct ubo index for the model matrix
     918                  // Then I need to copy the matrix into the right place in the ubo and
     919                  // copy the index into the correct uniform
     920
     921                  // STEP 1: Make sure the UBO offset for the explosion shader is correct
     922
     923                  cout << "UBO OFFSET: " << objExplosion->ubo_offset << endl;
     924                  //glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     925                  //glBufferSubData(GL_UNIFORM_BUFFER, objExplosion->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(objExplosion->model_mat));
     926
    833927                  glUniform1f(explosion_start_time_loc, (GLfloat)glfwGetTime());
    834                   cout << "REMOVED" << endl;
    835                   cout << i << endl;
    836928               }
    837929            }
     
    12881380   }
    12891381
    1290    if (obj->type != TYPE_LASER) {
     1382   if (obj->type == TYPE_SHIP || obj->type == TYPE_ASTEROID) {
    12911383      calculateObjectBoundingBox(obj);
    12921384
     
    13041396   GLuint ubo,
    13051397   GLuint model_mat_idx_vbo,
    1306    GLuint asteroid_sp) {
     1398   GLuint asteroid_sp,
     1399   GLuint explosion_sp) {
    13071400   objects.push_back(obj);
    13081401
     
    13111404   // Check if the buffers aren't large enough to fit the new object and, if so, call
    13121405   // populateBuffers() to resize and repopupulate them
    1313    if (bufferInfo->vbo_capacity < (bufferInfo->ubo_offset + obj->num_points) ||
     1406   if (bufferInfo->vbo_capacity < (bufferInfo->vbo_offset + obj->num_points) ||
    13141407      bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) {
    13151408
     
    13291422         ubo,
    13301423         model_mat_idx_vbo,
    1331          asteroid_sp);
     1424         asteroid_sp,
     1425         explosion_sp);
    13321426   } else {
    13331427      copyObjectDataToBuffers(*objects.back(), shaderBufferInfo,
     
    18051899*/
    18061900// TODO: Make the color parameter have an effect
    1807 // TODO: Come up with a better way of passing the object back than copying it
    18081901Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) {
    18091902   Laser* obj = new Laser();
     
    19122005}
    19132006
    1914 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp) {
     2007GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp,
     2008                  map<GLuint, BufferInfo>& shaderBufferInfo,
     2009                  GLuint points_vbo,
     2010                  GLuint colors_vbo,
     2011                  GLuint selected_colors_vbo,
     2012                  GLuint texcoords_vbo,
     2013                  GLuint normals_vbo,
     2014                  GLuint ubo,
     2015                  GLuint model_mat_idx_vbo) {
    19152016   //unsigned int num_explosions = 1;
    19162017
     
    19692070   glBindVertexArray(vao);
    19702071
     2072   glEnableVertexAttribArray(0);
     2073   glEnableVertexAttribArray(1);
     2074   glEnableVertexAttribArray(2);
     2075
    19712076   glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
    19722077   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     
    19752080   glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
    19762081
    1977    glEnableVertexAttribArray(0);
    1978    glEnableVertexAttribArray(1);
     2082   // TODO: I think I can call glVertexAttribIPointer whenever the vbo_base changes
     2083   // to rebind the ubo index vbo
     2084   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     2085   glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL);
     2086
     2087   objExplosion = createExplosion(explosion_sp);
     2088   objExplosion->num_points = EXPLOSION_PARTICLE_COUNT;
     2089   addObjectToScene(objExplosion, shaderBufferInfo,
     2090      points_vbo,
     2091      colors_vbo,
     2092      selected_colors_vbo,
     2093      texcoords_vbo,
     2094      normals_vbo,
     2095      ubo,
     2096      model_mat_idx_vbo,
     2097      0,
     2098      explosion_sp);
    19792099
    19802100   return vao;
     
    19902110                  GLuint ubo,
    19912111                  GLuint ubo_idx_vbo,
    1992                   GLuint asteroid_sp) {
     2112                  GLuint asteroid_sp,
     2113                  GLuint explosion_sp) {
    19932114   GLsizeiptr num_points = 0;
    19942115   GLsizeiptr num_objects = 0;
     
    19962117   map<GLuint, unsigned int> shaderCounts;
    19972118   map<GLuint, unsigned int> shaderUboCounts;
     2119
     2120   map<GLuint, BufferInfo>::iterator shaderIt;
     2121
     2122   for (shaderIt = shaderBufferInfo.begin(); shaderIt != shaderBufferInfo.end(); shaderIt++) {
     2123      shaderCounts[shaderIt->first] = 0;
     2124      shaderUboCounts[shaderIt->first] = 0;
     2125   }
    19982126
    19992127   vector<SceneObject*>::iterator it;
     
    20132141         num_objects++;
    20142142
    2015          if (shaderCounts.count((*it)->shader_program) == 0) {
    2016             shaderCounts[(*it)->shader_program] = (*it)->num_points;
    2017             shaderUboCounts[(*it)->shader_program] = 1;
    2018          } else {
    2019             shaderCounts[(*it)->shader_program] += (*it)->num_points;
    2020             shaderUboCounts[(*it)->shader_program]++;
    2021          }
     2143         shaderCounts[(*it)->shader_program] += (*it)->num_points;
     2144         shaderUboCounts[(*it)->shader_program]++;
    20222145
    20232146         it++;
     
    20292152   num_objects *= 2;
    20302153
    2031    map<GLuint, unsigned int>::iterator shaderIt;
     2154   map<GLuint, unsigned int>::iterator shaderCountIt;
    20322155   unsigned int lastShaderCount = 0;
    20332156   unsigned int lastShaderUboCount = 0;
     
    20402163   * object being added.
    20412164   */
    2042    for (shaderIt = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) {
     2165   for (shaderCountIt = shaderCounts.begin(); shaderCountIt != shaderCounts.end(); shaderCountIt++) {
    20432166      // When populating the buffers, leave as much empty space as space taken up by existing objects
    20442167      // to allow new objects to be added without immediately having to resize the buffers
    2045       shaderBufferInfo[shaderIt->first].vbo_base = lastShaderCount * 2;
    2046       shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2;
     2168      shaderBufferInfo[shaderCountIt->first].vbo_base = lastShaderCount * 2;
     2169      shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2;
     2170
     2171      if (shaderCountIt->first == explosion_sp) {
     2172         cout << "EXPLOSION VBO_BASE: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl;
     2173      }
    20472174
    20482175      /*
    2049       cout << "shader: " << shaderIt->first << endl;
    2050       cout << "point counts: " << shaderCounts[shaderIt->first] << endl;
    2051       cout << "object counts: " << shaderUboCounts[shaderIt->first] << endl;
    2052       cout << "vbo_base: " << shaderBufferInfo[shaderIt->first].vbo_base << endl;
    2053       cout << "ubo_base: " << shaderBufferInfo[shaderIt->first].ubo_base << endl;
     2176      cout << "shader: " << shaderCountIt->first << endl;
     2177      cout << "point counts: " << shaderCounts[shaderCountIt->first] << endl;
     2178      cout << "object counts: " << shaderUboCounts[shaderCountIt->first] << endl;
     2179      cout << "vbo_base: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl;
     2180      cout << "ubo_base: " << shaderBufferInfo[shaderCountIt->first].ubo_base << endl;
    20542181      */
    20552182
    2056       shaderBufferInfo[shaderIt->first].vbo_offset = 0;
    2057       shaderBufferInfo[shaderIt->first].ubo_offset = 0;
    2058 
    2059       shaderBufferInfo[shaderIt->first].vbo_capacity = shaderCounts[shaderIt->first] * 2;
    2060       shaderBufferInfo[shaderIt->first].ubo_capacity = shaderUboCounts[shaderIt->first] * 2;
    2061 
    2062       lastShaderCount += shaderCounts[shaderIt->first];
    2063       lastShaderUboCount += shaderUboCounts[shaderIt->first];
     2183      shaderBufferInfo[shaderCountIt->first].vbo_offset = 0;
     2184      shaderBufferInfo[shaderCountIt->first].ubo_offset = 0;
     2185
     2186      shaderBufferInfo[shaderCountIt->first].vbo_capacity = shaderCounts[shaderCountIt->first] * 2;
     2187      shaderBufferInfo[shaderCountIt->first].ubo_capacity = shaderUboCounts[shaderCountIt->first] * 2;
     2188
     2189      lastShaderCount += shaderCounts[shaderCountIt->first];
     2190      lastShaderUboCount += shaderUboCounts[shaderCountIt->first];
    20642191   }
    20652192
     
    21152242   obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset;
    21162243
    2117    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    2118    glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
    2119 
    2120    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    2121    glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
    2122 
     2244   if (obj.type == TYPE_EXPLOSION) {
     2245      cout << "UBO OFFSET: " << obj.ubo_offset << endl;
     2246   }
    21232247   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    21242248   for (unsigned int i = 0; i < obj.num_points; i++) {
     
    21262250   }
    21272251
    2128    if (obj.type != TYPE_LASER) {
    2129       glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2130       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
    2131 
    2132       glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2133       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
    2134 
    2135       glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    2136       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
    2137    }
    2138 
    2139    obj.model_mat = obj.model_transform * obj.model_base;
    2140    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    2141    glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    2142 
    2143    if (obj.type == TYPE_ASTEROID) {
    2144       glUseProgram(asteroid_sp);
    2145 
    2146       ostringstream oss;
    2147       oss << "hp[" << obj.ubo_offset << "]";
    2148       glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), ((Asteroid*)&obj)->hp);
     2252   if (obj.type != TYPE_EXPLOSION) {
     2253      glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     2254      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
     2255
     2256      glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     2257      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
     2258
     2259      if (obj.type != TYPE_LASER) {
     2260         glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     2261         glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
     2262
     2263         glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
     2264         glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
     2265
     2266         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     2267         glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
     2268      }
     2269
     2270      obj.model_mat = obj.model_transform * obj.model_base;
     2271      glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     2272      glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
     2273
     2274      if (obj.type == TYPE_ASTEROID) {
     2275         glUseProgram(asteroid_sp);
     2276
     2277         ostringstream oss;
     2278         oss << "hp[" << obj.ubo_offset << "]";
     2279         glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), ((Asteroid*)&obj)->hp);
     2280      }
    21492281   }
    21502282
     
    23252457   if (selectedObject != NULL) {
    23262458      glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    2327       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     2459      glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    23282460
    23292461      glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points);
     
    23332465   // Uncomment this code when I want to use selected colors again
    23342466   // glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    2335    // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     2467   // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    23362468
    23372469   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset);
     
    23542486   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
    23552487
    2356    glDisable(GL_BLEND);
    2357 
    2358    renderExplosion(explosion_sp, explosion_vao, 0);
    2359 }
    2360 
    2361 void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex) {
    2362    glEnable(GL_BLEND);
     2488   glUseProgram(explosion_sp);
     2489
     2490   //glActiveTexture(GL_TEXTURE1);
     2491   //glBindTexture(GL_TEXTURE_2D, tex); // tex is 0 here
     2492
    23632493   glEnable(GL_PROGRAM_POINT_SIZE);
    2364 
    2365    //glActiveTexture(GL_TEXTURE1);
    2366    //glBindTexture(GL_TEXTURE_2D, tex);
    2367    glUseProgram(explosion_sp);
    2368 
    23692494   glBindVertexArray(explosion_vao);
    23702495
    2371    glDrawArrays(GL_POINTS, 0, EXPLOSION_PARTICLE_COUNT);
     2496   glDrawArrays(GL_POINTS, 0, shaderBufferInfo[explosion_sp].vbo_offset);
     2497   //glDrawArrays(GL_POINTS, shaderBufferInfo[explosion_sp].vbo_base, shaderBufferInfo[explosion_sp].vbo_offset);
     2498   //cout << shaderBufferInfo[explosion_sp].vbo_base << ", " << shaderBufferInfo[explosion_sp].vbo_offset << endl;
    23722499
    23732500   glDisable(GL_PROGRAM_POINT_SIZE);
     
    24742601   Asteroid* obj = new Asteroid();
    24752602   obj->type = TYPE_ASTEROID;
     2603   obj->shader_program = shader;
    24762604   obj->hp = 10.0f;
    2477    obj->shader_program = shader;
    24782605
    24792606   obj->points = {
     
    25952722}
    25962723
     2724SceneObject* createExplosion(GLuint shader) {
     2725   SceneObject* obj = new SceneObject();
     2726   obj->type = TYPE_EXPLOSION;
     2727   obj->shader_program = shader;
     2728
     2729   obj->points = {};
     2730   obj->colors = {};
     2731
     2732   initObject(obj);
     2733
     2734   return obj;
     2735}
     2736
    25972737float getRandomNum(float low, float high) {
    25982738   return low + ((float)rand()/RAND_MAX) * (high-low);
Note: See TracChangeset for help on using the changeset viewer.