Home | History | Annotate | Download | only in balls
      1 #pragma version(1)
      2 #pragma rs java_package_name(com.example.android.rs.balls)
      3 #include "rs_graphics.rsh"
      4 
      5 #include "balls.rsh"
      6 
      7 #pragma stateVertex(parent)
      8 #pragma stateStore(parent)
      9 
     10 rs_program_fragment gPFPoints;
     11 rs_program_fragment gPFLines;
     12 rs_mesh partMesh;
     13 
     14 typedef struct __attribute__((packed, aligned(4))) Point {
     15     float2 position;
     16     float size;
     17 } Point_t;
     18 Point_t *point;
     19 
     20 typedef struct VpConsts {
     21     rs_matrix4x4 MVP;
     22 } VpConsts_t;
     23 VpConsts_t *vpConstants;
     24 
     25 rs_script physics_script;
     26 
     27 Ball_t *balls1;
     28 Ball_t *balls2;
     29 
     30 static int frame = 0;
     31 
     32 void initParts(int w, int h)
     33 {
     34     uint32_t dimX = rsAllocationGetDimX(rsGetAllocation(balls1));
     35 
     36     for (uint32_t ct=0; ct < dimX; ct++) {
     37         balls1[ct].position.x = rsRand(0.f, (float)w);
     38         balls1[ct].position.y = rsRand(0.f, (float)h);
     39         balls1[ct].delta.x = 0.f;
     40         balls1[ct].delta.y = 0.f;
     41         balls1[ct].size = 1.f;
     42 
     43         float r = rsRand(100.f);
     44         if (r > 90.f) {
     45             balls1[ct].size += pow(10.f, rsRand(0.f, 2.f)) * 0.07;
     46         }
     47     }
     48 }
     49 
     50 
     51 
     52 int root() {
     53     rsgClearColor(0.f, 0.f, 0.f, 1.f);
     54 
     55     BallControl_t bc;
     56     Ball_t *bout;
     57 
     58     if (frame & 1) {
     59         bc.ain = rsGetAllocation(balls2);
     60         bc.aout = rsGetAllocation(balls1);
     61         bout = balls2;
     62     } else {
     63         bc.ain = rsGetAllocation(balls1);
     64         bc.aout = rsGetAllocation(balls2);
     65         bout = balls1;
     66     }
     67 
     68     bc.dimX = rsAllocationGetDimX(bc.ain);
     69     bc.dt = 1.f / 30.f;
     70 
     71     rsForEach(physics_script, bc.ain, bc.aout, &bc, sizeof(bc));
     72 
     73     for (uint32_t ct=0; ct < bc.dimX; ct++) {
     74         point[ct].position = bout[ct].position;
     75         point[ct].size = 6.f /*+ bout[ct].color.g * 6.f*/ * bout[ct].size;
     76     }
     77 
     78     frame++;
     79     rsgBindProgramFragment(gPFPoints);
     80     rsgDrawMesh(partMesh);
     81     return 1;
     82 }
     83 
     84