This commit is contained in:
2026-04-08 12:17:41 -05:00
parent f75a438422
commit a8f55d2ebe
9 changed files with 481 additions and 19 deletions

View File

@@ -0,0 +1,196 @@
// This only works with opengl!!!!
#include "Rubiks.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
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.2, 0.2, 0.4, 0.7 };
cubeShader = Archimedes::Shader(cubeVS, cubeFS, Archimedes::Shader::LoadType::FromSource);
window->getRenderer()->setupShader(cubeShader);
cube = Archimedes::Body(
Archimedes::VertexBuffer(vertices),
Archimedes::IndexArray(indices),
layout,
cubeShader
);
window->getRenderer()->setupRenderTarget(cube.getMesh());
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));
}
void Rubiks::run() {
static float scale = 1.0f, 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, 3.0f), camRot(0.0f, glm::pi<float>(), 0.0f);
static glm::vec3 camPosPrev(0.0f, 0.0f, 3.0f), camRotPrev(0.0f, glm::pi<float>(), 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;
window->getRenderer()->draw(
cube.getMesh(),
cube.getTransform(),
camera.getTransform(),
camera.getPerspective()
);
{
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<float>(), glm::pi<float>());
ImGui::SliderFloat("cam yaw", &camRot.y, 0, 2 * glm::pi<float>());
ImGui::SliderFloat("cam roll", &camRot.z, -glm::pi<float>(), glm::pi<float>());
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);
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;
//cube.rotateRel(glm::vec3(glm::pi<float>() * event.dy / 50.0f, 0.0f, 0.0f));
//cube.rotateRel(glm::vec3(0.0f, glm::pi<float>() * event.dx / 50.0f, 0.0f));
hexagon.scaleAdd(0.001f * event.dy);
return true;
} else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent()) && !io.WantCaptureMouse) {
return false;
}
return false;
}