Changeset 14ff67c in opengl-game
- Timestamp:
- May 24, 2018, 2:35:35 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 4f3262f
- Parents:
- e165b85
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
color.vert
re165b85 r14ff67c 1 1 #version 410 2 2 3 uniform mat4 model, view, proj; 3 #define MAX_NUM_OBJECTS 1024 4 4 5 layout (std140) uniform shader_data { 6 mat4 model_mat; 5 uniform mat4 view, proj; 6 7 layout (std140) uniform models { 8 mat4 model_mats[MAX_NUM_OBJECTS]; 7 9 }; 8 10 … … 10 12 layout(location = 1) in vec3 vertex_color; 11 13 layout(location = 2) in vec3 vertex_normal; 14 layout(location = 3) in uint ubo_index; 12 15 13 16 out vec3 position_eye, normal_eye, color, light_position_eye; … … 17 20 18 21 void main() { 19 position_eye = vec3(view * model * vec4(vertex_position, 1.0));20 normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0));22 position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0)); 23 normal_eye = vec3(view * model_mats[ubo_index] * vec4 (vertex_normal, 0.0)); 21 24 color = vertex_color; 22 25 light_position_eye = vec3(view * vec4(light_position_world, 1.0)); -
new-game.cpp
re165b85 r14ff67c 63 63 64 64 const bool FULLSCREEN = false; 65 const bool SHOW_FPS = false; 66 const bool DISABLE_VSYNC = true; 67 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime 68 65 69 int width = 640; 66 70 int height = 480; … … 103 107 GLuint color_sp, GLuint texture_sp, 104 108 GLuint vao1, GLuint vao2, 105 GLuint shader1_mat_loc, GLuint shader2_mat_loc,106 109 GLuint points_vbo, GLuint normals_vbo, 107 110 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, … … 210 213 glewExperimental = GL_TRUE; 211 214 glewInit(); 215 216 /* 217 * RENDERING ALGORITHM NOTES: 218 * 219 * Basically, I need to split my objects into groups, so that each group fits into 220 * GL_MAX_UNIFORM_BLOCK_SIZE. I need to have an offset and a size for each group. 221 * Getting the offset is straitforward. The size may as well be GL_MAX_UNIFORM_BLOCK_SIZE 222 * for each group, since it seems that smaller sizes just round up to the nearest GL_MAX_UNIFORM_BLOCK_SIZE 223 * 224 * I'll need to have a loop inside my render loop that calls glBindBufferRange(GL_UNIFORM_BUFFER, ... 225 * for every 1024 objects and then draws all those objects with one glDraw call. 226 * 227 * Since I'm currently drawing all my objects dynamically (i.e switcing the shaders they use), 228 * I'll table the implementation of this algorithm until I have a reasonable number of objects always using the same shader 229 */ 230 231 GLint UNIFORM_BUFFER_OFFSET_ALIGNMENT, MAX_UNIFORM_BLOCK_SIZE; 232 glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UNIFORM_BUFFER_OFFSET_ALIGNMENT); 233 glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &MAX_UNIFORM_BLOCK_SIZE); 234 235 MAX_UNIFORMS = MAX_UNIFORM_BLOCK_SIZE / sizeof(mat4); 236 237 cout << "UNIFORM_BUFFER_OFFSET_ALIGNMENT: " << UNIFORM_BUFFER_OFFSET_ALIGNMENT << endl; 238 cout << "MAX_UNIFORMS: " << MAX_UNIFORMS << endl; 212 239 213 240 // Setup Dear ImGui binding … … 396 423 GLsizeiptr textures_buffer_size = 0; 397 424 GLsizeiptr ubo_buffer_size = 0; 425 GLsizeiptr model_mat_idx_buffer_size = 0; 398 426 399 427 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { … … 401 429 textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat); 402 430 ubo_buffer_size += 16 * sizeof(GLfloat); 431 model_mat_idx_buffer_size += obj_it->num_points * sizeof(GLuint); 403 432 } 404 433 … … 457 486 offset += obj_it->normals.size() * sizeof(GLfloat); 458 487 } 459 460 /* 488 glBindBuffer(GL_UNIFORM_BUFFER, 0); 489 461 490 GLuint ubo = 0; 462 491 glGenBuffers(1, &ubo); 463 464 //glBindBuffer(GL_UNIFORM_BUFFER, ubo); 465 //glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size/2, NULL, GL_DYNAMIC_DRAW); 492 glBindBuffer(GL_UNIFORM_BUFFER, ubo); 493 glBufferData(GL_UNIFORM_BUFFER, ubo_buffer_size, NULL, GL_DYNAMIC_DRAW); 466 494 467 495 offset = 0; 468 496 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 469 //glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(obj_it->model_mat), value_ptr(obj_it->model_mat)); 470 //glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(obj_it->model_mat), value_ptr(objects[0].model_mat)); 471 offset += 16 * sizeof(GLfloat); 472 } 473 //glBindBuffer(GL_UNIFORM_BUFFER, 0); 474 */ 497 glBufferSubData(GL_UNIFORM_BUFFER, offset, sizeof(obj_it->model_mat), value_ptr(obj_it->model_mat)); 498 offset += sizeof(obj_it->model_mat); 499 } 500 glBindBuffer(GL_UNIFORM_BUFFER, 0); 501 502 GLuint model_mat_idx_vbo = 0; 503 glGenBuffers(1, &model_mat_idx_vbo); 504 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 505 glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW); 506 507 offset = 0; 508 unsigned int idx = 0; 509 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 510 for (int i = 0; i < obj_it->num_points; i++) { 511 glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(GLuint), &idx); 512 offset += sizeof(GLuint); 513 } 514 idx++; 515 } 475 516 476 517 GLuint vao = 0; … … 481 522 glEnableVertexAttribArray(1); 482 523 glEnableVertexAttribArray(2); 524 glEnableVertexAttribArray(3); 483 525 484 526 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); … … 487 529 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 488 530 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); 531 532 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 533 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0); 489 534 490 535 GLuint vao2 = 0; … … 495 540 glEnableVertexAttribArray(1); 496 541 glEnableVertexAttribArray(2); 542 glEnableVertexAttribArray(3); 497 543 498 544 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); … … 504 550 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 505 551 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0); 552 553 glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo); 554 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0); 506 555 507 556 float speed = 1.0f; … … 538 587 proj_mat = make_mat4(proj_arr); 539 588 540 GLuint model_test_loc = glGetUniformLocation(color_sp, "model"); 589 GLuint ub_binding_point = 0; 590 541 591 GLuint view_test_loc = glGetUniformLocation(color_sp, "view"); 542 592 GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj"); 543 GLuint ub_index = glGetUniformBlockIndex(color_sp, "shader_data"); 544 cout << "UBO index: " << ub_index << endl; 545 546 GLuint model_mat_loc = glGetUniformLocation(texture_sp, "model"); 593 GLuint color_sp_ub_index = glGetUniformBlockIndex(color_sp, "models"); 594 547 595 GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view"); 548 596 GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj"); 597 GLuint texture_sp_ub_index = glGetUniformBlockIndex(texture_sp, "models"); 549 598 550 599 glUseProgram(color_sp); … … 552 601 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat)); 553 602 554 //glUniformBlockBinding(color_sp, ub_index, 0); 603 glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point); 604 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 555 605 556 606 glUseProgram(texture_sp); … … 558 608 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 559 609 610 glUniformBlockBinding(texture_sp, texture_sp_ub_index, ub_binding_point); 611 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 612 560 613 bool cam_moved = false; 561 614 … … 568 621 569 622 // disable vsync to see real framerate 570 //glfwSwapInterval(0); 623 if (DISABLE_VSYNC && SHOW_FPS) { 624 glfwSwapInterval(0); 625 } 571 626 572 627 State curState = STATE_MAIN_MENU; … … 577 632 previous_seconds = current_seconds; 578 633 579 elapsed_seconds_fps += elapsed_seconds; 580 if (elapsed_seconds_fps > 0.25f) { 581 fps = (double)frame_count / elapsed_seconds_fps; 582 cout << "FPS: " << fps << endl; 583 584 frame_count = 0; 585 elapsed_seconds_fps = 0.0f; 586 } 587 588 frame_count++; 634 if (SHOW_FPS) { 635 elapsed_seconds_fps += elapsed_seconds; 636 if (elapsed_seconds_fps > 0.25f) { 637 fps = (double)frame_count / elapsed_seconds_fps; 638 cout << "FPS: " << fps << endl; 639 640 frame_count = 0; 641 elapsed_seconds_fps = 0.0f; 642 } 643 644 frame_count++; 645 } 589 646 590 647 if (fabs(last_position) > 1.0f) { … … 638 695 color_sp, texture_sp, 639 696 vao, vao2, 640 model_test_loc, model_mat_loc,641 697 points_vbo, normals_vbo, 642 698 colors_vbo, texcoords_vbo, selected_colors_vbo, … … 863 919 GLuint color_sp, GLuint texture_sp, 864 920 GLuint vao1, GLuint vao2, 865 GLuint shader1_mat_loc, GLuint shader2_mat_loc,866 921 GLuint points_vbo, GLuint normals_vbo, 867 922 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, … … 890 945 891 946 for (it = colored_objs.begin(); it != colored_objs.end(); it++) { 892 glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));893 894 947 glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points); 895 948 } … … 899 952 900 953 for (it = selected_objs.begin(); it != selected_objs.end(); it++) { 901 glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));902 903 954 glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points); 904 955 } … … 908 959 909 960 for (it = textured_objs.begin(); it != textured_objs.end(); it++) { 910 glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));911 912 961 glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points); 913 962 } -
texture.vert
re165b85 r14ff67c 1 1 #version 410 2 2 3 uniform mat4 model, view, proj; 3 #define MAX_NUM_OBJECTS 1024 4 5 uniform mat4 view, proj; 6 7 layout (std140) uniform models { 8 mat4 model_mats[MAX_NUM_OBJECTS]; 9 }; 4 10 5 11 layout(location = 0) in vec3 vertex_position; 6 12 layout(location = 1) in vec2 vt; 7 13 layout(location = 2) in vec3 vertex_normal; 14 layout(location = 3) in uint ubo_index; 8 15 9 16 out vec2 texture_coordinates; … … 14 21 15 22 void main() { 16 position_eye = vec3(view * model * vec4(vertex_position, 1.0));17 normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0));23 position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0)); 24 normal_eye = vec3(view * model_mats[ubo_index] * vec4 (vertex_normal, 0.0)); 18 25 texture_coordinates = vt; 19 26 light_position_eye = vec3(view * vec4(light_position_world, 1.0));
Note:
See TracChangeset
for help on using the changeset viewer.