Changeset 49db5fc in opengl-game
- Timestamp:
- Mar 15, 2019, 5:25:46 PM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- b220f78
- Parents:
- a0eb547
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
ra0eb547 r49db5fc 57 57 ATTRIB_OBJECT_VARYING, 58 58 ATTRIB_POINT_VARYING, 59 }; 60 61 // Add more types as I need them 62 enum UniformType { 63 UNIFORM_NONE, 64 UNIFORM_MATRIX_4F, 65 UNIFORM_1F, 66 UNIFORM_3F, 59 67 }; 60 68 … … 119 127 GLint size; 120 128 GLenum type; 121 GLuint buffer; 129 UniformType uniType; 130 GLuint buffer; // For uniforms, this is the uniform location 122 131 size_t fieldOffset; 132 GLfloat* data; // pointer to data source for uniform attributes 123 133 }; 124 134 … … 173 183 174 184 void defineModelGroupAttrib(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, GLenum type, size_t fieldOffset); 185 void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType, GLint size, UniformType type, GLfloat* data); 175 186 void initModelGroupAttribs(ShaderModelGroup& modelGroup); 187 void bindUniformData(AttribInfo& attrib); 176 188 177 189 size_t GLsizeof(GLenum); … … 212 224 map<GLuint, BufferInfo>& shaderBufferInfo, 213 225 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); 220 227 221 228 void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo); … … 470 477 */ 471 478 479 GLfloat laserColor[3] = {0.2f, 1.0f, 0.2f}; 480 472 481 GLuint points_vbo, colors_vbo, texcoords_vbo, normals_vbo, ubo, model_mat_idx_vbo; 473 482 … … 496 505 1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset)); 497 506 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 498 512 initModelGroupAttribs(modelGroups[TYPE_SHIP]); 499 513 … … 527 541 1, GL_UNSIGNED_INT, offsetof(SceneObject, ubo_offset)); 528 542 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 529 548 initModelGroupAttribs(modelGroups[TYPE_ASTEROID]); 530 549 … … 555 574 defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING, 556 575 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); 557 583 558 584 initModelGroupAttribs(modelGroups[TYPE_LASER]); … … 671 697 GLuint ub_binding_point = 0; 672 698 673 GLuint ship_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "view");674 GLuint ship_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "proj");675 699 GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_SHIP].shaderProgram, "models"); 676 700 677 GLuint asteroid_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, "view");678 GLuint asteroid_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, "proj");679 701 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 680 725 681 726 // may want to do initialization for basic_texture uniform here too 682 727 // Right now, I think I'm getting away without getting that uniform location because I'm only 683 728 // 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 710 729 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"]); 714 733 715 734 glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point); … … 899 918 900 919 objExplosion->model_mat = model_mat; 920 GLfloat curTime = glfwGetTime(); 901 921 902 922 // initiate an explosion … … 988 1008 989 1009 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"]); 991 1011 992 1012 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"]); 994 1014 995 1015 cam_moved = false; … … 1392 1412 model_mat_idx_vbo); 1393 1413 } 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); 1401 1415 } 1402 1416 } … … 1973 1987 } 1974 1988 1989 void 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 1975 2001 void initModelGroupAttribs(ShaderModelGroup& modelGroup) { 1976 2002 glBindVertexArray(modelGroup.vao); … … 1978 2004 map<string, AttribInfo>::iterator it; 1979 2005 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 } 1989 2023 } 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 2028 void 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; 1995 2039 } 1996 2040 } … … 2241 2285 2242 2286 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); 2250 2288 } 2251 2289 } … … 2254 2292 map<GLuint, BufferInfo>& shaderBufferInfo, 2255 2293 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) { 2262 2295 BufferInfo* bufferInfo = &shaderBufferInfo[modelGroups[obj.type].shaderProgram]; 2263 2296
Note:
See TracChangeset
for help on using the changeset viewer.