Changeset b62c109 in opengl-game for new-game.cpp
- Timestamp:
- Feb 11, 2019, 4:01:08 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- dd9771c
- Parents:
- 4c7cd57
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r4c7cd57 rb62c109 106 106 struct BufferInfo { 107 107 unsigned int vbo_base; 108 unsigned int vbo_offset;109 108 unsigned int vbo_capacity; 110 109 unsigned int ubo_base; … … 216 215 217 216 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 218 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo, 219 GLuint laser_sp, GLuint laser_vao); 217 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo); 220 218 221 219 void renderSceneGui(); … … 460 458 map<ObjectType, ShaderModelGroup> modelGroups; 461 459 462 GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");463 464 shaderBufferInfo[laser_sp] = BufferInfo();465 466 460 modelGroups[TYPE_SHIP] = createModelGroup( 467 461 loadShaderProgram("./ship.vert", "./ship.frag")); … … 471 465 loadShaderProgram("./asteroid.vert", "./asteroid.frag")); 472 466 shaderBufferInfo[modelGroups[TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary 467 468 modelGroups[TYPE_LASER] = createModelGroup( 469 loadShaderProgram("./laser.vert", "./laser.frag")); 470 shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram] = BufferInfo(); // temporary 473 471 474 472 modelGroups[TYPE_EXPLOSION] = createModelGroup( … … 544 542 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL); 545 543 546 GLuint laser_vao = 0; 547 glGenVertexArrays(1, &laser_vao); 548 glBindVertexArray(laser_vao); 544 glBindVertexArray(modelGroups[TYPE_LASER].vao); 549 545 550 546 glEnableVertexAttribArray(0); … … 647 643 GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_ASTEROID].shaderProgram, "models"); 648 644 649 GLuint laser_view_mat_loc = glGetUniformLocation( laser_sp, "view");650 GLuint laser_proj_mat_loc = glGetUniformLocation( laser_sp, "proj");651 GLuint laser_color_loc = glGetUniformLocation( laser_sp, "laser_color");652 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex( laser_sp, "models");645 GLuint laser_view_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "view"); 646 GLuint laser_proj_mat_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "proj"); 647 GLuint laser_color_loc = glGetUniformLocation(modelGroups[TYPE_LASER].shaderProgram, "laser_color"); 648 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[TYPE_LASER].shaderProgram, "models"); 653 649 654 650 GLuint explosion_start_time_loc = glGetUniformLocation(modelGroups[TYPE_EXPLOSION].shaderProgram, "explosion_start_time"); … … 673 669 674 670 675 glUseProgram( laser_sp);671 glUseProgram(modelGroups[TYPE_LASER].shaderProgram); 676 672 glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 677 673 glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 678 674 glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f); 679 675 680 glUniformBlockBinding( laser_sp, laser_sp_models_ub_index, ub_binding_point);676 glUniformBlockBinding(modelGroups[TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point); 681 677 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 682 678 … … 812 808 813 809 leftLaser = createLaser(vec3(-0.21f, -1.19f, 1.76f)+offset, vec3(-0.21f, -1.19f, -3.0f)+offset, 814 vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);810 vec3(0.0f, 1.0f, 0.0f), 0.03f, modelGroups[TYPE_LASER].shaderProgram); 815 811 addObjectToScene(leftLaser, shaderBufferInfo, modelGroups, 816 812 points_vbo, … … 828 824 829 825 rightLaser = createLaser(vec3(0.21f, -1.19f, 1.76f) + offset, vec3(0.21f, -1.19f, -3.0f) + offset, 830 vec3(0.0f, 1.0f, 0.0f), 0.03f, laser_sp);826 vec3(0.0f, 1.0f, 0.0f), 0.03f, modelGroups[TYPE_LASER].shaderProgram); 831 827 addObjectToScene(rightLaser, shaderBufferInfo, modelGroups, 832 828 points_vbo, … … 953 949 glUniformMatrix4fv(ship_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 954 950 955 glUseProgram( laser_sp);951 glUseProgram(modelGroups[TYPE_LASER].shaderProgram); 956 952 glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 957 953 … … 972 968 break; 973 969 case STATE_GAME: 974 renderScene(shaderBufferInfo, modelGroups, ubo, 975 laser_sp, laser_vao); 970 renderScene(shaderBufferInfo, modelGroups, ubo); 976 971 renderSceneGui(); 977 972 break; … … 1336 1331 BufferInfo* bufferInfo = &shaderBufferInfo[obj->shader_program]; 1337 1332 1338 unsigned int numPoints =1339 obj->type == TYPE_SHIP ||1340 obj->type == TYPE_ASTEROID ||1341 obj->type == TYPE_EXPLOSION ?1342 modelGroups[obj->type].numPoints :1343 bufferInfo->vbo_offset;1344 1345 1333 // Check if the buffers aren't large enough to fit the new object and, if so, call 1346 1334 // populateBuffers() to resize and repopupulate them 1347 if (bufferInfo->vbo_capacity < ( numPoints + obj->num_points) ||1335 if (bufferInfo->vbo_capacity < (modelGroups[obj->type].numPoints + obj->num_points) || 1348 1336 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1349 1337 … … 2089 2077 shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2; 2090 2078 2091 if (shaderCountIt->first != modelGroups[TYPE_SHIP].shaderProgram &&2092 shaderCountIt->first != modelGroups[TYPE_ASTEROID].shaderProgram &&2093 shaderCountIt->first != modelGroups[TYPE_EXPLOSION].shaderProgram) {2094 shaderBufferInfo[shaderCountIt->first].vbo_offset = 0;2095 }2096 2079 shaderBufferInfo[shaderCountIt->first].ubo_offset = 0; 2097 2080 … … 2105 2088 map<ObjectType, ShaderModelGroup>::iterator modelGroupIt; 2106 2089 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 } 2090 modelGroups[modelGroupIt->first].numPoints = 0; 2112 2091 } 2113 2092 … … 2154 2133 BufferInfo* bufferInfo = &shaderBufferInfo[obj.shader_program]; 2155 2134 2156 if (obj.type == TYPE_SHIP || 2157 obj.type == TYPE_ASTEROID || 2158 obj.type == TYPE_EXPLOSION) { 2159 obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints; 2160 } else { 2161 obj.vertex_vbo_offset = bufferInfo->vbo_base + bufferInfo->vbo_offset; 2162 } 2163 2135 obj.vertex_vbo_offset = bufferInfo->vbo_base + modelGroups[obj.type].numPoints; 2164 2136 obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset; 2165 2137 … … 2201 2173 } 2202 2174 2203 if (obj.type == TYPE_SHIP || 2204 obj.type == TYPE_ASTEROID || 2205 obj.type == TYPE_EXPLOSION) { 2206 modelGroups[obj.type].numPoints += obj.num_points; 2207 } else { 2208 bufferInfo->vbo_offset += obj.num_points; 2209 } 2175 modelGroups[obj.type].numPoints += obj.num_points; 2210 2176 bufferInfo->ubo_offset++; 2211 2177 } … … 2372 2338 2373 2339 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 2374 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo, 2375 GLuint laser_sp, GLuint laser_vao) { 2340 map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) { 2376 2341 2377 2342 glUseProgram(modelGroups[TYPE_SHIP].shaderProgram); … … 2387 2352 glEnable(GL_BLEND); 2388 2353 2389 glUseProgram( laser_sp);2390 glBindVertexArray( laser_vao);2391 2392 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[ laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);2354 glUseProgram(modelGroups[TYPE_LASER].shaderProgram); 2355 glBindVertexArray(modelGroups[TYPE_LASER].vao); 2356 2357 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[modelGroups[TYPE_LASER].shaderProgram].vbo_base, modelGroups[TYPE_LASER].numPoints); 2393 2358 2394 2359 glUseProgram(modelGroups[TYPE_EXPLOSION].shaderProgram);
Note:
See TracChangeset
for help on using the changeset viewer.