Changeset c5fb958 in opengl-game
- Timestamp:
- Jan 18, 2019, 2:35:11 PM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- bebfd5c
- Parents:
- fe5e3ca
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
explosion.vert
rfe5e3ca rc5fb958 4 4 5 5 uniform mat4 view, proj; 6 uniform mat4 model_mat;7 6 8 7 /* … … 11 10 }; 12 11 */ 12 uniform mat4 model_mat; 13 13 14 14 uniform float explosion_start_time; … … 43 43 p += normalize(v_i) * mod(t, duration) / duration * 0.3; // allow time to loop around so particle emitter keeps going 44 44 45 //gl_Position = proj * view * model_mats[ 0] * vec4(p, 1.0);45 //gl_Position = proj * view * model_mats[ubo_index] * vec4(p, 1.0); 46 46 gl_Position = proj * view * model_mat * vec4(p, 1.0); 47 47 gl_PointSize = 15.0; // size in pixels -
new-game.cpp
rfe5e3ca rc5fb958 50 50 TYPE_ASTEROID, 51 51 TYPE_LASER, 52 TYPE_EXPLOSION, 52 53 }; 53 54 … … 143 144 GLuint ubo, 144 145 GLuint model_mat_idx_vbo, 145 GLuint asteroid_sp); 146 GLuint asteroid_sp, 147 GLuint explosion_sp); 146 148 void removeObjectFromScene(SceneObject& obj, GLuint ubo); 147 149 … … 157 159 GLuint* model_mat_idx_vbo); 158 160 159 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp); 161 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp, 162 map<GLuint, BufferInfo>& shaderBufferInfo, 163 GLuint points_vbo, 164 GLuint colors_vbo, 165 GLuint selected_colors_vbo, 166 GLuint texcoords_vbo, 167 GLuint normals_vbo, 168 GLuint ubo, 169 GLuint model_mat_idx_vbo); 160 170 161 171 void populateBuffers(vector<SceneObject*>& objects, … … 168 178 GLuint ubo, 169 179 GLuint model_mat_idx_vbo, 170 GLuint asteroid_sp); 180 GLuint asteroid_sp, 181 GLuint explosion_sp); 171 182 172 183 void copyObjectDataToBuffers(SceneObject& obj, … … 183 194 void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo); 184 195 196 // instead of using these methods, create constructors for these 185 197 SceneObject* createShip(GLuint shader); 186 198 Asteroid* createAsteroid(vec3 pos, GLuint shader); 187 199 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp); 200 SceneObject* createExplosion(GLuint shader); 188 201 189 202 void translateLaser(Laser* laser, const vec3& translation, GLuint ubo); … … 199 212 GLuint colors_vbo, GLuint selected_colors_vbo, 200 213 SceneObject* selectedObject); 201 void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex);202 214 203 215 void renderSceneGui(); … … 248 260 Laser* rightLaser = NULL; 249 261 EffectOverTime* rightLaserEffect = NULL; 262 263 SceneObject* objExplosion; 250 264 251 265 /* … … 450 464 shaderBufferInfo[texture_sp] = BufferInfo(); 451 465 shaderBufferInfo[laser_sp] = BufferInfo(); 466 shaderBufferInfo[explosion_sp] = BufferInfo(); 452 467 453 468 cam_pos = vec3(0.0f, 0.0f, 2.0f); … … 482 497 ubo, 483 498 model_mat_idx_vbo, 484 asteroid_sp); 499 asteroid_sp, 500 explosion_sp); 485 501 486 502 GLuint color_vao = 0; … … 494 510 495 511 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 496 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);512 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 497 513 498 514 // Comment these two lines out when I want to use selected colors 499 515 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 500 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);516 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 501 517 502 518 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 503 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);519 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL); 504 520 505 521 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 506 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);522 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL); 507 523 508 524 GLuint asteroid_vao = 0; … … 516 532 517 533 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 518 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);534 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 519 535 520 536 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 521 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);537 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 522 538 523 539 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 524 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);540 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL); 525 541 526 542 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 527 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);543 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL); 528 544 529 545 GLuint texture_vao = 0; … … 537 553 538 554 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 539 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);555 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 540 556 541 557 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 542 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);558 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); 543 559 544 560 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 545 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);561 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL); 546 562 547 563 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 548 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);564 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, NULL); 549 565 550 566 GLuint laser_vao = 0; … … 557 573 558 574 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 559 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);575 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 560 576 561 577 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 562 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);578 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); 563 579 564 580 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 565 glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, 0);581 glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL); 566 582 567 583 float cam_speed = 1.0f; … … 597 613 proj_mat = make_mat4(proj_arr); 598 614 599 GLuint explosion_vao = initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat, explosion_sp); 615 GLuint explosion_vao = initializeParticleEffectBuffers(vec3(0.0f, -1.2f, 0.65f), proj_mat, view_mat, 616 explosion_sp, 617 shaderBufferInfo, 618 points_vbo, 619 colors_vbo, 620 selected_colors_vbo, 621 texcoords_vbo, 622 normals_vbo, 623 ubo, 624 model_mat_idx_vbo); 625 626 /* TODO: Fix the UBO binding code based on the following forum post (in order to support multiple ubos): 627 628 No, you're misunderstanding how this works. UBO binding works exactly like texture object binding. 629 630 The OpenGL context has a number of slots for binding UBOs. There are GL_MAX_UNIFORM_BUFFER_BINDINGS number of 631 slots for UBO binding. 632 633 Uniform Blocks in a program can be set to use one of the slots in the context. You do this by first querying 634 the block index using the block name (glGetUniformBlockIndex). This is similar to how you need to use 635 glGetUniformLocation in order to set a uniform's value with glUniform. Block indices, like uniform locations, 636 are specific to a program. 637 638 Once you have the block index, you use glUniformBlockBinding to set that specific program to use a particular 639 uniform buffer slot in the context. 640 641 Let's say you have a global UBO that you want to use for every program. To make using it easier, you want to 642 bind it just once. 643 644 So first, you pick a uniform buffer slot in the context, one that always will refer to this UBO. Let's say 645 you pick slot 8. 646 647 When you build a program object that may use this global uniform buffer, what you do is quite simple. First, 648 after linking the program, call glGetUniformBlockIndex(program, "NameOfGlobalUniformBlock"). If you get back 649 GL_INVALID_INDEX, then you know that the global uniform block isn't used in that program. Otherwise you get 650 back a block index. 651 652 If you got a valid block index, then you call glUniformBlockBinding(program, uniformBlockIndex, 8). Remember 653 that 8 is the uniform buffer context slot that we selected earlier. This causes this particular program to 654 use uniform buffer slot #8 to find the buffer for "NameOfGlobalUniformBlock". 655 656 Finally, to set the actual buffer in the context, call glBindBufferRange(GL_UNIFORM_BUFFER, 8, 657 bufferObjectName, offset, size); 658 */ 600 659 601 660 GLuint ub_binding_point = 0; … … 621 680 GLuint explosion_start_time_loc = glGetUniformLocation(explosion_sp, "explosion_start_time"); 622 681 GLuint cur_time_loc = glGetUniformLocation(explosion_sp, "cur_time"); 682 //GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(explosion_sp, "models"); 623 683 624 684 … … 654 714 glUniformBlockBinding(laser_sp, laser_sp_models_ub_index, ub_binding_point); 655 715 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 716 717 718 /* 719 glUseProgram(explosion_sp); 720 glUniformBlockBinding(explosion_sp, explosion_sp_models_ub_index, ub_binding_point); 721 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 722 */ 656 723 657 724 … … 732 799 ubo, 733 800 model_mat_idx_vbo, 734 asteroid_sp); 801 asteroid_sp, 802 explosion_sp); 735 803 736 804 elapsed_seconds_spawn -= 0.5f; … … 792 860 ubo, 793 861 model_mat_idx_vbo, 794 asteroid_sp); 862 asteroid_sp, 863 explosion_sp); 795 864 } else if (key_state[GLFW_KEY_Z] == GLFW_RELEASE) { 796 865 removeObjectFromScene(*leftLaser, ubo); … … 810 879 ubo, 811 880 model_mat_idx_vbo, 812 asteroid_sp); 881 asteroid_sp, 882 explosion_sp); 813 883 } else if (key_state[GLFW_KEY_X] == GLFW_RELEASE) { 814 884 removeObjectFromScene(*rightLaser, ubo); … … 826 896 } 827 897 if (((Asteroid*)objects[i])->hp <= 0) { 898 printVector("center", objects[i]->bounding_center); 899 900 // TODO: Optimize this so I don't recalculate the camera rotation every time 901 float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f; 902 mat4 pitch_mat = rotate(mat4(1.0f), cam_pitch, vec3(1.0f, 0.0f, 0.0f)); 903 mat4 model_mat = translate(mat4(1.0f), objects[i]->bounding_center + vec3(0.0f, 0.0f, 0.0f)) * pitch_mat; 904 828 905 removeObjectFromScene(*objects[i], ubo); 829 906 score++; 830 907 831 // render an explosion 908 objExplosion->model_mat = model_mat; 909 910 // initiate an explosion 832 911 glUseProgram(explosion_sp); 912 913 GLuint model_mat_loc = glGetUniformLocation(explosion_sp, "model_mat"); 914 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objExplosion->model_mat)); 915 916 // TODO: To get ubos working for the explosion, 917 // I need to calculate the correct ubo index for the model matrix 918 // Then I need to copy the matrix into the right place in the ubo and 919 // copy the index into the correct uniform 920 921 // STEP 1: Make sure the UBO offset for the explosion shader is correct 922 923 cout << "UBO OFFSET: " << objExplosion->ubo_offset << endl; 924 //glBindBuffer(GL_UNIFORM_BUFFER, ubo); 925 //glBufferSubData(GL_UNIFORM_BUFFER, objExplosion->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(objExplosion->model_mat)); 926 833 927 glUniform1f(explosion_start_time_loc, (GLfloat)glfwGetTime()); 834 cout << "REMOVED" << endl;835 cout << i << endl;836 928 } 837 929 } … … 1288 1380 } 1289 1381 1290 if (obj->type != TYPE_LASER) {1382 if (obj->type == TYPE_SHIP || obj->type == TYPE_ASTEROID) { 1291 1383 calculateObjectBoundingBox(obj); 1292 1384 … … 1304 1396 GLuint ubo, 1305 1397 GLuint model_mat_idx_vbo, 1306 GLuint asteroid_sp) { 1398 GLuint asteroid_sp, 1399 GLuint explosion_sp) { 1307 1400 objects.push_back(obj); 1308 1401 … … 1311 1404 // Check if the buffers aren't large enough to fit the new object and, if so, call 1312 1405 // populateBuffers() to resize and repopupulate them 1313 if (bufferInfo->vbo_capacity < (bufferInfo-> ubo_offset + obj->num_points) ||1406 if (bufferInfo->vbo_capacity < (bufferInfo->vbo_offset + obj->num_points) || 1314 1407 bufferInfo->ubo_capacity < (bufferInfo->ubo_offset + 1)) { 1315 1408 … … 1329 1422 ubo, 1330 1423 model_mat_idx_vbo, 1331 asteroid_sp); 1424 asteroid_sp, 1425 explosion_sp); 1332 1426 } else { 1333 1427 copyObjectDataToBuffers(*objects.back(), shaderBufferInfo, … … 1805 1899 */ 1806 1900 // TODO: Make the color parameter have an effect 1807 // TODO: Come up with a better way of passing the object back than copying it1808 1901 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1809 1902 Laser* obj = new Laser(); … … 1912 2005 } 1913 2006 1914 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp) { 2007 GLuint initializeParticleEffectBuffers(vec3 origin, mat4 proj, mat4 view, GLuint explosion_sp, 2008 map<GLuint, BufferInfo>& shaderBufferInfo, 2009 GLuint points_vbo, 2010 GLuint colors_vbo, 2011 GLuint selected_colors_vbo, 2012 GLuint texcoords_vbo, 2013 GLuint normals_vbo, 2014 GLuint ubo, 2015 GLuint model_mat_idx_vbo) { 1915 2016 //unsigned int num_explosions = 1; 1916 2017 … … 1969 2070 glBindVertexArray(vao); 1970 2071 2072 glEnableVertexAttribArray(0); 2073 glEnableVertexAttribArray(1); 2074 glEnableVertexAttribArray(2); 2075 1971 2076 glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo); 1972 2077 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); … … 1975 2080 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL); 1976 2081 1977 glEnableVertexAttribArray(0); 1978 glEnableVertexAttribArray(1); 2082 // TODO: I think I can call glVertexAttribIPointer whenever the vbo_base changes 2083 // to rebind the ubo index vbo 2084 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 2085 glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, NULL); 2086 2087 objExplosion = createExplosion(explosion_sp); 2088 objExplosion->num_points = EXPLOSION_PARTICLE_COUNT; 2089 addObjectToScene(objExplosion, shaderBufferInfo, 2090 points_vbo, 2091 colors_vbo, 2092 selected_colors_vbo, 2093 texcoords_vbo, 2094 normals_vbo, 2095 ubo, 2096 model_mat_idx_vbo, 2097 0, 2098 explosion_sp); 1979 2099 1980 2100 return vao; … … 1990 2110 GLuint ubo, 1991 2111 GLuint ubo_idx_vbo, 1992 GLuint asteroid_sp) { 2112 GLuint asteroid_sp, 2113 GLuint explosion_sp) { 1993 2114 GLsizeiptr num_points = 0; 1994 2115 GLsizeiptr num_objects = 0; … … 1996 2117 map<GLuint, unsigned int> shaderCounts; 1997 2118 map<GLuint, unsigned int> shaderUboCounts; 2119 2120 map<GLuint, BufferInfo>::iterator shaderIt; 2121 2122 for (shaderIt = shaderBufferInfo.begin(); shaderIt != shaderBufferInfo.end(); shaderIt++) { 2123 shaderCounts[shaderIt->first] = 0; 2124 shaderUboCounts[shaderIt->first] = 0; 2125 } 1998 2126 1999 2127 vector<SceneObject*>::iterator it; … … 2013 2141 num_objects++; 2014 2142 2015 if (shaderCounts.count((*it)->shader_program) == 0) { 2016 shaderCounts[(*it)->shader_program] = (*it)->num_points; 2017 shaderUboCounts[(*it)->shader_program] = 1; 2018 } else { 2019 shaderCounts[(*it)->shader_program] += (*it)->num_points; 2020 shaderUboCounts[(*it)->shader_program]++; 2021 } 2143 shaderCounts[(*it)->shader_program] += (*it)->num_points; 2144 shaderUboCounts[(*it)->shader_program]++; 2022 2145 2023 2146 it++; … … 2029 2152 num_objects *= 2; 2030 2153 2031 map<GLuint, unsigned int>::iterator shader It;2154 map<GLuint, unsigned int>::iterator shaderCountIt; 2032 2155 unsigned int lastShaderCount = 0; 2033 2156 unsigned int lastShaderUboCount = 0; … … 2040 2163 * object being added. 2041 2164 */ 2042 for (shader It = shaderCounts.begin(); shaderIt != shaderCounts.end(); shaderIt++) {2165 for (shaderCountIt = shaderCounts.begin(); shaderCountIt != shaderCounts.end(); shaderCountIt++) { 2043 2166 // When populating the buffers, leave as much empty space as space taken up by existing objects 2044 2167 // to allow new objects to be added without immediately having to resize the buffers 2045 shaderBufferInfo[shaderIt->first].vbo_base = lastShaderCount * 2; 2046 shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2; 2168 shaderBufferInfo[shaderCountIt->first].vbo_base = lastShaderCount * 2; 2169 shaderBufferInfo[shaderCountIt->first].ubo_base = lastShaderUboCount * 2; 2170 2171 if (shaderCountIt->first == explosion_sp) { 2172 cout << "EXPLOSION VBO_BASE: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl; 2173 } 2047 2174 2048 2175 /* 2049 cout << "shader: " << shader It->first << endl;2050 cout << "point counts: " << shaderCounts[shader It->first] << endl;2051 cout << "object counts: " << shaderUboCounts[shader It->first] << endl;2052 cout << "vbo_base: " << shaderBufferInfo[shader It->first].vbo_base << endl;2053 cout << "ubo_base: " << shaderBufferInfo[shader It->first].ubo_base << endl;2176 cout << "shader: " << shaderCountIt->first << endl; 2177 cout << "point counts: " << shaderCounts[shaderCountIt->first] << endl; 2178 cout << "object counts: " << shaderUboCounts[shaderCountIt->first] << endl; 2179 cout << "vbo_base: " << shaderBufferInfo[shaderCountIt->first].vbo_base << endl; 2180 cout << "ubo_base: " << shaderBufferInfo[shaderCountIt->first].ubo_base << endl; 2054 2181 */ 2055 2182 2056 shaderBufferInfo[shader It->first].vbo_offset = 0;2057 shaderBufferInfo[shader It->first].ubo_offset = 0;2058 2059 shaderBufferInfo[shader It->first].vbo_capacity = shaderCounts[shaderIt->first] * 2;2060 shaderBufferInfo[shader It->first].ubo_capacity = shaderUboCounts[shaderIt->first] * 2;2061 2062 lastShaderCount += shaderCounts[shader It->first];2063 lastShaderUboCount += shaderUboCounts[shader It->first];2183 shaderBufferInfo[shaderCountIt->first].vbo_offset = 0; 2184 shaderBufferInfo[shaderCountIt->first].ubo_offset = 0; 2185 2186 shaderBufferInfo[shaderCountIt->first].vbo_capacity = shaderCounts[shaderCountIt->first] * 2; 2187 shaderBufferInfo[shaderCountIt->first].ubo_capacity = shaderUboCounts[shaderCountIt->first] * 2; 2188 2189 lastShaderCount += shaderCounts[shaderCountIt->first]; 2190 lastShaderUboCount += shaderUboCounts[shaderCountIt->first]; 2064 2191 } 2065 2192 … … 2115 2242 obj.ubo_offset = bufferInfo->ubo_base + bufferInfo->ubo_offset; 2116 2243 2117 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 2118 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]); 2119 2120 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 2121 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]); 2122 2244 if (obj.type == TYPE_EXPLOSION) { 2245 cout << "UBO OFFSET: " << obj.ubo_offset << endl; 2246 } 2123 2247 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 2124 2248 for (unsigned int i = 0; i < obj.num_points; i++) { … … 2126 2250 } 2127 2251 2128 if (obj.type != TYPE_LASER) { 2129 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 2130 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]); 2131 2132 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 2133 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]); 2134 2135 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 2136 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]); 2137 } 2138 2139 obj.model_mat = obj.model_transform * obj.model_base; 2140 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 2141 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 2142 2143 if (obj.type == TYPE_ASTEROID) { 2144 glUseProgram(asteroid_sp); 2145 2146 ostringstream oss; 2147 oss << "hp[" << obj.ubo_offset << "]"; 2148 glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), ((Asteroid*)&obj)->hp); 2252 if (obj.type != TYPE_EXPLOSION) { 2253 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 2254 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]); 2255 2256 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 2257 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]); 2258 2259 if (obj.type != TYPE_LASER) { 2260 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 2261 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]); 2262 2263 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 2264 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]); 2265 2266 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 2267 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]); 2268 } 2269 2270 obj.model_mat = obj.model_transform * obj.model_base; 2271 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 2272 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 2273 2274 if (obj.type == TYPE_ASTEROID) { 2275 glUseProgram(asteroid_sp); 2276 2277 ostringstream oss; 2278 oss << "hp[" << obj.ubo_offset << "]"; 2279 glUniform1f(glGetUniformLocation(asteroid_sp, oss.str().c_str()), ((Asteroid*)&obj)->hp); 2280 } 2149 2281 } 2150 2282 … … 2325 2457 if (selectedObject != NULL) { 2326 2458 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 2327 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);2459 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 2328 2460 2329 2461 glDrawArrays(GL_TRIANGLES, selectedObject->vertex_vbo_offset, selectedObject->num_points); … … 2333 2465 // Uncomment this code when I want to use selected colors again 2334 2466 // glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 2335 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);2467 // glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 2336 2468 2337 2469 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[color_sp].vbo_base, shaderBufferInfo[color_sp].vbo_offset); … … 2354 2486 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset); 2355 2487 2356 glDisable(GL_BLEND); 2357 2358 renderExplosion(explosion_sp, explosion_vao, 0); 2359 } 2360 2361 void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex) { 2362 glEnable(GL_BLEND); 2488 glUseProgram(explosion_sp); 2489 2490 //glActiveTexture(GL_TEXTURE1); 2491 //glBindTexture(GL_TEXTURE_2D, tex); // tex is 0 here 2492 2363 2493 glEnable(GL_PROGRAM_POINT_SIZE); 2364 2365 //glActiveTexture(GL_TEXTURE1);2366 //glBindTexture(GL_TEXTURE_2D, tex);2367 glUseProgram(explosion_sp);2368 2369 2494 glBindVertexArray(explosion_vao); 2370 2495 2371 glDrawArrays(GL_POINTS, 0, EXPLOSION_PARTICLE_COUNT); 2496 glDrawArrays(GL_POINTS, 0, shaderBufferInfo[explosion_sp].vbo_offset); 2497 //glDrawArrays(GL_POINTS, shaderBufferInfo[explosion_sp].vbo_base, shaderBufferInfo[explosion_sp].vbo_offset); 2498 //cout << shaderBufferInfo[explosion_sp].vbo_base << ", " << shaderBufferInfo[explosion_sp].vbo_offset << endl; 2372 2499 2373 2500 glDisable(GL_PROGRAM_POINT_SIZE); … … 2474 2601 Asteroid* obj = new Asteroid(); 2475 2602 obj->type = TYPE_ASTEROID; 2603 obj->shader_program = shader; 2476 2604 obj->hp = 10.0f; 2477 obj->shader_program = shader;2478 2605 2479 2606 obj->points = { … … 2595 2722 } 2596 2723 2724 SceneObject* createExplosion(GLuint shader) { 2725 SceneObject* obj = new SceneObject(); 2726 obj->type = TYPE_EXPLOSION; 2727 obj->shader_program = shader; 2728 2729 obj->points = {}; 2730 obj->colors = {}; 2731 2732 initObject(obj); 2733 2734 return obj; 2735 } 2736 2597 2737 float getRandomNum(float low, float high) { 2598 2738 return low + ((float)rand()/RAND_MAX) * (high-low);
Note:
See TracChangeset
for help on using the changeset viewer.