Fixed issue with some matrix, added rotate_camera(..)

This commit is contained in:
2025-11-30 17:26:57 +01:00
parent 4cc0c8c73e
commit c5d594c85b
4 changed files with 51 additions and 25 deletions

View File

@@ -1,29 +1,28 @@
package renderer
import "core:math"
import "core:math/linalg"
Camera :: struct {
position: [3]f32,
forward, up: [3]f32,
forward, up, right: [3]f32,
fov: f32,
fov_degrees: f32,
near, far: f32,
yaw, pitch: f32,
}
create_camera :: proc(renderer: ^Renderer, position: [3]f32, forward, up: [3]f32, fov, near, far: f32) -> Camera {
create_camera :: proc(renderer: ^Renderer, position: [3]f32, forward, up: [3]f32, fov_degrees, near, far: f32) -> Camera {
c: Camera
c.position = position
c.forward = forward
c.up = up
c.fov = fov
c.fov_degrees = fov_degrees
c.near = near
c.far = far
assert(c.forward != {})
assert(c.up != {})
assert(fov > 0)
assert(fov_degrees > 0)
assert(near > 0)
assert(far > 0)
@@ -36,23 +35,49 @@ get_camera_view_matrix :: proc(camera: ^Camera) -> (linalg.Matrix4f32, bool) {
}
center := camera.position + camera.forward
view_matrix := linalg.identity(linalg.Matrix4x4f32)
view_matrix *= linalg.matrix4_look_at_f32(camera.position, center, camera.up)
view_matrix := linalg.MATRIX4F32_IDENTITY
view_matrix *= linalg.matrix4_look_at_f32(camera.position, center, camera.up, flip_z_axis = true)
return view_matrix, true
}
get_camera_projection_matrix :: proc(renderer: ^Renderer, camera: ^Camera) -> (linalg.Matrix4f32, bool) {
if camera == nil {
return {}, false
}
projection_matrix := linalg.identity(linalg.Matrix4x4f32)
projection_matrix *= linalg.matrix4_perspective(camera.fov, get_aspect_ratio(renderer), camera.near, camera.far)
projection_matrix := linalg.MATRIX4F32_IDENTITY
projection_matrix *= linalg.matrix4_perspective(
linalg.to_radians(f32(camera.fov_degrees)),
get_aspect_ratio(renderer),
camera.near,
camera.far,
flip_z_axis = true,
)
return projection_matrix, true
}
rotate_camera :: proc(camera: ^Camera, yaw, pitch, roll: f32) {
pitch := clamp(pitch, -89.0, 89.0)
pitch_rad := linalg.to_radians(pitch)
yaw_rad := linalg.to_radians(yaw)
roll_rad := linalg.to_radians(roll)
camera.forward = [3]f32{
math.cos(pitch_rad) * math.cos(yaw_rad),
math.sin(pitch_rad),
math.cos(pitch_rad) * math.sin(yaw_rad),
}
WORLD_UP :: [3]f32{0, 1, 0}
camera.right = linalg.normalize(linalg.cross(camera.forward, WORLD_UP))
camera.up = linalg.normalize(linalg.cross(camera.right, camera.forward))
// TODO: SS - Support 'roll'.
}
set_active_camera :: proc(renderer: ^Renderer, camera: ^Camera) {
renderer.active_camera = camera
}

View File

@@ -19,6 +19,7 @@ Draw_Command :: struct {
mesh: Mesh,
material: Material,
position: [3]f32,
rotation: [3]f32,
scale: [3]f32,
// TODO: SS - Add rotation.
}

View File

@@ -105,18 +105,19 @@ render_frame :: proc(renderer: ^Renderer) {
model_matrix := linalg.identity(linalg.Matrix4x4f32)
scale := linalg.matrix4_scale(command.scale)
rotation := linalg.matrix4_rotate(linalg.to_radians(f32(0.0)), [3]f32 { 1, 0, 0 })
// Translate.
translation := linalg.matrix4_translate(command.position)
// Scale.
model_matrix *= scale
// Rotate.
model_matrix *= rotation
rot_x := linalg.matrix4_rotate(linalg.to_radians(command.rotation.x), [3]f32 { 1, 0, 0 })
rot_y := linalg.matrix4_rotate(linalg.to_radians(command.rotation.y), [3]f32 { 0, 1, 0 })
rot_z := linalg.matrix4_rotate(linalg.to_radians(command.rotation.z), [3]f32 { 0, 0, 1 })
rotation := rot_z * rot_y * rot_x
// Translate.
model_matrix *= translation
// Scale.
scale := linalg.matrix4_scale(command.scale)
model_matrix *= translation * rotation * scale
activate_material(&command.material, model_matrix, view_matrix, projection_matrix)

View File

@@ -169,7 +169,6 @@ when RENDER_BACKEND_OPENGL {
gl.VertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, 8 * size_of(f32), uintptr(6 * size_of(f32)))
gl.EnableVertexAttribArray(2)
}
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)