feat: added phong and basic shaders, updated mesh format

This commit is contained in:
Daniel Ledda
2023-01-10 03:54:24 +01:00
parent 93dadfbf4b
commit 658b5d693a
10 changed files with 384 additions and 116 deletions

46
src/gfx/Color.cpp Normal file
View 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);
}