@@ -34,19 +34,29 @@ impl Bunny {
3434 self . position [ 0 ] += self . velocity [ 0 ] * delta;
3535 self . position [ 1 ] += self . velocity [ 1 ] * delta;
3636 self . velocity [ 1 ] += GRAVITY * delta;
37+
3738 if ( self . velocity [ 0 ] > 0.0 && self . position [ 0 ] + 0.5 * BUNNY_SIZE > extent[ 0 ] as f32 )
3839 || ( self . velocity [ 0 ] < 0.0 && self . position [ 0 ] - 0.5 * BUNNY_SIZE < 0.0 )
3940 {
4041 self . velocity [ 0 ] *= -1.0 ;
4142 }
43+
4244 if self . velocity [ 1 ] < 0.0 && self . position [ 1 ] < 0.5 * BUNNY_SIZE {
4345 self . velocity [ 1 ] *= -1.0 ;
4446 }
47+
48+ // Top boundary check
49+ if self . velocity [ 1 ] > 0.0 && self . position [ 1 ] + 0.5 * BUNNY_SIZE > extent[ 1 ] as f32 {
50+ self . velocity [ 1 ] *= -1.0 ;
51+ }
4552 }
4653}
4754
4855/// Example struct holds references to wgpu resources and frame persistent data
4956struct Example {
57+ view : wgpu:: TextureView ,
58+ sampler : wgpu:: Sampler ,
59+ global_bind_group_layout : wgpu:: BindGroupLayout ,
5060 global_group : wgpu:: BindGroup ,
5161 local_group : wgpu:: BindGroup ,
5262 pipeline : wgpu:: RenderPipeline ,
@@ -286,6 +296,7 @@ impl crate::framework::Example for Example {
286296 size : [ BUNNY_SIZE ; 2 ] ,
287297 pad : [ 0.0 ; 2 ] ,
288298 } ;
299+
289300 let global_buffer = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
290301 label : Some ( "global" ) ,
291302 contents : bytemuck:: bytes_of ( & globals) ,
@@ -335,6 +346,9 @@ impl crate::framework::Example for Example {
335346 let rng = WyRand :: new_seed ( 42 ) ;
336347
337348 let mut ex = Example {
349+ view,
350+ sampler,
351+ global_bind_group_layout,
338352 pipeline,
339353 global_group,
340354 local_group,
@@ -366,11 +380,51 @@ impl crate::framework::Example for Example {
366380
367381 fn resize (
368382 & mut self ,
369- _sc_desc : & wgpu:: SurfaceConfiguration ,
370- _device : & wgpu:: Device ,
383+ sc_desc : & wgpu:: SurfaceConfiguration ,
384+ device : & wgpu:: Device ,
371385 _queue : & wgpu:: Queue ,
372386 ) {
373- //empty
387+ self . extent = [ sc_desc. width , sc_desc. height ] ;
388+
389+ let globals = Globals {
390+ mvp : glam:: Mat4 :: orthographic_rh (
391+ 0.0 ,
392+ sc_desc. width as f32 ,
393+ 0.0 ,
394+ sc_desc. height as f32 ,
395+ -1.0 ,
396+ 1.0 ,
397+ )
398+ . to_cols_array_2d ( ) ,
399+ size : [ BUNNY_SIZE ; 2 ] ,
400+ pad : [ 0.0 ; 2 ] ,
401+ } ;
402+
403+ let global_buffer = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
404+ label : Some ( "global" ) ,
405+ contents : bytemuck:: bytes_of ( & globals) ,
406+ usage : wgpu:: BufferUsages :: COPY_DST | wgpu:: BufferUsages :: UNIFORM ,
407+ } ) ;
408+
409+ let global_group = device. create_bind_group ( & wgpu:: BindGroupDescriptor {
410+ layout : & self . global_bind_group_layout ,
411+ entries : & [
412+ wgpu:: BindGroupEntry {
413+ binding : 0 ,
414+ resource : global_buffer. as_entire_binding ( ) ,
415+ } ,
416+ wgpu:: BindGroupEntry {
417+ binding : 1 ,
418+ resource : wgpu:: BindingResource :: TextureView ( & self . view ) ,
419+ } ,
420+ wgpu:: BindGroupEntry {
421+ binding : 2 ,
422+ resource : wgpu:: BindingResource :: Sampler ( & self . sampler ) ,
423+ } ,
424+ ] ,
425+ label : None ,
426+ } ) ;
427+ self . global_group = global_group;
374428 }
375429
376430 fn render ( & mut self , view : & wgpu:: TextureView , device : & wgpu:: Device , queue : & wgpu:: Queue ) {
0 commit comments