Changeset 14ff67c in opengl-game for new-game.cpp


Ignore:
Timestamp:
May 24, 2018, 2:35:35 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
4f3262f
Parents:
e165b85
Message:

Use uniform buffers to store model matrices and add constants to toggle FPS display and vsync.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    re165b85 r14ff67c  
    6363
    6464const bool FULLSCREEN = false;
     65const bool SHOW_FPS = false;
     66const bool DISABLE_VSYNC = true;
     67unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime
     68
    6569int width = 640;
    6670int height = 480;
     
    103107                  GLuint color_sp, GLuint texture_sp,
    104108                  GLuint vao1, GLuint vao2,
    105                   GLuint shader1_mat_loc, GLuint shader2_mat_loc,
    106109                  GLuint points_vbo, GLuint normals_vbo,
    107110                  GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
     
    210213   glewExperimental = GL_TRUE;
    211214   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;
    212239
    213240   // Setup Dear ImGui binding
     
    396423   GLsizeiptr textures_buffer_size = 0;
    397424   GLsizeiptr ubo_buffer_size = 0;
     425   GLsizeiptr model_mat_idx_buffer_size = 0;
    398426
    399427   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     
    401429      textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
    402430      ubo_buffer_size += 16 * sizeof(GLfloat);
     431      model_mat_idx_buffer_size += obj_it->num_points * sizeof(GLuint);
    403432   }
    404433
     
    457486      offset += obj_it->normals.size() * sizeof(GLfloat);
    458487   }
    459 
    460    /*
     488   glBindBuffer(GL_UNIFORM_BUFFER, 0);
     489
    461490   GLuint ubo = 0;
    462491   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);
    466494
    467495   offset = 0;
    468496   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   }
    475516
    476517   GLuint vao = 0;
     
    481522   glEnableVertexAttribArray(1);
    482523   glEnableVertexAttribArray(2);
     524   glEnableVertexAttribArray(3);
    483525
    484526   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     
    487529   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    488530   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);
    489534
    490535   GLuint vao2 = 0;
     
    495540   glEnableVertexAttribArray(1);
    496541   glEnableVertexAttribArray(2);
     542   glEnableVertexAttribArray(3);
    497543
    498544   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     
    504550   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    505551   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);
    506555
    507556   float speed = 1.0f;
     
    538587   proj_mat = make_mat4(proj_arr);
    539588
    540    GLuint model_test_loc = glGetUniformLocation(color_sp, "model");
     589   GLuint ub_binding_point = 0;
     590
    541591   GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
    542592   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
    547595   GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
    548596   GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
     597   GLuint texture_sp_ub_index = glGetUniformBlockIndex(texture_sp, "models");
    549598
    550599   glUseProgram(color_sp);
     
    552601   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    553602
    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);
    555605
    556606   glUseProgram(texture_sp);
     
    558608   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    559609
     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
    560613   bool cam_moved = false;
    561614
     
    568621
    569622   // disable vsync to see real framerate
    570    //glfwSwapInterval(0);
     623   if (DISABLE_VSYNC && SHOW_FPS) {
     624      glfwSwapInterval(0);
     625   }
    571626
    572627   State curState = STATE_MAIN_MENU;
     
    577632      previous_seconds = current_seconds;
    578633
    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      }
    589646
    590647      if (fabs(last_position) > 1.0f) {
     
    638695               color_sp, texture_sp,
    639696               vao, vao2,
    640                model_test_loc, model_mat_loc,
    641697               points_vbo, normals_vbo,
    642698               colors_vbo, texcoords_vbo, selected_colors_vbo,
     
    863919                  GLuint color_sp, GLuint texture_sp,
    864920                  GLuint vao1, GLuint vao2,
    865                   GLuint shader1_mat_loc, GLuint shader2_mat_loc,
    866921                  GLuint points_vbo, GLuint normals_vbo,
    867922                  GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
     
    890945
    891946   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 
    894947      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
    895948   }
     
    899952
    900953   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 
    903954      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
    904955   }
     
    908959
    909960   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 
    912961      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
    913962   }
Note: See TracChangeset for help on using the changeset viewer.