Home | History | Annotate | Download | only in phasebeam
      1 #pragma version(1)
      2 
      3 #pragma rs java_package_name(com.android.phasebeam)
      4 
      5 #include "rs_graphics.rsh"
      6 #pragma stateVertex(parent);
      7 #pragma stateStore(parent);
      8 
      9 rs_allocation textureDot;
     10 rs_allocation textureBeam;
     11 
     12 rs_program_vertex vertBg;
     13 rs_program_fragment fragBg;
     14 
     15 rs_program_vertex vertDots;
     16 rs_program_fragment fragDots;
     17 
     18 static int numBeamParticles;
     19 static int numDotParticles;
     20 static int numVertColors;
     21 
     22 typedef struct __attribute__((packed, aligned(4))) Particle {
     23     float3 position;
     24     float offsetX;
     25 } Particle_t;
     26 
     27 typedef struct VpConsts {
     28     rs_matrix4x4 MVP;
     29     float scaleSize;
     30 } VpConsts_t;
     31 VpConsts_t *vpConstants;
     32 
     33 typedef struct VertexColor_s {
     34     float3 position;
     35     float offsetX;
     36     float4 color;
     37 } VertexColor;
     38 
     39 VertexColor* vertexColors;
     40 Particle_t *dotParticles;
     41 Particle_t *beamParticles;
     42 rs_mesh dotMesh;
     43 rs_mesh beamMesh;
     44 rs_mesh gBackgroundMesh;
     45 
     46 float densityDPI;
     47 float xOffset = 0.5;
     48 
     49 static float screenWidth;
     50 static float screenHeight;
     51 static float halfScreenWidth;
     52 static float quarterScreenWidth;
     53 static float quarterScreenHeight;
     54 static float halfScreenHeight;
     55 
     56 static float newOffset = 0.5;
     57 static float oldOffset = 0.5;
     58 
     59 void positionParticles() {
     60     screenWidth = rsgGetWidth();
     61     screenHeight = rsgGetHeight();
     62     halfScreenWidth = screenWidth/2.0f;
     63     halfScreenHeight = screenHeight/2.0f;
     64     quarterScreenWidth = screenWidth/4.0f;
     65     quarterScreenHeight = screenHeight/4.0f;
     66     Particle_t* particle = dotParticles;
     67     numDotParticles = rsAllocationGetDimX(rsGetAllocation(dotParticles));
     68     numVertColors = rsAllocationGetDimX(rsGetAllocation(vertexColors));
     69     for(int i=0; i<numDotParticles; i++) {
     70         particle->position.x = rsRand(0.0f, 3.0f);
     71         particle->position.y = rsRand(-1.25f, 1.25f);
     72 
     73         float z;
     74         if (i < 3) {
     75             z = 14.0f;
     76         } else if(i < 7) {
     77             z = 25.0f;
     78         } else if(i < 4) {
     79             z = rsRand(10.f, 20.f);
     80         } else if(i == 10) {
     81             z = 24.0f;
     82             particle->position.x = 1.0;
     83         } else {
     84             z = rsRand(6.0f, 14.0f);
     85         }
     86         particle->position.z = z;
     87         particle->offsetX = 0;
     88 
     89         particle++;
     90     }
     91 
     92     Particle_t* beam = beamParticles;
     93     numBeamParticles = rsAllocationGetDimX(rsGetAllocation(beamParticles));
     94     for(int i=0; i<numBeamParticles; i++) {
     95         float z;
     96         if(i < 20) {
     97             z = rsRand(4.0f, 10.0f)/2.0f;
     98         } else {
     99             z = rsRand(4.0f, 35.0f)/2.0f;
    100         }
    101         beamParticles->position.x = rsRand(-1.25f, 1.25f);
    102         beamParticles->position.y = rsRand(-1.05f, 1.205f);
    103 
    104         beamParticles->position.z = z;
    105         beamParticles->offsetX = 0;
    106         beamParticles++;
    107     }
    108 }
    109 
    110 int root() {
    111 
    112     newOffset = xOffset*2;
    113     rsgClearColor(0.0f, 0.f, 0.f,1.0f);
    114 
    115     if(newOffset != oldOffset) {
    116         VertexColor* vert = vertexColors;
    117         for(int i=0; i<numVertColors; i++) {
    118             vert->offsetX = -xOffset/2.0;
    119             vert++;
    120         }
    121     }
    122 
    123     rsgBindProgramVertex(vertBg);
    124     rsgBindProgramFragment(fragBg);
    125 
    126     rsgDrawMesh(gBackgroundMesh);
    127 
    128     Particle_t* beam = beamParticles;
    129     Particle_t* particle = dotParticles;
    130 
    131     for(int i=0; i<numDotParticles; i++) {
    132 
    133         if(newOffset==oldOffset) {
    134             if(beam->position.x/beam->position.z > 0.5) {
    135                 beam->position.x = -1.0;
    136             }
    137             if(particle->position.x/particle->position.z > 0.5) {
    138                 particle->position.x = -1.0;
    139             }
    140 
    141             if(beam->position.y > 1.05) {
    142                 beam->position.y = -1.05;
    143                 beam->position.x = rsRand(-1.25f, 1.25f);
    144             } else {
    145                 beam->position.y = beam->position.y + 0.000160*beam->position.z;
    146             }
    147             if(particle->position.y > 1.25) {
    148                 particle->position.y = -1.25;
    149                 particle->position.x = rsRand(0.0f, 3.0f);
    150 
    151             } else {
    152                 particle->position.y = particle->position.y + 0.00022*particle->position.z;
    153             }
    154         }
    155 
    156         beam->position.x = beam->position.x + 0.0001*beam->position.z;
    157         beam->offsetX = newOffset;
    158         beam++;
    159         particle->offsetX = newOffset;
    160         particle->position.x = particle->position.x + 0.0001560*beam->position.z;
    161         particle++;
    162     }
    163 
    164     rsgBindProgramVertex(vertDots);
    165     rsgBindProgramFragment(fragDots);
    166 
    167     rsgBindTexture(fragDots, 0, textureBeam);
    168     rsgDrawMesh(beamMesh);
    169 
    170     rsgBindTexture(fragDots, 0, textureDot);
    171     rsgDrawMesh(dotMesh);
    172 
    173     oldOffset = newOffset;
    174 
    175     return 66;
    176 
    177 }
    178