Changeset 93462c6 in opengl-game for new-game.cpp
- Timestamp:
- May 11, 2018, 3:24:20 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- b1d8ddc
- Parents:
- f0cc877
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rf0cc877 r93462c6 31 31 #include <array> 32 32 #include <vector> 33 #include <queue> 33 34 34 35 using namespace std; … … 51 52 }; 52 53 54 enum State { 55 STATE_MAIN_MENU, 56 STATE_GAME, 57 }; 58 59 enum Event { 60 EVENT_GO_TO_MAIN_MENU, 61 EVENT_GO_TO_GAME, 62 EVENT_QUIT, 63 }; 64 53 65 const bool FULLSCREEN = false; 54 66 int width = 640; … … 63 75 64 76 vector<SceneObject> objects; 77 queue<Event> events; 65 78 66 79 SceneObject* clickedObject = NULL; … … 85 98 void print4DVector(string label, vec4 v); 86 99 87 void renderGui(); 100 void renderMainMenu(); 101 void renderMainMenuGui(); 102 103 void renderScene(vector<SceneObject>& objects, 104 GLuint shader_program1, GLuint shader_program2, 105 GLuint vao1, GLuint vao2, 106 GLuint shader1_mat_loc, GLuint shader2_mat_loc, 107 GLuint points_vbo, GLuint normals_vbo, 108 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, 109 SceneObject* selectedObject); 110 void renderSceneGui(); 88 111 89 112 void glfw_error_callback(int error, const char* description) { … … 485 508 proj_mat = make_mat4(proj_arr); 486 509 487 GL int model_test_loc = glGetUniformLocation(shader_program, "model");488 GL int view_test_loc = glGetUniformLocation(shader_program, "view");489 GL int proj_test_loc = glGetUniformLocation(shader_program, "proj");490 491 GL int model_mat_loc = glGetUniformLocation(shader_program2, "model");492 GL int view_mat_loc = glGetUniformLocation(shader_program2, "view");493 GL int proj_mat_loc = glGetUniformLocation(shader_program2, "proj");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"); 494 517 495 518 glUseProgram(shader_program); … … 506 529 objects[1].shader_program = shader_program2; 507 530 508 vector<int> program1_objects, program2_objects;509 vector<int>::iterator it;510 511 531 bool cam_moved = false; 512 532 … … 520 540 // disable vsync to see real framerate 521 541 //glfwSwapInterval(0); 542 543 State curState = STATE_MAIN_MENU; 522 544 523 545 while (!glfwWindowShouldClose(window) && isRunning) { … … 541 563 } 542 564 543 program1_objects.clear();544 program2_objects.clear();545 546 565 // Handle events (Ideally, move all event-handling code 547 566 // before the render code) … … 550 569 glfwPollEvents(); 551 570 552 if (clickedObject == &objects[0]) { 553 selectedObject = &objects[0]; 554 } 555 if (clickedObject == &objects[1]) { 556 selectedObject = &objects[1]; 571 while (!events.empty()) { 572 switch (events.front()) { 573 case EVENT_GO_TO_MAIN_MENU: 574 curState = STATE_MAIN_MENU; 575 break; 576 case EVENT_GO_TO_GAME: 577 curState = STATE_GAME; 578 break; 579 case EVENT_QUIT: 580 isRunning = false; 581 break; 582 } 583 events.pop(); 584 } 585 586 if (curState == STATE_GAME) { 587 if (clickedObject == &objects[0]) { 588 selectedObject = &objects[0]; 589 } 590 if (clickedObject == &objects[1]) { 591 selectedObject = &objects[1]; 592 } 557 593 } 558 594 … … 565 601 } 566 602 567 // group scene objects by shader568 for (unsigned int i=0; i < objects.size(); i++) {569 if (objects[i].shader_program == shader_program) {570 program1_objects.push_back(i);571 } else if (objects[i].shader_program == shader_program2) {572 program2_objects.push_back(i);573 }574 }575 576 603 /* 577 604 model[12] = last_position + speed*elapsed_seconds; … … 581 608 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 582 609 583 glUseProgram(shader_program); 584 glBindVertexArray(vao); 585 586 for (it=program1_objects.begin(); it != program1_objects.end(); it++) { 587 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 588 589 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 590 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 591 592 if (selectedObject == &objects[*it]) { 593 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 594 } else { 595 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo); 596 } 597 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 598 599 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 600 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 601 602 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points); 603 } 604 605 glUseProgram(shader_program2); 606 glBindVertexArray(vao2); 607 608 for (it = program2_objects.begin(); it != program2_objects.end(); it++) { 609 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 610 611 glBindBuffer(GL_ARRAY_BUFFER, points_vbo); 612 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 613 614 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 615 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, objects[*it].texture_vbo_offset); 616 617 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 618 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 619 620 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points); 621 } 622 623 renderGui(); 610 switch (curState) { 611 case STATE_MAIN_MENU: 612 renderMainMenu(); 613 renderMainMenuGui(); 614 break; 615 case STATE_GAME: 616 renderScene(objects, 617 shader_program, shader_program2, 618 vao, vao2, 619 model_test_loc, model_mat_loc, 620 points_vbo, normals_vbo, 621 colors_vbo, texcoords_vbo, selected_colors_vbo, 622 selectedObject); 623 renderSceneGui(); 624 break; 625 } 624 626 625 627 glfwSwapBuffers(window); … … 815 817 } 816 818 817 void renderGui() { 819 void renderScene(vector<SceneObject>& objects, 820 GLuint shader_program1, GLuint shader_program2, 821 GLuint vao1, GLuint vao2, 822 GLuint shader1_mat_loc, GLuint shader2_mat_loc, 823 GLuint points_vbo, GLuint normals_vbo, 824 GLuint colors_vbo, GLuint texcoords_vbo, GLuint selected_colors_vbo, 825 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 835 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); 841 } 842 } 843 844 vector<int>::iterator it; 845 846 glUseProgram(shader_program1); 847 glBindVertexArray(vao1); 848 849 for (it = program1_objects.begin(); it != program1_objects.end(); it++) { 850 glUniformMatrix4fv(shader1_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 851 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); 869 glBindVertexArray(vao2); 870 871 for (it = program2_objects.begin(); it != program2_objects.end(); it++) { 872 glUniformMatrix4fv(shader2_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat)); 873 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); 884 } 885 } 886 887 void renderSceneGui() { 818 888 ImGui_ImplGlfwGL3_NewFrame(); 819 889 … … 858 928 ImGuiWindowFlags_NoResize | 859 929 ImGuiWindowFlags_NoMove); 860 ImGui::InvisibleButton("", ImVec2(1 90, 18));930 ImGui::InvisibleButton("", ImVec2(155, 18)); 861 931 ImGui::SameLine(); 862 if (ImGui::Button(" Quit")) {863 isRunning = false;932 if (ImGui::Button("Main Menu")) { 933 events.push(EVENT_GO_TO_MAIN_MENU); 864 934 } 865 935 ImGui::End(); 866 936 } 867 937 868 // Main Menu 869 /* 938 ImGui::Render(); 939 ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData()); 940 } 941 942 void renderMainMenu() { 943 } 944 945 void renderMainMenuGui() { 946 ImGui_ImplGlfwGL3_NewFrame(); 947 870 948 { 871 949 int padding = 4; 872 950 ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once); 873 ImGui::SetNextWindowSize(ImVec2(width +2*padding, height+2*padding), ImGuiCond_Once);951 ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once); 874 952 ImGui::Begin("WndMain", NULL, 875 953 ImGuiWindowFlags_NoTitleBar | 876 954 ImGuiWindowFlags_NoResize | 877 955 ImGuiWindowFlags_NoMove); 956 957 ImGui::InvisibleButton("", ImVec2(10, 80)); 958 ImGui::InvisibleButton("", ImVec2(285, 18)); 959 ImGui::SameLine(); 960 if (ImGui::Button("New Game")) { 961 events.push(EVENT_GO_TO_GAME); 962 } 963 964 ImGui::InvisibleButton("", ImVec2(10, 15)); 965 ImGui::InvisibleButton("", ImVec2(300, 18)); 966 ImGui::SameLine(); 967 if (ImGui::Button("Quit")) { 968 events.push(EVENT_QUIT); 969 } 970 878 971 ImGui::End(); 879 972 } 880 */881 973 882 974 ImGui::Render();
Note:
See TracChangeset
for help on using the changeset viewer.