11use lambda:: {
22 core:: {
33 component:: Component ,
4+ events:: WindowEvent ,
45 render:: {
56 buffer:: BufferBuilder ,
67 command:: RenderCommand ,
@@ -47,7 +48,7 @@ use lambda_platform::{
4748const VERTEX_SHADER_SOURCE : & str = r#"
4849#version 450
4950
50- layout (location = 0) in vec2 vertex_position;
51+ layout (location = 0) in vec3 vertex_position;
5152layout (location = 1) in vec3 vertex_normal;
5253layout (location = 2) in vec3 vertex_color;
5354
@@ -59,7 +60,7 @@ layout ( push_constant ) uniform PushConstant {
5960} push_constants;
6061
6162void main() {
62- gl_Position = push_constants.render_matrix * vec4(vertex_position, 0.0, 1.0);
63+ gl_Position = push_constants.render_matrix * vec4(vertex_position, 1.0);
6364 frag_color = vertex_color;
6465}
6566
@@ -103,9 +104,13 @@ pub fn push_constants_to_bytes(push_constants: &PushConstant) -> &[u32] {
103104pub struct PushConstantsExample {
104105 frame_number : u64 ,
105106 shader : Shader ,
107+ fs : Shader ,
106108 mesh : Option < Mesh > ,
107109 render_pipeline : Option < ResourceId > ,
108110 render_pass : Option < ResourceId > ,
111+ last_frame : std:: time:: Duration ,
112+ width : u32 ,
113+ height : u32 ,
109114}
110115
111116impl Component for PushConstantsExample {
@@ -119,17 +124,17 @@ impl Component for PushConstantsExample {
119124 // Create triangle mesh.
120125 let vertices = [
121126 VertexBuilder :: new ( )
122- . with_position ( [ 0 .0, 0.5 , 0.0 ] )
127+ . with_position ( [ 1 .0, 1.0 , 0.0 ] )
123128 . with_normal ( [ 0.0 , 0.0 , 0.0 ] )
124129 . with_color ( [ 1.0 , 0.0 , 0.0 ] )
125130 . build ( ) ,
126131 VertexBuilder :: new ( )
127- . with_position ( [ -0.5 , - 0.5 , 0.0 ] )
132+ . with_position ( [ -1.0 , 1.0 , 0.0 ] )
128133 . with_normal ( [ 0.0 , 0.0 , 0.0 ] )
129134 . with_color ( [ 0.0 , 1.0 , 0.0 ] )
130135 . build ( ) ,
131136 VertexBuilder :: new ( )
132- . with_position ( [ 0.5 , -0.5 , 0.0 ] )
137+ . with_position ( [ 0.0 , -1.0 , 0.0 ] )
133138 . with_normal ( [ 0.0 , 0.0 , 0.0 ] )
134139 . with_color ( [ 0.0 , 0.0 , 1.0 ] )
135140 . build ( ) ,
@@ -151,24 +156,26 @@ impl Component for PushConstantsExample {
151156 } ,
152157 } ,
153158 VertexAttribute {
154- location: 1 ,
159+ location: 2 ,
155160 offset: 0 ,
156161 element: VertexElement {
157162 format: ColorFormat :: Rgb32Sfloat ,
158- offset: 12 ,
163+ offset: 24 ,
159164 } ,
160165 } ,
161166 ] )
162167 . build ( ) ;
163168
169+ println ! ( "mesh: {:?}" , mesh) ;
170+
164171 let pipeline = RenderPipelineBuilder :: new ( )
165172 . with_push_constant ( PipelineStage :: VERTEX , push_constant_size)
166173 . with_buffer (
167174 BufferBuilder :: build_from_mesh ( & mesh, render_context)
168175 . expect ( "Failed to create buffer" ) ,
169176 mesh. attributes ( ) . to_vec ( ) ,
170177 )
171- . build ( render_context, & render_pass, & self . shader , None ) ;
178+ . build ( render_context, & render_pass, & self . shader , Some ( & self . fs ) ) ;
172179
173180 self . render_pass = Some ( render_context. attach_render_pass ( render_pass) ) ;
174181 self . render_pipeline = Some ( render_context. attach_pipeline ( pipeline) ) ;
@@ -183,11 +190,26 @@ impl Component for PushConstantsExample {
183190 }
184191
185192 fn on_event ( & mut self , event : lambda:: core:: events:: Events ) {
186- println ! ( "Event: {:?}" , event) ;
193+ match event {
194+ lambda:: core:: events:: Events :: Window { event, issued_at } => {
195+ match event {
196+ WindowEvent :: Resize { width, height } => {
197+ self . width = width;
198+ self . height = height;
199+ println ! ( "Window resized to {}x{}" , width, height) ;
200+ }
201+ _ => { }
202+ }
203+ }
204+ _ => { }
205+ }
187206 }
188207
189208 /// Update the frame number every frame.
190- fn on_update ( & mut self , last_frame : & std:: time:: Duration ) { }
209+ fn on_update ( & mut self , last_frame : & std:: time:: Duration ) {
210+ self . last_frame = * last_frame;
211+ self . frame_number += 1 ;
212+ }
191213
192214 fn on_render (
193215 & mut self ,
@@ -198,22 +220,26 @@ impl Component for PushConstantsExample {
198220 let view: [ [ f32 ; 4 ] ; 4 ] = matrix:: translation_matrix ( camera) ;
199221
200222 // Create a projection matrix.
201- let mut projection: [ [ f32 ; 4 ] ; 4 ] =
202- matrix:: perspective_matrix ( 0.25 , 1700.0 / 900.0 , 0.1 , 200.0 ) ;
203- projection. as_mut ( ) [ 1 ] . as_mut ( ) [ 1 ] *= -1.0 ;
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+ ) ;
204229
205230 // Rotate model.
206231 let model: [ [ f32 ; 4 ] ; 4 ] = matrix:: rotate_matrix (
207- matrix:: filled_matrix ( 4 , 4 , 1.0 ) ,
232+ matrix:: identity_matrix ( 4 , 4 ) ,
208233 [ 0.0 , 1.0 , 0.0 ] ,
209- 0.4 * self . frame_number as f32 ,
234+ 0.001 * self . frame_number as f32 ,
210235 ) ;
211236
212237 // Create render matrix.
213238 let mesh_matrix = projection. multiply ( & view) . multiply ( & model) ;
214239
215240 // Create viewport.
216- let viewport = viewport:: ViewportBuilder :: new ( ) . build ( 800 , 600 ) ;
241+ let viewport =
242+ viewport:: ViewportBuilder :: new ( ) . build ( self . width , self . height ) ;
217243
218244 let render_pipeline = self
219245 . render_pipeline
@@ -271,15 +297,27 @@ impl Default for PushConstantsExample {
271297 name : "push_constants" . to_string ( ) ,
272298 } ;
273299
300+ let triangle_fragment_shader = VirtualShader :: Source {
301+ source : FRAGMENT_SHADER_SOURCE . to_string ( ) ,
302+ kind : ShaderKind :: Fragment ,
303+ entry_point : "main" . to_string ( ) ,
304+ name : "push_constants" . to_string ( ) ,
305+ } ;
306+
274307 let mut builder = ShaderBuilder :: new ( ) ;
275308 let shader = builder. build ( triangle_in_3d) ;
309+ let fs = builder. build ( triangle_fragment_shader) ;
276310
277311 return Self {
278312 frame_number : 0 ,
279313 shader,
314+ fs,
315+ last_frame : std:: time:: Duration :: from_secs ( 0 ) ,
280316 mesh : None ,
281317 render_pipeline : None ,
282318 render_pass : None ,
319+ width : 800 ,
320+ height : 600 ,
283321 } ;
284322 }
285323}
@@ -291,6 +329,9 @@ fn main() {
291329 . with_dimensions ( 800 , 600 )
292330 . with_name ( "3D Push Constants Example" ) ;
293331 } )
332+ . with_renderer_configured_as ( |renderer_builder| {
333+ return renderer_builder. with_render_timeout ( 1_000_000_000 ) ;
334+ } )
294335 . with_component ( move |runtime, triangles : PushConstantsExample | {
295336 return ( runtime, triangles) ;
296337 } )
0 commit comments