@@ -34,19 +34,29 @@ impl Bunny {
34
34
self . position [ 0 ] += self . velocity [ 0 ] * delta;
35
35
self . position [ 1 ] += self . velocity [ 1 ] * delta;
36
36
self . velocity [ 1 ] += GRAVITY * delta;
37
+
37
38
if ( self . velocity [ 0 ] > 0.0 && self . position [ 0 ] + 0.5 * BUNNY_SIZE > extent[ 0 ] as f32 )
38
39
|| ( self . velocity [ 0 ] < 0.0 && self . position [ 0 ] - 0.5 * BUNNY_SIZE < 0.0 )
39
40
{
40
41
self . velocity [ 0 ] *= -1.0 ;
41
42
}
43
+
42
44
if self . velocity [ 1 ] < 0.0 && self . position [ 1 ] < 0.5 * BUNNY_SIZE {
43
45
self . velocity [ 1 ] *= -1.0 ;
44
46
}
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
+ }
45
52
}
46
53
}
47
54
48
55
/// Example struct holds references to wgpu resources and frame persistent data
49
56
struct Example {
57
+ view : wgpu:: TextureView ,
58
+ sampler : wgpu:: Sampler ,
59
+ global_bind_group_layout : wgpu:: BindGroupLayout ,
50
60
global_group : wgpu:: BindGroup ,
51
61
local_group : wgpu:: BindGroup ,
52
62
pipeline : wgpu:: RenderPipeline ,
@@ -286,6 +296,7 @@ impl crate::framework::Example for Example {
286
296
size : [ BUNNY_SIZE ; 2 ] ,
287
297
pad : [ 0.0 ; 2 ] ,
288
298
} ;
299
+
289
300
let global_buffer = device. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
290
301
label : Some ( "global" ) ,
291
302
contents : bytemuck:: bytes_of ( & globals) ,
@@ -335,6 +346,9 @@ impl crate::framework::Example for Example {
335
346
let rng = WyRand :: new_seed ( 42 ) ;
336
347
337
348
let mut ex = Example {
349
+ view,
350
+ sampler,
351
+ global_bind_group_layout,
338
352
pipeline,
339
353
global_group,
340
354
local_group,
@@ -366,11 +380,51 @@ impl crate::framework::Example for Example {
366
380
367
381
fn resize (
368
382
& mut self ,
369
- _sc_desc : & wgpu:: SurfaceConfiguration ,
370
- _device : & wgpu:: Device ,
383
+ sc_desc : & wgpu:: SurfaceConfiguration ,
384
+ device : & wgpu:: Device ,
371
385
_queue : & wgpu:: Queue ,
372
386
) {
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;
374
428
}
375
429
376
430
fn render ( & mut self , view : & wgpu:: TextureView , device : & wgpu:: Device , queue : & wgpu:: Queue ) {
0 commit comments