feat: added phong and basic shaders, updated mesh format
This commit is contained in:
46
src/gfx/Color.cpp
Normal file
46
src/gfx/Color.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <cstdint>
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include "Color.h"
|
||||
|
||||
auto hue_to_rgb(float p, float q, float t) -> float {
|
||||
if (t < 0) {
|
||||
t += 1;
|
||||
} else if (t > 1) {
|
||||
t -= 1;
|
||||
}
|
||||
if (t < 1.0f / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1.0f / 2) return q;
|
||||
if (t < 2.0f / 3) return p + (q - p) * (2.0f / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
|
||||
auto hsl_to_hex(float h, float s, float l) -> glm::vec3 {
|
||||
h /= 360;
|
||||
s /= 100;
|
||||
l /= 100;
|
||||
float 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;
|
||||
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);
|
||||
}
|
||||
return glm::vec3(r, g, b);
|
||||
}
|
||||
|
||||
auto Color::color_from_index(int index) -> glm::vec3 {
|
||||
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;
|
||||
return hsl_to_hex(hue, saturation, lightness);
|
||||
}
|
||||
5
src/gfx/Color.h
Normal file
5
src/gfx/Color.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <glm/ext/vector_float3.hpp>
|
||||
|
||||
namespace Color {
|
||||
auto color_from_index(int index) -> glm::vec3;
|
||||
};
|
||||
@@ -6,33 +6,58 @@ auto Mesh::init(const char* obj_file) -> void {
|
||||
auto reader = tinyobj::ObjReader();
|
||||
auto success = reader.ParseFromFile(obj_file);
|
||||
std::cout << reader.Error() << std::endl;
|
||||
|
||||
auto attrib = reader.GetAttrib();
|
||||
|
||||
auto indices_t = reader.GetShapes().at(0).mesh.indices;
|
||||
auto indices = std::vector<unsigned int>(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());
|
||||
|
||||
for (int i = 0; i < indices_t.size(); i++) {
|
||||
indices[i] = indices_t[i].vertex_index;
|
||||
auto 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];
|
||||
|
||||
normals[3*i] = attrib.normals[3*vertex_data.normal_index];
|
||||
normals[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
|
||||
normals[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
|
||||
|
||||
texcoords[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
|
||||
texcoords[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
|
||||
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
num_indices = indices.size();
|
||||
num_indices = indices_t.size();
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo_xyz);
|
||||
glGenBuffers(1, &vbo_uv);
|
||||
glGenBuffers(1, &ebo);
|
||||
glGenBuffers(1, &vbo_norm);
|
||||
//glGenBuffers(1, &ebo);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
|
||||
glBufferData(GL_ARRAY_BUFFER, attrib.vertices.size() * sizeof(float), attrib.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);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv);
|
||||
glBufferData(GL_ARRAY_BUFFER, attrib.texcoords.size() * sizeof(float), attrib.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);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 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);
|
||||
}
|
||||
|
||||
auto Mesh::init(const LeddaGeometry::Shape* shape) -> void {
|
||||
|
||||
@@ -8,6 +8,7 @@ struct Mesh {
|
||||
unsigned int vao;
|
||||
unsigned int vbo_xyz;
|
||||
unsigned int vbo_uv;
|
||||
unsigned int vbo_norm;
|
||||
unsigned int ebo;
|
||||
unsigned int num_indices;
|
||||
auto init(const char* obj_file) -> void;
|
||||
|
||||
Reference in New Issue
Block a user