fix normal transform

This commit is contained in:
2025-10-02 12:06:58 +02:00
parent a53a9dac39
commit fee17192e3
2 changed files with 11 additions and 9 deletions

View File

@@ -3,14 +3,15 @@
layout(location=0) in vec3 position;
layout(location=1) in vec4 vertex_colors;
layout(location=2) in vec3 normal;
uniform mat4 transform;
uniform mat4 mvp_transform;
uniform mat4 model_transform;
out vec4 fragment_colors;
out vec3 normal_vec;
void main()
{
gl_Position = transform * vec4(position, 1.0f);
gl_Position = mvp_transform * vec4(position, 1.0f);
fragment_colors = vertex_colors;
normal_vec = normalize(vec3(transform * vec4(normal, 0.0f)));
normal_vec = normalize(mat3(model_transform) * normal);
}

View File

@@ -12,7 +12,7 @@ use crate::toolbox;
unsafe fn draw_scene(node: &SceneNode,
view_projection_matrix: &glm::Mat4,
mut transformation_so_far: glm::Mat4,
mut model_transform: glm::Mat4,
shader: &Shader,
elapsed: f32
) {
@@ -29,10 +29,12 @@ unsafe fn draw_scene(node: &SceneNode,
model_matrix = glm::translate(&model_matrix, &-node.reference_point);
// combine with parent
transformation_so_far = transformation_so_far * model_matrix;
model_transform = model_transform * model_matrix;
let loc = shader.get_uniform_location("transform");
gl::UniformMatrix4fv(loc, 1, gl::FALSE, (view_projection_matrix * transformation_so_far).as_ptr());
let mvp_location = shader.get_uniform_location("mvp_transform");
gl::UniformMatrix4fv(mvp_location, 1, gl::FALSE, (view_projection_matrix * model_transform).as_ptr());
let model_location = shader.get_uniform_location("model_transform");
gl::UniformMatrix4fv(model_location, 1, gl::FALSE, model_transform.as_ptr());
if node.index_count >= 0 {
gl::BindVertexArray(node.vao_id);
@@ -40,7 +42,7 @@ unsafe fn draw_scene(node: &SceneNode,
}
for &child in &node.children {
draw_scene(&*child, view_projection_matrix, transformation_so_far, shader, elapsed);
draw_scene(&*child, view_projection_matrix, model_transform, shader, elapsed);
}
}
@@ -56,7 +58,6 @@ pub enum Nodes {
}
pub struct World {
meshes: HashMap<Nodes, Mesh>,
pub nodes: HashMap<Nodes, ManuallyDrop<Pin<Box<SceneNode>>>>,
}