This commit is contained in:
Daniel Ledda
2025-02-09 21:57:23 +01:00
parent 0d1367fb02
commit 394aa07bb1
2 changed files with 29 additions and 36 deletions

View File

@@ -13,13 +13,13 @@ void main() {
vec3 normal_norm = normalize(normal); vec3 normal_norm = normalize(normal);
vec3 light_direction_norm = normalize(light_pos - frag_position); vec3 light_direction_norm = normalize(light_pos - frag_position);
float ambient_strength = 0.2; float ambient_strength = 0.15;
vec3 ambient = ambient_strength * light_color; vec3 ambient = ambient_strength * light_color;
float diffuse_strength = max(dot(normal_norm, light_direction_norm), 0.0); float diffuse_strength = max(dot(normal_norm, light_direction_norm), 0.0);
vec3 diffuse = diffuse_strength * light_color; vec3 diffuse = diffuse_strength * light_color;
float specular_strength = 0.5; float specular_strength = 0.9;
vec3 view_direction_norm = normalize(camera - frag_position); vec3 view_direction_norm = normalize(camera - frag_position);
vec3 reflect_dir = reflect(-light_direction_norm, normal_norm); vec3 reflect_dir = reflect(-light_direction_norm, normal_norm);
float spec = pow(max(dot(view_direction_norm, reflect_dir), 0.0), 32); float spec = pow(max(dot(view_direction_norm, reflect_dir), 0.0), 32);
@@ -28,25 +28,3 @@ void main() {
vec3 phong = specular + (ambient + diffuse) * solid_color; vec3 phong = specular + (ambient + diffuse) * solid_color;
frag_color = vec4(phong, 1.0); frag_color = vec4(phong, 1.0);
} }
void main_old() {
vec3 light_color = vec3(1, 1, 1);
vec3 light_direction_norm = normalize(light_pos - frag_position);
vec3 normal_norm = normalize(normal);
vec3 eyes = vec3(1, 1, 0);
float ambient_strength = 0.1;
vec3 ambient = ambient_strength * light_color;
float diff = max(dot(normal_norm, light_direction_norm), 0.0);
vec3 diffuse = diff * light_color;
float specular_strength = 0.5;
vec3 view_direction_norm = normalize(eyes - frag_position);
vec3 reflect_dir = reflect(-light_direction_norm, normal_norm);
float spec = pow(max(dot(view_direction_norm, reflect_dir), 0.0), 32);
vec3 specular = specular_strength * spec * light_color;
vec3 result = specular + (ambient + diffuse) * solid_color;
frag_color = vec4(result, 1.0);
}

View File

@@ -1,4 +1,5 @@
// stdlib TODO(djledda): get rid of this // stdlib
// TODO(djledda): get rid of this
#include <vector> #include <vector>
// Graphics bindings and libs // Graphics bindings and libs
@@ -24,6 +25,8 @@
#define TINYOBJLOADER_IMPLEMENTATION #define TINYOBJLOADER_IMPLEMENTATION
#include "lib/loaders/tinyobj.h" #include "lib/loaders/tinyobj.h"
#define PI (real32)3.14159265358979323846264338327950288
struct Entity; struct Entity;
struct SceneGraphNode; struct SceneGraphNode;
uint32 new_entity(); uint32 new_entity();
@@ -124,6 +127,8 @@ struct GlobalAppState {
Shader *active_shader; Shader *active_shader;
std::vector<Polycube> polycubes; std::vector<Polycube> polycubes;
Camera* camera; Camera* camera;
glm::vec3 rotAxisX;
glm::vec3 rotAxisY;
}; };
GlobalAppState app_state = {}; GlobalAppState app_state = {};
@@ -303,24 +308,21 @@ void process_input(GLFWwindow *window, Input *input, Input *prevInput) {
} }
if (input->mouse.btnLeft) { if (input->mouse.btnLeft) {
real64 deltaX = (input->mouse.x - prevInput->mouse.x) * 0.006;
Polycube *current_polycube = &app_state.polycubes[app_state.current_polycube]; Polycube *current_polycube = &app_state.polycubes[app_state.current_polycube];
SceneGraphNode *polycube_graph_node = get_scene_graph_node(current_polycube->graph_node); SceneGraphNode *polycube_graph_node = get_scene_graph_node(current_polycube->graph_node);
real64 deltaX = (input->mouse.x - prevInput->mouse.x) * 0.005;
if (deltaX > 0.00000001 || deltaX < -0.00000001) { if (deltaX > 0.00000001 || deltaX < -0.00000001) {
polycube_graph_node->rotation = glm::quat(glm::vec3(0, deltaX, 0)) * polycube_graph_node->rotation; polycube_graph_node->rotation *= glm::angleAxis((real32)deltaX, app_state.rotAxisY);
} }
real64 deltaY = (input->mouse.y - prevInput->mouse.y) * 0.006;
real64 deltaY = (input->mouse.y - prevInput->mouse.y) * 0.005;
if (deltaY > 0.00000001 || deltaY < -0.00000001) { if (deltaY > 0.00000001 || deltaY < -0.00000001) {
auto eyes = glm::normalize(app_state.camera->pos - polycube_graph_node->translation); polycube_graph_node->rotation = glm::angleAxis(-(real32)deltaY, app_state.rotAxisX) * polycube_graph_node->rotation;
auto rotationAxis = glm::cross(eyes, glm::vec3(0, 1, 0));
auto localRotationAxis = glm::vec3(glm::inverse(polycube_graph_node->world) * glm::vec4(rotationAxis, 0));
auto rotation = glm::quat(deltaY, localRotationAxis);
polycube_graph_node->rotation = rotation * polycube_graph_node->rotation;
} }
} }
} }
uint32 new_entity() { uint32 new_entity() {
entities.emplace_back(); entities.emplace_back();
scene_graph_nodes.emplace_back(); scene_graph_nodes.emplace_back();
@@ -407,7 +409,7 @@ int main_gfx() {
app_state.camera = main_frame.cam; app_state.camera = main_frame.cam;
SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node); SceneGraphNode *light_graph_node = get_scene_graph_node(get_entity(app_state.light)->scene_graph_node);
light_graph_node->translation = glm::vec3(4.0f, 24.0f, 6.0f); light_graph_node->translation = glm::vec3(4.0f, 6.0f, 24.0f);
Shader phong_shader = createShader( Shader phong_shader = createShader(
"./assets/shaders/phong-solid.vertex.glsl"_s, "./assets/shaders/phong-solid.vertex.glsl"_s,
@@ -429,9 +431,20 @@ int main_gfx() {
root_node.children.push_back(app_state.polycubes.back().graph_node); root_node.children.push_back(app_state.polycubes.back().graph_node);
} }
app_state.camera->pos = glm::vec3(4.0f, 4.0f, 4.0f); app_state.camera->pos = glm::vec3(0.0f, 0.0f, 8.0f);
camera_look_at(app_state.camera, 0.0f, 0.0f, 0.0f); camera_look_at(app_state.camera, 0.0f, 0.0f, 0.0f);
SceneGraphNode *reference_polycube_gn = get_scene_graph_node(app_state.polycubes.back().graph_node);
app_state.rotAxisY = glm::normalize(glm::vec4(0, 1, 0, 0) * glm::inverse(reference_polycube_gn->world));
glm::vec3 eyes = glm::normalize(app_state.camera->pos - reference_polycube_gn->translation);
app_state.rotAxisX = glm::normalize(glm::cross(eyes, app_state.rotAxisY));
for (int i = 0; i < ArrayCount(STD_SOMA); i++) {
auto gn = get_scene_graph_node(app_state.polycubes.at(i).graph_node);
gn->rotation *= glm::angleAxis(PI / 4, glm::vec3(1, 0, 0));
gn->rotation *= glm::angleAxis(PI / 4, glm::vec3(0, 1, 0));
}
glUseProgram(app_state.active_shader->prog_id); glUseProgram(app_state.active_shader->prog_id);
setUniformMat4fv(app_state.active_shader, "projection", &main_frame.cam->proj); setUniformMat4fv(app_state.active_shader, "projection", &main_frame.cam->proj);
@@ -440,6 +453,8 @@ int main_gfx() {
real64 last_frame = glfwGetTime(); real64 last_frame = glfwGetTime();
real64 time_delta = 1.0f/60.0f; real64 time_delta = 1.0f/60.0f;
recalculate_scene_graph(&root_node);
glm::vec3 lastLightPos = {}; glm::vec3 lastLightPos = {};
Input prevInput = {}; Input prevInput = {};
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {