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


Ignore:
Timestamp:
Mar 22, 2019, 3:36:04 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
de53394
Parents:
49db5fc
Message:

Create an overloaded version of bindUniformData that takes the data as a parameter instead of relying on a previously set pointer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r49db5fc rb220f78  
    107107
    108108   // TODO: Why not just use an initializer list for all the instance variables
     109   // TODO: Maybe pass in startTime instead of calling glfwGetTime() here
    109110   EffectOverTime(float& effectedValue, float changePerSecond, SceneObject* object)
    110111      : effectedValue(effectedValue), changePerSecond(changePerSecond), effectedObject(object) {
     
    186187void initModelGroupAttribs(ShaderModelGroup& modelGroup);
    187188void bindUniformData(AttribInfo& attrib);
     189void bindUniformData(AttribInfo& attrib, GLfloat* data);
    188190
    189191size_t GLsizeof(GLenum);
     
    201203                  GLuint* model_mat_idx_vbo);
    202204
    203 void initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view,
     205void initializeParticleEffectBuffers(vec3 origin,
    204206                  map<GLuint, BufferInfo>& shaderBufferInfo,
    205207                  map<ObjectType, ShaderModelGroup>& modelGroups,
     
    478480
    479481   GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f};
     482   GLfloat curTime, prevTime, elapsedTime;
    480483
    481484   GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo;
     
    600603   shaderBufferInfo[modelGroups[TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary
    601604
     605   // The last parameter (offset) is only used for populating the buffers since the distance
     606   // between each item is needed there. However, that code isn't run for explosions right now anyway,
     607   // so I may as well pass in 0 here.
     608   defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "v_i", ATTRIB_POINT_VARYING,
     609      3, GL_FLOAT, 0);
     610   defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING,
     611      1, GL_FLOAT, 0);
     612
     613   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "explosion_start_time", ATTRIB_UNIFORM,
     614      1, UNIFORM_1F, &curTime);
     615   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM,
     616      1, UNIFORM_1F, &curTime);
     617   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "model_mat", ATTRIB_UNIFORM,
     618      1, UNIFORM_MATRIX_4F, NULL);
     619   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "view", ATTRIB_UNIFORM,
     620      1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
     621   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "proj", ATTRIB_UNIFORM,
     622      1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
     623   // Still need to do the model mat
     624
     625  initModelGroupAttribs(modelGroups[TYPE_EXPLOSION]);
     626
    602627   cam_pos = vec3(0.0f, 0.0f, 2.0f);
    603628   float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
     
    651676   proj_mat = make_mat4(proj_arr);
    652677
    653    initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat,
     678   initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f),
    654679      shaderBufferInfo,
    655680      modelGroups,
     
    703728   GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models");
    704729
    705    GLuint explosion_start_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "explosion_start_time");
    706    GLuint cur_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "cur_time");
    707730   GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models");
    708731
     
    746769   double elapsed_seconds_fps = 0.0f;
    747770   double elapsed_seconds_spawn = 0.0f;
    748    double previous_seconds = glfwGetTime();
     771
     772   prevTime = glfwGetTime();
    749773
    750774   // This draws wireframes. Useful for seeing separate faces and occluded objects.
     
    757781
    758782   while (!glfwWindowShouldClose(window) && isRunning) {
    759       double current_seconds = glfwGetTime();
    760       double elapsed_seconds = current_seconds - previous_seconds;
     783      curTime = glfwGetTime();
     784      elapsedTime = curTime - prevTime;
    761785
    762786      // temporary code to get around vsync issue in OSX Sierra
    763       if (elapsed_seconds < (1.0f / TARGET_FPS)) {
     787      if (elapsedTime < (1.0f / TARGET_FPS)) {
    764788        continue;
    765789      }
    766790
    767       previous_seconds = current_seconds;
    768 
    769       elapsed_seconds_fps += elapsed_seconds;
     791      prevTime = curTime;
     792
     793      elapsed_seconds_fps += elapsedTime;
    770794      if (elapsed_seconds_fps > 0.25f) {
    771795         fps = (double)frame_count / elapsed_seconds_fps;
     
    806830      if (curState == STATE_GAME) {
    807831
    808          elapsed_seconds_spawn += elapsed_seconds;
     832         elapsed_seconds_spawn += elapsedTime;
    809833         if (elapsed_seconds_spawn > 0.5f) {
    810834            SceneObject* obj = createAsteroid(vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f)));
     
    918942
    919943                  objExplosion->model_mat = model_mat;
    920                   GLfloat curTime = glfwGetTime();
    921944
    922945                  // initiate an explosion
    923946                  glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
    924947
    925                   GLuint model_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "model_mat");
    926                   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objExplosion->model_mat));
    927 
    928                   glUniform1f(explosion_start_time_loc, (GLfloat)glfwGetTime());
     948                  bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["explosion_start_time"]);
     949                  bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(objExplosion->model_mat));
    929950               }
    930951            }
     
    945966         } else {
    946967            EffectOverTime* eot = *it;
    947             eot->effectedValue = eot->startValue + (current_seconds - eot->startTime) * eot->changePerSecond;
     968            eot->effectedValue = eot->startValue + (curTime - eot->startTime) * eot->changePerSecond;
    948969
    949970            it++;
     
    955976      }
    956977
    957       float dist = cam_speed * elapsed_seconds;
     978      float dist = cam_speed * elapsedTime;
    958979      if (key_down[GLFW_KEY_A]) {
    959980         vec3 dir = vec3(inverse(R) * vec4(-1.0f, 0.0f, 0.0f, 1.0f));
     
    9821003      /*
    9831004      if (key_down[GLFW_KEY_LEFT]) {
    984       cam_yaw += cam_yaw_speed * elapsed_seconds;
     1005      cam_yaw += cam_yaw_speed * elapsedTime;
    9851006      cam_moved = true;
    9861007      }
    9871008      if (key_down[GLFW_KEY_RIGHT]) {
    988       cam_yaw -= cam_yaw_speed * elapsed_seconds;
     1009      cam_yaw -= cam_yaw_speed * elapsedTime;
    9891010      cam_moved = true;
    9901011      }
    9911012      if (key_down[GLFW_KEY_UP]) {
    992       cam_pitch += cam_pitch_speed * elapsed_seconds;
     1013      cam_pitch += cam_pitch_speed * elapsedTime;
    9931014      cam_moved = true;
    9941015      }
    9951016      if (key_down[GLFW_KEY_DOWN]) {
    996       cam_pitch -= cam_pitch_speed * elapsed_seconds;
     1017      cam_pitch -= cam_pitch_speed * elapsedTime;
    9971018      cam_moved = true;
    9981019      }
     
    10171038
    10181039      glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
    1019       glUniform1f(cur_time_loc, (GLfloat)current_seconds);
     1040      bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["cur_time"]);
    10201041
    10211042      // Render scene
     
    20322053         break;
    20332054      case UNIFORM_1F:
    2034          glUniform3fv(attrib.buffer, attrib.size, attrib.data);
     2055         glUniform1fv(attrib.buffer, attrib.size, attrib.data);
    20352056         break;
    20362057      case UNIFORM_3F:
    20372058         glUniform3fv(attrib.buffer, attrib.size, attrib.data);
     2059         break;
     2060      case UNIFORM_NONE:
     2061         break;
     2062   }
     2063}
     2064
     2065void bindUniformData(AttribInfo& attrib, GLfloat *data) {
     2066   switch(attrib.uniType) {
     2067      case UNIFORM_MATRIX_4F:
     2068         glUniformMatrix4fv(attrib.buffer, attrib.size, GL_FALSE, data);
     2069         break;
     2070      case UNIFORM_1F:
     2071         glUniform1fv(attrib.buffer, attrib.size, data);
     2072         break;
     2073      case UNIFORM_3F:
     2074         glUniform3fv(attrib.buffer, attrib.size, data);
     2075         break;
     2076      case UNIFORM_NONE:
    20382077         break;
    20392078   }
     
    20972136}
    20982137
    2099 void initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view,
     2138void initializeParticleEffectBuffers(vec3 origin,
    21002139                  map<GLuint, BufferInfo>& shaderBufferInfo,
    21012140                  map<ObjectType, ShaderModelGroup>& modelGroups,
     
    21252164   glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
    21262165
    2127    GLuint proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "proj");
    2128    GLuint view_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "view");
    2129 
    2130    glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj));
    2131    glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view));
    2132 
    2133    GLuint model_mat_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "model_mat");
    2134 
    2135    glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat));
     2166   bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["proj"]);
     2167   bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["view"]);
     2168   bindUniformData(modelGroups[TYPE_EXPLOSION].attribs["model_mat"], value_ptr(model_mat));
    21362169
    21372170   GLuint velocity_vbo;
    21382171   glGenBuffers(1, &velocity_vbo);
     2172
     2173   GLuint time_vbo;
     2174   glGenBuffers(1, &time_vbo);
     2175
     2176   glBindVertexArray(modelGroups[TYPE_EXPLOSION].vao);
     2177
     2178   // the glBufferData and glVertexAttribPointer need to stay here while the corresponding arrays
     2179   // are local to this function. Once they're made part of the explosion object, I might be able
     2180   // to move this code out of here
     2181
    21392182   glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
    21402183   glBufferData(GL_ARRAY_BUFFER, sizeof(vv), vv, GL_STATIC_DRAW);
    2141 
    2142    GLuint time_vbo;
    2143    glGenBuffers(1, &time_vbo);
     2184   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     2185
    21442186   glBindBuffer(GL_ARRAY_BUFFER, time_vbo);
    21452187   glBufferData(GL_ARRAY_BUFFER, sizeof(vt), vt, GL_STATIC_DRAW);
    2146 
    2147    glBindVertexArray(modelGroups[TYPE_EXPLOSION].vao);
    2148 
    2149    glEnableVertexAttribArray(0);
    2150    glEnableVertexAttribArray(1);
    2151 
    2152    glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
    2153    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    2154 
    2155    glBindBuffer(GL_ARRAY_BUFFER, time_vbo);
    21562188   glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
    21572189
     
    27722804
    27732805float getRandomNum(float low, float high) {
    2774    return low + ((float)rand()/RAND_MAX) * (high-low);
    2775 }
     2806   return low + ((float)rand() / RAND_MAX) * (high-low);
     2807}
Note: See TracChangeset for help on using the changeset viewer.