test
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "extratools.h"
|
||||
|
||||
#include "utils/Renderer/RenderTarget.h"
|
||||
|
||||
#include "Object.h"
|
||||
|
||||
namespace Archimedes {
|
||||
|
||||
27
src/include/utils/Objects/Light.h
Normal file
27
src/include/utils/Objects/Light.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include "pch.hpp"
|
||||
|
||||
#include "extratools.h"
|
||||
|
||||
#include "Object.h"
|
||||
|
||||
namespace Archimedes {
|
||||
|
||||
class Light : public Object {
|
||||
|
||||
public:
|
||||
Light(glm::mat4 t = glm::mat4(1.0f), float a = 2 * glm::pi<float>()) : Object(t), angle(a) {};
|
||||
|
||||
Light() : Object(glm::mat4(1.0f)), angle(2 * glm::pi<float>()) {}
|
||||
|
||||
~Light() {};
|
||||
|
||||
private:
|
||||
|
||||
float angle;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
#include "extratools.h"
|
||||
|
||||
#include "utils/Renderer/RenderTarget.h"
|
||||
|
||||
namespace Archimedes {
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ namespace Archimedes {
|
||||
|
||||
class LayoutElement {
|
||||
|
||||
friend class VertexLayout;
|
||||
|
||||
public:
|
||||
|
||||
enum class Type {
|
||||
@@ -54,8 +56,25 @@ namespace Archimedes {
|
||||
Double
|
||||
};
|
||||
|
||||
LayoutElement(Type type, size_t count, size_t stride, size_t offset)
|
||||
: type(type), count(count), stride(stride), offset(offset) {}
|
||||
LayoutElement(Type type, size_t count)
|
||||
: type(type), count(count), size(count) {
|
||||
switch(type) {
|
||||
case Type::Float:
|
||||
size *= sizeof(float);
|
||||
break;
|
||||
case Type::UInt:
|
||||
size *= sizeof(unsigned int);
|
||||
break;
|
||||
case Type::Int:
|
||||
size *= sizeof(int);
|
||||
break;
|
||||
case Type::Double:
|
||||
size *= sizeof(double);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
~LayoutElement() {}
|
||||
|
||||
@@ -63,16 +82,11 @@ namespace Archimedes {
|
||||
|
||||
size_t getCount() const { return count; }
|
||||
|
||||
size_t getStride() const { return stride; }
|
||||
|
||||
size_t getOffset() const { return offset; }
|
||||
|
||||
private:
|
||||
|
||||
Type type;
|
||||
size_t count;
|
||||
size_t stride;
|
||||
size_t offset;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
class VertexLayout {
|
||||
@@ -83,6 +97,25 @@ namespace Archimedes {
|
||||
~VertexLayout() {}
|
||||
|
||||
std::vector<LayoutElement>& getElements() { return elements; }
|
||||
|
||||
size_t getStride() {
|
||||
size_t stride = 0;
|
||||
for(LayoutElement& e : elements) {
|
||||
stride += e.size;
|
||||
}
|
||||
return stride;
|
||||
}
|
||||
|
||||
size_t getElementOffset(unsigned int idx) {
|
||||
if(idx < 0 || idx >= elements.size()) {
|
||||
return 0;
|
||||
}
|
||||
size_t offset = 0;
|
||||
for(unsigned int i = 0; i < idx; i++) {
|
||||
offset += elements.at(i).size;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<LayoutElement> elements;
|
||||
@@ -256,7 +289,11 @@ namespace Archimedes {
|
||||
|
||||
std::vector<unsigned char> s;
|
||||
|
||||
s.reserve(10000);
|
||||
file.seekg(std::ios::end);
|
||||
|
||||
s.reserve(file.tellg());
|
||||
|
||||
file.seekg(std::ios::beg);
|
||||
|
||||
while(!file.eof()) {
|
||||
s.push_back(file.get());
|
||||
|
||||
@@ -156,31 +156,30 @@ namespace Archimedes {
|
||||
}
|
||||
|
||||
|
||||
unsigned int i = 0;
|
||||
for(LayoutElement e : rt.layout.getElements()) {
|
||||
for(unsigned int i = 0; i < rt.layout.getElements().size(); i++) {
|
||||
|
||||
LayoutElement& e = rt.layout.getElements().at(i);
|
||||
|
||||
switch(e.getType()) {
|
||||
case LayoutElement::Type::Float:
|
||||
glVertexAttribPointer(i, e.getCount(), GL_FLOAT, GL_FALSE, e.getStride(), (void*)e.getOffset());
|
||||
glVertexAttribPointer(i, e.getCount(), GL_FLOAT, GL_FALSE, rt.layout.getStride(), (void*)rt.layout.getElementOffset(i));
|
||||
glEnableVertexAttribArray(i);
|
||||
break;
|
||||
case LayoutElement::Type::Double:
|
||||
glVertexAttribPointer(i, e.getCount(), GL_DOUBLE, GL_FALSE, e.getStride(), (void*)e.getOffset());
|
||||
glVertexAttribPointer(i, e.getCount(), GL_DOUBLE, GL_FALSE, rt.layout.getStride(), (void*)rt.layout.getElementOffset(i));
|
||||
glEnableVertexAttribArray(i);
|
||||
break;
|
||||
case LayoutElement::Type::Int:
|
||||
glVertexAttribPointer(i, e.getCount(), GL_INT, GL_FALSE, e.getStride(), (void*)e.getOffset());
|
||||
glVertexAttribPointer(i, e.getCount(), GL_INT, GL_FALSE, rt.layout.getStride(), (void*)rt.layout.getElementOffset(i));
|
||||
glEnableVertexAttribArray(i);
|
||||
break;
|
||||
case LayoutElement::Type::UInt:
|
||||
glVertexAttribPointer(i, e.getCount(), GL_UNSIGNED_INT, GL_FALSE, e.getStride(), (void*)e.getOffset());
|
||||
glVertexAttribPointer(i, e.getCount(), GL_UNSIGNED_INT, GL_FALSE, rt.layout.getStride(), (void*)rt.layout.getElementOffset(i));
|
||||
glEnableVertexAttribArray(i);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
196
src/modules/Archimedes-Modules/Rubiks/Rubiks.cpp
Normal file
196
src/modules/Archimedes-Modules/Rubiks/Rubiks.cpp
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
144
src/modules/Archimedes-Modules/Rubiks/Rubiks.h
Normal file
144
src/modules/Archimedes-Modules/Rubiks/Rubiks.h
Normal 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
|
||||
58
src/modules/Archimedes-Modules/Rubiks/default.nix
Normal file
58
src/modules/Archimedes-Modules/Rubiks/default.nix
Normal 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
|
||||
'';
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -60,7 +60,7 @@ class Sandbox : public Archimedes::Module {
|
||||
Archimedes::Body cube, grid, hexagon;
|
||||
|
||||
Archimedes::VertexLayout layout = Archimedes::VertexLayout({
|
||||
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3, 0, 0)
|
||||
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3)
|
||||
});
|
||||
|
||||
Archimedes::Camera camera;
|
||||
|
||||
Reference in New Issue
Block a user