Changeset 14ff67c in opengl-game for new-game.cpp
- Timestamp:
- May 24, 2018, 2:35:35 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 4f3262f
- Parents:
- e165b85
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.