// This only works with opengl!!!! #include "Rubiks.h" #define STB_TRUETYPE_IMPLEMENTATION #include #define STB_IMAGE_WRITE_IMPLEMENTATION #include Rubiks::Rubiks(Archimedes::App* a, void* h) : Archimedes::Module(a, h) { name = "Rubiks"; WindowModule* wm = new WindowModule(a, h); deps[*wm] = wm; ImguiModule* im = new ImguiModule(a, h); deps[*im] = im; } Rubiks::~Rubiks() { if(app) { WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; } ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; } im->releaseContext(ImGui::GetCurrentContext()); wm->releaseWindow(window); } } void Rubiks::onLoad() { // get window WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; } ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; } if(!wm) { std::cout << "No WindowModule for Rubiks!\n"; std::abort(); } if(!im) { std::cout << "No ImguiModule for Rubiks!\n"; std::abort(); } window = wm->aquireWindow(); ImGui::SetCurrentContext(im->aquireContext()); window->getRenderer()->clearColor = { 0.2f, 0.2f, 0.2f, 1.0f }; int w, h; window->getSize(w, h); app->emitEvent(new Archimedes::ResizeWindowEvent(w, h)); camera.setTransform(glm::lookAt( glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f) )); camera.setPerspective(glm::perspective(glm::radians(45.0f), (float)w/(float)h, 0.1f, 100.0f)); //camera.setPerspective(glm::ortho(-(float)w / 2.0f, (float)w / 2.0f, -(float)h / 2.0f, (float)h / 2.0f, 0.1f, 100.0f)); rubiksCube.init(window->getRenderer()); } void Rubiks::run() { static float scale = 0.8f, scalePrev = 1.0f; static glm::vec3 pos(0), rot(0); static glm::vec3 posPrev(0), rotPrev(0); static glm::vec3 camPos(0.0f, 0.0f, 10.0f), camRot(0.0f, glm::pi(), 0.0f); static glm::vec3 camPosPrev(0.0f, 0.0f, 10.0f), camRotPrev(0.0f, glm::pi(), 0.0f); static glm::vec4 color = { 0.4f, 3.0f, 0.4f, 1.0f }; static int w, h; window->getSize(w, h); //camPos = camera.moveRel(camPos - camPosPrev); //camRot = camera.rotateRel(camRot - camRotPrev); camera.setTransform(glm::lookAt( camPos, camPos + glm::normalize(glm::vec3(glm::cos(camRot.x) * glm::sin(camRot.y), glm::sin(camRot.x), glm::cos(camRot.x) * glm::cos(camRot.y))), glm::vec3(0.0f, 1.0f, 0.0f) )); camPosPrev = camPos; camRotPrev = camRot; for(auto& b : rubiksCube.getBlocks()) { b.scaleTo(scale); window->getRenderer()->draw( b.getMesh(), b.getTransform(), camera.getTransform(), camera.getPerspective(), color ); } { ImGuiIO& io = ImGui::GetIO(); static glm::vec4& clearColor = window->getRenderer()->clearColor; ImGui::Begin("Rubiks Module"); ImGui::Text("Pick a clear color!"); ImGui::SliderFloat("clear r", &clearColor.r, 0.0f, 1.0f); ImGui::SliderFloat("clear g", &clearColor.g, 0.0f, 1.0f); ImGui::SliderFloat("clear b", &clearColor.b, 0.0f, 1.0f); ImGui::SliderFloat("clear a", &clearColor.a, 0.0f, 1.0f); ImGui::Text("Camera Properties"); ImGui::SliderFloat("cam pitch", &camRot.x, -glm::pi(), glm::pi()); ImGui::SliderFloat("cam yaw", &camRot.y, 0, 2 * glm::pi()); ImGui::SliderFloat("cam roll", &camRot.z, -glm::pi(), glm::pi()); ImGui::SliderFloat("cam x", &camPos.x, -10.0f, 10.0f); ImGui::SliderFloat("cam y", &camPos.y, -10.0f, 10.0f); ImGui::SliderFloat("cam z", &camPos.z, -10.0f, 10.0f); ImGui::Text("Rubiks Cube Properties"); ImGui::SliderFloat("cube pitch", &rot.x, -glm::pi(), glm::pi()); ImGui::SliderFloat("cube yaw", &rot.y, 0, 2 * glm::pi()); ImGui::SliderFloat("cube roll", &rot.z, -glm::pi(), glm::pi()); ImGui::SliderFloat("cube x", &pos.x, -10.0f, 10.0f); ImGui::SliderFloat("cube y", &pos.y, -10.0f, 10.0f); ImGui::SliderFloat("cube z", &pos.z, -10.0f, 10.0f); ImGui::SliderFloat("cube scale", &scale, 0.01f, 10.0f); if(ImGui::Button("Reset Window Size")) { app->emitEvent(new Archimedes::ResizeWindowEvent(500, 500)); } ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::End(); } } bool Rubiks::onEvent(const Archimedes::Event& e) { unsigned int type = app->getEventType(e); ImGuiIO& io = ImGui::GetIO(); (void)io; if(type == app->getEventType(Archimedes::ResizeWindowEvent())) { Archimedes::ResizeWindowEvent event = (Archimedes::ResizeWindowEvent&) e; camera.setPerspective(glm::perspective(glm::radians(45.0f), (float)event.width/(float)event.height, 0.1f, 100.0f)); } else if(type == app->getEventType(Archimedes::KeyPressedWindowEvent()) && !io.WantCaptureKeyboard) { return false; } else if(type == app->getEventType(Archimedes::KeyReleasedWindowEvent()) && !io.WantCaptureKeyboard) { return false; } else if(type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent()) && !io.WantCaptureMouse) { return false; } else if(type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent()) && !io.WantCaptureMouse) { return false; } else if(type == app->getEventType(Archimedes::ScrollWindowEvent()) && !io.WantCaptureMouse) { Archimedes::ScrollWindowEvent event = (Archimedes::ScrollWindowEvent&) e; //camera.moveRel(glm::vec3(0.0f, 0.0f, event.dy)); return true; } else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent()) && !io.WantCaptureMouse) { return false; } return false; }