Files
Archimedes/modules/Archimedes-Modules/Sandbox/Sandbox.cpp
2026-02-13 08:24:03 -06:00

192 lines
5.7 KiB
C++

// This only works with opengl!!!!
#include "Sandbox.h"
Sandbox::Sandbox(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
name = "Sandbox";
WindowModule* wm = new WindowModule(a, h);
deps[*wm] = wm;
ImguiModule* im = new ImguiModule(a, h);
deps[*im] = im;
}
Sandbox::~Sandbox() {
if(app) {
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
im->releaseContext(ImGui::GetCurrentContext());
wm->releaseWindow(window);
}
}
void Sandbox::onLoad() {
// get window
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
if(!wm) {
std::cout << "No WindowModule for Sandbox!\n";
std::abort();
}
if(!im) {
std::cout << "No ImguiModule for Sandbox!\n";
std::abort();
}
window = wm->aquireWindow();
ImGui::SetCurrentContext(im->aquireContext());
window->getRenderer()->clearColor = { 0.2, 0.2, 0.4, 0.7 };
gridShader = Archimedes::Shader(cubeVS, gridFS, Archimedes::Shader::LoadType::FromSource);
window->getRenderer()->useShader(gridShader);
grid = Archimedes::RenderTarget(
Archimedes::VertexBuffer(gridVertices, 12 * sizeof(float)),
Archimedes::IndexArray(gridIndices, 6),
Archimedes::VertexLayout(),
gridShader
);
window->getRenderer()->useRenderTarget(grid);
cubeShader = Archimedes::Shader(cubeVS, cubeFS, Archimedes::Shader::LoadType::FromSource);
window->getRenderer()->useShader(cubeShader);
cube = Archimedes::RenderTarget(
Archimedes::VertexBuffer(vertices, 24 * sizeof(float)),
Archimedes::IndexArray(indices, 36),
Archimedes::VertexLayout(),
cubeShader
);
window->getRenderer()->useRenderTarget(cube);
int w, h;
window->getSize(w, h);
app->emitEvent(new Archimedes::ResizeWindowEvent(w, h));
}
void Sandbox::run() {
static float scale = 100.0f;
static glm::vec3 pos(0), rot(0);
static glm::vec3 camPos(0.0f, 0.0f, 3.0f), camRot(0.0f, 0.0f, 0.0f);
static glm::mat4 orthoCamera, perspCamera, cameraTransform;
cameraTransform = glm::mat4(1.0f);
cameraTransform = glm::translate(cameraTransform, camPos);
cameraTransform = glm::rotate(cameraTransform, camRot.x, glm::vec3(1.0f, 0.0f, 0.0f));
cameraTransform = glm::rotate(cameraTransform, camRot.y, glm::vec3(0.0f, 1.0f, 0.0f));
cameraTransform = glm::rotate(cameraTransform, camRot.z, glm::vec3(0.0f, 0.0f, 1.0f));
int w, h;
window->getSize(w, h);
orthoCamera = glm::ortho(-(float)w / 2.0f, (float)w / 2.0f, -(float)h / 2.0f, (float)h / 2.0f, 0.1f, 100.0f)
* cameraTransform;
perspCamera = glm::perspective(glm::radians(45.0f), (float)w/(float)h, 0.1f, 100.0f)
* cameraTransform;
glm::mat4& cubeTransform = cube.transform;
//glm::mat4& gridTransform = grid.transform;
cubeTransform = glm::mat4(1.0f);
cubeTransform = glm::translate(cubeTransform, pos);
cubeTransform = glm::rotate(cubeTransform, rot.x, glm::vec3(1.0f, 0.0f, 0.0f));
cubeTransform = glm::rotate(cubeTransform, rot.y, glm::vec3(0.0f, 1.0f, 0.0f));
cubeTransform = glm::rotate(cubeTransform, rot.z, glm::vec3(0.0f, 0.0f, 1.0f));
cubeTransform = glm::scale(cubeTransform, glm::vec3(scale));
//gridTransform = glm::mat4(1.0f);
//gridTransform = glm::scale(gridTransform, glm::vec3(800.0f));
cubeTransform = orthoCamera * cubeTransform;
//gridTransform = orthoCamera * gridTransform;
/*
cubeTransform = glm::perspective(glm::radians(45.0f), (float)w/(float)h, 0.1f, 100.0f)
* 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))
* cubeTransform;
*/
//window->getRenderer()->draw(grid);
window->getRenderer()->draw(cube);
{
ImGuiIO& io = ImGui::GetIO();
static glm::vec4& clearColor = window->getRenderer()->clearColor;
ImGui::Begin("Sandbox Module");
ImGui::Text("Pick a clear color!");
ImGui::SliderFloat("r", &clearColor.r, 0.0f, 1.0f);
ImGui::SliderFloat("g", &clearColor.g, 0.0f, 1.0f);
ImGui::SliderFloat("b", &clearColor.b, 0.0f, 1.0f);
ImGui::SliderFloat("a", &clearColor.a, 0.0f, 1.0f);
ImGui::Text("Properties");
ImGui::SliderFloat("pitch", &rot.x, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("yaw", &rot.y, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("roll", &rot.z, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("scale", &scale, 0.1f, 1000.0f);
ImGui::SliderFloat("x", &pos.x, -100.0f, 100.0f);
ImGui::SliderFloat("y", &pos.y, -100.0f, 100.0f);
ImGui::SliderFloat("z", &pos.z, -100.0f, 100.0f);
ImGui::Text("Camera Properties");
ImGui::SliderFloat("c_pitch", &camRot.x, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("c_yaw", &camRot.y, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("c_roll", &camRot.z, -3.14159265359f, 3.14159265359f);
ImGui::SliderFloat("c_x", &camPos.x, -100.0f, 100.0f);
ImGui::SliderFloat("c_y", &camPos.y, -100.0f, 100.0f);
ImGui::SliderFloat("c_z", &camPos.z, -100.0f, 100.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 Sandbox::onEvent(const Archimedes::Event& e) {
return false;
}