Skip to content

Commit 53f3586

Browse files
committed
[update] mesh rendering in push_constants.
1 parent e82d7e4 commit 53f3586

2 files changed

Lines changed: 63 additions & 34 deletions

File tree

lambda/examples/push_constants.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ pub fn push_constants_to_bytes(push_constants: &PushConstant) -> &[u32] {
9999
return bytes;
100100
}
101101

102+
fn make_transform(
103+
translate: [f32; 3],
104+
angle: f32,
105+
scale: f32,
106+
) -> [[f32; 4]; 4] {
107+
let c = angle.cos() * scale;
108+
let s = angle.sin() * scale;
109+
110+
let [x, y, z] = translate;
111+
112+
return [
113+
[c, 0.0, s, 0.0],
114+
[0.0, scale, 0.0, 0.0],
115+
[-s, 0.0, c, 0.0],
116+
[x, y, z, 1.0],
117+
];
118+
}
119+
102120
// --------------------------------- COMPONENT ---------------------------------
103121

104122
pub struct PushConstantsExample {
@@ -190,6 +208,7 @@ impl Component for PushConstantsExample {
190208
}
191209

192210
fn on_event(&mut self, event: lambda::core::events::Events) {
211+
// Only handle resizes.
193212
match event {
194213
lambda::core::events::Events::Window { event, issued_at } => {
195214
match event {
@@ -216,16 +235,12 @@ impl Component for PushConstantsExample {
216235
render_context: &mut lambda::core::render::RenderContext,
217236
) -> Vec<lambda::core::render::command::RenderCommand> {
218237
self.frame_number += 1;
219-
let mut camera = [0.0, 0.0, -2.0];
238+
let camera = [0.0, 0.0, -2.0];
220239
let view: [[f32; 4]; 4] = matrix::translation_matrix(camera);
221240

222241
// Create a projection matrix.
223-
let mut projection: [[f32; 4]; 4] = matrix::perspective_matrix(
224-
0.25,
225-
(self.width / self.height) as f32,
226-
0.1,
227-
200.0,
228-
);
242+
let projection: [[f32; 4]; 4] =
243+
matrix::perspective_matrix(0.25, (4 / 3) as f32, 0.1, 100.0);
229244

230245
// Rotate model.
231246
let model: [[f32; 4]; 4] = matrix::rotate_matrix(
@@ -236,6 +251,8 @@ impl Component for PushConstantsExample {
236251

237252
// Create render matrix.
238253
let mesh_matrix = projection.multiply(&view).multiply(&model);
254+
let mesh_matrix =
255+
make_transform([0.0, 0.0, 0.5], self.frame_number as f32 * 0.01, 0.5);
239256

240257
// Create viewport.
241258
let viewport =
@@ -245,7 +262,7 @@ impl Component for PushConstantsExample {
245262
.render_pipeline
246263
.expect("No render pipeline actively set for rendering.");
247264

248-
let mut commands = vec![
265+
return vec![
249266
RenderCommand::SetViewports {
250267
start_at: 0,
251268
viewports: vec![viewport.clone()],
@@ -280,11 +297,8 @@ impl Component for PushConstantsExample {
280297
RenderCommand::Draw {
281298
vertices: 0..self.mesh.as_ref().unwrap().vertices().len() as u32,
282299
},
300+
RenderCommand::EndRenderPass,
283301
];
284-
285-
commands.push(RenderCommand::EndRenderPass);
286-
287-
return commands;
288302
}
289303
}
290304

lambda/src/math/matrix.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn rotate_matrix<
103103

104104
let angle_in_radians = turns_to_radians(angle_in_turns);
105105
let cosine_of_angle = angle_in_radians.cos();
106-
let sin_of_angle = -angle_in_radians.sin();
106+
let sin_of_angle = angle_in_radians.sin();
107107

108108
let t = 1.0 - cosine_of_angle;
109109
let x = axis_to_rotate.at(0);
@@ -112,27 +112,42 @@ pub fn rotate_matrix<
112112

113113
let mut rotation_matrix = OutputMatrix::default();
114114

115-
let rotation = [
116-
[
117-
t * x * x + cosine_of_angle,
118-
t * x * y - (sin_of_angle * z),
119-
t * x * z + (sin_of_angle * y),
120-
0.0,
121-
],
122-
[
123-
t * x * y + (sin_of_angle * z),
124-
t * y * y + cosine_of_angle,
125-
t * y * z - (sin_of_angle * x),
126-
0.0,
127-
],
128-
[
129-
t * x * z - (sin_of_angle * y),
130-
t * y * z - (sin_of_angle * x),
131-
t * z * z + cosine_of_angle,
132-
0.0,
133-
],
134-
[0.0, 0.0, 0.0, 1.0],
135-
];
115+
let rotation = match (x as u8, y as u8, z as u8) {
116+
(0, 0, 0) => {
117+
// No rotation
118+
return matrix_to_rotate;
119+
}
120+
(0, 0, 1) => {
121+
// Rotate around z-axis
122+
[
123+
[cosine_of_angle, sin_of_angle, 0.0, 0.0],
124+
[-sin_of_angle, cosine_of_angle, 0.0, 0.0],
125+
[0.0, 0.0, 1.0, 0.0],
126+
[0.0, 0.0, 0.0, 1.0],
127+
]
128+
}
129+
(0, 1, 0) => {
130+
// Rotate around y-axis
131+
[
132+
[cosine_of_angle, 0.0, -sin_of_angle, 0.0],
133+
[0.0, 1.0, 0.0, 0.0],
134+
[sin_of_angle, 0.0, cosine_of_angle, 0.0],
135+
[0.0, 0.0, 0.0, 1.0],
136+
]
137+
}
138+
(1, 0, 0) => {
139+
// Rotate around x-axis
140+
[
141+
[1.0, 0.0, 0.0, 0.0],
142+
[0.0, cosine_of_angle, sin_of_angle, 0.0],
143+
[0.0, -sin_of_angle, cosine_of_angle, 0.0],
144+
[0.0, 0.0, 0.0, 1.0],
145+
]
146+
}
147+
_ => {
148+
panic!("Axis must be a unit vector")
149+
}
150+
};
136151

137152
for i in 0..rows {
138153
for j in 0..columns {

0 commit comments

Comments
 (0)