sandbox
This commit is contained in:
191
modules/Archimedes-Modules/Sandbox/Sandbox.cpp
Normal file
191
modules/Archimedes-Modules/Sandbox/Sandbox.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
// 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;
|
||||
}
|
||||
161
modules/Archimedes-Modules/Sandbox/Sandbox.h
Normal file
161
modules/Archimedes-Modules/Sandbox/Sandbox.h
Normal file
@@ -0,0 +1,161 @@
|
||||
// This only works with opengl!!!!
|
||||
|
||||
|
||||
#include "Archimedes.h"
|
||||
|
||||
#include "modules/WindowModule/WindowModule.h"
|
||||
#include "modules/ImguiModule/ImguiModule.h"
|
||||
|
||||
class Sandbox : public Archimedes::Module {
|
||||
|
||||
public:
|
||||
Sandbox(Archimedes::App*, void*);
|
||||
|
||||
Sandbox() { name = "Sandbox"; }
|
||||
|
||||
~Sandbox();
|
||||
|
||||
void onLoad() override;
|
||||
|
||||
void run() override;
|
||||
|
||||
bool onEvent(const Archimedes::Event& e) override;
|
||||
|
||||
private:
|
||||
|
||||
Archimedes::Window* window;
|
||||
|
||||
std::string cubeVS = "#version 330 core\n"
|
||||
"layout (location = 0) in vec3 aPos;\n"
|
||||
"uniform mat4 model;\n"
|
||||
"uniform uvec2 res;\n"
|
||||
"uniform vec4 clearColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = model * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
||||
"}\0";
|
||||
std::string cubeFS = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"uniform mat4 model;\n"
|
||||
"uniform uvec2 res;\n"
|
||||
"uniform vec4 clearColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(0.6f, 0.1f, 0.1f, 1.0f);\n"
|
||||
"}\n\0";
|
||||
|
||||
std::string gridFS = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"uniform mat4 model;\n"
|
||||
"uniform uvec2 res;\n"
|
||||
"uniform vec4 clearColor;\n"
|
||||
"vec2 pos = gl_FragCoord.xy - res / 2.0f;\n"
|
||||
"int gridSpacing = 50;"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" switch((int(pos.x)) * (int(pos.y))) {\n"
|
||||
" case 0:\n"
|
||||
" FragColor = vec4(1.0f);\n"
|
||||
" break;\n"
|
||||
" default:\n"
|
||||
" switch((int(pos.x) % gridSpacing) * (int(pos.y) % gridSpacing)) {\n"
|
||||
" case 0:\n"
|
||||
" FragColor = vec4(0.7f, 0.7f, 0.7f, 1.0f);\n"
|
||||
" break;\n"
|
||||
" default:\n"
|
||||
" discard;\n"
|
||||
" break;\n"
|
||||
" }\n"
|
||||
" break;\n"
|
||||
" }\n"
|
||||
"}\n\0";
|
||||
|
||||
Archimedes::Shader cubeShader, gridShader;
|
||||
Archimedes::RenderTarget cube, grid;
|
||||
|
||||
float gridVertices[24] = {
|
||||
-1.0f, 0.0f, 1.0f,
|
||||
1.0f, 0.0f, 1.0f,
|
||||
1.0f, 0.0f, -1.0f,
|
||||
-1.0f, 0.0f, -1.0f,
|
||||
};
|
||||
|
||||
unsigned int gridIndices[6] = {
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
3,
|
||||
0
|
||||
};
|
||||
|
||||
float vertices[24] = {
|
||||
-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,
|
||||
|
||||
};
|
||||
|
||||
unsigned int indices[36] = {
|
||||
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 Sandbox mtype;
|
||||
#include "endModule.h"
|
||||
#endif
|
||||
@@ -57,9 +57,9 @@ bool WindowModule::onEvent(const Archimedes::Event& e) {
|
||||
Archimedes::ResizeWindowEvent& event = (Archimedes::ResizeWindowEvent&) e;
|
||||
renderer->w = event.width;
|
||||
renderer->h = event.height;
|
||||
#if WINDOW == 2
|
||||
window->getWindowImpl().setSize(event.width, event.height);
|
||||
#endif
|
||||
|
||||
window->setSize(event.width, event.height);
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::CloseWindowEvent())) {
|
||||
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
|
||||
|
||||
Reference in New Issue
Block a user