Changeset 4c7cd57 in opengl-game
- Timestamp:
- Feb 6, 2019, 5:50:56 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- b62c109
- Parents:
- 0414306
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r0414306 r4c7cd57 53 53 }; 54 54 55 // TODO: Once all object types use ShaderModelGroup objects,, I should be able to remove the 56 // shader_program field since it would be available via modelGroups[type].shaderProgram 55 57 struct SceneObject { 56 58 unsigned int id; … … 60 62 // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate 61 63 // matrices for each object that can be updated independently and then applied to the object in that order. 64 // TODO: Actually, to make this as generic as possible, each object should have a matrix stack to support, 65 // for instance, applying a rotate, then a translate, then another rotate. Think about and implement the best approach. 62 66 mat4 model_mat, model_base, model_transform; 63 67 mat4 translate_mat; // beginning of doing what's mentioned above … … 213 217 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 214 218 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo, 215 GLuint ship_sp, GLuint laser_sp, 216 GLuint ship_vao, GLuint laser_vao); 219 GLuint laser_sp, GLuint laser_vao); 217 220 218 221 void renderSceneGui(); … … 457 460 map<ObjectType, ShaderModelGroup> modelGroups; 458 461 459 GLuint ship_sp = loadShaderProgram("./ship.vert", "./ship.frag");460 462 GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag"); 461 463 462 shaderBufferInfo[ship_sp] = BufferInfo();463 464 shaderBufferInfo[laser_sp] = BufferInfo(); 465 466 modelGroups[TYPE_SHIP] = createModelGroup( 467 loadShaderProgram("./ship.vert", "./ship.frag")); 468 shaderBufferInfo[modelGroups[TYPE_SHIP].shaderProgram] = BufferInfo(); // temporary 464 469 465 470 modelGroups[TYPE_ASTEROID] = createModelGroup( 466 471 loadShaderProgram("./asteroid.vert", "./asteroid.frag")); 467 472 shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary 473 468 474 modelGroups[TYPE_EXPLOSION] = createModelGroup( 469 475 loadShaderProgram("./explosion.vert", "./explosion.frag")); … … 475 481 476 482 // player ship 477 SceneObject* ship = createShip( ship_sp);483 SceneObject* ship = createShip(modelGroups[TYPE_SHIP].shaderProgram); 478 484 objects.push_back(ship); 479 485 … … 499 505 model_mat_idx_vbo); 500 506 501 GLuint ship_vao = 0; 502 glGenVertexArrays(1, &ship_vao); 503 glBindVertexArray(ship_vao); 507 glBindVertexArray(modelGroups[TYPE_SHIP].vao); 504 508 505 509 glEnableVertexAttribArray(0); … … 635 639 GLuint ub_binding_point = 0; 636 640 637 GLuint ship_view_mat_loc = glGetUniformLocation( ship_sp, "view");638 GLuint ship_proj_mat_loc = glGetUniformLocation( ship_sp, "proj");639 GLuint ship_sp_models_ub_index = glGetUniformBlockIndex( ship_sp, "models");641 GLuint ship_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "view"); 642 GLuint ship_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_SHIP].shaderProgram, "proj"); 643 GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_SHIP].shaderProgram, "models"); 640 644 641 645 GLuint asteroid_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_ASTEROID].shaderProgram, "view"); … … 653 657 654 658 655 glUseProgram( ship_sp);659 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); 656 660 glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 657 661 glUniformMatrix4fv(ship_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 658 662 659 glUniformBlockBinding( ship_sp, ship_sp_models_ub_index, ub_binding_point);663 glUniformBlockBinding(modelGroups[TYPE_SHIP].shaderProgram, ship_sp_models_ub_index, ub_binding_point); 660 664 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 661 665 … … 946 950 //printVector("cam pos", cam_pos); 947 951 948 glUseProgram( ship_sp);952 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); 949 953 glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 950 954 … … 969 973 case STATE_GAME: 970 974 renderScene(shaderBufferInfo, modelGroups, ubo, 971 ship_sp, laser_sp, 972 ship_vao, laser_vao); 975 laser_sp, laser_vao); 973 976 renderSceneGui(); 974 977 break; … … 1333 1336 BufferInfo* bufferInfo = &shaderBufferInfo[obj->shader_program]; 1334 1337 1335 unsigned int numPoints = obj->type == TYPE_ASTEROID || obj->type == TYPE_EXPLOSION ? 1338 unsigned int numPoints = 1339 obj->type == TYPE_SHIP || 1340 obj->type == TYPE_ASTEROID || 1341 obj->type == TYPE_EXPLOSION ? 1336 1342 modelGroups[obj->type].numPoints : 1337 1343 bufferInfo->vbo_offset; … … 2083 2089 shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2; 2084 2090 2085 if (shaderCountIt->first != modelGroups[TYPE_ASTEROID].shaderProgram && 2091 if (shaderCountIt->first != modelGroups[TYPE_SHIP].shaderProgram && 2092 shaderCountIt->first != modelGroups[TYPE_ASTEROID].shaderProgram && 2086 2093 shaderCountIt->first != modelGroups[TYPE_EXPLOSION].shaderProgram) { 2087 2094 shaderBufferInfo[shaderCountIt->first].vbo_offset = 0; … … 2096 2103 } 2097 2104 2098 modelGroups[TYPE_ASTEROID].numPoints = 0; 2099 modelGroups[TYPE_EXPLOSION].numPoints = 0; 2105 map<ObjectType, ShaderModelGroup>::iterator modelGroupIt; 2106 for (modelGroupIt = modelGroups.begin(); modelGroupIt != modelGroups.end(); modelGroupIt++) { 2107 if (modelGroupIt->first == TYPE_SHIP || 2108 modelGroupIt->first == TYPE_ASTEROID || 2109 modelGroupIt->first == TYPE_EXPLOSION) { 2110 modelGroups[modelGroupIt->first].numPoints = 0; 2111 } 2112 } 2100 2113 2101 2114 // Allocate all the buffers using the counts calculated above … … 2141 2154 BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program]; 2142 2155 2143 if (obj.type == TYPE_ASTEROID || obj.type == TYPE_EXPLOSION) { 2156 if (obj.type == TYPE_SHIP || 2157 obj.type == TYPE_ASTEROID || 2158 obj.type == TYPE_EXPLOSION) { 2144 2159 obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints; 2145 2160 } else { … … 2186 2201 } 2187 2202 2188 if (obj.type == TYPE_ASTEROID || obj.type == TYPE_EXPLOSION) { 2203 if (obj.type == TYPE_SHIP || 2204 obj.type == TYPE_ASTEROID || 2205 obj.type == TYPE_EXPLOSION) { 2189 2206 modelGroups[obj.type].numPoints += obj.num_points; 2190 2207 } else { … … 2356 2373 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 2357 2374 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo, 2358 GLuint ship_sp, GLuint laser_sp, 2359 GLuint ship_vao, GLuint laser_vao) { 2360 2361 glUseProgram(ship_sp); 2362 glBindVertexArray(ship_vao); 2363 2364 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[ship_sp].vbo_base, shaderBufferInfo[ship_sp].vbo_offset); 2375 GLuint laser_sp, GLuint laser_vao) { 2376 2377 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); 2378 glBindVertexArray(modelGroups[TYPE_SHIP].vao); 2379 2380 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[modelGroups[TYPE_SHIP].shaderProgram].vbo_base, modelGroups[TYPE_SHIP].numPoints); 2365 2381 2366 2382 glUseProgram(modelGroups[TYPE_ASTEROID].shaderProgram);
Note:
See TracChangeset
for help on using the changeset viewer.