Changeset 49db5fc in opengl-game


Ignore:
Timestamp:
Mar 15, 2019, 5:25:46 PM (6 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
b220f78
Parents:
a0eb547
Message:

Add support for uniform attributes in a ShaderModelGroup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    ra0eb547 r49db5fc  
    5757   ATTRIB_OBJECT_VARYING,
    5858   ATTRIB_POINT_VARYING,
     59};
     60
     61// Add more types as I need them
     62enum UniformType {
     63   UNIFORM_NONE,
     64   UNIFORM_MATRIX_4F,
     65   UNIFORM_1F,
     66   UNIFORM_3F,
    5967};
    6068
     
    119127   GLint size;
    120128   GLenum type;
    121    GLuint buffer;
     129   UniformType uniType;
     130   GLuint buffer; // For uniforms, this is the uniform location
    122131   size_t fieldOffset;
     132   GLfloat* data; // pointer to data source for uniform attributes
    123133};
    124134
     
    173183
    174184void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset);
     185void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, UniformType type, GLfloat* data);
    175186void initModelGroupAttribs(ShaderModelGroup& modelGroup);
     187void bindUniformData(AttribInfo& attrib);
    176188
    177189size_t GLsizeof(GLenum);
     
    212224                  map<GLuint, BufferInfo>& shaderBufferInfo,
    213225                  map<ObjectType, ShaderModelGroup>& modelGroups,
    214                   GLuint points_vbo,
    215                   GLuint colors_vbo,
    216                   GLuint texcoords_vbo,
    217                   GLuint normals_vbo,
    218                   GLuint ubo,
    219                   GLuint model_mat_idx_vbo);
     226                  GLuint ubo);
    220227
    221228void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
     
    470477    */
    471478
     479   GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f};
     480
    472481   GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo;
    473482
     
    496505      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
    497506
     507   defineModelGroupUniform(modelGroups[TYPE_SHIP], "view", ATTRIB_UNIFORM,
     508      1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
     509   defineModelGroupUniform(modelGroups[TYPE_SHIP], "proj", ATTRIB_UNIFORM,
     510      1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
     511
    498512   initModelGroupAttribs(modelGroups[TYPE_SHIP]);
    499513
     
    527541      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
    528542
     543   defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "view", ATTRIB_UNIFORM,
     544      1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
     545   defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "proj", ATTRIB_UNIFORM,
     546      1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
     547
    529548   initModelGroupAttribs(modelGroups[TYPE_ASTEROID]);
    530549
     
    555574   defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING,
    556575      1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset));
     576
     577   defineModelGroupUniform(modelGroups[TYPE_LASER], "view", ATTRIB_UNIFORM,
     578      1, UNIFORM_MATRIX_4F, value_ptr(view_mat));
     579   defineModelGroupUniform(modelGroups[TYPE_LASER], "proj", ATTRIB_UNIFORM,
     580      1, UNIFORM_MATRIX_4F, value_ptr(proj_mat));
     581    defineModelGroupUniform(modelGroups[TYPE_LASER], "laser_color", ATTRIB_UNIFORM,
     582      1, UNIFORM_3F, laserColor);
    557583
    558584   initModelGroupAttribs(modelGroups[TYPE_LASER]);
     
    671697   GLuint ub_binding_point = 0;
    672698
    673    GLuint ship_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "view");
    674    GLuint ship_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "proj");
    675699   GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_SHIP].shaderProgram, "models");
    676700
    677    GLuint asteroid_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, "view");
    678    GLuint asteroid_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, "proj");
    679701   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models");
     702
     703   GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models");
     704
     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");
     707   GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models");
     708
     709
     710   glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
     711   bindUniformData(modelGroups[TYPE_SHIP].attribs["view"]);
     712   bindUniformData(modelGroups[TYPE_SHIP].attribs["proj"]);
     713
     714   glUniformBlockBinding(modelGroups[TYPE_SHIP].shaderProgram, ship_sp_models_ub_index, ub_binding_point);
     715   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     716
     717
     718   glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram);
     719   bindUniformData(modelGroups[TYPE_ASTEROID].attribs["view"]);
     720   bindUniformData(modelGroups[TYPE_ASTEROID].attribs["proj"]);
     721
     722   glUniformBlockBinding(modelGroups[TYPE_ASTEROID].shaderProgram, asteroid_sp_models_ub_index, ub_binding_point);
     723   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     724
    680725
    681726   // may want to do initialization for basic_texture uniform here too
    682727   // Right now, I think I'm getting away without getting that uniform location because I'm only
    683728   // using one texture, so setting it to GL_TEXTURE0 once works
    684    GLuint laser_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "view");
    685    GLuint laser_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "proj");
    686    GLuint laser_color_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "laser_color");
    687    GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models");
    688 
    689    GLuint explosion_start_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "explosion_start_time");
    690    GLuint cur_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "cur_time");
    691    GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_EXPLOSION].shaderProgram, "models");
    692 
    693 
    694    glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
    695    glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    696    glUniformMatrix4fv(ship_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    697 
    698    glUniformBlockBinding(modelGroups[TYPE_SHIP].shaderProgram, ship_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(modelGroups[TYPE_ASTEROID].shaderProgram);
    703    glUniformMatrix4fv(asteroid_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    704    glUniformMatrix4fv(asteroid_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    705 
    706    glUniformBlockBinding(modelGroups[TYPE_ASTEROID].shaderProgram, asteroid_sp_models_ub_index, ub_binding_point);
    707    glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    708 
    709 
    710729   glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
    711    glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    712    glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    713    glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
     730   bindUniformData(modelGroups[TYPE_LASER].attribs["view"]);
     731   bindUniformData(modelGroups[TYPE_LASER].attribs["proj"]);
     732   bindUniformData(modelGroups[TYPE_LASER].attribs["laser_color"]);
    714733
    715734   glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point);
     
    899918
    900919                  objExplosion->model_mat = model_mat;
     920                  GLfloat curTime = glfwGetTime();
    901921
    902922                  // initiate an explosion
     
    9881008
    9891009         glUseProgram(modelGroups[TYPE_SHIP].shaderProgram);
    990          glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     1010         bindUniformData(modelGroups[TYPE_SHIP].attribs["view"]);
    9911011
    9921012         glUseProgram(modelGroups[TYPE_LASER].shaderProgram);
    993          glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
     1013         bindUniformData(modelGroups[TYPE_LASER].attribs["view"]);
    9941014
    9951015         cam_moved = false;
     
    13921412         model_mat_idx_vbo);
    13931413   } else {
    1394       copyObjectDataToBuffers(*objects.back(), shaderBufferInfo, modelGroups,
    1395          points_vbo,
    1396          colors_vbo,
    1397          texcoords_vbo,
    1398          normals_vbo,
    1399          ubo,
    1400          model_mat_idx_vbo);
     1414      copyObjectDataToBuffers(*objects.back(), shaderBufferInfo, modelGroups, ubo);
    14011415   }
    14021416}
     
    19731987}
    19741988
     1989void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType,
     1990                  GLint size, UniformType type, GLfloat* data) {
     1991   AttribInfo attribInfo;
     1992
     1993   attribInfo.attribType = attribType;
     1994   attribInfo.size = size;
     1995   attribInfo.uniType = type;
     1996   attribInfo.data = data;
     1997
     1998   modelGroup.attribs[name] = attribInfo;
     1999}
     2000
    19752001void initModelGroupAttribs(ShaderModelGroup& modelGroup) {
    19762002   glBindVertexArray(modelGroup.vao);
     
    19782004   map<string, AttribInfo>::iterator it;
    19792005   for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) {
    1980       glEnableVertexAttribArray(it->second.index);
    1981 
    1982       glGenBuffers(1, &it->second.buffer);
    1983       glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
    1984 
    1985       switch (it->second.type) {
    1986          case GL_FLOAT: {
    1987             glVertexAttribPointer(it->second.index, it->second.size, it->second.type, GL_FALSE, 0, NULL);
    1988             break;
     2006      if (it->second.attribType == ATTRIB_UNIFORM) {
     2007         it->second.buffer = glGetUniformLocation(modelGroup.shaderProgram, it->first.c_str());
     2008      } else {
     2009         glEnableVertexAttribArray(it->second.index);
     2010
     2011         glGenBuffers(1, &it->second.buffer);
     2012         glBindBuffer(GL_ARRAY_BUFFER, it->second.buffer);
     2013
     2014         switch (it->second.type) {
     2015            case GL_FLOAT: {
     2016               glVertexAttribPointer(it->second.index, it->second.size, it->second.type, GL_FALSE, 0, NULL);
     2017               break;
     2018            }
     2019            case GL_UNSIGNED_INT: {
     2020               glVertexAttribIPointer(it->second.index, it->second.size, it->second.type, 0, NULL);
     2021               break;
     2022            }
    19892023         }
    1990          case GL_UNSIGNED_INT: {
    1991             glVertexAttribIPointer(it->second.index, it->second.size, it->second.type, 0, NULL);
    1992             break;
    1993          }
    1994       }
     2024      }
     2025   }
     2026}
     2027
     2028void bindUniformData(AttribInfo& attrib) {
     2029   switch(attrib.uniType) {
     2030      case UNIFORM_MATRIX_4F:
     2031         glUniformMatrix4fv(attrib.buffer, attrib.size, GL_FALSE, attrib.data);
     2032         break;
     2033      case UNIFORM_1F:
     2034         glUniform3fv(attrib.buffer, attrib.size, attrib.data);
     2035         break;
     2036      case UNIFORM_3F:
     2037         glUniform3fv(attrib.buffer, attrib.size, attrib.data);
     2038         break;
    19952039   }
    19962040}
     
    22412285
    22422286   for (it = objects.begin(); it != objects.end(); it++) {
    2243       copyObjectDataToBuffers(**it, shaderBufferInfo, modelGroups,
    2244          points_vbo,
    2245          colors_vbo,
    2246          texcoords_vbo,
    2247          normals_vbo,
    2248          ubo,
    2249          ubo_idx_vbo);
     2287      copyObjectDataToBuffers(**it, shaderBufferInfo, modelGroups, ubo);
    22502288   }
    22512289}
     
    22542292                  map<GLuint, BufferInfo>& shaderBufferInfo,
    22552293                  map<ObjectType, ShaderModelGroup>& modelGroups,
    2256                   GLuint points_vbo,
    2257                   GLuint colors_vbo,
    2258                   GLuint texcoords_vbo,
    2259                   GLuint normals_vbo,
    2260                   GLuint ubo,
    2261                   GLuint model_mat_idx_vbo) {
     2294                  GLuint ubo) {
    22622295   BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj.type].shaderProgram];
    22632296
Note: See TracChangeset for help on using the changeset viewer.