Skip to content

Commit 5eb9786

Browse files
committedMar 3, 2024
Tidy up docs, add Mat3-Mat4 and Vec3-Vec4 functions
1 parent 58e369e commit 5eb9786

File tree

15 files changed

+74
-49
lines changed

15 files changed

+74
-49
lines changed
 

‎gloog-core/src/funcs/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
mod buffers;
2-
mod debug;
3-
mod shaders;
4-
mod uniforms;
5-
mod vertex;
1+
pub(crate) mod buffers;
2+
pub(crate) mod debug;
3+
pub(crate) mod shaders;
4+
pub(crate) mod uniforms;
5+
pub(crate) mod vertex;
66

77
use std::ffi::CStr;
88

‎gloog-core/src/funcs/uniforms.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl GLContext {
4646
///
4747
/// ```
4848
/// # const program: ProgramID = ProgramID::new(0);
49-
/// # fn perspective(_fov_deg: f32, _aspect: f32, _near_clip: f32, _far_clip: f32) {}
49+
/// # fn perspective(_fov_deg: f32, _aspect: f32, _near_clip: f32, _far_clip: f32) -> Mat4 { Mat4::IDENTITY }
5050
///
5151
/// fn draw_loop(gl: &GLContext) {
5252
/// // --- snip ---
@@ -203,7 +203,7 @@ macro_rules! impl_uniform {
203203
type PtrType = GLfloat; // Only `fv` matrices
204204

205205
fn get_ptr(&self) -> *const Self::PtrType {
206-
self.as_ptr()
206+
self.as_ptr().cast()
207207
}
208208

209209
fn count(&self) -> GLsizei {
@@ -276,6 +276,10 @@ impl_uniform!(matrix, Mat2, uniform_matrix_2fv);
276276
impl_uniform!(matrix, Mat3, uniform_matrix_3fv);
277277
impl_uniform!(matrix, Mat4, uniform_matrix_4fv);
278278

279+
impl_uniform!(matrix, [[f32; 2]; 2], uniform_matrix_2fv);
280+
impl_uniform!(matrix, [[f32; 3]; 3], uniform_matrix_3fv);
281+
impl_uniform!(matrix, [[f32; 4]; 4], uniform_matrix_4fv);
282+
279283

280284
/// Because all uniforms make use of the `Uniform*v` functions, it can be safely implemented it for all slices by simply
281285
/// casting the pointer to the slice.

‎gloog-core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod types;
66
pub(crate) use crate::macros::*;
77
use crate::raw::GLPointers;
88
pub use crate::raw::InitFailureMode;
9+
pub use crate::funcs::uniforms::Uniform;
910
use crate::types::DebugMessage;
1011

1112

‎gloog-core/src/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub mod types {
132132
}
133133

134134

135-
/// How [`GLPointers::init`] should behave when a function pointer cannot be loaded.
135+
/// How [`GLPointers::load`] should behave when a function pointer cannot be loaded.
136136
///
137137
/// Be careful with using options other than `Abort`. They should be considered `unsafe`. See their documentation for
138138
/// more information.
@@ -144,7 +144,7 @@ pub enum InitFailureMode {
144144
/// As far as Rust safety guarantees go, **this is the only safe option.** All other options will result in
145145
/// `GLPointers` being partially uninitialized, which violates Rust's initialization invariant.
146146
Abort,
147-
/// When a function pointer fails to load, a warning will [be logged][https://docs.rs/log] and initialization will
147+
/// When a function pointer fails to load, a warning will [be logged](https://docs.rs/log) and initialization will
148148
/// continue. The function pointer will be left as [`null`][std::ptr::null] instead.
149149
///
150150
/// **This option is unsafe.** If a pointer fails to load, [`GLPointers`] will be left partially uninitialized,

‎gloog-math/src/base/matrix/mat3.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bytemuck::{Pod, Zeroable};
22

3-
use crate::Vec3;
3+
use crate::{Mat4, Vec3};
44

55
/// A 3×3 matrix of 32-bit floats.
66
///
@@ -76,6 +76,17 @@ impl Mat3 {
7676
)
7777
}
7878

79+
/// Creates a [`Mat3`] from a [`Mat4`] by trimming out the last row and column.
80+
#[inline]
81+
#[rustfmt::skip]
82+
pub fn from_mat4(mat: &Mat4) -> Mat3 {
83+
Mat3::new(
84+
mat[[0,0]], mat[[0,1]], mat[[0,2]],
85+
mat[[1,0]], mat[[1,1]], mat[[1,2]],
86+
mat[[2,0]], mat[[2,1]], mat[[2,2]],
87+
)
88+
}
89+
7990
/// Computes the determinant of this matrix.
8091
pub fn det(&self) -> f32 {
8192
// See equation 1.94 and 1.95 (p. 47/48) [Foundations of Game Development, Vol. 1]

‎gloog-math/src/base/matrix/mat4.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bytemuck::{Pod, Zeroable};
22

3-
use crate::{Vec3, Vec4};
3+
use crate::{Mat3, Vec3, Vec4};
44

55

66
/// A 4×4 matrix of 32-bit floats.
@@ -90,6 +90,17 @@ impl Mat4 {
9090
)
9191
}
9292

93+
/// Creates a [`Mat3`] by trimming out the last row and column of this matrix.
94+
#[inline]
95+
#[rustfmt::skip]
96+
pub fn to_mat3(&self) -> Mat3 {
97+
Mat3::new(
98+
self[[0,0]], self[[0,1]], self[[0,2]],
99+
self[[1,0]], self[[1,1]], self[[1,2]],
100+
self[[2,0]], self[[2,1]], self[[2,2]],
101+
)
102+
}
103+
93104
/// Function for accessing the columns of this 4D matrix as 3D vectors, getting the bottom row directly, and
94105
/// calculating the intermediate vectors `s`, `t`, `u`, and `v`.
95106
///

‎gloog-math/src/base/vector/vec2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Vec2 {
2121
pub const UNIT_X: Vec2 = Vec2::new(1.0, 0.0);
2222
pub const UNIT_Y: Vec2 = Vec2::new(0.0, 1.0);
2323

24-
/// Creates a new [`Vector3D`] out of this vector's `x` and `y` components and a given `z` component.
24+
/// Creates a new [`Vec3`] out of this vector's `x` and `y` components and a given `z` component.
2525
#[inline]
2626
pub const fn to3(&self, z: f32) -> Vec3 {
2727
Vec3::new(self.x, self.y, z)

‎gloog-math/src/base/vector/vec3.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ impl Vec3 {
4444

4545
// --------------------------------------------------------------------------------------------
4646

47-
/// Creates a new [`Vector4D`] from this vector's `x`, `y`, and `z` components and a given `w` component.
47+
/// Creates a new [`Vec4`] from this vector's `x`, `y`, and `z` components and a given `w` component.
4848
///
49-
/// See also: [`Vector4D::from3`].
49+
/// See also: [`Vec4::from_vec3`].
5050
#[inline]
51-
pub const fn to4(&self, w: f32) -> Vec4 {
51+
pub const fn to_vec4(&self, w: f32) -> Vec4 {
5252
Vec4::new(self.x, self.y, self.z, w)
5353
}
5454

55-
/// Creates a new [`Vector3D`] from a [`Vector2D`] and a float.
55+
/// Creates a new [`Vec3`] out of a [`Vec4`] by ignoring its `w` component.
56+
///
57+
/// See also: [`Vec4::to_vec3`].
58+
#[inline]
59+
pub const fn from_vec4(vec: Vec4) -> Vec3 {
60+
Vec3::new(vec.x, vec.y, vec.z)
61+
}
62+
63+
/// Creates a new [`Vec3`] from a [`Vec2`] and a float.
5664
#[inline]
57-
pub const fn from2(xy: Vec2, z: f32) -> Vec3 {
65+
pub const fn from_vec2(xy: Vec2, z: f32) -> Vec3 {
5866
Vec3::new(xy.x, xy.y, z)
5967
}
6068
}

‎gloog-math/src/base/vector/vec4.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ impl Vec4 {
2727

2828
// --------------------------------------------------------------------------------------------
2929

30-
/// Creates a new [`Vector4D`] out of a [`Vector3D`]'s `x`, `y`, and `z` components and a given `w` component.
30+
/// Creates a new [`Vec4`] out of a [`Vec3`]'s `x`, `y`, and `z` components and a given `w` component.
3131
///
32-
/// See also: [`Vector3D::to4`].
32+
/// See also: [`Vec3::to_vec4`].
3333
#[inline]
34-
pub const fn from3(xyz: Vec3, w: f32) -> Vec4 {
34+
pub const fn from_vec3(xyz: Vec3, w: f32) -> Vec4 {
3535
Vec4::new(xyz.x, xyz.y, xyz.z, w)
3636
}
37+
38+
/// Creates a new [`Vec3`] by ignoring this vector's `w` component.
39+
#[inline]
40+
pub const fn to_vec3(&self) -> Vec3 {
41+
Vec3::new(self.x, self.y, self.z)
42+
}
3743
}
3844

3945
impl FromStr for Vec4 {

‎gloog/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct SceneObject {
1818
/// in, the _highest_ byte of the ID becomes the 4th float, alpha. Additionally, we subtract our values from 255 so
1919
/// they start at 1.0 and transition downwards, giving us brighter colours.
2020
///
21-
/// ```rust,ignore
21+
/// ```text
2222
/// let id: u32 = 0xAA_RR_GG_BB;
2323
/// let id = Vec4 {
2424
/// w: (255 - 0xAA) / 255,

‎gloog/src/loader/obj/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
//!
66
//! For future reference, here are some of the documents I used for the OBJ and MTL specs:
77
//!
8-
//! - https://www.uhu.es/francisco.moreno/gii_rv/practicas/practica08/OBJ_SPEC.PDF
9-
//! - also: https://paulbourke.net/dataformats/obj/ (missing math section)
10-
//! - https://paulbourke.net/dataformats/mtl/
8+
//! - <https://www.uhu.es/francisco.moreno/gii_rv/practicas/practica08/OBJ_SPEC.PDF>
9+
//! - also: <https://paulbourke.net/dataformats/obj/> (missing math section)
10+
//! - <https://paulbourke.net/dataformats/mtl/>
1111
1212
mod error;
1313
mod mtl;

‎src/bin/model-test/main.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use gloog_core::types::{
2121
VertexAttribType,
2222
};
2323
use gloog_core::{GLContext, InitFailureMode};
24-
use gloog_math::{Mat3, Mat4, Vec3, Vec4};
24+
use gloog_math::{Mat4, Vec3, Vec4};
2525
use log::{debug, info, log};
2626

2727

@@ -90,7 +90,7 @@ fn run(model_path: String) -> Result<(), Box<dyn Error>> {
9090
gl.uniform(uniforms.lights[i].ambient, &light.ambient);
9191
gl.uniform(uniforms.lights[i].specular, &light.specular);
9292

93-
let lp4_ws = Vec4::from3(light.position, 1.0);
93+
let lp4_ws = Vec4::from_vec3(light.position, 1.0);
9494
let lp4_vs = view_matrix * lp4_ws;
9595
let lp3_vs = Vec3::new(lp4_vs[0], lp4_vs[1], lp4_vs[2]);
9696

@@ -217,18 +217,10 @@ impl<'gl, 'a> Thingy<'gl, 'a> {
217217
let &Self { gl, model, vao, .. } = self;
218218

219219
let model_matrix = model_matrix(&self.pos, &self.rot, &self.scl);
220-
let normal_matrix = (view_matrix * model_matrix).inverse().transpose();
221-
222-
// TODO: actually write a method to trim a Mat4 down to a Mat3 (and Vec4->Vec3 too lol)
223-
#[rustfmt::skip]
224-
let norm_matrix = Mat3::new(
225-
normal_matrix[[0,0]], normal_matrix[[0,1]], normal_matrix[[0,2]],
226-
normal_matrix[[1,0]], normal_matrix[[1,1]], normal_matrix[[1,2]],
227-
normal_matrix[[2,0]], normal_matrix[[2,1]], normal_matrix[[2,2]],
228-
);
220+
let normal_matrix = (view_matrix * model_matrix).inverse().transpose().to_mat3();
229221

230222
gl.uniform(uniforms.matrix.model, &model_matrix);
231-
gl.uniform(uniforms.matrix.normal, &norm_matrix);
223+
gl.uniform(uniforms.matrix.normal, &normal_matrix);
232224

233225
gl.bind_vertex_array(vao);
234226

‎src/bin/teapot-test/light.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ impl<'gl> Light<'gl> {
191191
gl.uniform(uniforms.ambient, &self.ambient);
192192
gl.uniform(uniforms.specular, &self.specular);
193193

194-
let position4 = Vec4::from3(self.position, 1.0);
195-
let vs_position = view_matrix * position4;
196-
let vs_position = Vec3::new(vs_position.x, vs_position.y, vs_position.z);
194+
let position4 = self.position.to_vec4(1.0);
195+
let vs_position = (view_matrix * position4).to_vec3();
197196
gl.uniform(uniforms.position, &vs_position);
198197
}
199198

‎src/bin/teapot-test/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ fn handle_input(delta_time: f32, keys: &KeyStatus, camera_pos: &mut Vec3, camera
371371
};
372372

373373
let r_mat = axis_angle_matrix(spin_speed, axis);
374-
let r_cam = r_mat * Vec4::from3(*camera_pos, 1.0);
374+
let r_cam = r_mat * Vec4::from_vec3(*camera_pos, 1.0);
375375
*camera_pos = Vec3::new(r_cam.x, r_cam.y, r_cam.z);
376376
} else if let Key::Up | Key::Down = key {
377377
let mut zoom_speed = CAMERA_ZOOM_SPEED * delta_time;

‎src/bin/teapot-test/teapot.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use gloog_core::types::{
1010
VertexAttribType,
1111
};
1212
use gloog_core::GLContext;
13-
use gloog_math::{Mat3, Mat4, Vec3, Vec4};
13+
use gloog_math::{Mat4, Vec3, Vec4};
1414
use rand::distributions::Uniform;
1515
use rand::Rng;
1616

@@ -217,14 +217,7 @@ impl<'gl> Teapot<'gl> {
217217

218218
let ctm = trans_matrix(self.position) * rotate_matrix(self.rotation) * scale_matrix(self.scale);
219219
let mv_matrix = view_matrix * ctm;
220-
221-
let n = mv_matrix.inverse().transpose();
222-
#[rustfmt::skip]
223-
let norm_matrix = Mat3::new(
224-
n[[0,0]], n[[0,1]], n[[0,2]],
225-
n[[1,0]], n[[1,1]], n[[1,2]],
226-
n[[2,0]], n[[2,1]], n[[2,2]],
227-
);
220+
let norm_matrix = mv_matrix.inverse().transpose().to_mat3();
228221

229222
gl.uniform(info.u_model_view_matrix, &mv_matrix);
230223
gl.uniform(info.u_normal_matrix, &norm_matrix);

0 commit comments

Comments
 (0)
Please sign in to comment.