Changeset baa5848 in opengl-game
- Timestamp:
- Apr 9, 2018, 2:01:04 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 7280257
- Parents:
- f70ab75
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rf70ab75 rbaa5848 42 42 struct SceneObject { 43 43 mat4 model_mat; 44 GLuint shader_program; 44 45 }; 45 46 … … 62 63 63 64 SceneObject* clickedObject = NULL; 64 SceneObject* selectedObject = NULL;65 SceneObject* selectedObject; 65 66 66 67 double fps; … … 269 270 // triangle 270 271 objects.push_back(SceneObject()); 271 272 T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f)); 272 objects[0].shader_program = 0; 273 274 T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f)); 273 275 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 274 276 objects[0].model_mat = T_model*R_model; … … 284 286 // square 285 287 objects.push_back(SceneObject()); 288 objects[1].shader_program = 0; 286 289 287 290 T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f)); … … 459 462 glBindBufferRange(GL_UNIFORM_BUFFER, ub_index2, ubo, sizeof(float) * 16, sizeof(float) * 16); 460 463 464 objects[0].shader_program = shader_program; 465 objects[1].shader_program = shader_program2; 466 467 vector<int> program1_objects, program2_objects; 468 vector<int>::iterator it; 469 461 470 bool cam_moved = false; 462 471 … … 485 494 } 486 495 496 program1_objects.clear(); 497 program2_objects.clear(); 498 499 // Handle events (Ideally, move all event-handling code 500 // before the render code) 501 502 clickedObject = NULL; 503 glfwPollEvents(); 504 487 505 if (clickedObject == &objects[0]) { 488 506 selectedObject = &objects[0]; 489 507 } 490 491 // At some point, I should change this to only rebind the buffer once per click, not once per frame 492 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 493 if (selectedObject == &objects[0]) { 494 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW); 495 } 496 else { 497 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); 508 if (clickedObject == &objects[1]) { 509 selectedObject = &objects[1]; 510 } 511 512 if (selectedObject == &objects[1] && 513 objects[1].shader_program == shader_program2) { 514 objects[1].shader_program = shader_program; 515 } else if (selectedObject != &objects[1] && 516 objects[1].shader_program == shader_program) { 517 objects[1].shader_program = shader_program2; 518 } 519 520 // group scene objects by shader 521 for (int i=0; i < objects.size(); i++) { 522 if (objects[i].shader_program == shader_program) { 523 program1_objects.push_back(i); 524 } else if (objects[i].shader_program == shader_program2) { 525 program2_objects.push_back(i); 526 } 498 527 } 499 528 … … 506 535 507 536 glUseProgram(shader_program); 508 509 // Since every object will have a different model matrix, maybe it shouldn't be a uniform510 511 // this is temporary.512 // It's needed to offset the code for the recoloring of the square working during click detection513 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));514 515 537 glBindVertexArray(vao); 516 538 517 glDrawArrays(GL_TRIANGLES, 0, numPoints); 518 519 if (clickedObject == &objects[1]) { 520 selectedObject = &objects[1]; 521 } 522 523 if (selectedObject == &objects[1]) { 524 glUseProgram(shader_program); 525 526 // this is temporary. 527 // It's needed to get the recoloring of the square working during click detection 528 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat)); 529 530 glBindVertexArray(vao2); 531 532 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo); 533 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 534 } else { 535 glUseProgram(shader_program2); 536 537 glBindVertexArray(vao2); 538 539 glBindBuffer(GL_ARRAY_BUFFER, vt_vbo); 540 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL); 541 } 542 543 glDrawArrays(GL_TRIANGLES, 0, numPoints2); 544 545 clickedObject = NULL; 546 547 glfwPollEvents(); 539 for (it=program1_objects.begin(); it != program1_objects.end(); it++) { 540 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 541 542 if (selectedObject == &objects[*it]) { 543 if (*it == 1) { 544 glBindBuffer(GL_ARRAY_BUFFER, points2_vbo); 545 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 546 547 glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo); 548 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 549 550 glDrawArrays(GL_TRIANGLES, 0, numPoints2); 551 } else { 552 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 553 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 554 555 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 556 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW); 557 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 558 559 glDrawArrays(GL_TRIANGLES, 0, numPoints); 560 } 561 } else { 562 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 563 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 564 565 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 566 glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); 567 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); 568 569 glDrawArrays(GL_TRIANGLES, 0, numPoints); 570 } 571 } 572 573 glUseProgram(shader_program2); 574 glBindVertexArray(vao2); 575 576 for (it = program2_objects.begin(); it != program2_objects.end(); it++) { 577 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 578 579 glDrawArrays(GL_TRIANGLES, 0, numPoints2); 580 } 581 548 582 glfwSwapBuffers(window); 549 583
Note:
See TracChangeset
for help on using the changeset viewer.