Changeset e66fd66 in opengl-game for new-game.cpp
- Timestamp:
- Dec 5, 2020, 7:14:31 PM (4 years ago)
- Branches:
- feature/imgui-sdl, master
- Children:
- 95c657f
- Parents:
- 78c3045
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r78c3045 re66fd66 9 9 10 10 #include "IMGUI/imgui.h" 11 #include "imgui_impl_glfw_gl3.h" 11 #include "IMGUI/imgui_impl_glfw.h" 12 #include "IMGUI/imgui_impl_opengl3.h" 12 13 13 14 #include <GL/glew.h> … … 36 37 using namespace glm; 37 38 38 enum State {39 STATE_MAIN_MENU,40 STATE_GAME,39 enum class State { 40 MAIN_MENU, 41 GAME, 41 42 }; 42 43 43 enum Event {44 EVENT_GO_TO_MAIN_MENU,45 EVENT_GO_TO_GAME,46 EVENT_QUIT,44 enum class Event { 45 GO_TO_MAIN_MENU, 46 GO_TO_GAME, 47 QUIT, 47 48 }; 48 49 49 50 /*** START OF REFACTORED CODE ***/ 50 enum ObjectType {51 TYPE_SHIP,52 TYPE_ASTEROID,53 TYPE_LASER,54 TYPE_EXPLOSION,51 enum class ObjectType { 52 SHIP, 53 ASTEROID, 54 LASER, 55 EXPLOSION, 55 56 }; 56 57 57 enum AttribType {58 ATTRIB_UNIFORM,59 ATTRIB_OBJECT_VARYING,60 ATTRIB_POINT_VARYING,58 enum class AttribType { 59 UNIFORM, 60 OBJECT_VARYING, 61 POINT_VARYING, 61 62 }; 62 63 … … 248 249 249 250 #define NUM_KEYS (512) 250 #define ONE_DEG_IN_RAD ((2.0f * M_PI) / 360.0f) // 0.017444444 (maybe make this a const instead)251 251 #define TARGET_FPS 60.0f 252 252 … … 332 332 return 1; 333 333 } 334 335 string glsl_version = "#version 410"; 334 336 335 337 #ifdef MAC … … 411 413 IMGUI_CHECKVERSION(); 412 414 ImGui::CreateContext(); 415 413 416 ImGuiIO& io = ImGui::GetIO(); (void)io; 414 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 415 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 416 ImGui_ImplGlfwGL3_Init(window, true); 417 418 // Setup style 417 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 418 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 419 420 // Setup Dear ImGui style 419 421 ImGui::StyleColorsDark(); 420 422 //ImGui::StyleColorsClassic(); 423 424 // Setup Platform/Renderer backends 425 ImGui_ImplGlfw_InitForOpenGL(window, true); 426 ImGui_ImplOpenGL3_Init(glsl_version.c_str()); 421 427 422 428 glfwSetMouseButtonCallback(window, mouse_button_callback); … … 497 503 map<ObjectType, ShaderModelGroup> modelGroups; 498 504 499 modelGroups[ TYPE_SHIP] = createModelGroup(505 modelGroups[ObjectType::SHIP] = createModelGroup( 500 506 loadShaderProgram("gl-shaders/ship.vert", "gl-shaders/ship.frag")); 501 shaderBufferInfo[modelGroups[ TYPE_SHIP].shaderProgram] = BufferInfo(); // temporary502 503 defineModelGroupAttrib(modelGroups[ TYPE_SHIP], "vertex_position", ATTRIB_POINT_VARYING,507 shaderBufferInfo[modelGroups[ObjectType::SHIP].shaderProgram] = BufferInfo(); // temporary 508 509 defineModelGroupAttrib(modelGroups[ObjectType::SHIP], "vertex_position", AttribType::POINT_VARYING, 504 510 3, GL_FLOAT, offset_of(&SceneObject::points)); 505 defineModelGroupAttrib(modelGroups[ TYPE_SHIP], "vertex_color", ATTRIB_POINT_VARYING,511 defineModelGroupAttrib(modelGroups[ObjectType::SHIP], "vertex_color", AttribType::POINT_VARYING, 506 512 3, GL_FLOAT, offset_of(&SceneObject::colors)); 507 defineModelGroupAttrib(modelGroups[ TYPE_SHIP], "vertex_normal", ATTRIB_POINT_VARYING,513 defineModelGroupAttrib(modelGroups[ObjectType::SHIP], "vertex_normal", AttribType::POINT_VARYING, 508 514 3, GL_FLOAT, offset_of(&SceneObject::normals)); 509 defineModelGroupAttrib(modelGroups[ TYPE_SHIP], "ubo_index", ATTRIB_OBJECT_VARYING,515 defineModelGroupAttrib(modelGroups[ObjectType::SHIP], "ubo_index", AttribType::OBJECT_VARYING, 510 516 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 511 517 512 defineModelGroupUniform(modelGroups[ TYPE_SHIP], "view", ATTRIB_UNIFORM,518 defineModelGroupUniform(modelGroups[ObjectType::SHIP], "view", AttribType::UNIFORM, 513 519 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); 514 defineModelGroupUniform(modelGroups[ TYPE_SHIP], "proj", ATTRIB_UNIFORM,520 defineModelGroupUniform(modelGroups[ObjectType::SHIP], "proj", AttribType::UNIFORM, 515 521 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat)); 516 522 517 initModelGroupAttribs(modelGroups[ TYPE_SHIP]);518 519 modelGroups[ TYPE_ASTEROID] = createModelGroup(523 initModelGroupAttribs(modelGroups[ObjectType::SHIP]); 524 525 modelGroups[ObjectType::ASTEROID] = createModelGroup( 520 526 loadShaderProgram("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag")); 521 shaderBufferInfo[modelGroups[ TYPE_ASTEROID].shaderProgram] = BufferInfo(); // temporary522 523 defineModelGroupAttrib(modelGroups[ TYPE_ASTEROID], "vertex_position", ATTRIB_POINT_VARYING,527 shaderBufferInfo[modelGroups[ObjectType::ASTEROID].shaderProgram] = BufferInfo(); // temporary 528 529 defineModelGroupAttrib(modelGroups[ObjectType::ASTEROID], "vertex_position", AttribType::POINT_VARYING, 524 530 3, GL_FLOAT, offset_of(&SceneObject::points)); 525 defineModelGroupAttrib(modelGroups[ TYPE_ASTEROID], "vertex_color", ATTRIB_POINT_VARYING,531 defineModelGroupAttrib(modelGroups[ObjectType::ASTEROID], "vertex_color", AttribType::POINT_VARYING, 526 532 3, GL_FLOAT, offset_of(&SceneObject::colors)); 527 defineModelGroupAttrib(modelGroups[ TYPE_ASTEROID], "vertex_normal", ATTRIB_POINT_VARYING,533 defineModelGroupAttrib(modelGroups[ObjectType::ASTEROID], "vertex_normal", AttribType::POINT_VARYING, 528 534 3, GL_FLOAT, offset_of(&SceneObject::normals)); 529 defineModelGroupAttrib(modelGroups[ TYPE_ASTEROID], "ubo_index", ATTRIB_OBJECT_VARYING,535 defineModelGroupAttrib(modelGroups[ObjectType::ASTEROID], "ubo_index", AttribType::OBJECT_VARYING, 530 536 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 531 537 532 defineModelGroupUniform(modelGroups[ TYPE_ASTEROID], "view", ATTRIB_UNIFORM,538 defineModelGroupUniform(modelGroups[ObjectType::ASTEROID], "view", AttribType::UNIFORM, 533 539 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); 534 defineModelGroupUniform(modelGroups[ TYPE_ASTEROID], "proj", ATTRIB_UNIFORM,540 defineModelGroupUniform(modelGroups[ObjectType::ASTEROID], "proj", AttribType::UNIFORM, 535 541 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat)); 536 542 537 initModelGroupAttribs(modelGroups[ TYPE_ASTEROID]);538 539 modelGroups[ TYPE_LASER] = createModelGroup(543 initModelGroupAttribs(modelGroups[ObjectType::ASTEROID]); 544 545 modelGroups[ObjectType::LASER] = createModelGroup( 540 546 loadShaderProgram("gl-shaders/laser.vert", "gl-shaders/laser.frag")); 541 shaderBufferInfo[modelGroups[ TYPE_LASER].shaderProgram] = BufferInfo(); // temporary542 543 defineModelGroupAttrib(modelGroups[ TYPE_LASER], "vertex_position", ATTRIB_POINT_VARYING,547 shaderBufferInfo[modelGroups[ObjectType::LASER].shaderProgram] = BufferInfo(); // temporary 548 549 defineModelGroupAttrib(modelGroups[ObjectType::LASER], "vertex_position", AttribType::POINT_VARYING, 544 550 3, GL_FLOAT, offset_of(&SceneObject::points)); 545 defineModelGroupAttrib(modelGroups[ TYPE_LASER], "vt", ATTRIB_POINT_VARYING,551 defineModelGroupAttrib(modelGroups[ObjectType::LASER], "vt", AttribType::POINT_VARYING, 546 552 2, GL_FLOAT, offset_of(&SceneObject::texcoords)); 547 defineModelGroupAttrib(modelGroups[ TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING,553 defineModelGroupAttrib(modelGroups[ObjectType::LASER], "ubo_index", AttribType::OBJECT_VARYING, 548 554 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 549 555 550 defineModelGroupUniform(modelGroups[ TYPE_LASER], "view", ATTRIB_UNIFORM,556 defineModelGroupUniform(modelGroups[ObjectType::LASER], "view", AttribType::UNIFORM, 551 557 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); 552 defineModelGroupUniform(modelGroups[ TYPE_LASER], "proj", ATTRIB_UNIFORM,558 defineModelGroupUniform(modelGroups[ObjectType::LASER], "proj", AttribType::UNIFORM, 553 559 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat)); 554 defineModelGroupUniform(modelGroups[ TYPE_LASER], "laser_color", ATTRIB_UNIFORM,560 defineModelGroupUniform(modelGroups[ObjectType::LASER], "laser_color", AttribType::UNIFORM, 555 561 1, UNIFORM_3F, laserColor); 556 562 557 initModelGroupAttribs(modelGroups[ TYPE_LASER]);558 559 modelGroups[ TYPE_EXPLOSION] = createModelGroup(563 initModelGroupAttribs(modelGroups[ObjectType::LASER]); 564 565 modelGroups[ObjectType::EXPLOSION] = createModelGroup( 560 566 loadShaderProgram("gl-shaders/explosion.vert", "gl-shaders/explosion.frag")); 561 shaderBufferInfo[modelGroups[ TYPE_EXPLOSION].shaderProgram] = BufferInfo(); // temporary562 563 defineModelGroupAttrib(modelGroups[ TYPE_EXPLOSION], "v_i", ATTRIB_POINT_VARYING,567 shaderBufferInfo[modelGroups[ObjectType::EXPLOSION].shaderProgram] = BufferInfo(); // temporary 568 569 defineModelGroupAttrib(modelGroups[ObjectType::EXPLOSION], "v_i", AttribType::POINT_VARYING, 564 570 3, GL_FLOAT, offset_of(&ParticleEffect::particleVelocities)); 565 defineModelGroupAttrib(modelGroups[ TYPE_EXPLOSION], "start_time", ATTRIB_POINT_VARYING,571 defineModelGroupAttrib(modelGroups[ObjectType::EXPLOSION], "start_time", AttribType::POINT_VARYING, 566 572 1, GL_FLOAT, offset_of(&ParticleEffect::particleTimes)); 567 defineModelGroupAttrib(modelGroups[ TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING,573 defineModelGroupAttrib(modelGroups[ObjectType::EXPLOSION], "ubo_index", AttribType::OBJECT_VARYING, 568 574 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 569 575 570 defineModelGroupUniform(modelGroups[ TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM,576 defineModelGroupUniform(modelGroups[ObjectType::EXPLOSION], "cur_time", AttribType::UNIFORM, 571 577 1, UNIFORM_1F, &curTime); 572 defineModelGroupUniform(modelGroups[ TYPE_EXPLOSION], "view", ATTRIB_UNIFORM,578 defineModelGroupUniform(modelGroups[ObjectType::EXPLOSION], "view", AttribType::UNIFORM, 573 579 1, UNIFORM_MATRIX_4F, value_ptr(view_mat)); 574 defineModelGroupUniform(modelGroups[ TYPE_EXPLOSION], "proj", ATTRIB_UNIFORM,580 defineModelGroupUniform(modelGroups[ObjectType::EXPLOSION], "proj", AttribType::UNIFORM, 575 581 1, UNIFORM_MATRIX_4F, value_ptr(proj_mat)); 576 582 577 initModelGroupAttribs(modelGroups[ TYPE_EXPLOSION]);583 initModelGroupAttribs(modelGroups[ObjectType::EXPLOSION]); 578 584 579 585 cam_pos = vec3(0.0f, 0.0f, 2.0f); … … 587 593 588 594 float cam_speed = 1.0f; 589 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;590 float cam_pitch_speed = 60.0f*ONE_DEG_IN_RAD;595 float cam_yaw_speed = radians(60.0f); 596 float cam_pitch_speed = radians(60.0f); 591 597 592 598 // glm::lookAt can create the view matrix … … 601 607 // TODO: Create a function to construct the projection matrix 602 608 // (Maybe I should just use glm::perspective, after making sure it matches what I have now) 603 float fov = 67.0f * ONE_DEG_IN_RAD;609 float fov = radians(67.0f); 604 610 float aspect = (float)windowWidth / (float)windowHeight; 605 611 … … 657 663 GLuint ub_binding_point = 0; 658 664 659 GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ TYPE_SHIP].shaderProgram, "models");660 661 GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ TYPE_ASTEROID].shaderProgram, "models");662 663 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ TYPE_LASER].shaderProgram, "models");664 665 GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ TYPE_EXPLOSION].shaderProgram, "models");666 667 668 glUseProgram(modelGroups[ TYPE_SHIP].shaderProgram);669 bindUniformData(modelGroups[ TYPE_SHIP].attribs["view"]);670 bindUniformData(modelGroups[ TYPE_SHIP].attribs["proj"]);671 672 glUniformBlockBinding(modelGroups[ TYPE_SHIP].shaderProgram, ship_sp_models_ub_index, ub_binding_point);665 GLuint ship_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ObjectType::SHIP].shaderProgram, "models"); 666 667 GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ObjectType::ASTEROID].shaderProgram, "models"); 668 669 GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ObjectType::LASER].shaderProgram, "models"); 670 671 GLuint explosion_sp_models_ub_index = glGetUniformBlockIndex(modelGroups[ObjectType::EXPLOSION].shaderProgram, "models"); 672 673 674 glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram); 675 bindUniformData(modelGroups[ObjectType::SHIP].attribs["view"]); 676 bindUniformData(modelGroups[ObjectType::SHIP].attribs["proj"]); 677 678 glUniformBlockBinding(modelGroups[ObjectType::SHIP].shaderProgram, ship_sp_models_ub_index, ub_binding_point); 673 679 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 674 680 675 681 676 glUseProgram(modelGroups[ TYPE_ASTEROID].shaderProgram);677 bindUniformData(modelGroups[ TYPE_ASTEROID].attribs["view"]);678 bindUniformData(modelGroups[ TYPE_ASTEROID].attribs["proj"]);679 680 glUniformBlockBinding(modelGroups[ TYPE_ASTEROID].shaderProgram, asteroid_sp_models_ub_index, ub_binding_point);682 glUseProgram(modelGroups[ObjectType::ASTEROID].shaderProgram); 683 bindUniformData(modelGroups[ObjectType::ASTEROID].attribs["view"]); 684 bindUniformData(modelGroups[ObjectType::ASTEROID].attribs["proj"]); 685 686 glUniformBlockBinding(modelGroups[ObjectType::ASTEROID].shaderProgram, asteroid_sp_models_ub_index, ub_binding_point); 681 687 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 682 688 … … 685 691 // Right now, I think I'm getting away without getting that uniform location because I'm only 686 692 // using one texture, so setting it to GL_TEXTURE0 once works 687 glUseProgram(modelGroups[ TYPE_LASER].shaderProgram);688 bindUniformData(modelGroups[ TYPE_LASER].attribs["view"]);689 bindUniformData(modelGroups[ TYPE_LASER].attribs["proj"]);690 bindUniformData(modelGroups[ TYPE_LASER].attribs["laser_color"]);691 692 glUniformBlockBinding(modelGroups[ TYPE_LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point);693 glUseProgram(modelGroups[ObjectType::LASER].shaderProgram); 694 bindUniformData(modelGroups[ObjectType::LASER].attribs["view"]); 695 bindUniformData(modelGroups[ObjectType::LASER].attribs["proj"]); 696 bindUniformData(modelGroups[ObjectType::LASER].attribs["laser_color"]); 697 698 glUniformBlockBinding(modelGroups[ObjectType::LASER].shaderProgram, laser_sp_models_ub_index, ub_binding_point); 693 699 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 694 700 695 701 696 glUseProgram(modelGroups[ TYPE_EXPLOSION].shaderProgram);697 bindUniformData(modelGroups[ TYPE_EXPLOSION].attribs["view"]);698 bindUniformData(modelGroups[ TYPE_EXPLOSION].attribs["proj"]);699 700 glUniformBlockBinding(modelGroups[ TYPE_EXPLOSION].shaderProgram, explosion_sp_models_ub_index, ub_binding_point);702 glUseProgram(modelGroups[ObjectType::EXPLOSION].shaderProgram); 703 bindUniformData(modelGroups[ObjectType::EXPLOSION].attribs["view"]); 704 bindUniformData(modelGroups[ObjectType::EXPLOSION].attribs["proj"]); 705 706 glUniformBlockBinding(modelGroups[ObjectType::EXPLOSION].shaderProgram, explosion_sp_models_ub_index, ub_binding_point); 701 707 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 702 708 /*** END OF REFACTORED CODE ***/ … … 720 726 //glfwSwapInterval(0); 721 727 722 State curState = S TATE_MAIN_MENU;728 State curState = State::MAIN_MENU; 723 729 724 730 initGuiValueLists(valueLists); … … 762 768 while (!events.empty()) { 763 769 switch (events.front()) { 764 case EVENT_GO_TO_MAIN_MENU:765 curState = S TATE_MAIN_MENU;770 case Event::GO_TO_MAIN_MENU: 771 curState = State::MAIN_MENU; 766 772 break; 767 case E VENT_GO_TO_GAME:768 curState = S TATE_GAME;773 case Event::GO_TO_GAME: 774 curState = State::GAME; 769 775 break; 770 case E VENT_QUIT:776 case Event::QUIT: 771 777 isRunning = false; 772 778 break; … … 775 781 } 776 782 777 if (curState == S TATE_GAME) {783 if (curState == State::GAME) { 778 784 779 785 /*** START OF REFACTORED CODE ***/ … … 857 863 for (unsigned int i = 0; i < objects.size(); i++) { 858 864 if (!objects[i]->deleted) { 859 if (objects[i]->type == TYPE_ASTEROID) {865 if (objects[i]->type == ObjectType::ASTEROID) { 860 866 transformObject(*objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo); 861 867 … … 878 884 addObjectToScene(createExplosion(model_mat), shaderBufferInfo, modelGroups, ubo); 879 885 } 880 } else if (objects[i]->type == TYPE_EXPLOSION) {886 } else if (objects[i]->type == ObjectType::EXPLOSION) { 881 887 ParticleEffect* explosion = (ParticleEffect*)objects[i]; 882 888 if (glfwGetTime() >= explosion->startTime + explosion->duration) { … … 888 894 889 895 if (leftLaser != NULL && !leftLaser->deleted) { 890 updateLaserTarget(leftLaser, objects, modelGroups[ TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);896 updateLaserTarget(leftLaser, objects, modelGroups[ObjectType::LASER], modelGroups[ObjectType::ASTEROID].shaderProgram); 891 897 } 892 898 if (rightLaser != NULL && !rightLaser->deleted) { 893 updateLaserTarget(rightLaser, objects, modelGroups[ TYPE_LASER], modelGroups[TYPE_ASTEROID].shaderProgram);899 updateLaserTarget(rightLaser, objects, modelGroups[ObjectType::LASER], modelGroups[ObjectType::ASTEROID].shaderProgram); 894 900 } 895 901 } … … 963 969 view_mat = R * T; 964 970 965 glUseProgram(modelGroups[ TYPE_SHIP].shaderProgram);966 bindUniformData(modelGroups[ TYPE_SHIP].attribs["view"]);967 968 glUseProgram(modelGroups[ TYPE_LASER].shaderProgram);969 bindUniformData(modelGroups[ TYPE_LASER].attribs["view"]);971 glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram); 972 bindUniformData(modelGroups[ObjectType::SHIP].attribs["view"]); 973 974 glUseProgram(modelGroups[ObjectType::LASER].shaderProgram); 975 bindUniformData(modelGroups[ObjectType::LASER].attribs["view"]); 970 976 971 977 cam_moved = false; 972 978 } 973 979 974 glUseProgram(modelGroups[ TYPE_EXPLOSION].shaderProgram);975 bindUniformData(modelGroups[ TYPE_EXPLOSION].attribs["cur_time"]);980 glUseProgram(modelGroups[ObjectType::EXPLOSION].shaderProgram); 981 bindUniformData(modelGroups[ObjectType::EXPLOSION].attribs["cur_time"]); 976 982 977 983 // Render scene … … 981 987 982 988 switch (curState) { 983 case S TATE_MAIN_MENU:989 case State::MAIN_MENU: 984 990 renderMainMenu(); 985 991 renderMainMenuGui(); 986 992 break; 987 case S TATE_GAME:993 case State::GAME: 988 994 renderScene(modelGroups, ubo); 989 995 renderSceneGui(valueLists); … … 994 1000 } 995 1001 996 ImGui_ImplGlfwGL3_Shutdown(); 1002 ImGui_ImplOpenGL3_Shutdown(); 1003 ImGui_ImplGlfw_Shutdown(); 997 1004 ImGui::DestroyContext(); 998 1005 … … 1040 1047 1041 1048 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 1042 if ((*it)->type == TYPE_LASER) continue;1049 if ((*it)->type == ObjectType::LASER) continue; 1043 1050 for (unsigned int p_idx = 0; p_idx < (*it)->points.size(); p_idx += 9) { 1044 1051 if (faceClicked( … … 1342 1349 } 1343 1350 1344 if (obj->type == TYPE_SHIP || obj->type == TYPE_ASTEROID) {1351 if (obj->type == ObjectType::SHIP || obj->type == ObjectType::ASTEROID) { 1345 1352 calculateObjectBoundingBox(obj); 1346 1353 … … 1444 1451 SceneObject* ship = new SceneObject(); 1445 1452 1446 ship->type = TYPE_SHIP;1453 ship->type = ObjectType::SHIP; 1447 1454 1448 1455 ship->points = { … … 1839 1846 Laser* createLaser(vec3 start, vec3 end, vec3 color, GLfloat width) { 1840 1847 Laser* obj = new Laser(); 1841 obj->type = TYPE_LASER;1848 obj->type = ObjectType::LASER; 1842 1849 obj->targetAsteroid = NULL; 1843 1850 … … 1970 1977 map<string, AttribInfo>::iterator it; 1971 1978 for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) { 1972 if (it->second.attribType == A TTRIB_UNIFORM) {1979 if (it->second.attribType == AttribType::UNIFORM) { 1973 1980 it->second.buffer = glGetUniformLocation(modelGroup.shaderProgram, it->first.c_str()); 1974 1981 } else { … … 2133 2140 map<string, AttribInfo>::iterator attrIt; 2134 2141 for (attrIt = smg->attribs.begin(); attrIt != smg->attribs.end(); attrIt++) { 2135 if (attrIt->second.attribType != A TTRIB_UNIFORM) {2142 if (attrIt->second.attribType != AttribType::UNIFORM) { 2136 2143 glBindBuffer(GL_ARRAY_BUFFER, attrIt->second.buffer); 2137 2144 glBufferData(GL_ARRAY_BUFFER, smg->vboCapacity * GLsizeof(attrIt->second.type) * attrIt->second.size, NULL, GL_DYNAMIC_DRAW); … … 2165 2172 2166 2173 switch (it->second.attribType) { 2167 case A TTRIB_POINT_VARYING:2174 case AttribType::POINT_VARYING: 2168 2175 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * GLsizeof(it->second.type) * it->second.size, 2169 2176 obj.num_points * GLsizeof(it->second.type) * it->second.size, getVectorAttribPtr(obj, it->second.fieldOffset)); 2170 2177 break; 2171 case A TTRIB_OBJECT_VARYING:2178 case AttribType::OBJECT_VARYING: 2172 2179 for (unsigned int i = 0; i < obj.num_points; i++) { 2173 2180 glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * GLsizeof(it->second.type) * it->second.size, … … 2175 2182 } 2176 2183 break; 2177 case A TTRIB_UNIFORM:2184 case AttribType::UNIFORM: 2178 2185 break; 2179 2186 } … … 2185 2192 glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat)); 2186 2193 2187 if (obj.type == TYPE_ASTEROID) {2188 glUseProgram(modelGroups[ TYPE_ASTEROID].shaderProgram);2194 if (obj.type == ObjectType::ASTEROID) { 2195 glUseProgram(modelGroups[ObjectType::ASTEROID].shaderProgram); 2189 2196 2190 2197 ostringstream oss; 2191 2198 oss << "hp[" << obj.ubo_offset << "]"; 2192 glUniform1f(glGetUniformLocation(modelGroups[ TYPE_ASTEROID].shaderProgram, oss.str().c_str()), ((Asteroid&)obj).hp);2193 } else if (obj.type == TYPE_EXPLOSION) {2194 glUseProgram(modelGroups[ TYPE_EXPLOSION].shaderProgram);2199 glUniform1f(glGetUniformLocation(modelGroups[ObjectType::ASTEROID].shaderProgram, oss.str().c_str()), ((Asteroid&)obj).hp); 2200 } else if (obj.type == ObjectType::EXPLOSION) { 2201 glUseProgram(modelGroups[ObjectType::EXPLOSION].shaderProgram); 2195 2202 2196 2203 ostringstream oss; 2197 2204 oss << "explosion_start_time[" << obj.ubo_offset << "]"; 2198 glUniform1f(glGetUniformLocation(modelGroups[ TYPE_EXPLOSION].shaderProgram, oss.str().c_str()), ((ParticleEffect&)obj).startTime);2205 glUniform1f(glGetUniformLocation(modelGroups[ObjectType::EXPLOSION].shaderProgram, oss.str().c_str()), ((ParticleEffect&)obj).startTime); 2199 2206 } 2200 2207 … … 2255 2262 2256 2263 for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) { 2257 if ((*it)->type == TYPE_ASTEROID && !(*it)->deleted && getLaserAndAsteroidIntersection(start, end, **it, intersection)) {2264 if ((*it)->type == ObjectType::ASTEROID && !(*it)->deleted && getLaserAndAsteroidIntersection(start, end, **it, intersection)) { 2258 2265 // TODO: Implement a more generic algorithm for testing the closest object by getting the distance between the points 2259 2266 if (closestAsteroid == NULL || intersection.z > closestIntersection.z) { … … 2367 2374 void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) { 2368 2375 2369 glUseProgram(modelGroups[ TYPE_SHIP].shaderProgram);2370 glBindVertexArray(modelGroups[ TYPE_SHIP].vao);2371 2372 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ TYPE_SHIP].numPoints);2373 2374 glUseProgram(modelGroups[ TYPE_ASTEROID].shaderProgram);2375 glBindVertexArray(modelGroups[ TYPE_ASTEROID].vao);2376 2377 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ TYPE_ASTEROID].numPoints);2376 glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram); 2377 glBindVertexArray(modelGroups[ObjectType::SHIP].vao); 2378 2379 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ObjectType::SHIP].numPoints); 2380 2381 glUseProgram(modelGroups[ObjectType::ASTEROID].shaderProgram); 2382 glBindVertexArray(modelGroups[ObjectType::ASTEROID].vao); 2383 2384 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ObjectType::ASTEROID].numPoints); 2378 2385 2379 2386 glEnable(GL_BLEND); 2380 2387 2381 glUseProgram(modelGroups[ TYPE_LASER].shaderProgram);2382 glBindVertexArray(modelGroups[ TYPE_LASER].vao);2383 2384 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ TYPE_LASER].numPoints);2385 2386 glUseProgram(modelGroups[ TYPE_EXPLOSION].shaderProgram);2387 glBindVertexArray(modelGroups[ TYPE_EXPLOSION].vao);2388 glUseProgram(modelGroups[ObjectType::LASER].shaderProgram); 2389 glBindVertexArray(modelGroups[ObjectType::LASER].vao); 2390 2391 glDrawArrays(GL_TRIANGLES, 0, modelGroups[ObjectType::LASER].numPoints); 2392 2393 glUseProgram(modelGroups[ObjectType::EXPLOSION].shaderProgram); 2394 glBindVertexArray(modelGroups[ObjectType::EXPLOSION].vao); 2388 2395 2389 2396 glEnable(GL_PROGRAM_POINT_SIZE); 2390 2397 2391 glDrawArrays(GL_POINTS, 0, modelGroups[ TYPE_EXPLOSION].numPoints);2398 glDrawArrays(GL_POINTS, 0, modelGroups[ObjectType::EXPLOSION].numPoints); 2392 2399 2393 2400 glDisable(GL_PROGRAM_POINT_SIZE); … … 2397 2404 2398 2405 void renderSceneGui(map<string, vector<UIValue>> valueLists) { 2399 ImGui_ImplGlfwGL3_NewFrame(); 2406 ImGui_ImplOpenGL3_NewFrame(); 2407 ImGui_ImplGlfw_NewFrame(); 2408 ImGui::NewFrame(); 2400 2409 2401 2410 // 1. Show a simple window. … … 2443 2452 ImGui::SameLine(); 2444 2453 if (ImGui::Button("Main Menu")) { 2445 events.push(E VENT_GO_TO_MAIN_MENU);2454 events.push(Event::GO_TO_MAIN_MENU); 2446 2455 } 2447 2456 ImGui::End(); … … 2462 2471 2463 2472 ImGui::Render(); 2464 ImGui_Impl GlfwGL3_RenderDrawData(ImGui::GetDrawData());2473 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 2465 2474 } 2466 2475 … … 2470 2479 /*** START OF REFACTORED CODE ***/ 2471 2480 void renderMainMenuGui() { 2472 ImGui_ImplGlfwGL3_NewFrame(); 2481 ImGui_ImplOpenGL3_NewFrame(); 2482 ImGui_ImplGlfw_NewFrame(); 2483 ImGui::NewFrame(); 2473 2484 2474 2485 { … … 2485 2496 ImGui::SameLine(); 2486 2497 if (ImGui::Button("New Game")) { 2487 events.push(E VENT_GO_TO_GAME);2498 events.push(Event::GO_TO_GAME); 2488 2499 } 2489 2500 … … 2492 2503 ImGui::SameLine(); 2493 2504 if (ImGui::Button("Quit")) { 2494 events.push(E VENT_QUIT);2505 events.push(Event::QUIT); 2495 2506 } 2496 2507 … … 2499 2510 2500 2511 ImGui::Render(); 2501 ImGui_Impl GlfwGL3_RenderDrawData(ImGui::GetDrawData());2512 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 2502 2513 } 2503 2514 /*** END OF REFACTORED CODE ***/ … … 2544 2555 Asteroid* createAsteroid(vec3 pos) { 2545 2556 Asteroid* obj = new Asteroid(); 2546 obj->type = TYPE_ASTEROID;2557 obj->type = ObjectType::ASTEROID; 2547 2558 obj->hp = 10.0f; 2548 2559 … … 2648 2659 2649 2660 mat4 T = translate(mat4(1.0f), pos); 2650 mat4 R = rotate(mat4(1.0f), 60.0f * (float)ONE_DEG_IN_RAD, vec3(1.0f, 1.0f, -1.0f));2661 mat4 R = rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)); 2651 2662 obj->model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 2652 2663 … … 2668 2679 ParticleEffect* obj = new ParticleEffect(); 2669 2680 2670 obj->type = TYPE_EXPLOSION;2681 obj->type = ObjectType::EXPLOSION; 2671 2682 2672 2683 initObject(obj);
Note:
See TracChangeset
for help on using the changeset viewer.