update
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include "VoxelSpace.h"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <math.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) {
|
||||
t += 1;
|
||||
} else if (t > 1) {
|
||||
@@ -18,12 +18,12 @@ glm::vec3 hsl_to_hex(real32 h, real32 s, real32 l) {
|
||||
h /= 360;
|
||||
s /= 100;
|
||||
l /= 100;
|
||||
float r, g, b;
|
||||
real32 r, g, b;
|
||||
if (s == 0) {
|
||||
r = g = b = l;
|
||||
} else {
|
||||
auto q = l < 0.5f ? l * (1 + s) : l + s - l * s;
|
||||
auto p = 2 * l - q;
|
||||
real32 q = l < 0.5f ? l * (1 + s) : l + s - l * s;
|
||||
real32 p = 2 * l - q;
|
||||
r = hue_to_rgb(p, q, h + 1.0f / 3);
|
||||
g = hue_to_rgb(p, q, h);
|
||||
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) {
|
||||
auto color_wheel_cycle = floorf(index / 6.0f);
|
||||
auto darkness_cycle = floorf(index / 12.0f);
|
||||
auto spacing = (360.0f / 6.0f);
|
||||
auto offset = color_wheel_cycle == 0 ? 0 : spacing / (color_wheel_cycle + 2);
|
||||
auto hue = spacing * (index % 6) + offset;
|
||||
auto saturation = 100.0f;
|
||||
auto lightness = 1.0f / (2 + darkness_cycle) * 100;
|
||||
real32 color_wheel_cycle = floorf(index / 6.0f);
|
||||
real32 darkness_cycle = floorf(index / 12.0f);
|
||||
real32 spacing = (360.0f / 6.0f);
|
||||
real32 offset = color_wheel_cycle == 0 ? 0 : spacing / (color_wheel_cycle + 2);
|
||||
real32 hue = spacing * (index % 6) + offset;
|
||||
real32 saturation = 100.0f;
|
||||
real32 lightness = 1.0f / (2 + darkness_cycle) * 100;
|
||||
return hsl_to_hex(hue, saturation, lightness);
|
||||
}
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "Mesh.h"
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include "../lib/loaders/tinyobj.h"
|
||||
#include "../lib/djstdlib/core.h"
|
||||
|
||||
auto Mesh::init(const char* obj_file) -> void {
|
||||
auto reader = tinyobj::ObjReader();
|
||||
auto success = reader.ParseFromFile(obj_file);
|
||||
Mesh createMesh(const char* obj_file) {
|
||||
Mesh result = {0};
|
||||
|
||||
tinyobj::ObjReader reader = tinyobj::ObjReader();
|
||||
bool success = reader.ParseFromFile(obj_file);
|
||||
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;
|
||||
auto indices = std::vector<unsigned int>(indices_t.size());
|
||||
std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
|
||||
std::vector<uint32> indices = std::vector<uint32>(indices_t.size());
|
||||
|
||||
auto vertices = std::vector<float>(3*indices_t.size());
|
||||
auto normals = std::vector<float>(3*indices_t.size());
|
||||
auto texcoords = std::vector<float>(2*indices_t.size());
|
||||
std::vector<real32> vertices = std::vector<real32>(3*indices_t.size());
|
||||
std::vector<real32> normals = std::vector<real32>(3*indices_t.size());
|
||||
std::vector<real32> texcoords = std::vector<real32>(2*indices_t.size());
|
||||
|
||||
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+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
|
||||
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;
|
||||
}
|
||||
|
||||
num_indices = indices_t.size();
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo_xyz);
|
||||
glGenBuffers(1, &vbo_uv);
|
||||
glGenBuffers(1, &vbo_norm);
|
||||
result.num_indices = indices_t.size();
|
||||
glGenVertexArrays(1, &result.vao);
|
||||
glGenBuffers(1, &result.vbo_xyz);
|
||||
glGenBuffers(1, &result.vbo_uv);
|
||||
glGenBuffers(1, &result.vbo_norm);
|
||||
//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);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)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);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
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);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
//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 {
|
||||
num_indices = shape->indices_size;
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo_xyz);
|
||||
glGenBuffers(1, &vbo_uv);
|
||||
glGenBuffers(1, &ebo);
|
||||
Mesh createMesh(const Shape* shape) {
|
||||
Mesh result = {0};
|
||||
|
||||
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);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)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);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
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);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,9 @@ struct Mesh {
|
||||
unsigned int vbo_norm;
|
||||
unsigned int ebo;
|
||||
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
|
||||
|
||||
@@ -10,7 +10,7 @@ enum ShaderType {
|
||||
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::ifstream shader_file;
|
||||
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;
|
||||
}
|
||||
|
||||
void Shader::init(const char* vertex_path, const char* fragment_path) {
|
||||
char info_log[512] = {0};
|
||||
uint32 vertex_shader = create_shader(vertex_path, ShaderType::vertex, info_log);
|
||||
uint32 fragment_shader = create_shader(fragment_path, ShaderType::fragment, info_log);
|
||||
Shader createShader(const char* vertex_path, const char* fragment_path) {
|
||||
Shader result = {0};
|
||||
|
||||
prog_id = glCreateProgram();
|
||||
glAttachShader(prog_id, vertex_shader);
|
||||
glAttachShader(prog_id, fragment_shader);
|
||||
glLinkProgram(prog_id);
|
||||
char info_log[512] = {0};
|
||||
uint32 vertex_shader = createGlShader(vertex_path, ShaderType::vertex, info_log);
|
||||
uint32 fragment_shader = createGlShader(fragment_path, ShaderType::fragment, info_log);
|
||||
|
||||
result.prog_id = glCreateProgram();
|
||||
glAttachShader(result.prog_id, vertex_shader);
|
||||
glAttachShader(result.prog_id, fragment_shader);
|
||||
glLinkProgram(result.prog_id);
|
||||
|
||||
int success;
|
||||
glGetProgramiv(prog_id, GL_LINK_STATUS, &success);
|
||||
glGetProgramiv(result.prog_id, GL_LINK_STATUS, &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;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
struct Shader {
|
||||
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
|
||||
|
||||
@@ -3,21 +3,24 @@
|
||||
#include "../lib/loaders/stb_image.h"
|
||||
#include "../lib/glad/glad.h"
|
||||
|
||||
void Texture::init(const char* source_path) {
|
||||
glGenTextures(1, &tex_id);
|
||||
glBindTexture(GL_TEXTURE_2D, tex_id);
|
||||
Texture createTexture(const char* source_path) {
|
||||
Texture result = {0};
|
||||
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_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
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) {
|
||||
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);
|
||||
} else {
|
||||
std::cout << "Failed to load texture." << std::endl;
|
||||
}
|
||||
stbi_image_free(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ struct Texture {
|
||||
unsigned int tex_id;
|
||||
int width;
|
||||
int height;
|
||||
void init(const char* source_path);
|
||||
};
|
||||
|
||||
Texture createTexture(const char* source_path);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,6 +11,7 @@ struct Shape {
|
||||
float* xyz;
|
||||
size_t xyz_size;
|
||||
};
|
||||
|
||||
extern const Shape TRIANGLE;
|
||||
extern const Shape SQUARE;
|
||||
extern const Shape CUBE;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
17
src/main.cpp
17
src/main.cpp
@@ -11,12 +11,9 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include "lib/loaders/stb_image.h"
|
||||
|
||||
#include "gfx/Texture.h"
|
||||
#include "gfx/Mesh.h"
|
||||
#include "gfx/Shader.h"
|
||||
#include "gfx/Color.h"
|
||||
#include "VoxelSpace.h"
|
||||
#include "SomaSolve.h"
|
||||
#include "gfx/main.cpp"
|
||||
#include "VoxelSpace.cpp"
|
||||
#include "SomaSolve.cpp"
|
||||
#include "lib/djstdlib/core.cpp"
|
||||
|
||||
struct Entity;
|
||||
@@ -28,7 +25,7 @@ SceneGraphNode *get_scene_graph_node(int id);
|
||||
int new_graph_node();
|
||||
|
||||
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[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;
|
||||
@@ -197,9 +194,9 @@ std::vector<Entity> entities = std::vector<Entity>();
|
||||
std::vector<SceneGraphNode> scene_graph_nodes = std::vector<SceneGraphNode>();
|
||||
|
||||
void process_input(GLFWwindow *window) {
|
||||
static bool wireframe = false;
|
||||
static bool last_frame_state_press_enter = false;
|
||||
static bool last_frame_state_press = false;
|
||||
local_persist bool wireframe = false;
|
||||
local_persist bool last_frame_state_press_enter = false;
|
||||
local_persist bool last_frame_state_press = false;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
Reference in New Issue
Block a user