Changeset 5c403fe in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jun 10, 2018, 10:17:10 PM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
809ce16
Parents:
f7d35da
git-author:
Dmitry Portnoy <dmp1488@…> (06/10/18 22:17:05)
git-committer:
Dmitry Portnoy <dmp1488@…> (06/10/18 22:17:10)
Message:

Change the SceneObject definition to include a base model matrix and a transform model matrix, which are multiplied to create the final model matrix. Add a transformObject function that sets the transform matrix and updates the model ubo with the object's new model matrix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rf7d35da r5c403fe  
    3939struct SceneObject {
    4040   unsigned int id;
    41    mat4 model_mat;
     41   mat4 model_mat, model_base, model_transform;
    4242   GLuint shader_program;
    4343   unsigned int num_points;
    4444   GLint vertex_vbo_offset;
     45   GLint ubo_offset;
    4546   vector<GLfloat> points;
    4647   vector<GLfloat> colors;
     
    123124                     map<GLuint, unsigned int>& shaderCounts,
    124125                     map<GLuint, unsigned int>& curShaderBase);
     126
     127void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo);
    125128
    126129void renderMainMenu();
     
    324327   T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
    325328   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    326    obj.model_mat = T_model*R_model;
     329   obj.model_base = T_model*R_model;
    327330
    328331   addObjectToScene(obj);
     
    366369   T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
    367370   R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
    368    obj.model_mat = T_model*R_model;
     371   obj.model_base = T_model*R_model;
    369372
    370373   addObjectToScene(obj);
     
    408411   T_model = translate(mat4(), vec3(0.0f, -0.9f, 0.0f));
    409412   R_model = rotate(mat4(), -1.0f, vec3(1.0f, 0.0f, 0.0f));
    410    obj.model_mat = T_model; //T_model * R_model;
     413   obj.model_base = T_model; //T_model * R_model;
    411414
    412415   addObjectToScene(obj);
     
    480483
    481484   mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    482    mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     485   mat4 R = mat4();
    483486   view_mat = R*T;
    484487
     
    593596         /*
    594597         if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
    595             //transformObject(objects[1], translate(mat4(), vec3(0.3f, 0.0f, 0.0f)), ubo);
     598            transformObject(objects[1], translate(mat4(), vec3(0.3f, 0.0f, 0.0f)), ubo);
    596599         }
    597600         if (key_pressed[GLFW_KEY_RIGHT]) {
     
    885888   obj.id = objects.size(); // currently unused
    886889   obj.num_points = obj.points.size() / 3;
     890   obj.model_transform = mat4();
    887891
    888892   obj.normals.reserve(obj.points.size());
     
    10091013   glBufferData(GL_ARRAY_BUFFER, model_mat_idx_buffer_size, NULL, GL_DYNAMIC_DRAW);
    10101014
    1011    GLint vertex_ubo_offset;
    10121015   for (it = objects.begin(); it != objects.end(); it++) {
    10131016      it->vertex_vbo_offset = curShaderBase[it->shader_program] + curShaderOffset[it->shader_program];
    1014       vertex_ubo_offset = curShaderUboBase[it->shader_program] + curShaderUboOffset[it->shader_program];
     1017      it->ubo_offset = curShaderUboBase[it->shader_program] + curShaderUboOffset[it->shader_program];
    10151018
    10161019      glBindBuffer(GL_ARRAY_BUFFER, *points_vbo);
     
    10311034      glBindBuffer(GL_ARRAY_BUFFER, *model_mat_idx_vbo);
    10321035      for (int i = 0; i < it->num_points; i++) {
    1033          glBufferSubData(GL_ARRAY_BUFFER, (it->vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &vertex_ubo_offset);
     1036         glBufferSubData(GL_ARRAY_BUFFER, (it->vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &it->ubo_offset);
    10341037      }
    10351038
    10361039      curShaderOffset[it->shader_program] += it->num_points;
    10371040
     1041      it->model_mat = it->model_base * it->model_transform;
    10381042      glBindBuffer(GL_UNIFORM_BUFFER, *ubo);
    1039       glBufferSubData(GL_UNIFORM_BUFFER, vertex_ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(it->model_mat));
     1043      glBufferSubData(GL_UNIFORM_BUFFER, it->ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(it->model_mat));
    10401044
    10411045      curShaderUboOffset[it->shader_program]++;
    10421046   }
     1047}
     1048
     1049void transformObject(SceneObject& obj, const mat4& transform, GLuint ubo) {
     1050   obj.model_transform = obj.model_transform * transform;
     1051   obj.model_mat = obj.model_transform * obj.model_base;
     1052
     1053   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     1054   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    10431055}
    10441056
Note: See TracChangeset for help on using the changeset viewer.