Compare commits

...

12 Commits

Author SHA1 Message Date
f75a438422 obj renders 2026-02-23 16:53:57 -06:00
7a2131fc93 text appears 2026-02-23 12:50:16 -06:00
a5c21ea30d work on text 2026-02-22 22:48:26 -06:00
ce43c56ea6 render from json 2026-02-19 22:41:46 -06:00
74c980adb0 render obj file 2026-02-19 07:54:54 -06:00
fec99fc737 render obj file 2026-02-18 23:07:53 -06:00
da1292d9e5 work on object system 2026-02-17 14:09:20 -06:00
e9f1f49416 add JObject 2026-02-16 22:41:42 -06:00
fda88a906b add draw types 2026-02-16 22:41:20 -06:00
5fd5eb892b add nvim devShells 2026-02-16 12:39:33 -06:00
6abcaeb0d1 add zsh devShell 2026-02-16 12:30:25 -06:00
a422e5cfa2 use perspective projection 2026-02-16 12:28:13 -06:00
17 changed files with 1345 additions and 342 deletions

View File

@@ -2,28 +2,109 @@
perSystem = { config, system, pkgs, self', inputs', ... }: {
devShells.default = pkgs.mkShellNoCC {
devShells = {
bash = pkgs.mkShellNoCC {
packages = with pkgs; [
clang
packages = with pkgs; [
clang
glfw
glew
glfw
glew
sdl3
sdl3
curl
glm
nlohmann_json
stb
curl
glm
nlohmann_json
stb
gamenetworkingsockets
gamenetworkingsockets
];
];
shellHook = ''
shellHook = ''
'';
};
nvim-bash = pkgs.mkShellNoCC {
packages = with pkgs; [
clang
glfw
glew
sdl3
curl
glm
nlohmann_json
stb
gamenetworkingsockets
];
shellHook = ''
exec nvim
'';
};
zsh = pkgs.mkShellNoCC {
packages = with pkgs; [
clang
glfw
glew
sdl3
curl
glm
nlohmann_json
stb
gamenetworkingsockets
];
shellHook = ''
export SHELL=$(realpath `which zsh`)
exec zsh
'';
};
nvim-zsh = pkgs.mkShellNoCC {
packages = with pkgs; [
clang
glfw
glew
sdl3
curl
glm
nlohmann_json
stb
gamenetworkingsockets
];
shellHook = ''
export SHELL=$(realpath `which zsh`)
exec nvim
'';
};
default = self'.devShells.bash;
};
};
}

View File

@@ -21,6 +21,8 @@
glm
nlohmann_json
curl
stb
];
buildPhase = ''

View File

@@ -2,6 +2,7 @@
#define PCH_HPP
#include <iostream>
#include <memory>
#include <fstream>
#include <cstring>
#include <cassert>

View File

@@ -0,0 +1,32 @@
#ifndef BODY_H
#define BODY_H
#include "pch.hpp"
#include "extratools.h"
#include "Object.h"
namespace Archimedes {
class Body : public Object {
public:
Body(RenderTarget rt, glm::mat4 t = glm::mat4(1.0f)) : Object(t), mesh(rt) {};
Body(VertexBuffer vb, IndexArray ia, VertexLayout vl, Shader s, std::optional<Texture> tex = std::nullopt, RenderMode rm = RenderMode::Triangles, glm::mat4 t = glm::mat4(1.0f))
: Object(t), mesh(vb, ia, vl, s, tex, rm) {}
Body() : Object(glm::mat4(1.0f)) {}
~Body() {};
RenderTarget& getMesh() { return mesh; }
private:
RenderTarget mesh;
};
}
#endif

View File

@@ -0,0 +1,30 @@
#ifndef CAMERA_H
#define CAMERA_H
#include "pch.hpp"
#include "extratools.h"
#include "Object.h"
namespace Archimedes {
class Camera : public Object {
public:
Camera() : Object(glm::mat4(1.0f)) {}
Camera(glm::mat4 t)
: Object(t) {}
~Camera() {};
const glm::mat4& getPerspective() { return perspective; }
void setPerspective(const glm::mat4 m) { perspective = m; }
private:
glm::mat4 perspective = glm::mat4(1.0f);
};
}
#endif

View File

@@ -5,18 +5,77 @@
#include "extratools.h"
#include "utils/Renderer/RenderTarget.h"
namespace Archimedes {
class Object {
public:
Object() {};
Object(glm::mat4 t)
: worldTransform(t) {}
~Object() {};
///scales an object absolutely
///scale factors less than zero do nothing
float scaleTo(float s) {
if(s > 0) {
worldTransform = glm::scale(worldTransform, glm::vec3(s / scale));
scale = s;
}
return scale;
}
float scaleRel(float s) {
return scaleTo(scale * s);
}
float scaleAdd(float s) {
return scaleTo(scale + s);
}
glm::vec3 moveTo(glm::vec3 pos) {
worldTransform = glm::translate(worldTransform, pos - position);
position = pos;
return position;
}
glm::vec3 moveRel(glm::vec3 pos) {
return moveTo(pos + position);
}
glm::vec3 rotateTo(glm::vec3 angles) {
worldTransform = glm::rotate(worldTransform, angles.x - rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
worldTransform = glm::rotate(worldTransform, angles.y - rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
worldTransform = glm::rotate(worldTransform, angles.z - rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
rotation = angles;
return rotation;
}
glm::vec3 rotateRel(glm::vec3 angles) {
return rotateTo(angles + rotation);
}
glm::vec3& getPosition() { return position; }
glm::vec3& getRotation() { return rotation; }
float& getScale() { return scale; }
const glm::mat4& getTransform() { return worldTransform; }
void setTransform(const glm::mat4 m) { worldTransform = m; }
private:
glm::vec3 position = glm::vec3(0);
glm::vec3 rotation = glm::vec3(0);
float scale = 1.0f;
glm::mat4 worldTransform = glm::mat4(1.0f);
};
}

View File

@@ -7,32 +7,82 @@
namespace Archimedes {
enum class RenderMode {
Points,
Lines,
ConnectedLines,
ConnectedLinesLooped,
Triangles,
Quads,
Polygon
};
class Texture {
public:
Texture() {}
Texture(std::vector<unsigned char> bin, size_t width, size_t height)
: bin(bin), width(width), height(height) {}
~Texture() {}
const void* getBin() { return bin.data(); }
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
size_t getWidth() { return width; }
size_t getHeight() { return height; }
unsigned int id;
private:
std::vector<unsigned char> bin;
bool active = false;
size_t width, height;
};
class LayoutElement {
public:
LayoutElement(unsigned int type, size_t count) : type(type), count(count) {}
enum class Type {
Float,
UInt,
Int,
Double
};
LayoutElement(Type type, size_t count, size_t stride, size_t offset)
: type(type), count(count), stride(stride), offset(offset) {}
~LayoutElement() {}
unsigned int getType() const { return type; }
size_t getSize() const { return size; }
Type getType() const { return type; }
size_t getCount() const { return count; }
size_t getStride() const { return stride; }
size_t getOffset() const { return offset; }
private:
unsigned int type;
size_t size;
Type type;
size_t count;
size_t stride;
size_t offset;
};
class VertexLayout {
public:
VertexLayout() {}
VertexLayout(std::vector<LayoutElement> elements) : elements(elements) {}
~VertexLayout() {}
const std::vector<LayoutElement>& getElements() const { return elements; }
std::vector<LayoutElement>& getElements() { return elements; }
private:
std::vector<LayoutElement> elements;
@@ -41,20 +91,23 @@ namespace Archimedes {
class VertexBuffer {
public:
VertexBuffer(const void* data, size_t size) : data(data), size(size) {
VertexBuffer(const std::vector<float> data) : data(data) {
}
VertexBuffer() {}
~VertexBuffer() {}
const void* getData() const { return data; }
size_t getSize() const { return size; }
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
const void* getData() const { return data.data(); }
size_t getSize() const { return data.size() * sizeof(float); }
unsigned int id;
private:
const void* data;
size_t size;
bool active = false;
std::vector<float> data;
};
class VertexArray {
@@ -65,28 +118,34 @@ namespace Archimedes {
~VertexArray() {}
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
unsigned int id;
private:
bool active = false;
};
class IndexArray {
public:
IndexArray(const unsigned int* indices, size_t count) : indices(indices), count(count) {
IndexArray(const std::vector<unsigned int> indices) : indices(indices) {
}
IndexArray() {}
~IndexArray() {}
const void* getIndicies() const { return indices; }
size_t getCount() const { return count; }
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
const void* getIndicies() const { return indices.data(); }
size_t getCount() const { return indices.size(); }
unsigned int id;
private:
const unsigned int* indices;
size_t count;
bool active = false;
std::vector<unsigned int> indices;
};
class Shader {
@@ -110,14 +169,31 @@ namespace Archimedes {
void init(const std::string& vs, const std::string& fs, LoadType loadType) {
switch(loadType) {
case LoadType::FromSource:
this->vs = vs;
this->fs = fs;
this->shader = source { vs, fs };
break;
case LoadType::FromBin:
break;
case LoadType::FromFileSource:
this->vs = readSourceFile(vs);
this->fs = readSourceFile(fs);
this->shader = source {
readSourceFile(vs),
readSourceFile(fs)
};
break;
case LoadType::FromFileBin:
break;
default:
break;
}
}
void init(const std::vector<unsigned char>& vs, const std::vector<unsigned char>& fs, LoadType loadType) {
switch(loadType) {
case LoadType::FromSource:
break;
case LoadType::FromBin:
this->shader = bin { vs, fs };
break;
case LoadType::FromFileSource:
break;
case LoadType::FromFileBin:
break;
@@ -126,13 +202,31 @@ namespace Archimedes {
}
}
std::string getVSource() const { return vs; }
std::string getFSource() const { return fs; }
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
std::string getVSource() const { return std::get<source>(shader).vertex; }
std::string getFSource() const { return std::get<source>(shader).fragment; }
std::vector<unsigned char>& getVbin() { return std::get<bin>(shader).vertex; }
std::vector<unsigned char>& getFbin() { return std::get<bin>(shader).fragment; }
unsigned int id;
private:
std::string vs;
std::string fs;
struct source {
std::string vertex;
std::string fragment;
};
struct bin {
std::vector<unsigned char> vertex;
std::vector<unsigned char> fragment;
};
std::variant<source, bin> shader;
bool active = false;
std::string readSourceFile(const std::string& path) {
@@ -151,24 +245,39 @@ namespace Archimedes {
return s;
}
std::vector<unsigned char> readBinFile(const std::string& path) {
std::ifstream file(path, std::ios::binary);
if(!file.is_open()) {
return std::vector<unsigned char>();
}
std::vector<unsigned char> s;
s.reserve(10000);
while(!file.eof()) {
s.push_back(file.get());
}
return s;
}
};
class RenderTarget {
public:
RenderTarget(const void* data, size_t size, unsigned int* indices, size_t count, VertexLayout layout, const std::string& vs, const std::string& fs)
: vertexBuffer(data, size),
indexArray(indices, count),
layout(layout),
shader(vs, fs, Shader::LoadType::FromFileSource) {
}
RenderTarget(VertexBuffer vb, IndexArray ia, VertexLayout vl, Shader s)
RenderTarget(VertexBuffer vb, IndexArray ia, VertexLayout vl, Shader s, std::optional<Texture> t = std::nullopt, RenderMode rm = RenderMode::Triangles)
: vertexBuffer(vb),
indexArray(ia),
layout(vl),
shader(s) {
shader(s),
texture(t),
renderMode(rm) {
}
@@ -176,6 +285,18 @@ namespace Archimedes {
~RenderTarget() {}
void setup(VertexBuffer vb, IndexArray ia, VertexLayout vl, Shader s, std::optional<Texture> t = std::nullopt, RenderMode rm = RenderMode::Triangles) {
vertexBuffer = vb;
indexArray = ia;
layout = vl;
shader = s;
texture = t;
renderMode = rm;
}
void setActive(bool b) { active = b; }
const bool getActive() const { return active; }
//private:
VertexBuffer vertexBuffer;
@@ -188,7 +309,12 @@ namespace Archimedes {
Shader shader;
glm::mat4 transform = glm::mat4(1.0f);
std::optional<Texture> texture;
RenderMode renderMode;
bool active = false;
};
}

View File

@@ -12,6 +12,8 @@ namespace Archimedes {
class Renderer {
public:
int w, h;
glm::vec4 clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
@@ -24,21 +26,26 @@ namespace Archimedes {
virtual void render() = 0;
virtual Shader createShader(const std::string& vs, const std::string& fs, const Shader::LoadType& lt) = 0;
virtual void setupShader(Shader& shader) = 0;
virtual void useShader(Shader& shader) = 0;
virtual void setupTexture(Texture& texture) = 0;
virtual void updateTexture(Texture& texture) = 0;
virtual RenderTarget createRenderTarget(
VertexBuffer vb,
IndexArray ia,
VertexLayout layout,
Shader& s
virtual void setupRenderTarget(RenderTarget& rt) = 0;
virtual void updateRenderTarget(RenderTarget& rt) = 0;
virtual void freeRenderTarget(RenderTarget& rt) = 0;
virtual void draw(
const RenderTarget& rt,
const glm::mat4 world = glm::mat4(1.0f),
const glm::mat4 view = glm::mat4(1.0f),
const glm::mat4 proj = glm::mat4(1.0f),
const glm::vec4 color = { 1.0f, 0.0f, 1.0f, 1.0f }
) = 0;
virtual void useRenderTarget(RenderTarget& rt) = 0;
virtual void draw(const RenderTarget&) = 0;
virtual Renderer* getRendererImpl() = 0;
};

View File

@@ -39,60 +39,7 @@ namespace Archimedes {
}
Shader createShader(const std::string& vs, const std::string& fs, const Shader::LoadType& lt) override {
Shader shader(vs, fs, lt);
std::string vss = shader.getVSource();
std::string fss = shader.getFSource();
const char* vsc = vss.c_str();
const char* fsc = fss.c_str();
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vsc, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fsc, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
shader.id = shaderProgram;
return shader;
}
void useShader(Shader& shader) override {
void setupShader(Shader& shader) override {
std::string vss = shader.getVSource();
std::string fss = shader.getFSource();
@@ -141,84 +88,184 @@ namespace Archimedes {
}
RenderTarget createRenderTarget(VertexBuffer vb, IndexArray ia, VertexLayout layout, Shader& s) override {
auto rt = RenderTarget(vb, ia, layout, s);
void setupTexture(Texture& texture) override {
glGenTextures(1, &texture.id);
updateTexture(texture);
}
glGenVertexArrays(1, &rt.vertexArray.id);
void updateTexture(Texture& texture) override {
glBindTexture(GL_TEXTURE_2D, texture.id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture.getWidth(), texture.getHeight(), 0, GL_ALPHA, GL_UNSIGNED_BYTE, texture.getBin());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glGenBuffers(1, &rt.vertexBuffer.id);
glBindVertexArray(rt.vertexArray.id);
glBindBuffer(GL_ARRAY_BUFFER, rt.vertexBuffer.id);
glBufferData(GL_ARRAY_BUFFER, vb.getSize(), vb.getData(), GL_STATIC_DRAW);
glGenBuffers(1, &rt.indexArray.id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.getCount() * sizeof(unsigned int), rt.indexArray.getIndicies(), GL_STATIC_DRAW);
glUseProgram(s.id);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return rt;
};
void useRenderTarget(RenderTarget& rt) override {
void setupRenderTarget(RenderTarget& rt) override {
glGenVertexArrays(1, &rt.vertexArray.id);
glGenBuffers(1, &rt.vertexBuffer.id);
glBindVertexArray(rt.vertexArray.id);
glBindBuffer(GL_ARRAY_BUFFER, rt.vertexBuffer.id);
glBufferData(GL_ARRAY_BUFFER, rt.vertexBuffer.getSize(), rt.vertexBuffer.getData(), GL_STATIC_DRAW);
glGenBuffers(1, &rt.indexArray.id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.getCount() * sizeof(unsigned int), rt.indexArray.getIndicies(), GL_STATIC_DRAW);
glUseProgram(rt.shader.id);
rt.vertexArray.setActive(true);
rt.vertexBuffer.setActive(true);
rt.indexArray.setActive(true);
if(rt.texture.has_value()) {
glGenTextures(1, &rt.texture.value().id);
rt.texture.value().setActive(true);
}
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
updateRenderTarget(rt);
rt.setActive(true);
};
void draw(const RenderTarget& rt) override {
void updateRenderTarget(RenderTarget& rt) override {
if(rt.vertexArray.getActive()) {
glBindVertexArray(rt.vertexArray.id);
if(rt.vertexBuffer.getActive()) {
glBindBuffer(GL_ARRAY_BUFFER, rt.vertexBuffer.id);
glBufferData(GL_ARRAY_BUFFER, rt.vertexBuffer.getSize(), rt.vertexBuffer.getData(), GL_STATIC_DRAW);
}
if(rt.indexArray.getActive()) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, rt.indexArray.getCount() * sizeof(unsigned int), rt.indexArray.getIndicies(), GL_STATIC_DRAW);
}
if(rt.texture.has_value()) {
if(rt.texture.value().getActive()) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rt.texture.value().id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, rt.texture.value().getWidth(), rt.texture.value().getHeight(), 0, GL_RED, GL_UNSIGNED_BYTE, rt.texture.value().getBin());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
}
if(rt.shader.getActive()) {
glUseProgram(rt.shader.id);
}
unsigned int i = 0;
for(LayoutElement e : rt.layout.getElements()) {
switch(e.getType()) {
case LayoutElement::Type::Float:
glVertexAttribPointer(i, e.getCount(), GL_FLOAT, GL_FALSE, e.getStride(), (void*)e.getOffset());
glEnableVertexAttribArray(i);
break;
case LayoutElement::Type::Double:
glVertexAttribPointer(i, e.getCount(), GL_DOUBLE, GL_FALSE, e.getStride(), (void*)e.getOffset());
glEnableVertexAttribArray(i);
break;
case LayoutElement::Type::Int:
glVertexAttribPointer(i, e.getCount(), GL_INT, GL_FALSE, e.getStride(), (void*)e.getOffset());
glEnableVertexAttribArray(i);
break;
case LayoutElement::Type::UInt:
glVertexAttribPointer(i, e.getCount(), GL_UNSIGNED_INT, GL_FALSE, e.getStride(), (void*)e.getOffset());
glEnableVertexAttribArray(i);
break;
default:
break;
}
i++;
}
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
};
void freeRenderTarget(RenderTarget& rt) override {
glDeleteVertexArrays(1, &rt.vertexArray.id);
glDeleteBuffers(1, &rt.vertexBuffer.id);
glDeleteBuffers(1, &rt.indexArray.id);
rt.vertexArray.setActive(false);
rt.vertexBuffer.setActive(false);
rt.indexArray.setActive(false);
rt.setActive(false);
};
void draw(
const RenderTarget& rt,
const glm::mat4 world = glm::mat4(1.0f),
const glm::mat4 view = glm::mat4(1.0f),
const glm::mat4 proj = glm::mat4(1.0f),
const glm::vec4 color = { 1.0f, 0.0f, 1.0f, 1.0f }
) override {
glUseProgram(rt.shader.id);
unsigned int transformLoc = glGetUniformLocation(rt.shader.id, "model");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rt.transform));
unsigned int modelLoc = glGetUniformLocation(rt.shader.id, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(world));
unsigned int viewLoc = glGetUniformLocation(rt.shader.id, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
unsigned int projLoc = glGetUniformLocation(rt.shader.id, "proj");
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
unsigned int colorLoc = glGetUniformLocation(rt.shader.id, "color");
glUniform4f(colorLoc, color.r, color.g, color.b, color.a);
unsigned int resLoc = glGetUniformLocation(rt.shader.id, "res");
glUniform2ui(resLoc, w, h);
unsigned int ccLoc = glGetUniformLocation(rt.shader.id, "clearColor");
glUniform4f(ccLoc, clearColor.r, clearColor.g, clearColor.b, clearColor.a);
if(rt.texture.has_value()) {
if(rt.texture.value().getActive()) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rt.texture.value().id);
glUniform1i(glGetUniformLocation(rt.shader.id, "tex"), 0);
}
}
glBindVertexArray(rt.vertexArray.id);
glDrawElements(GL_TRIANGLES, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
switch(rt.renderMode) {
case RenderMode::Triangles:
glDrawElements(GL_TRIANGLES, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::Lines:
glDrawElements(GL_LINES, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::ConnectedLines:
glDrawElements(GL_LINE_STRIP, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::ConnectedLinesLooped:
glDrawElements(GL_LINE_LOOP, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::Quads:
glDrawElements(GL_QUADS, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::Polygon:
glDrawElements(GL_POLYGON, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
case RenderMode::Points:
glDrawElements(GL_POINTS, rt.indexArray.getCount(), GL_UNSIGNED_INT, nullptr);
break;
default:
break;
}
glBindVertexArray(0);
}

View File

@@ -1,21 +0,0 @@
#ifndef RENDERER_CONCEPTS
#define RENDERER_CONCEPTS
#include "pch.hpp"
namespace Archimedes {
class RendererOpenGL;
class RendererSDL3;
template <class RendererImpl>
concept allowed_renderers = requires (RendererImpl r) {
#ifndef CUSTOM_RENDERER
requires std::same_as<RendererImpl, RendererOpenGL>
|| std::same_as<RendererImpl, RendererSDL3>;
#endif
};
}
#endif

View File

@@ -30,7 +30,7 @@ namespace Archimedes {
std::abort();
}
#ifdef RENDERER_OPENGL
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

View File

@@ -1,25 +0,0 @@
#ifndef WINDOW_CONCEPTS
#define WINDOW_CONCEPTS
#include "pch.hpp"
#include "utils/Renderer/concepts.h"
namespace Archimedes {
template <class R>
class WindowGLFW;
template <class R>
class WindowSDL3;
template <class WindowImpl, class R>
concept allowed_windows = requires (WindowImpl a, R b) {
#ifndef CUSTOM_WINDOW
requires std::same_as<WindowImpl, WindowGLFW<R>>
|| std::same_as<WindowImpl, WindowSDL3<R>>;
#endif
};
}
#endif

View File

@@ -0,0 +1,453 @@
#include "pch.hpp"
#include "utils/Objects/Body.h"
#include <stb/stb_truetype.h>
#include <stb/stb_image_write.h>
#include <GL/glew.h>
namespace Archimedes {
class Text {
unsigned int tex;
std::vector<unsigned char> ttf;
stbtt_bakedchar bcdata[96];
std::string fontPath;
std::string vs = "#version 430 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTex;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = proj * view * model * vec4(aPos.x, 1.0f - aPos.y, aPos.z, 1.0);\n"
" TexCoord = aTex;\n"
"}\0";
std::string fs = "#version 430 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D tex;\n"
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color.rgb, texture(tex, TexCoord).r);\n"
"}\n\0";
public:
Shader shader;
Texture texture;
float fontSize = 200.0f;
Text() {}
~Text() {}
void textInit(std::string p) {
fontPath = p;
unsigned char temp_bitmap[1024*1024];
std::ifstream file(fontPath, std::ios::binary);
file.seekg(0, std::ios::end);
auto&& size = file.tellg();
file.seekg(0, std::ios::beg);
ttf.reserve(size);
if(!file.is_open()) {
std::cout << "ttf won't open!\n";
}
while(!file.eof()) {
ttf.push_back(file.get());
}
file.close();
stbtt_BakeFontBitmap(ttf.data(),0, fontSize, temp_bitmap,1024,1024, 32,96, bcdata);
std::vector<unsigned char> bin;
bin.reserve(1024 * 1024);
for(unsigned char c : temp_bitmap) {
bin.push_back(c);
}
texture = Texture(bin, 1024, 1024);
shader = Shader(vs, fs, Shader::LoadType::FromSource);
}
Body* genText(std::vector<std::string> texts) {
std::vector<float> v;
std::vector<unsigned int> i;
float x = 0, y = 0, oX = 0, dY = fontSize;
unsigned int k = 0;
for(std::string text : texts) {
for(unsigned int j = 0; j < text.length(); j++) {
if (text[j] >= 32) {
stbtt_aligned_quad q;
stbtt_GetBakedQuad(bcdata, 1024,1024, text[j]-32, &x,&y,&q,0);//1=opengl & d3d10+,0=d3d9
//bcdata[text[j]-32].xadvance
v.push_back(q.x0);
v.push_back(q.y0);
v.push_back(0.0f);
v.push_back(q.s0);
v.push_back(q.t0);
v.push_back(q.x1);
v.push_back(q.y0);
v.push_back(0.0f);
v.push_back(q.s1);
v.push_back(q.t0);
v.push_back(q.x1);
v.push_back(q.y1);
v.push_back(0.0f);
v.push_back(q.s1);
v.push_back(q.t1);
v.push_back(q.x0);
v.push_back(q.y1);
v.push_back(0.0f);
v.push_back(q.s0);
v.push_back(q.t1);
i.push_back(k * 4);
i.push_back(k * 4 + 1);
i.push_back(k * 4 + 2);
i.push_back(k * 4 + 2);
i.push_back(k * 4 + 3);
i.push_back(k * 4);
k++;
}
}
x = oX;
y += dY;
}
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout({
LayoutElement(LayoutElement::Type::Float, 3, 5 * sizeof(float), 0),
LayoutElement(LayoutElement::Type::Float, 2, 5 * sizeof(float), 3 * sizeof(float))
}),
shader,
texture,
RenderMode::Triangles,
glm::scale(glm::mat4(1.0f), glm::vec3(0.2f))
);
}
float textWidth(std::string str, float height) {
float w = 0;
for (int i = 0; i < str.length(); i++) {
stbtt_bakedchar c = bcdata[str[i]];
w += c.xadvance;
}
return w * height / fontSize;
}
};
using json = nlohmann::json;
class JObject {
public:
JObject() {}
void load(std::string pathJSON, std::string pathFont) {
std::ifstream file(pathJSON);
if (!file.is_open()) goodJSON = false;
document = json::parse(file);
file.close();
goodJSON = true;
templates = document["Templates"];
objects = document["Objects"];
setup = document["Set Up"];
cameras = document["Cameras"];
lights = document["Lights"];
actions = document["Actions"];
text.textInit(pathFont);
}
bool isValid() const { return goodJSON; }
Body* createCircle(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float d = element["diameter"];
for(int it = 0; it < resolution; it++) {
v.push_back(d / 2.0f * glm::cos(it * 2.0f * glm::pi<float>() / resolution));
v.push_back(d / 2.0f * glm::sin(it * 2.0f * glm::pi<float>() / resolution));
v.push_back(0.0f);
i.push_back(it);
}
return new Body(
VertexBuffer(v),
IndexArray(i),
layout,
shader,
std::nullopt,
RenderMode::ConnectedLinesLooped
);
}
Body* createSquare(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float width = element["width"];
v.push_back(width / 2.0f);
v.push_back(width / 2.0f);
v.push_back(0.0f);
i.push_back(0);
v.push_back(-width / 2.0f);
v.push_back(width / 2.0f);
v.push_back(0.0f);
i.push_back(1);
v.push_back(-width / 2.0f);
v.push_back(-width / 2.0f);
v.push_back(0.0f);
i.push_back(2);
v.push_back(width / 2.0f);
v.push_back(-width / 2.0f);
v.push_back(0.0f);
i.push_back(3);
return new Body(
VertexBuffer(v),
IndexArray(i),
layout,
shader,
std::nullopt,
RenderMode::ConnectedLinesLooped
);
}
Body* createRect(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float width = element["width"];
float height = element["height"];
v.push_back(width / 2.0f);
v.push_back(height / 2.0f);
v.push_back(0.0f);
i.push_back(0);
v.push_back(-width / 2.0f);
v.push_back(height / 2.0f);
v.push_back(0.0f);
i.push_back(1);
v.push_back(-width / 2.0f);
v.push_back(-height / 2.0f);
v.push_back(0.0f);
i.push_back(2);
v.push_back(width / 2.0f);
v.push_back(-height / 2.0f);
v.push_back(0.0f);
i.push_back(3);
return new Body(
VertexBuffer(v),
IndexArray(i),
layout,
shader,
std::nullopt,
RenderMode::ConnectedLinesLooped
);
}
Body* createPoly(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
json coords = element["coords"];
unsigned int j = 0;
for(json::const_iterator it = coords.cbegin(); it != coords.cend(); it++) {
json c = *it;
v.push_back(c["x"]);
v.push_back(c["y"]);
v.push_back(c["z"]);
i.push_back(j++);
}
return new Body(
VertexBuffer(v),
IndexArray(i),
layout,
shader,
std::nullopt,
RenderMode::ConnectedLinesLooped
);
}
Body* createText(json& element, std::string name) {
//do something
json lines = element["text"];
std::vector<std::string> theText;
for (json::const_iterator it = lines.cbegin(); it != lines.cend(); it++)
{
std::string line = *it;
theText.push_back(line);
}
return text.genText(theText);
}
void buildObjects() {
for (json::const_iterator it = objects.cbegin(); it != objects.cend(); it++)
{
json element = *it;
std::string name = element["name"];
std::string type = element["type"];
if (type == "CIRCLE") {
objectMap[name] = createCircle(element, name);
}
if (type == "SQUARE") {
objectMap[name] = createSquare(element, name);
}
if (type == "RECTANGLE") {
objectMap[name] = createRect(element, name);
}
if (type == "POLYGON") {
objectMap[name] = createPoly(element, name);
}
if (type == "TEXT") {
objectMap[name] = createText(element, name);
}
}
}
void objectActions()
{
for (json::const_iterator it = setup.cbegin(); it != setup.cend(); it++)
{
json element = *it;
std::string name = element["name"];
Body* o = objectMap[name];
if (o == nullptr) continue;
if (element.contains("moveto"))
{
json moveTo = element["moveto"];
float x = moveTo["x"];
float y = moveTo["y"];
float z = moveTo["z"];
o->moveTo(glm::vec3(x, y, z));
}
if (element.contains("moveby"))
{
json moveBy = element["moveby"];
float x = moveBy["x"];
float y = moveBy["y"];
float z = moveBy["z"];
o->moveRel(glm::vec3(x, y, z));
}
if (element.contains("rotateto"))
{
json rotateTo = element["rotateto"];
float p = rotateTo["pitch"];
float y = rotateTo["yaw"];
float r = rotateTo["roll"];
o->rotateTo(glm::vec3(p, y, r));
}
if (element.contains("rotateby"))
{
json rotateBy = element["rotateby"];
float p = rotateBy["pitch"];
float y = rotateBy["yaw"];
float r = rotateBy["roll"];
o->rotateRel(glm::vec3(p, y, r));
}
}
}
std::unordered_map<std::string, Body*> objectMap;
Shader shader;
Text text;
private:
Archimedes::VertexLayout layout = Archimedes::VertexLayout({
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3, 0, 0)
});
std::unordered_map<std::string, Body> templateMap;
bool goodJSON = false;
unsigned int resolution = 50;
json document, templates, objects, setup, cameras, lights, actions;
};
}

View File

@@ -3,7 +3,11 @@
#include "Sandbox.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb/stb_truetype.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
Sandbox::Sandbox(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
@@ -51,92 +55,77 @@ void Sandbox::onLoad() {
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);
window->getRenderer()->setupShader(cubeShader);
cube = Archimedes::RenderTarget(
Archimedes::VertexBuffer(vertices, 24 * sizeof(float)),
Archimedes::IndexArray(indices, 36),
Archimedes::VertexLayout(),
cube = Archimedes::Body(
Archimedes::VertexBuffer(vertices),
Archimedes::IndexArray(indices),
layout,
cubeShader
);
window->getRenderer()->useRenderTarget(cube);
window->getRenderer()->setupRenderTarget(cube.getMesh());
grid = Archimedes::Body(
Archimedes::VertexBuffer(gridVertices),
Archimedes::IndexArray(gridIndices),
layout,
cubeShader
);
window->getRenderer()->setupRenderTarget(grid.getMesh());
int w, h;
window->getSize(w, h);
app->emitEvent(new Archimedes::ResizeWindowEvent(w, h));
hexagon = readOBJ("/home/nathan/Projects/tests/buildzone/hexagon_pad.obj", cubeShader);
window->getRenderer()->setupRenderTarget(hexagon.getMesh());
hexagon.scaleTo(0.01f);
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 Sandbox::run() {
static float scale = 100.0f;
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::vec3 camPos(0.0f, 0.0f, 3.0f), camRot(0.0f, 0.0f, 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);
static glm::mat4 orthoCamera, perspCamera, cameraTransform;
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)
));
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);
camPosPrev = camPos;
camRotPrev = camRot;
{
@@ -147,32 +136,121 @@ void Sandbox::run() {
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::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("Properties");
if(jObject.isValid()) {
ImGui::Text("Object 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);
static std::string object = jObject.objectMap.begin()->first;
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);
if(ImGui::BeginCombo("Object", object.c_str())) {
for (auto it : jObject.objectMap)
{
const bool is_selected = (object == it.first);
if (ImGui::Selectable(it.first.c_str(), is_selected)) {
object = it.first;
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
for (auto it : jObject.objectMap) {
if(it.second != nullptr) {
window->getRenderer()->draw(
it.second->getMesh(),
it.second->getTransform(),
camera.getTransform(),
camera.getPerspective(),
color
);
}
}
ImGui::SliderFloat("pitch", &rot.x, -glm::pi<float>(), glm::pi<float>());
ImGui::SliderFloat("yaw", &rot.y, -glm::pi<float>(), glm::pi<float>());
ImGui::SliderFloat("roll", &rot.z, -glm::pi<float>(), glm::pi<float>());
ImGui::SliderFloat("scale", &scale, 0.001f, 10.0f);
ImGui::SliderFloat("x", &pos.x, -10.0f, 10.0f);
ImGui::SliderFloat("y", &pos.y, -10.0f, 10.0f);
ImGui::SliderFloat("z", &pos.z, -10.0f, 10.0f);
ImGui::SliderFloat("r", &color.r, 0.0f, 1.0f);
ImGui::SliderFloat("g", &color.g, 0.0f, 1.0f);
ImGui::SliderFloat("b", &color.b, 0.0f, 1.0f);
ImGui::SliderFloat("a", &color.a, 0.0f, 1.0f);
if(ImGui::Button("Apply") && jObject.objectMap[object] != nullptr) {
pos = jObject.objectMap[object]->moveRel(pos - posPrev);
rot = jObject.objectMap[object]->rotateRel(rot - rotPrev);
//scale = jObject.objectMap[object]->scaleRel(scale / scalePrev);
jObject.objectMap[object]->scaleTo(scale / (float)h);
posPrev = pos;
rotPrev = rot;
//scalePrev = scale;
}
} else {
static std::string pathJSON = "/home/nathan/Projects/tests/buildzone/test.json";
static std::string pathFont = "/nix/store/05zf1s3wlw0k90l34iaby4ysrafv6w95-nerd-fonts-fira-code-3.4.0+6.2/share/fonts/truetype/NerdFonts/FiraCode/FiraCodeNerdFontMono-Regular.ttf";
ImGui::InputText("JSON Path", &pathJSON);
ImGui::InputText("Font Path", &pathFont);
if(ImGui::Button("load")) {
jObject.shader = cubeShader;
jObject.load(pathJSON, pathFont);
window->getRenderer()->setupShader(jObject.text.shader);
//window->getRenderer()->setupTexture(jObject.text.texture);
jObject.buildObjects();
for(auto o : jObject.objectMap) {
if(o.second != nullptr) {
o.second->scaleTo(1.0f / (float)h);
window->getRenderer()->setupRenderTarget(o.second->getMesh());
}
}
jObject.objectActions();
}
}
static bool show_obj = true;
ImGui::Checkbox("show obj", &show_obj);
if(show_obj) {
window->getRenderer()->draw(
hexagon.getMesh(),
hexagon.getTransform(),
camera.getTransform(),
camera.getPerspective(),
color
);
}
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("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("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);
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")) {
@@ -184,8 +262,48 @@ void Sandbox::run() {
}
}
}
bool Sandbox::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

@@ -1,11 +1,18 @@
// 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"
#include "JObject.h"
class Sandbox : public Archimedes::Module {
public:
@@ -25,62 +32,47 @@ class Sandbox : public Archimedes::Module {
Archimedes::Window* window;
std::string cubeVS = "#version 330 core\n"
std::string cubeVS = "#version 430 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 model;\n"
"uniform uvec2 res;\n"
"uniform vec4 clearColor;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" gl_Position = model * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
" gl_Position = proj * view * model * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
std::string cubeFS = "#version 330 core\n"
std::string cubeFS = "#version 430 core\n"
"out vec4 FragColor;\n"
"uniform mat4 model;\n"
"uniform uvec2 res;\n"
"uniform vec4 clearColor;\n"
"uniform mat4 view;\n"
"uniform mat4 proj;\n"
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(0.6f, 0.1f, 0.1f, 1.0f);\n"
" FragColor = color;\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::JObject jObject;
Archimedes::Shader cubeShader, gridShader;
Archimedes::RenderTarget cube, grid;
Archimedes::Body cube, grid, hexagon;
float gridVertices[24] = {
Archimedes::VertexLayout layout = Archimedes::VertexLayout({
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3, 0, 0)
});
Archimedes::Camera camera;
std::vector<float> gridVertices = {
-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] = {
std::vector<unsigned int> gridIndices = {
0,
1,
2,
@@ -89,7 +81,7 @@ class Sandbox : public Archimedes::Module {
0
};
float vertices[24] = {
std::vector<float> vertices = {
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
@@ -103,7 +95,7 @@ class Sandbox : public Archimedes::Module {
};
unsigned int indices[36] = {
std::vector<unsigned int> indices = {
0,
1,
2,
@@ -152,6 +144,79 @@ class Sandbox : public Archimedes::Module {
1,
};
Archimedes::Body readOBJ(std::string path, Archimedes::Shader shader) {
std::ifstream file(path);
if(!file.is_open()) return Archimedes::Body();
std::string s;
std::vector<float> verticies;
std::vector<unsigned int> indicies;
verticies.reserve(3 * 100);
indicies.reserve(100);
unsigned int i = 0;
unsigned int j = 0;
float point;
unsigned int idx;
while(!file.eof()) {
getline(file, s, ' ');
//std::cout << "\nline start: " << s << std::endl;
if(s == "v") {
file >> point;
verticies.push_back(point);
//std::cout << "point1: " << point << std::endl;
file >> point;
verticies.push_back(point);
//std::cout << "point2: " << point << std::endl;
file >> point;
verticies.push_back(point);
//std::cout << "point3: " << point << std::endl;
//std::cout << "index: " << j << std::endl;
getline(file, s);
//file.ignore();
} else if(s == "f") {
file >> idx;
indicies.push_back(--idx);
file.ignore(5, ' ');
file >> idx;
indicies.push_back(--idx);
file.ignore(5, ' ');
file >> idx;
indicies.push_back(--idx);
getline(file, s);
} else {
getline(file, s);
}
}
file.close();
return Archimedes::Body(
Archimedes::VertexBuffer(verticies),
Archimedes::IndexArray(indicies),
Archimedes::VertexLayout({
Archimedes::LayoutElement(Archimedes::LayoutElement::Type::Float, 3, 3 * sizeof(float), 0),
}),
shader
);
}
};
@@ -159,3 +224,5 @@ class Sandbox : public Archimedes::Module {
typedef Sandbox mtype;
#include "endModule.h"
#endif
#endif

View File

@@ -21,6 +21,8 @@
glm
curl
nlohmann_json
stb
];
buildPhase = ''

View File

@@ -112,5 +112,29 @@ void ImguiModule::run() {
bool ImguiModule::onEvent(const Archimedes::Event &e) {
unsigned int type = app->getEventType(e);
ImGuiIO& io = ImGui::GetIO(); (void)io;
if(type == app->getEventType(Archimedes::KeyPressedWindowEvent())) {
return io.WantCaptureKeyboard;
} else if(type == app->getEventType(Archimedes::KeyReleasedWindowEvent())) {
return io.WantCaptureKeyboard;
} else if(type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::ScrollWindowEvent())) {
return io.WantCaptureMouse;
} else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent())) {
return io.WantCaptureMouse;
}
return false;
}