Changeset 0e0f851 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Oct 12, 2018, 12:52:54 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
25b47d7
Parents:
2b0214c
Message:

Add a dedicated shader for asteroids, add an OpenGL debug callback, and start implementing the ability for asteroids to change color as they take damage.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r2b0214c r0e0f851  
    7676
    7777struct Asteroid : SceneObject {
    78    double hp;
     78   float hp;
    7979};
    8080
     
    8484
    8585struct EffectOverTime {
    86    double& effectedValue;
    87    double startValue;
     86   float& effectedValue;
     87   float startValue;
    8888   double startTime;
    89    double changePerSecond;
     89   float changePerSecond;
    9090   bool deleted;
    9191   SceneObject* effectedObject;
    9292
    93    EffectOverTime(double& effectedValue, double changePerSecond, SceneObject* object)
    94       : effectedValue(effectedValue), effectedObject(object) {
     93   EffectOverTime(float& effectedValue, float changePerSecond, SceneObject* object)
     94      : effectedValue(effectedValue), changePerSecond(changePerSecond), effectedObject(object) {
    9595      startValue = effectedValue;
    9696      startTime = glfwGetTime();
    97       this->changePerSecond = changePerSecond;
    9897      deleted = false;
    9998   }
     
    113112void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
    114113void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
     114
     115void APIENTRY debugGlCallback(
     116   GLenum source,
     117   GLenum type,
     118   GLuint id,
     119   GLenum severity,
     120   GLsizei length,
     121   const GLchar* message,
     122   const void* userParam
     123);
    115124
    116125bool faceClicked(array<vec3, 3> points, SceneObject* obj, vec4 world_ray, vec4 cam, vec4& click_point);
     
    133142                  GLuint normals_vbo,
    134143                  GLuint ubo,
     144                  GLuint asteroid_hp_ubo,
    135145                  GLuint model_mat_idx_vbo);
    136146void removeObjectFromScene(SceneObject& obj, GLuint ubo);
     
    145155                  GLuint* normals_vbo,
    146156                  GLuint* ubo,
     157                  GLuint* asteroid_hp_ubo,
    147158                  GLuint* model_mat_idx_vbo);
    148159
     
    155166                  GLuint normals_vbo,
    156167                  GLuint ubo,
     168                  GLuint asteroid_hp_ubo,
    157169                  GLuint model_mat_idx_vbo);
    158170
     
    165177                  GLuint normals_vbo,
    166178                  GLuint ubo,
     179                  GLuint asteroid_hp_ubo,
    167180                  GLuint model_mat_idx_vbo);
    168181
     
    174187
    175188void translateLaser(Laser* laser, const vec3& translation, GLuint ubo);
    176 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo);
     189void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp, GLuint asteroid_hp_loc);
    177190bool getLaserAndAsteroidIntersection(vec3& start, vec3& end, SceneObject& asteroid, vec3& intersection);
    178191
     
    181194
    182195void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    183                   GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
    184                   GLuint color_vao, GLuint texture_vao, GLuint laser_vao,
     196                  GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp,
     197                  GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao,
    185198                  GLuint colors_vbo, GLuint selected_colors_vbo,
    186199                  SceneObject* selectedObject);
     
    254267#endif
    255268
    256    glfwWindowHint(GLFW_SAMPLES, 16);
    257 
    258269   GLFWwindow* window = NULL;
    259270   GLFWmonitor* mon = NULL;
     271
     272   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
     273   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
     274   glfwWindowHint(GLFW_SAMPLES, 16);
     275   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
    260276
    261277   if (FULLSCREEN) {
     
    278294   glewExperimental = GL_TRUE;
    279295   glewInit();
     296
     297   if (GLEW_KHR_debug) {
     298      cout << "FOUND GLEW debug extension" << endl;
     299      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
     300      glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, nullptr);
     301      cout << "Bound debug callback" << endl;
     302   }
    280303
    281304   srand(time(0));
     
    322345   const GLubyte* renderer = glGetString(GL_RENDERER);
    323346   const GLubyte* version = glGetString(GL_VERSION);
    324    printf("Renderer: %s\n", renderer);
    325    printf("OpenGL version supported %s\n", version);
     347   cout << "Renderer: " << renderer << endl;
     348   cout << "OpenGL version supported " << version << endl;
    326349
    327350   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
    406429   map<GLuint, BufferInfo> shaderBufferInfo;
    407430
    408    GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
     431   // TODO: Rename color_sp to ship_sp and comment out texture_sp)
     432
     433   GLuint color_sp = loadShaderProgram("./ship.vert", "./ship.frag");
     434   GLuint asteroid_sp = loadShaderProgram("./asteroid.vert", "./asteroid.frag");
    409435   GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
    410436   GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
    411437
    412438   shaderBufferInfo[color_sp] = BufferInfo();
     439   shaderBufferInfo[asteroid_sp] = BufferInfo();
    413440   shaderBufferInfo[texture_sp] = BufferInfo();
    414441   shaderBufferInfo[laser_sp] = BufferInfo();
     
    425452
    426453   GLuint points_vbo, colors_vbo, selected_colors_vbo, texcoords_vbo,
    427       normals_vbo, ubo, model_mat_idx_vbo;
    428 
     454      normals_vbo, ubo, asteroid_hp_ubo, model_mat_idx_vbo;
     455
     456   cout << "Initializing buffers" << endl;
    429457   initializeBuffers(
    430458      &points_vbo,
     
    434462      &normals_vbo,
    435463      &ubo,
     464      &asteroid_hp_ubo,
    436465      &model_mat_idx_vbo);
    437466
     467   cout << "Populating buffers" << endl;
    438468   populateBuffers(objects,
    439469      shaderBufferInfo,
     
    444474      normals_vbo,
    445475      ubo,
     476      asteroid_hp_ubo,
    446477      model_mat_idx_vbo);
     478
     479   cout << "Done" << endl;
    447480
    448481   GLuint color_vao = 0;
     
    459492
    460493   // Comment these two lines out when I want to use selected colors
     494   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     495   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     496
     497   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     498   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
     499
     500   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     501   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
     502
     503   GLuint asteroid_vao = 0;
     504   glGenVertexArrays(1, &asteroid_vao);
     505   glBindVertexArray(asteroid_vao);
     506
     507   glEnableVertexAttribArray(0);
     508   glEnableVertexAttribArray(1);
     509   glEnableVertexAttribArray(2);
     510   glEnableVertexAttribArray(3);
     511
     512   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     513   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     514
    461515   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    462516   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     
    543597   GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
    544598   GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
    545    GLuint color_sp_ub_index = glGetUniformBlockIndex(color_sp, "models");
     599   GLuint color_sp_models_ub_index = glGetUniformBlockIndex(color_sp, "models");
     600
     601   GLuint asteroid_view_mat_loc = glGetUniformLocation(asteroid_sp, "view");
     602   GLuint asteroid_proj_mat_loc = glGetUniformLocation(asteroid_sp, "proj");
     603   GLuint asteroid_hp_loc = glGetUniformLocation(asteroid_sp, "hp");
     604   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(asteroid_sp, "models");
     605   //GLuint asteroid_sp_hp_ub_index = glGetUniformBlockIndex(asteroid_sp, "hp_uniform");
    546606
    547607   GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
    548608   GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
    549    GLuint texture_sp_ub_index = glGetUniformBlockIndex(texture_sp, "models");
     609   GLuint texture_sp_models_ub_index = glGetUniformBlockIndex(texture_sp, "models");
    550610
    551611   GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
    552612   GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj");
    553613   GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
    554    GLuint laser_sp_ub_index = glGetUniformBlockIndex(laser_sp, "models");
     614   GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(laser_sp, "models");
    555615
    556616
     
    559619   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    560620
    561    glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point);
     621   glUniformBlockBinding(color_sp, color_sp_models_ub_index, ub_binding_point);
    562622   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    563623
     624
     625   GLfloat tempHp = 10.0f;
     626   mat4 hp(tempHp);
     627
     628   glUseProgram(asteroid_sp);
     629   glUniformMatrix4fv(asteroid_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     630   glUniformMatrix4fv(asteroid_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     631   glUniformMatrix4fv(asteroid_hp_loc, 1, GL_FALSE, value_ptr(hp));
     632
     633   glUniformBlockBinding(asteroid_sp, asteroid_sp_models_ub_index, ub_binding_point);
     634   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     635
     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   */
    564640
    565641   glUseProgram(texture_sp);
     
    567643   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    568644
    569    glUniformBlockBinding(texture_sp, texture_sp_ub_index, ub_binding_point);
     645   glUniformBlockBinding(texture_sp, texture_sp_models_ub_index, ub_binding_point);
    570646   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    571647
     
    576652   glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
    577653
    578    glUniformBlockBinding(laser_sp, laser_sp_ub_index, ub_binding_point);
     654   glUniformBlockBinding(laser_sp, laser_sp_models_ub_index, ub_binding_point);
    579655   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    580656
     
    641717         elapsed_seconds_spawn += elapsed_seconds;
    642718         if (elapsed_seconds_spawn > 0.5f) {
    643             SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)), color_sp);
     719            SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)), asteroid_sp);
    644720            addObjectToScene(obj, shaderBufferInfo,
    645721               points_vbo,
     
    649725               normals_vbo,
    650726               ubo,
     727               asteroid_hp_ubo,
    651728               model_mat_idx_vbo);
    652729
     
    708785               normals_vbo,
    709786               ubo,
     787               asteroid_hp_ubo,
    710788               model_mat_idx_vbo);
    711789         } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) {
     
    725803               normals_vbo,
    726804               ubo,
     805               asteroid_hp_ubo,
    727806               model_mat_idx_vbo);
    728807         } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) {
     
    747826
    748827         if (leftLaser != NULL && !leftLaser->deleted) {
    749             updateLaserTarget(leftLaser, objects, points_vbo);
     828            updateLaserTarget(leftLaser, objects, points_vbo, asteroid_sp, asteroid_hp_loc);
    750829         }
    751830         if (rightLaser != NULL && !rightLaser->deleted) {
    752             updateLaserTarget(rightLaser, objects, points_vbo);
     831            updateLaserTarget(rightLaser, objects, points_vbo, asteroid_sp, asteroid_hp_loc);
    753832         }
    754833      }
     
    847926         case STATE_GAME:
    848927            renderScene(shaderBufferInfo,
    849                color_sp, texture_sp, laser_sp,
    850                color_vao, texture_vao, laser_vao,
     928               color_sp, asteroid_sp, texture_sp, laser_sp,
     929               color_vao, asteroid_vao, texture_vao, laser_vao,
    851930               colors_vbo, selected_colors_vbo,
    852931               selectedObject);
     
    9361015}
    9371016
     1017void APIENTRY debugGlCallback(
     1018   GLenum source,
     1019   GLenum type,
     1020   GLuint id,
     1021   GLenum severity,
     1022   GLsizei length,
     1023   const GLchar* message,
     1024   const void* userParam
     1025) {
     1026   string strMessage(message);
     1027
     1028   // TODO: Use C++ strings directly
     1029   char source_str[2048];
     1030   char type_str[2048];
     1031   char severity_str[2048];
     1032
     1033   switch (source) {
     1034   case 0x8246:
     1035      strcpy(source_str, "API");
     1036      break;
     1037   case 0x8247:
     1038      strcpy(source_str, "WINDOW_SYSTEM");
     1039      break;
     1040   case 0x8248:
     1041      strcpy(source_str, "SHADER_COMPILER");
     1042      break;
     1043   case 0x8249:
     1044      strcpy(source_str, "THIRD_PARTY");
     1045      break;
     1046   case 0x824A:
     1047      strcpy(source_str, "APPLICATION");
     1048      break;
     1049   case 0x824B:
     1050      strcpy(source_str, "OTHER");
     1051      break;
     1052   default:
     1053      strcpy(source_str, "undefined");
     1054      break;
     1055   }
     1056
     1057   switch (type) {
     1058   case 0x824C:
     1059      strcpy(type_str, "ERROR");
     1060      break;
     1061   case 0x824D:
     1062      strcpy(type_str, "DEPRECATED_BEHAVIOR");
     1063      break;
     1064   case 0x824E:
     1065      strcpy(type_str, "UNDEFINED_BEHAVIOR");
     1066      break;
     1067   case 0x824F:
     1068      strcpy(type_str, "PORTABILITY");
     1069      break;
     1070   case 0x8250:
     1071      strcpy(type_str, "PERFORMANCE");
     1072      break;
     1073   case 0x8251:
     1074      strcpy(type_str, "OTHER");
     1075      break;
     1076   case 0x8268:
     1077      strcpy(type_str, "MARKER");
     1078      break;
     1079   case 0x8269:
     1080      strcpy(type_str, "PUSH_GROUP");
     1081      break;
     1082   case 0x826A:
     1083      strcpy(type_str, "POP_GROUP");
     1084      break;
     1085   default:
     1086      strcpy(type_str, "undefined");
     1087      break;
     1088   }
     1089   switch (severity) {
     1090   case 0x9146:
     1091      strcpy(severity_str, "HIGH");
     1092      break;
     1093   case 0x9147:
     1094      strcpy(severity_str, "MEDIUM");
     1095      break;
     1096   case 0x9148:
     1097      strcpy(severity_str, "LOW");
     1098      break;
     1099   case 0x826B:
     1100      strcpy(severity_str, "NOTIFICATION");
     1101      break;
     1102   default:
     1103      strcpy(severity_str, "undefined");
     1104      break;
     1105   }
     1106
     1107   if (string(severity_str) != "NOTIFICATION") {
     1108      cout << "OpenGL Error!!!" << endl;
     1109      cout << "Source: " << string(source_str) << endl;
     1110      cout << "Type: " << string(type_str) << endl;
     1111      cout << "Severity: " << string(severity_str) << endl;
     1112      cout << strMessage << endl;
     1113   }
     1114}
     1115
    9381116
    9391117GLuint loadShader(GLenum type, string file) {
     
    11091287   GLuint normals_vbo,
    11101288   GLuint ubo,
     1289   GLuint asteroid_hp_ubo,
    11111290   GLuint model_mat_idx_vbo) {
    11121291   objects.push_back(obj);
     
    11331312         normals_vbo,
    11341313         ubo,
     1314         asteroid_hp_ubo,
    11351315         model_mat_idx_vbo);
    11361316   } else {
     
    11421322         normals_vbo,
    11431323         ubo,
     1324         asteroid_hp_ubo,
    11441325         model_mat_idx_vbo);
    11451326   }
     
    16921873                  GLuint* normals_vbo,
    16931874                  GLuint* ubo,
     1875                  GLuint* asteroid_hp_ubo,
    16941876                  GLuint* model_mat_idx_vbo) {
    16951877   *points_vbo = 0;
     
    17101892   *ubo = 0;
    17111893   glGenBuffers(1, ubo);
     1894
     1895   *asteroid_hp_ubo = 0;
     1896   glGenBuffers(1, asteroid_hp_ubo);
    17121897
    17131898   *model_mat_idx_vbo = 0;
     
    17231908                  GLuint normals_vbo,
    17241909                  GLuint ubo,
    1725                   GLuint model_mat_idx_vbo) {
    1726    GLsizeiptr points_buffer_size = 0;
    1727    GLsizeiptr textures_buffer_size = 0;
    1728    GLsizeiptr ubo_buffer_size = 0;
    1729    GLsizeiptr model_mat_idx_buffer_size = 0;
     1910                  GLuint asteroid_hp_ubo,
     1911                  GLuint ubo_idx_vbo) {
     1912   GLsizeiptr num_points = 0;
     1913   GLsizeiptr num_objects = 0;
    17301914
    17311915   map<GLuint, unsigned int> shaderCounts;
     
    17451929         it = objects.erase(it);
    17461930      } else {
    1747          points_buffer_size += (*it)->num_points * sizeof(GLfloat) * 3;
    1748          textures_buffer_size += (*it)->num_points * sizeof(GLfloat) * 2;
    1749          ubo_buffer_size += 16 * sizeof(GLfloat);
    1750          model_mat_idx_buffer_size += (*it)->num_points * sizeof(GLuint);
     1931         num_points += (*it)->num_points;
     1932         num_objects++;
    17511933
    17521934         if (shaderCounts.count((*it)->shader_program) == 0) {
     
    17631945
    17641946   // double the buffer sizes to leave room for new objects
    1765    points_buffer_size *= 2;
    1766    textures_buffer_size *= 2;
    1767    ubo_buffer_size *= 2;
    1768    model_mat_idx_buffer_size *= 2;
     1947   num_points *= 2;
     1948   num_objects *= 2;
    17691949
    17701950   map<GLuint, unsigned int>::iterator shaderIt;
     
    17801960   */
    17811961   for (shaderIt = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) {
     1962      // When populating the buffers, leave as much empty space as space taken up by existing objects
     1963      // to allow new objects to be added without immediately having to resize the buffers
    17821964      shaderBufferInfo[shaderIt->first].vbo_base = lastShaderCount * 2;
    17831965      shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2;
     
    18041986
    18051987   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    1806    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     1988   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    18071989
    18081990   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    1809    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     1991   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    18101992
    18111993   glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    1812    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     1994   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    18131995
    18141996   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    1815    glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
     1997   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 2, NULL, GL_DYNAMIC_DRAW);
    18161998
    18171999   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    1818    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     2000   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLfloat) * 3, NULL, GL_DYNAMIC_DRAW);
    18192001
    18202002   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    1821    glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size, NULL, GL_DYNAMIC_DRAW);
    1822 
    1823    glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    1824    glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW);
     2003   glBufferData(GL_UNIFORM_BUFFER, num_objects * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
     2004
     2005   /*
     2006   glBindBuffer(GL_UNIFORM_BUFFER, asteroid_hp_ubo);
     2007   glBufferData(GL_UNIFORM_BUFFER, num_objects * sizeof(mat4), NULL, GL_DYNAMIC_DRAW);
     2008   */
     2009
     2010   glBindBuffer(GL_ARRAY_BUFFER, ubo_idx_vbo);
     2011   glBufferData(GL_ARRAY_BUFFER, num_points * sizeof(GLuint), NULL, GL_DYNAMIC_DRAW);
    18252012
    18262013   for (it = objects.begin(); it != objects.end(); it++) {
     
    18322019         normals_vbo,
    18332020         ubo,
    1834          model_mat_idx_vbo);
     2021         asteroid_hp_ubo,
     2022         ubo_idx_vbo);
    18352023   }
    18362024}
     
    18442032                  GLuint normals_vbo,
    18452033                  GLuint ubo,
     2034                  GLuint asteroid_hp_ubo,
    18462035                  GLuint model_mat_idx_vbo) {
    18472036   BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program];
     
    18752064   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    18762065   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
     2066
     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   */
    18772075
    18782076   bufferInfo->vbo_offset += obj.num_points;
     
    19232121}
    19242122
    1925 void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo) {
     2123void updateLaserTarget(Laser* laser, vector<SceneObject*>& objects, GLuint points_vbo, GLuint asteroid_sp, GLuint asteroid_hp_loc) {
    19262124   // TODO: A lot of the values calculated here can be calculated once and saved when the laser is created,
    19272125   // and then re-used here
     
    19582156
    19592157      if (closestAsteroid != NULL) {
    1960          eot = new EffectOverTime(closestAsteroid->hp, -10, closestAsteroid);
     2158         eot = new EffectOverTime(closestAsteroid->hp, -10.0f, closestAsteroid);
    19612159         effects.push_back(eot);
    19622160      }
     
    19732171   if (closestAsteroid != NULL) {
    19742172      length = glm::length(closestIntersection - start);
     2173
     2174      // TODO: Find a better way of doing this
     2175
     2176      GLfloat tempHp = closestAsteroid->hp;
     2177      mat4 hp(tempHp);
     2178
     2179      glUseProgram(asteroid_sp);
     2180      glUniformMatrix4fv(asteroid_hp_loc, 1, GL_FALSE, value_ptr(hp));
    19752181   }
    19762182
     
    20352241
    20362242void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    2037                   GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
    2038                   GLuint color_vao, GLuint texture_vao, GLuint laser_vao,
     2243                  GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp,
     2244                  GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao,
    20392245                  GLuint colors_vbo, GLuint selected_colors_vbo,
    20402246                  SceneObject* selectedObject) {
     
    20572263
    20582264   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset);
     2265
     2266   glUseProgram(asteroid_sp);
     2267   glBindVertexArray(asteroid_vao);
     2268
     2269   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[asteroid_sp].vbo_base, shaderBufferInfo[asteroid_sp].vbo_offset);
    20592270
    20602271   glUseProgram(texture_sp);
Note: See TracChangeset for help on using the changeset viewer.