Changeset cffca4d in opengl-game


Ignore:
Timestamp:
May 16, 2018, 3:48:08 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
e165b85
Parents:
b1d8ddc
Message:

Change the rendering for colored objects by drawing selected and unselected objects separately in order to avoid rebinding the color vbos multiple times each frame.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rb1d8ddc rcffca4d  
    4343   GLuint shader_program;
    4444   unsigned int num_points;
    45    GLvoid* vertex_vbo_offset;
    46    GLvoid* texture_vbo_offset;
     45   GLint vertex_vbo_offset;
    4746   vector<GLfloat> points;
    4847   vector<GLfloat> colors;
     
    102101
    103102void renderScene(vector<SceneObject>& objects,
    104                   GLuint shader_program1, GLuint shader_program2,
     103                  GLuint color_sp, GLuint texture_sp,
    105104                  GLuint vao1, GLuint vao2,
    106105                  GLuint shader1_mat_loc, GLuint shader2_mat_loc,
     
    257256   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    258257   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    259 
    260    mat4 T_model, R_model;
    261 
    262    // triangle
    263    objects.push_back(SceneObject());
    264    objects[0].id = 0;
    265    objects[0].shader_program = 0;
    266    objects[0].vertex_vbo_offset = (GLvoid*) (0 * sizeof(float) * 3);
    267    objects[0].texture_vbo_offset = (GLvoid*)(0 * sizeof(float) * 2);
    268    objects[0].points = {
    269        0.0f,  0.5f,  0.0f,
    270       -0.5f, -0.5f,  0.0f,
    271        0.5f, -0.5f,  0.0f,
    272        0.5f, -0.5f,  0.0f,
    273       -0.5f, -0.5f,  0.0f,
    274        0.0f,  0.5f,  0.0f,
    275    };
    276    objects[0].colors = {
    277       1.0f, 0.0f, 0.0f,
    278       0.0f, 0.0f, 1.0f,
    279       0.0f, 1.0f, 0.0f,
    280       0.0f, 1.0f, 0.0f,
    281       0.0f, 0.0f, 1.0f,
    282       1.0f, 0.0f, 0.0f,
    283    };
    284    objects[0].texcoords = {
    285       1.0f, 1.0f,
    286       0.0f, 1.0f,
    287       0.0f, 0.0f,
    288       1.0f, 1.0f,
    289       0.0f, 0.0f,
    290       1.0f, 0.0f
    291    };
    292    objects[0].normals = {
    293        0.0f,  0.0f,  1.0f,
    294        0.0f,  0.0f,  1.0f,
    295        0.0f,  0.0f,  1.0f,
    296        0.0f,  0.0f, -1.0f,
    297        0.0f,  0.0f, -1.0f,
    298        0.0f,  0.0f, -1.0f,
    299    };
    300    objects[0].selected_colors = {
    301       0.0f, 1.0f, 0.0f,
    302       0.0f, 1.0f, 0.0f,
    303       0.0f, 1.0f, 0.0f,
    304       0.0f, 1.0f, 0.0f,
    305       0.0f, 1.0f, 0.0f,
    306       0.0f, 1.0f, 0.0f,
    307    };
    308    objects[0].num_points = objects[0].points.size() / 3;
    309 
    310    T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
    311    R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    312    objects[0].model_mat = T_model*R_model;
    313 
    314    // square
    315    objects.push_back(SceneObject());
    316    objects[1].id = 1;
    317    objects[1].shader_program = 0;
    318    objects[1].vertex_vbo_offset = (GLvoid*) (6 * sizeof(float) * 3);
    319    objects[1].texture_vbo_offset = (GLvoid*)(6 * sizeof(float) * 2);
    320    objects[1].points = {
    321        0.5f,  0.5f,  0.0f,
    322       -0.5f,  0.5f,  0.0f,
    323       -0.5f, -0.5f,  0.0f,
    324        0.5f,  0.5f,  0.0f,
    325       -0.5f, -0.5f,  0.0f,
    326        0.5f, -0.5f,  0.0f,
    327    };
    328    objects[1].colors = {
    329       1.0f, 0.0f, 0.0f,
    330       0.0f, 0.0f, 1.0f,
    331       0.0f, 1.0f, 0.0f,
    332       0.0f, 1.0f, 0.0f,
    333       0.0f, 0.0f, 1.0f,
    334       1.0f, 0.0f, 0.0f,
    335    };
    336    objects[1].texcoords = {
    337       1.0f, 1.0f,
    338       0.0f, 1.0f,
    339       0.0f, 0.0f,
    340       1.0f, 1.0f,
    341       0.0f, 0.0f,
    342       1.0f, 0.0f
    343    };
    344    objects[1].normals = {
    345       0.0f,  0.0f,  1.0f,
    346       0.0f,  0.0f,  1.0f,
    347       0.0f,  0.0f,  1.0f,
    348       0.0f,  0.0f,  1.0f,
    349       0.0f,  0.0f,  1.0f,
    350       0.0f,  0.0f,  1.0f,
    351    };
    352    objects[1].selected_colors = {
    353       0.0f, 0.6f, 0.9f,
    354       0.0f, 0.6f, 0.9f,
    355       0.0f, 0.6f, 0.9f,
    356       0.0f, 0.6f, 0.9f,
    357       0.0f, 0.6f, 0.9f,
    358       0.0f, 0.6f, 0.9f,
    359    };
    360    objects[1].num_points = objects[1].points.size() / 3;
    361 
    362    T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
    363    R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
    364    objects[1].model_mat = T_model*R_model;
    365 
    366    vector<SceneObject>::iterator obj_it;
    367    GLsizeiptr offset;
    368 
    369    GLsizeiptr points_buffer_size = 0;
    370    GLsizeiptr textures_buffer_size = 0;
    371 
    372    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    373       points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
    374       textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
    375    }
    376 
    377    GLuint points_vbo = 0;
    378    glGenBuffers(1, &points_vbo);
    379    glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    380    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
    381 
    382    offset = 0;
    383    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    384       glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
    385       offset += obj_it->points.size() * sizeof(GLfloat);
    386    }
    387 
    388    GLuint colors_vbo = 0;
    389    glGenBuffers(1, &colors_vbo);
    390    glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    391    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
    392 
    393    offset = 0;
    394    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    395       glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
    396       offset += obj_it->colors.size() * sizeof(GLfloat);
    397    }
    398 
    399    GLuint selected_colors_vbo = 0;
    400    glGenBuffers(1, &selected_colors_vbo);
    401    glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    402    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
    403 
    404    offset = 0;
    405    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    406       glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
    407       offset += obj_it->selected_colors.size() * sizeof(GLfloat);
    408    }
    409 
    410    GLuint texcoords_vbo = 0;
    411    glGenBuffers(1, &texcoords_vbo);
    412    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    413    glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
    414 
    415    offset = 0;
    416    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    417       glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
    418       offset += obj_it->texcoords.size() * sizeof(GLfloat);
    419    }
    420 
    421    GLuint normals_vbo = 0;
    422    glGenBuffers(1, &normals_vbo);
    423    glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    424    glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
    425 
    426    offset = 0;
    427    for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
    428       glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]);
    429       offset += obj_it->normals.size() * sizeof(GLfloat);
    430    }
    431 
    432    GLuint vao = 0;
    433    glGenVertexArrays(1, &vao);
    434    glBindVertexArray(vao);
    435 
    436    glEnableVertexAttribArray(0);
    437    glEnableVertexAttribArray(1);
    438    glEnableVertexAttribArray(2);
    439 
    440    GLuint vao2 = 0;
    441    glGenVertexArrays(1, &vao2);
    442    glBindVertexArray(vao2);
    443 
    444    glEnableVertexAttribArray(0);
    445    glEnableVertexAttribArray(1);
    446    glEnableVertexAttribArray(2);
    447258
    448259   // I can create a vbo to store all points for all models,
     
    472283   // For now, I could implement this with a glDrawElements call per object and update the model uniform for each object
    473284
    474    GLuint shader_program = loadShaderProgram("./color.vert", "./color.frag");
    475    GLuint shader_program2 = loadShaderProgram("./texture.vert", "./texture.frag");
     285   GLuint color_sp = loadShaderProgram("./color.vert", "./color.frag");
     286   GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
     287
     288   mat4 T_model, R_model;
     289
     290   // triangle
     291   objects.push_back(SceneObject());
     292   objects[0].id = 0;
     293   objects[0].shader_program = color_sp;
     294   objects[0].vertex_vbo_offset = 0;
     295   objects[0].points = {
     296       0.0f,  0.5f,  0.0f,
     297      -0.5f, -0.5f,  0.0f,
     298       0.5f, -0.5f,  0.0f,
     299       0.5f, -0.5f,  0.0f,
     300      -0.5f, -0.5f,  0.0f,
     301       0.0f,  0.5f,  0.0f,
     302   };
     303   objects[0].colors = {
     304      1.0f, 0.0f, 0.0f,
     305      0.0f, 0.0f, 1.0f,
     306      0.0f, 1.0f, 0.0f,
     307      0.0f, 1.0f, 0.0f,
     308      0.0f, 0.0f, 1.0f,
     309      1.0f, 0.0f, 0.0f,
     310   };
     311   objects[0].texcoords = {
     312      1.0f, 1.0f,
     313      0.0f, 1.0f,
     314      0.0f, 0.0f,
     315      1.0f, 1.0f,
     316      0.0f, 0.0f,
     317      1.0f, 0.0f
     318   };
     319   objects[0].normals = {
     320       0.0f,  0.0f,  1.0f,
     321       0.0f,  0.0f,  1.0f,
     322       0.0f,  0.0f,  1.0f,
     323       0.0f,  0.0f, -1.0f,
     324       0.0f,  0.0f, -1.0f,
     325       0.0f,  0.0f, -1.0f,
     326   };
     327   objects[0].selected_colors = {
     328      0.0f, 1.0f, 0.0f,
     329      0.0f, 1.0f, 0.0f,
     330      0.0f, 1.0f, 0.0f,
     331      0.0f, 1.0f, 0.0f,
     332      0.0f, 1.0f, 0.0f,
     333      0.0f, 1.0f, 0.0f,
     334   };
     335   objects[0].num_points = objects[0].points.size() / 3;
     336
     337   T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
     338   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
     339   objects[0].model_mat = T_model*R_model;
     340
     341   // square
     342   objects.push_back(SceneObject());
     343   objects[1].id = 1;
     344   objects[1].shader_program = texture_sp;
     345   objects[1].vertex_vbo_offset = 6;
     346   objects[1].points = {
     347       0.5f,  0.5f,  0.0f,
     348      -0.5f,  0.5f,  0.0f,
     349      -0.5f, -0.5f,  0.0f,
     350       0.5f,  0.5f,  0.0f,
     351      -0.5f, -0.5f,  0.0f,
     352       0.5f, -0.5f,  0.0f,
     353   };
     354   objects[1].colors = {
     355      1.0f, 0.0f, 0.0f,
     356      0.0f, 0.0f, 1.0f,
     357      0.0f, 1.0f, 0.0f,
     358      0.0f, 1.0f, 0.0f,
     359      0.0f, 0.0f, 1.0f,
     360      1.0f, 0.0f, 0.0f,
     361   };
     362   objects[1].texcoords = {
     363      1.0f, 1.0f,
     364      0.0f, 1.0f,
     365      0.0f, 0.0f,
     366      1.0f, 1.0f,
     367      0.0f, 0.0f,
     368      1.0f, 0.0f
     369   };
     370   objects[1].normals = {
     371      0.0f,  0.0f,  1.0f,
     372      0.0f,  0.0f,  1.0f,
     373      0.0f,  0.0f,  1.0f,
     374      0.0f,  0.0f,  1.0f,
     375      0.0f,  0.0f,  1.0f,
     376      0.0f,  0.0f,  1.0f,
     377   };
     378   objects[1].selected_colors = {
     379      0.0f, 0.6f, 0.9f,
     380      0.0f, 0.6f, 0.9f,
     381      0.0f, 0.6f, 0.9f,
     382      0.0f, 0.6f, 0.9f,
     383      0.0f, 0.6f, 0.9f,
     384      0.0f, 0.6f, 0.9f,
     385   };
     386   objects[1].num_points = objects[1].points.size() / 3;
     387
     388   T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
     389   R_model = rotate(mat4(), 0.5f, vec3(0.0f, 1.0f, 0.0f));
     390   objects[1].model_mat = T_model*R_model;
     391
     392   vector<SceneObject>::iterator obj_it;
     393   GLsizeiptr offset;
     394
     395   GLsizeiptr points_buffer_size = 0;
     396   GLsizeiptr textures_buffer_size = 0;
     397
     398   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     399      points_buffer_size += obj_it->points.size() * sizeof(GLfloat);
     400      textures_buffer_size += obj_it->texcoords.size() * sizeof(GLfloat);
     401   }
     402
     403   GLuint points_vbo = 0;
     404   glGenBuffers(1, &points_vbo);
     405   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     406   glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     407
     408   offset = 0;
     409   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     410      glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->points.size() * sizeof(GLfloat), &obj_it->points[0]);
     411      offset += obj_it->points.size() * sizeof(GLfloat);
     412   }
     413
     414   GLuint colors_vbo = 0;
     415   glGenBuffers(1, &colors_vbo);
     416   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     417   glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     418
     419   offset = 0;
     420   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     421      glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->colors.size() * sizeof(GLfloat), &obj_it->colors[0]);
     422      offset += obj_it->colors.size() * sizeof(GLfloat);
     423   }
     424
     425   GLuint selected_colors_vbo = 0;
     426   glGenBuffers(1, &selected_colors_vbo);
     427   glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
     428   glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     429
     430   offset = 0;
     431   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     432      glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->selected_colors.size() * sizeof(GLfloat), &obj_it->selected_colors[0]);
     433      offset += obj_it->selected_colors.size() * sizeof(GLfloat);
     434   }
     435
     436   GLuint texcoords_vbo = 0;
     437   glGenBuffers(1, &texcoords_vbo);
     438   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     439   glBufferData(GL_ARRAY_BUFFER, textures_buffer_size, NULL, GL_DYNAMIC_DRAW);
     440
     441   offset = 0;
     442   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     443      glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->texcoords.size() * sizeof(GLfloat), &obj_it->texcoords[0]);
     444      offset += obj_it->texcoords.size() * sizeof(GLfloat);
     445   }
     446
     447   GLuint normals_vbo = 0;
     448   glGenBuffers(1, &normals_vbo);
     449   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     450   glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW);
     451
     452   offset = 0;
     453   for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) {
     454      glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]);
     455      offset += obj_it->normals.size() * sizeof(GLfloat);
     456   }
     457
     458   GLuint vao = 0;
     459   glGenVertexArrays(1, &vao);
     460   glBindVertexArray(vao);
     461
     462   glEnableVertexAttribArray(0);
     463   glEnableVertexAttribArray(1);
     464   glEnableVertexAttribArray(2);
     465
     466   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     467   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     468
     469   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     470   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
     471
     472   GLuint vao2 = 0;
     473   glGenVertexArrays(1, &vao2);
     474   glBindVertexArray(vao2);
     475
     476   glEnableVertexAttribArray(0);
     477   glEnableVertexAttribArray(1);
     478   glEnableVertexAttribArray(2);
     479
     480   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     481   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     482
     483   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     484   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
     485
     486   glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     487   glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
    476488
    477489   float speed = 1.0f;
     
    508520   proj_mat = make_mat4(proj_arr);
    509521
    510    GLuint model_test_loc = glGetUniformLocation(shader_program, "model");
    511    GLuint view_test_loc = glGetUniformLocation(shader_program, "view");
    512    GLuint proj_test_loc = glGetUniformLocation(shader_program, "proj");
    513 
    514    GLuint model_mat_loc = glGetUniformLocation(shader_program2, "model");
    515    GLuint view_mat_loc = glGetUniformLocation(shader_program2, "view");
    516    GLuint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
    517 
    518    glUseProgram(shader_program);
     522   GLuint model_test_loc = glGetUniformLocation(color_sp, "model");
     523   GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
     524   GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
     525
     526   GLuint model_mat_loc = glGetUniformLocation(texture_sp, "model");
     527   GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
     528   GLuint proj_mat_loc = glGetUniformLocation(texture_sp, "proj");
     529
     530   glUseProgram(color_sp);
    519531   glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
    520532   glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    521533   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    522534
    523    glUseProgram(shader_program2);
     535   glUseProgram(texture_sp);
    524536   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
    525537   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    526538   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    527 
    528    objects[0].shader_program = shader_program;
    529    objects[1].shader_program = shader_program2;
    530539
    531540   bool cam_moved = false;
     
    593602      }
    594603
    595       if (selectedObject == &objects[1] &&
    596          objects[1].shader_program == shader_program2) {
    597          objects[1].shader_program = shader_program;
    598       } else if (selectedObject != &objects[1] &&
    599          objects[1].shader_program == shader_program) {
    600          objects[1].shader_program = shader_program2;
    601       }
    602 
    603604      /*
    604605      model[12] = last_position + speed*elapsed_seconds;
     
    615616         case STATE_GAME:
    616617            renderScene(objects,
    617                shader_program, shader_program2,
     618               color_sp, texture_sp,
    618619               vao, vao2,
    619620               model_test_loc, model_mat_loc,
     
    665666         view_mat = R*T;
    666667
    667          glUseProgram(shader_program);
     668         glUseProgram(color_sp);
    668669         glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    669670
    670          glUseProgram(shader_program2);
     671         glUseProgram(texture_sp);
    671672         glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    672673
     
    817818}
    818819
     820// The easiest thing here seems to be to set all the colors we want in a CPU array once per frame,
     821// them copy it over to the GPU and make one draw call
     822// This method also easily allows us to use any colors we want for each shape.
     823// Try to compare the frame times for the current method and the new one
     824//
     825// Alternatively, I have one large color buffer that has selected and unselected colors
     826// Then, when I know which object is selected, I can use glVertexAttribPointer to decide
     827// whether to use the selected or unselected color for it
     828
     829// I'll have to think of the best way to do something similar when using
     830// one draw call. Probably, in that case, I'll use one draw call for all unselectable objects
     831// and use the approach mentioned above for all selectable objects.
     832// I can have one colors vbo for unselectable objects and another for selected+unselected colors
     833// of selectable objects
     834
     835// For both colored and textured objects, using a single draw call will only work for objects
     836// that don't change state (i.e. don't change colors or switch from colored to textured).
     837
     838// This means I'll probably have one call for static colored objects, one call for static textured objects,
     839// a loop of calls for dynamic currently colored objects, and a loop of calls for dynamic currently textured objects.
     840// This will increase if I add new shaders since I'll need either one new call or one new loop of calls per shader
     841
    819842void renderScene(vector<SceneObject>& objects,
    820                   GLuint shader_program1, GLuint shader_program2,
     843                  GLuint color_sp, GLuint texture_sp,
    821844                  GLuint vao1, GLuint vao2,
    822845                  GLuint shader1_mat_loc, GLuint shader2_mat_loc,
     
    824847                  GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo,
    825848                  SceneObject* selectedObject) {
    826    if (selectedObject == &objects[1]) {
    827       objects[1].shader_program = shader_program1;
    828    } else if (selectedObject != &objects[1]) {
    829       objects[1].shader_program = shader_program2;
    830    }
    831 
    832    vector<int> program1_objects, program2_objects;
    833 
    834    // group scene objects by shader
     849
     850   vector<int> colored_objs, selected_objs, textured_objs, static_colored_objs, static_textured_objs;
     851
     852   // group scene objects by shader and vbo
    835853   for (unsigned int i = 0; i < objects.size(); i++) {
    836       if (objects[i].shader_program == shader_program1) {
    837          program1_objects.push_back(i);
    838       }
    839       else if (objects[i].shader_program == shader_program2) {
    840          program2_objects.push_back(i);
     854      if (selectedObject == &objects[i]) {
     855         selected_objs.push_back(i);
     856      } else if (objects[i].shader_program == color_sp) {
     857         colored_objs.push_back(i);
     858      } else if (objects[i].shader_program == texture_sp) {
     859         textured_objs.push_back(i);
    841860      }
    842861   }
     
    844863   vector<int>::iterator it;
    845864
    846    glUseProgram(shader_program1);
     865   glUseProgram(color_sp);
    847866   glBindVertexArray(vao1);
    848867
    849    for (it = program1_objects.begin(); it != program1_objects.end(); it++) {
     868   glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     869   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     870
     871   for (it = colored_objs.begin(); it != colored_objs.end(); it++) {
    850872      glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
    851873
    852       glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    853       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    854 
    855       if (selectedObject == &objects[*it]) {
    856          glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    857       } else {
    858          glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    859       }
    860       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    861 
    862       glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    863       glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    864 
    865       glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
    866    }
    867 
    868    glUseProgram(shader_program2);
     874      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
     875   }
     876
     877   glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
     878   glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
     879
     880   for (it = selected_objs.begin(); it != selected_objs.end(); it++) {
     881      glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
     882
     883      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
     884   }
     885
     886   glUseProgram(texture_sp);
    869887   glBindVertexArray(vao2);
    870888
    871    for (it = program2_objects.begin(); it != program2_objects.end(); it++) {
     889   for (it = textured_objs.begin(); it != textured_objs.end(); it++) {
    872890      glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
    873891
    874       glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
    875       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    876 
    877       glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    878       glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, objects[*it].texture_vbo_offset);
    879 
    880       glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    881       glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    882 
    883       glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
     892      glDrawArrays(GL_TRIANGLES, objects[*it].vertex_vbo_offset, objects[*it].num_points);
    884893   }
    885894}
Note: See TracChangeset for help on using the changeset viewer.