Changeset 1a530df in opengl-game


Ignore:
Timestamp:
Apr 6, 2018, 4:21:03 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
f70ab75
Parents:
046ce72
Message:

Design an algorithm for rendering objects using colors or shaders and try to use a ubo for storing model matrices

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TODO.txt

    r046ce72 r1a530df  
     1DEBUGGING
     2==========
     3-Read the sections about shader debugging, starting from line 59
     4-Change the background to gray in case my square is being rendered in black
     5
    16TODO
    2 =====
     7==========
    38-Change the logger class to use cout instead of printf. Consider how easy variable argument support would be.
    49-Change all error messages to use the logger class so they get printed to the log file as well.
     
    914
    1015DONE
    11 =====
     16==========
    1217-Print a warning if texture images don't sizes of 2^x
    1318-Fix the texture-mapping code to not flip the texture upside down.
  • color.vert

    r046ce72 r1a530df  
    11#version 410
     2
     3layout (std140) uniform model_block {
     4   mat4 M;
     5};
    26
    37uniform mat4 model, view, proj;
  • new-game.cpp

    r046ce72 r1a530df  
    304304      vec3(points2[15], points2[16], points2[17]),
    305305   };
     306
     307   int ubo_id = 0;
     308   GLuint ubo = 0;
     309   glGenBuffers(1, &ubo);
     310   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     311   glBufferData(GL_ARRAY_BUFFER, sizeof(float)*16*2, NULL, GL_STATIC_DRAW);
     312
     313   glBindBufferBase(GL_UNIFORM_BUFFER, ubo_id, ubo);
     314   glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(float) * 16, value_ptr(objects[0].model_mat));
     315   glBufferSubData(GL_UNIFORM_BUFFER, sizeof(float) * 16, sizeof(float) * 16, value_ptr(objects[1].model_mat));
    306316
    307317   GLuint points_vbo = 0;
     
    354364   glEnableVertexAttribArray(1);
    355365
     366   // I can create a vbo to store all points for all models,
     367   // and another vbo to store all colors for all models, but how do I allow alternating between
     368   // using colors and textures for each model?
     369   // Do I create a third vbo for texture coordinates and change which vertex attribute array I have bound
     370   // when I want to draw a textured model?
     371   // Or do I create one vao with vertices and colors and another with vertices and textures and switch between the two?
     372   // Since I would have to switch shader programs to toggle between using colors or textures,
     373   // I think I should use one vao for both cases and have a points vbo, a colors vbo, and a textures vbo
     374   // One program will use the points and colors, and the other will use the points and texture coords
     375   // Review how to bind vbos to vertex attributes in the shader.
     376   //
     377   // Binding vbos is done using glVertexAttribPointer(...) on a per-vao basis and is not tied to any specific shader.
     378   // This means, I could create two vaos, one for each shader and have one use points+colors, while the other
     379   // uses points+texxcoords.
     380   //
     381   // At some point, when I have lots of objects, I want to group them by shader when drawing them.
     382   // I'd probably create some sort of set per shader and have each set contain the ids of all objects currently using that shader
     383   // Most likely, I'd want to implement each set using a bit field. Makes it constant time for updates and iterating through them
     384   // should not be much of an issue either.
     385   // Assuming making lots of draw calls instead of one is not innefficient, I should be fine.
     386   // I might also want to use one glDrawElements call per shader to draw multiple non-memory-adjacent models
     387   //
     388   // DECISION: Use a glDrawElements call per shader since I use a regular array to specify the elements to draw
     389   // Actually, this will only work once I get UBOs working since each object will have a different model matrix
     390   // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
     391
    356392   GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
    357393   GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
     394
     395   GLuint ub_index = glGetUniformBlockIndex(shader_program, "model_block");
     396   glUniformBlockBinding(shader_program, ub_index, ubo_id);
     397
     398   GLuint ub_index2 = glGetUniformBlockIndex(shader_program2, "model_block");
     399   glUniformBlockBinding(shader_program2, ub_index2, ubo_id);
     400
     401   cout << "Uniform Buffer Debugging" << endl;
     402   cout << "ubo: " << ubo << endl;
     403   cout << "ub_index: " << ub_index << endl;
     404   cout << "ub_index2: " << ub_index2 << endl;
    358405
    359406   float speed = 1.0f;
     
    403450   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    404451
     452   glBindBufferRange(GL_UNIFORM_BUFFER, ub_index, ubo, 0, sizeof(float) * 16);
     453
    405454   glUseProgram(shader_program2);
    406455   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
    407456   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    408457   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     458
     459   glBindBufferRange(GL_UNIFORM_BUFFER, ub_index2, ubo, sizeof(float) * 16, sizeof(float) * 16);
    409460
    410461   bool cam_moved = false;
  • texture.vert

    r046ce72 r1a530df  
    11#version 410
     2
     3layout (std140) uniform model_block {
     4   mat4 M;
     5};
    26
    37uniform mat4 model, view, proj;
Note: See TracChangeset for help on using the changeset viewer.