Changeset 25b47d7 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Oct 17, 2018, 1:47:23 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
1e3dddf, 8e8aed6
Parents:
0e0f851
Message:

Correctly send the hp of each asteroid to the shader using a uniform array and allow the player to damage and destroy the asteroids with lasers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r0e0f851 r25b47d7  
    142142                  GLuint normals_vbo,
    143143                  GLuint ubo,
    144                   GLuint asteroid_hp_ubo,
    145                   GLuint model_mat_idx_vbo);
     144                  GLuint model_mat_idx_vbo,
     145                  GLuint asteroid_sp);
    146146void removeObjectFromScene(SceneObject& obj, GLuint ubo);
    147147
     
    155155                  GLuint* normals_vbo,
    156156                  GLuint* ubo,
    157                   GLuint* asteroid_hp_ubo,
    158157                  GLuint* model_mat_idx_vbo);
    159158
     
    166165                  GLuint normals_vbo,
    167166                  GLuint ubo,
    168                   GLuint asteroid_hp_ubo,
    169                   GLuint model_mat_idx_vbo);
     167                  GLuint model_mat_idx_vbo,
     168                  GLuint asteroid_sp);
    170169
    171170void copyObjectDataToBuffers(SceneObject& obj,
     
    177176                  GLuint normals_vbo,
    178177                  GLuint ubo,
    179                   GLuint asteroid_hp_ubo,
    180                   GLuint model_mat_idx_vbo);
     178                  GLuint model_mat_idx_vbo,
     179                  GLuint asteroid_sp);
    181180
    182181void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
     
    187186
    188187void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
    189 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp, GLuint asteroid_hp_loc);
     188void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp);
    190189bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection);
    191190
     
    452451
    453452   GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
    454       normals_vbo, ubo, asteroid_hp_ubo, model_mat_idx_vbo;
    455 
    456    cout << "Initializing buffers" << endl;
     453      normals_vbo, ubo, model_mat_idx_vbo;
     454
    457455   initializeBuffers(
    458456      &points_vbo,
     
    462460      &normals_vbo,
    463461      &ubo,
    464       &asteroid_hp_ubo,
    465462      &model_mat_idx_vbo);
    466463
    467    cout << "Populating buffers" << endl;
    468464   populateBuffers(objects,
    469465      shaderBufferInfo,
     
    474470      normals_vbo,
    475471      ubo,
    476       asteroid_hp_ubo,
    477       model_mat_idx_vbo);
    478 
    479    cout << "Done" << endl;
     472      model_mat_idx_vbo,
     473      asteroid_sp);
    480474
    481475   GLuint color_vao = 0;
     
    601595   GLuint asteroid_view_mat_loc = glGetUniformLocation(asteroid_sp, "view");
    602596   GLuint asteroid_proj_mat_loc = glGetUniformLocation(asteroid_sp, "proj");
    603    GLuint asteroid_hp_loc = glGetUniformLocation(asteroid_sp, "hp");
    604597   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(asteroid_sp, "models");
    605598   //GLuint asteroid_sp_hp_ub_index = glGetUniformBlockIndex(asteroid_sp, "hp_uniform");
     
    623616
    624617
    625    GLfloat tempHp = 10.0f;
    626    mat4 hp(tempHp);
    627 
    628618   glUseProgram(asteroid_sp);
    629619   glUniformMatrix4fv(asteroid_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    630620   glUniformMatrix4fv(asteroid_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    631    glUniformMatrix4fv(asteroid_hp_loc, 1, GL_FALSE, value_ptr(hp));
    632621
    633622   glUniformBlockBinding(asteroid_sp, asteroid_sp_models_ub_index, ub_binding_point);
    634623   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    635624
    636    /*
    637    glUniformBlockBinding(asteroid_sp, asteroid_sp_hp_ub_index, ub_binding_point);
    638    glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, asteroid_hp_ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    639    */
    640625
    641626   glUseProgram(texture_sp);
     
    725710               normals_vbo,
    726711               ubo,
    727                asteroid_hp_ubo,
    728                model_mat_idx_vbo);
     712               model_mat_idx_vbo,
     713               asteroid_sp);
    729714
    730715            elapsed_seconds_spawn -= 0.5f;
     
    785770               normals_vbo,
    786771               ubo,
    787                asteroid_hp_ubo,
    788                model_mat_idx_vbo);
     772               model_mat_idx_vbo,
     773               asteroid_sp);
    789774         } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
    790775            removeObjectFromScene(*leftLaser, ubo);
     
    803788               normals_vbo,
    804789               ubo,
    805                asteroid_hp_ubo,
    806                model_mat_idx_vbo);
     790               model_mat_idx_vbo,
     791               asteroid_sp);
    807792         } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
    808793            removeObjectFromScene(*rightLaser, ubo);
     
    826811
    827812         if (leftLaser != NULL && !leftLaser->deleted) {
    828             updateLaserTarget(leftLaser, objects, points_vbo, asteroid_sp, asteroid_hp_loc);
     813            updateLaserTarget(leftLaser, objects, points_vbo, asteroid_sp);
    829814         }
    830815         if (rightLaser != NULL && !rightLaser->deleted) {
    831             updateLaserTarget(rightLaser, objects, points_vbo, asteroid_sp, asteroid_hp_loc);
     816            updateLaserTarget(rightLaser, objects, points_vbo, asteroid_sp);
    832817         }
    833818      }
     
    12871272   GLuint normals_vbo,
    12881273   GLuint ubo,
    1289    GLuint asteroid_hp_ubo,
    1290    GLuint model_mat_idx_vbo) {
     1274   GLuint model_mat_idx_vbo,
     1275   GLuint asteroid_sp) {
    12911276   objects.push_back(obj);
    12921277
     
    13121297         normals_vbo,
    13131298         ubo,
    1314          asteroid_hp_ubo,
    1315          model_mat_idx_vbo);
     1299         model_mat_idx_vbo,
     1300         asteroid_sp);
    13161301   } else {
    13171302      copyObjectDataToBuffers(*objects.back(), shaderBufferInfo,
     
    13221307         normals_vbo,
    13231308         ubo,
    1324          asteroid_hp_ubo,
    1325          model_mat_idx_vbo);
     1309         model_mat_idx_vbo,
     1310         asteroid_sp);
    13261311   }
    13271312}
     
    18731858                  GLuint* normals_vbo,
    18741859                  GLuint* ubo,
    1875                   GLuint* asteroid_hp_ubo,
    18761860                  GLuint* model_mat_idx_vbo) {
    18771861   *points_vbo = 0;
     
    18921876   *ubo = 0;
    18931877   glGenBuffers(1, ubo);
    1894 
    1895    *asteroid_hp_ubo = 0;
    1896    glGenBuffers(1, asteroid_hp_ubo);
    18971878
    18981879   *model_mat_idx_vbo = 0;
     
    19081889                  GLuint normals_vbo,
    19091890                  GLuint ubo,
    1910                   GLuint asteroid_hp_ubo,
    1911                   GLuint ubo_idx_vbo) {
     1891                  GLuint ubo_idx_vbo,
     1892                  GLuint asteroid_sp) {
    19121893   GLsizeiptr num_points = 0;
    19131894   GLsizeiptr num_objects = 0;
     
    20031984   glBufferData(GL_UNIFORM_BUFFER, num_objects * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
    20041985
    2005    /*
    2006    glBindBuffer(GL_UNIFORM_BUFFER, asteroid_hp_ubo);
    2007    glBufferData(GL_UNIFORM_BUFFER, num_objects * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
    2008    */
    2009 
    20101986   glBindBuffer(GL_ARRAY_BUFFER, ubo_idx_vbo);
    20111987   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLuint), NULL, GL_DYNAMIC_DRAW);
     
    20191995         normals_vbo,
    20201996         ubo,
    2021          asteroid_hp_ubo,
    2022          ubo_idx_vbo);
     1997         ubo_idx_vbo,
     1998         asteroid_sp);
    20231999   }
    20242000}
     
    20322008                  GLuint normals_vbo,
    20332009                  GLuint ubo,
    2034                   GLuint asteroid_hp_ubo,
    2035                   GLuint model_mat_idx_vbo) {
     2010                  GLuint model_mat_idx_vbo,
     2011                  GLuint asteroid_sp) {
    20362012   BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
    20372013
     
    20652041   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    20662042
    2067    /*
    2068    //if (obj.type == TYPE_ASTEROID) {
    2069       glBindBuffer(GL_UNIFORM_BUFFER, asteroid_hp_ubo);
    2070       GLfloat tempHp = 0.1f; // this value has no impact on the actual color, wtf
    2071       mat4 tempMat(tempHp);
    2072       glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(tempMat));
    2073    //}
    2074    */
     2043   if (obj.type == TYPE_ASTEROID) {
     2044      glUseProgram(asteroid_sp);
     2045
     2046      ostringstream oss;
     2047      oss << "hp[" << obj.ubo_offset << "]";
     2048      glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), ((Asteroid*)&obj)->hp);
     2049   }
    20752050
    20762051   bufferInfo->vbo_offset += obj.num_points;
     
    21212096}
    21222097
    2123 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp, GLuint asteroid_hp_loc) {
     2098void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp) {
    21242099   // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created,
    21252100   // and then re-used here
     
    21562131
    21572132      if (closestAsteroid != NULL) {
    2158          eot = new EffectOverTime(closestAsteroid->hp, -10.0f, closestAsteroid);
     2133         eot = new EffectOverTime(closestAsteroid->hp, -20.0f, closestAsteroid);
    21592134         effects.push_back(eot);
    21602135      }
     
    21722147      length = glm::length(closestIntersection - start);
    21732148
    2174       // TODO: Find a better way of doing this
    2175 
    2176       GLfloat tempHp = closestAsteroid->hp;
    2177       mat4 hp(tempHp);
     2149      // TODO: Find a more generic way of updating the laser hp than in updateLaserTarget
    21782150
    21792151      glUseProgram(asteroid_sp);
    2180       glUniformMatrix4fv(asteroid_hp_loc, 1, GL_FALSE, value_ptr(hp));
     2152
     2153      ostringstream oss;
     2154      oss << "hp[" << closestAsteroid->ubo_offset << "]";
     2155      glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), closestAsteroid->hp);
    21812156   }
    21822157
     
    24362411   obj->colors = {
    24372412      // front
    2438       0.8f, 0.0f, 0.0f,
    2439       0.8f, 0.0f, 0.0f,
    2440       0.8f, 0.0f, 0.0f,
    2441       0.8f, 0.0f, 0.0f,
    2442       0.8f, 0.0f, 0.0f,
    2443       0.8f, 0.0f, 0.0f,
     2413      0.4f, 0.4f, 0.4f,
     2414      0.4f, 0.4f, 0.4f,
     2415      0.4f, 0.4f, 0.4f,
     2416      0.4f, 0.4f, 0.4f,
     2417      0.4f, 0.4f, 0.4f,
     2418      0.4f, 0.4f, 0.4f,
    24442419
    24452420      // top
    2446       0.8f, 0.0f, 0.0f,
    2447       0.8f, 0.0f, 0.0f,
    2448       0.8f, 0.0f, 0.0f,
    2449       0.8f, 0.0f, 0.0f,
    2450       0.8f, 0.0f, 0.0f,
    2451       0.8f, 0.0f, 0.0f,
     2421      0.4f, 0.4f, 0.4f,
     2422      0.4f, 0.4f, 0.4f,
     2423      0.4f, 0.4f, 0.4f,
     2424      0.4f, 0.4f, 0.4f,
     2425      0.4f, 0.4f, 0.4f,
     2426      0.4f, 0.4f, 0.4f,
    24522427
    24532428      // bottom
    2454       0.8f, 0.0f, 0.0f,
    2455       0.8f, 0.0f, 0.0f,
    2456       0.8f, 0.0f, 0.0f,
    2457       0.8f, 0.0f, 0.0f,
    2458       0.8f, 0.0f, 0.0f,
    2459       0.8f, 0.0f, 0.0f,
     2429      0.4f, 0.4f, 0.4f,
     2430      0.4f, 0.4f, 0.4f,
     2431      0.4f, 0.4f, 0.4f,
     2432      0.4f, 0.4f, 0.4f,
     2433      0.4f, 0.4f, 0.4f,
     2434      0.4f, 0.4f, 0.4f,
    24602435
    24612436      // back
    2462       0.8f, 0.0f, 0.0f,
    2463       0.8f, 0.0f, 0.0f,
    2464       0.8f, 0.0f, 0.0f,
    2465       0.8f, 0.0f, 0.0f,
    2466       0.8f, 0.0f, 0.0f,
    2467       0.8f, 0.0f, 0.0f,
     2437      0.4f, 0.4f, 0.4f,
     2438      0.4f, 0.4f, 0.4f,
     2439      0.4f, 0.4f, 0.4f,
     2440      0.4f, 0.4f, 0.4f,
     2441      0.4f, 0.4f, 0.4f,
     2442      0.4f, 0.4f, 0.4f,
    24682443
    24692444      // right
    2470       0.8f, 0.0f, 0.0f,
    2471       0.8f, 0.0f, 0.0f,
    2472       0.8f, 0.0f, 0.0f,
    2473       0.8f, 0.0f, 0.0f,
    2474       0.8f, 0.0f, 0.0f,
    2475       0.8f, 0.0f, 0.0f,
     2445      0.4f, 0.4f, 0.4f,
     2446      0.4f, 0.4f, 0.4f,
     2447      0.4f, 0.4f, 0.4f,
     2448      0.4f, 0.4f, 0.4f,
     2449      0.4f, 0.4f, 0.4f,
     2450      0.4f, 0.4f, 0.4f,
    24762451
    24772452      // left
    2478       0.8f, 0.0f, 0.0f,
    2479       0.8f, 0.0f, 0.0f,
    2480       0.8f, 0.0f, 0.0f,
    2481       0.8f, 0.0f, 0.0f,
    2482       0.8f, 0.0f, 0.0f,
    2483       0.8f, 0.0f, 0.0f,
     2453      0.4f, 0.4f, 0.4f,
     2454      0.4f, 0.4f, 0.4f,
     2455      0.4f, 0.4f, 0.4f,
     2456      0.4f, 0.4f, 0.4f,
     2457      0.4f, 0.4f, 0.4f,
     2458      0.4f, 0.4f, 0.4f,
    24842459   };
    24852460   obj->texcoords = { 0.0f };
Note: See TracChangeset for help on using the changeset viewer.