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;
}

View File

@@ -0,0 +1,144 @@
// This only works with opengl!!!!
#ifndef JOBJECT_H
#define JOBJECT_H
#include "Archimedes.h"
#include "modules/WindowModule/WindowModule.h"
#include "modules/ImguiModule/ImguiModule.h"
#include "utils/Objects/Body.h"
#include "utils/Objects/Camera.h"
class Rubiks : public Archimedes::Module {
public:
Rubiks(Archimedes::App*, void*);
Rubiks() { name = "Rubiks"; }
~Rubiks();
void onLoad() override;
void run() override;
bool onEvent(const Archimedes::Event& e) override;
private:
Archimedes::Window* window;
std::string cubeVS = "#version 430 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aNorm;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"uniform vec4 color;\n"
"out vec3 Norm;\n"
"out vec3 FragPos;\n"
"void main()\n"
"{\n"
" gl_Position = proj * view * model * vec4(aPos, 1.0f);\n"
" Norm = aNorm;\n"
" FragPos = vec3(model * vec4(aPos, 1.0f));\n"
"}\0";
std::string cubeFS = "#version 430 core\n"
"in vec3 Norm;\n"
"in vec3 FragPos;\n"
"out vec4 FragColor;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"uniform vec4 color;\n"
"uniform float amblight;\n"
"void main()\n"
"{\n"
" FragColor = color;\n"
"}\n\0";
Archimedes::Shader cubeShader;
Archimedes::Body cube;
Archimedes::VertexLayout layout = Archimedes::VertexLayout({
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3),
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3)
});
Archimedes::Camera camera;
std::vector<float> vertices = {
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
};
std::vector<unsigned int> indices = {
0,
1,
2,
2,
3,
0,
4,
5,
6,
6,
7,
4,
0,
1,
5,
5,
4,
0,
3,
2,
6,
6,
7,
3,
0,
3,
7,
7,
4,
0,
1,
2,
6,
6,
5,
1,
};
};
#ifdef SANDBOX_DYNAMIC
typedef Rubiks mtype;
#include "endModule.h"
#endif
#endif

View File

@@ -0,0 +1,58 @@
{ inputs, ... }: {
perSystem = { system, pkgs, self', ... }: {
packages.Rubiks = pkgs.stdenvNoCC.mkDerivation {
name = "Rubiks";
src = inputs.src;
imgui = inputs.imgui;
nativeBuildInputs = with pkgs; [
clang
];
buildInputs = with pkgs; [
glfw
glew
glm
curl
nlohmann_json
stb
];
buildPhase = ''
clang++ \
modules/Archimedes-Modules/Rubiks/*.cpp \
modules/WindowModule/*.cpp \
modules/ImguiModule/*.cpp \
$imgui/backends/imgui_impl_glfw.cpp \
$imgui/backends/imgui_impl_opengl3.cpp \
$imgui/misc/cpp/*.cpp \
$imgui/*.cpp \
-DRENDERER_OPENGL=1 \
-DWINDOW_GLFW=1 \
-DSANDBOX_DYNAMIC \
-fpic -shared \
-I include -I $imgui -I . \
-lEGL -lglfw -lGLEW \
$(curl-config --cflags) \
$(curl-config --libs) \
-Wall \
-o $name -DIMGUI_IMPL_GLFW_DISABLE_X11
'';
installPhase = ''
mkdir -p $out/bin
cp $name $out/bin
'';
};
};
}