This commit is contained in:
Daniel Ledda
2025-01-04 19:19:01 +01:00
parent 256292c20d
commit 50f4501c86
11 changed files with 3213 additions and 1484 deletions

View File

@@ -1,7 +1,4 @@
#include <vector> #include <vector>
#include <bitset>
#include <algorithm>
#include <iostream>
#include <cstdint> #include <cstdint>
#include "VoxelSpace.h" #include "VoxelSpace.h"

View File

@@ -2,7 +2,7 @@
#include <math.h> #include <math.h>
#include "../lib/djstdlib/core.h" #include "../lib/djstdlib/core.h"
real32 hue_to_rgb(float p, float q, float t) { real32 hue_to_rgb(real32 p, real32 q, real32 t) {
if (t < 0) { if (t < 0) {
t += 1; t += 1;
} else if (t > 1) { } else if (t > 1) {
@@ -18,12 +18,12 @@ glm::vec3 hsl_to_hex(real32 h, real32 s, real32 l) {
h /= 360; h /= 360;
s /= 100; s /= 100;
l /= 100; l /= 100;
float r, g, b; real32 r, g, b;
if (s == 0) { if (s == 0) {
r = g = b = l; r = g = b = l;
} else { } else {
auto q = l < 0.5f ? l * (1 + s) : l + s - l * s; real32 q = l < 0.5f ? l * (1 + s) : l + s - l * s;
auto p = 2 * l - q; real32 p = 2 * l - q;
r = hue_to_rgb(p, q, h + 1.0f / 3); r = hue_to_rgb(p, q, h + 1.0f / 3);
g = hue_to_rgb(p, q, h); g = hue_to_rgb(p, q, h);
b = hue_to_rgb(p, q, h - 1.0f / 3); b = hue_to_rgb(p, q, h - 1.0f / 3);
@@ -32,12 +32,12 @@ glm::vec3 hsl_to_hex(real32 h, real32 s, real32 l) {
} }
glm::vec3 color_from_index(int index) { glm::vec3 color_from_index(int index) {
auto color_wheel_cycle = floorf(index / 6.0f); real32 color_wheel_cycle = floorf(index / 6.0f);
auto darkness_cycle = floorf(index / 12.0f); real32 darkness_cycle = floorf(index / 12.0f);
auto spacing = (360.0f / 6.0f); real32 spacing = (360.0f / 6.0f);
auto offset = color_wheel_cycle == 0 ? 0 : spacing / (color_wheel_cycle + 2); real32 offset = color_wheel_cycle == 0 ? 0 : spacing / (color_wheel_cycle + 2);
auto hue = spacing * (index % 6) + offset; real32 hue = spacing * (index % 6) + offset;
auto saturation = 100.0f; real32 saturation = 100.0f;
auto lightness = 1.0f / (2 + darkness_cycle) * 100; real32 lightness = 1.0f / (2 + darkness_cycle) * 100;
return hsl_to_hex(hue, saturation, lightness); return hsl_to_hex(hue, saturation, lightness);
} }

View File

@@ -1,23 +1,28 @@
#include <iostream> #include <iostream>
#include <vector>
#include "Mesh.h" #include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "../lib/loaders/tinyobj.h" #include "../lib/loaders/tinyobj.h"
#include "../lib/djstdlib/core.h"
auto Mesh::init(const char* obj_file) -> void { Mesh createMesh(const char* obj_file) {
auto reader = tinyobj::ObjReader(); Mesh result = {0};
auto success = reader.ParseFromFile(obj_file);
tinyobj::ObjReader reader = tinyobj::ObjReader();
bool success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl; std::cout << reader.Error() << std::endl;
auto attrib = reader.GetAttrib(); const tinyobj::attrib_t attrib = reader.GetAttrib();
auto indices_t = reader.GetShapes().at(0).mesh.indices; std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
auto indices = std::vector<unsigned int>(indices_t.size()); std::vector<uint32> indices = std::vector<uint32>(indices_t.size());
auto vertices = std::vector<float>(3*indices_t.size()); std::vector<real32> vertices = std::vector<real32>(3*indices_t.size());
auto normals = std::vector<float>(3*indices_t.size()); std::vector<real32> normals = std::vector<real32>(3*indices_t.size());
auto texcoords = std::vector<float>(2*indices_t.size()); std::vector<real32> texcoords = std::vector<real32>(2*indices_t.size());
for (int i = 0; i < indices_t.size(); i++) { for (int i = 0; i < indices_t.size(); i++) {
auto vertex_data = indices_t[i]; tinyobj::index_t vertex_data = indices_t[i];
vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index]; vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1]; vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2]; vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
@@ -32,54 +37,59 @@ auto Mesh::init(const char* obj_file) -> void {
indices[i] = i; indices[i] = i;
} }
num_indices = indices_t.size(); result.num_indices = indices_t.size();
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &result.vao);
glGenBuffers(1, &vbo_xyz); glGenBuffers(1, &result.vbo_xyz);
glGenBuffers(1, &vbo_uv); glGenBuffers(1, &result.vbo_uv);
glGenBuffers(1, &vbo_norm); glGenBuffers(1, &result.vbo_norm);
//glGenBuffers(1, &ebo); //glGenBuffers(1, &ebo);
glBindVertexArray(vao); glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo_norm); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_norm);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW); //glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
return result;
} }
auto Mesh::init(const LeddaGeometry::Shape* shape) -> void { Mesh createMesh(const Shape* shape) {
num_indices = shape->indices_size; Mesh result = {0};
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo_xyz);
glGenBuffers(1, &vbo_uv);
glGenBuffers(1, &ebo);
glBindVertexArray(vao); result.num_indices = shape->indices_size;
glGenVertexArrays(1, &result.vao);
glGenBuffers(1, &result.vbo_xyz);
glGenBuffers(1, &result.vbo_uv);
glGenBuffers(1, &result.ebo);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz); glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, shape->xyz_size * sizeof(float), shape->xyz, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, shape->xyz_size * sizeof(float), shape->xyz, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv); glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv);
glBufferData(GL_ARRAY_BUFFER, shape->uv_size * sizeof(float), shape->uv, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, shape->uv_size * sizeof(float), shape->uv, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW);
return result;
} }

View File

@@ -11,8 +11,9 @@ struct Mesh {
unsigned int vbo_norm; unsigned int vbo_norm;
unsigned int ebo; unsigned int ebo;
unsigned int num_indices; unsigned int num_indices;
void init(const char* obj_file);
void init(const Shape* shape);
}; };
Mesh createMesh(const char* obj_file);
Mesh createMesh(const Shape* shape);
#endif #endif

View File

@@ -10,7 +10,7 @@ enum ShaderType {
vertex=GL_VERTEX_SHADER, vertex=GL_VERTEX_SHADER,
}; };
uint32 create_shader(const char* file_path, ShaderType shader_type, char* info_log) { uint32 createGlShader(const char* file_path, ShaderType shader_type, char* info_log) {
std::stringstream shader_stream; std::stringstream shader_stream;
std::ifstream shader_file; std::ifstream shader_file;
shader_file.open(file_path); shader_file.open(file_path);
@@ -33,20 +33,22 @@ uint32 create_shader(const char* file_path, ShaderType shader_type, char* info_l
return vertex_shader; return vertex_shader;
} }
void Shader::init(const char* vertex_path, const char* fragment_path) { Shader createShader(const char* vertex_path, const char* fragment_path) {
char info_log[512] = {0}; Shader result = {0};
uint32 vertex_shader = create_shader(vertex_path, ShaderType::vertex, info_log);
uint32 fragment_shader = create_shader(fragment_path, ShaderType::fragment, info_log);
prog_id = glCreateProgram(); char info_log[512] = {0};
glAttachShader(prog_id, vertex_shader); uint32 vertex_shader = createGlShader(vertex_path, ShaderType::vertex, info_log);
glAttachShader(prog_id, fragment_shader); uint32 fragment_shader = createGlShader(fragment_path, ShaderType::fragment, info_log);
glLinkProgram(prog_id);
result.prog_id = glCreateProgram();
glAttachShader(result.prog_id, vertex_shader);
glAttachShader(result.prog_id, fragment_shader);
glLinkProgram(result.prog_id);
int success; int success;
glGetProgramiv(prog_id, GL_LINK_STATUS, &success); glGetProgramiv(result.prog_id, GL_LINK_STATUS, &success);
if (!success) { if (!success) {
glGetProgramInfoLog(prog_id, 512, NULL, info_log); glGetProgramInfoLog(result.prog_id, 512, NULL, info_log);
std::cout << "ERROR::SHADER::PROGRAM::LINK_FAILED\n" << info_log << std::endl; std::cout << "ERROR::SHADER::PROGRAM::LINK_FAILED\n" << info_log << std::endl;
} }

View File

@@ -3,7 +3,8 @@
struct Shader { struct Shader {
unsigned int prog_id; unsigned int prog_id;
void init(const char* vertex_path, const char* fragment_path);
}; };
Shader createShader(const char* vertex_path, const char* fragment_path);
#endif #endif

View File

@@ -3,21 +3,24 @@
#include "../lib/loaders/stb_image.h" #include "../lib/loaders/stb_image.h"
#include "../lib/glad/glad.h" #include "../lib/glad/glad.h"
void Texture::init(const char* source_path) { Texture createTexture(const char* source_path) {
glGenTextures(1, &tex_id); Texture result = {0};
glBindTexture(GL_TEXTURE_2D, tex_id); glGenTextures(1, &result.tex_id);
glBindTexture(GL_TEXTURE_2D, result.tex_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int nr_channels; int nr_channels;
stbi_uc *data = stbi_load(source_path, &width, &height, &nr_channels, 0); stbi_uc *data = stbi_load(source_path, &result.width, &result.height, &nr_channels, 0);
if (data) { if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, result.width, result.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
} else { } else {
std::cout << "Failed to load texture." << std::endl; std::cout << "Failed to load texture." << std::endl;
} }
stbi_image_free(data); stbi_image_free(data);
return result;
} }

View File

@@ -5,7 +5,8 @@ struct Texture {
unsigned int tex_id; unsigned int tex_id;
int width; int width;
int height; int height;
void init(const char* source_path);
}; };
Texture createTexture(const char* source_path);
#endif #endif

View File

@@ -11,6 +11,7 @@ struct Shape {
float* xyz; float* xyz;
size_t xyz_size; size_t xyz_size;
}; };
extern const Shape TRIANGLE; extern const Shape TRIANGLE;
extern const Shape SQUARE; extern const Shape SQUARE;
extern const Shape CUBE; extern const Shape CUBE;

File diff suppressed because it is too large Load Diff

View File

@@ -11,12 +11,9 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "lib/loaders/stb_image.h" #include "lib/loaders/stb_image.h"
#include "gfx/Texture.h" #include "gfx/main.cpp"
#include "gfx/Mesh.h" #include "VoxelSpace.cpp"
#include "gfx/Shader.h" #include "SomaSolve.cpp"
#include "gfx/Color.h"
#include "VoxelSpace.h"
#include "SomaSolve.h"
#include "lib/djstdlib/core.cpp" #include "lib/djstdlib/core.cpp"
struct Entity; struct Entity;
@@ -28,7 +25,7 @@ SceneGraphNode *get_scene_graph_node(int id);
int new_graph_node(); int new_graph_node();
void print_mat(glm::mat4* matrix) { void print_mat(glm::mat4* matrix) {
auto mat = *matrix; glm::mat4 mat = *matrix;
std::cout << mat[0][0] << mat[0][1] << mat[0][2] << mat[0][3] << std::endl; std::cout << mat[0][0] << mat[0][1] << mat[0][2] << mat[0][3] << std::endl;
std::cout << mat[1][0] << mat[1][1] << mat[1][2] << mat[1][3] << std::endl; std::cout << mat[1][0] << mat[1][1] << mat[1][2] << mat[1][3] << std::endl;
std::cout << mat[2][0] << mat[2][1] << mat[2][2] << mat[2][3] << std::endl; std::cout << mat[2][0] << mat[2][1] << mat[2][2] << mat[2][3] << std::endl;
@@ -197,9 +194,9 @@ std::vector<Entity> entities = std::vector<Entity>();
std::vector<SceneGraphNode> scene_graph_nodes = std::vector<SceneGraphNode>(); std::vector<SceneGraphNode> scene_graph_nodes = std::vector<SceneGraphNode>();
void process_input(GLFWwindow *window) { void process_input(GLFWwindow *window) {
static bool wireframe = false; local_persist bool wireframe = false;
static bool last_frame_state_press_enter = false; local_persist bool last_frame_state_press_enter = false;
static bool last_frame_state_press = false; local_persist bool last_frame_state_press = false;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);