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 numBeamParticles = rsAllocationGetDimX(rsGetAllocation(beamParticles)); 93 for(int i=0; i<numBeamParticles; i++) { 94 float z; 95 if(i < 20) { 96 z = rsRand(4.0f, 10.0f)/2.0f; 97 } else { 98 z = rsRand(4.0f, 35.0f)/2.0f; 99 } 100 beamParticles->position.x = rsRand(-1.25f, 1.25f); 101 beamParticles->position.y = rsRand(-1.05f, 1.205f); 102 103 beamParticles->position.z = z; 104 beamParticles->offsetX = 0; 105 beamParticles++; 106 } 107 } 108 109 int root() { 110 111 newOffset = xOffset*2; 112 rsgClearColor(0.0f, 0.f, 0.f,1.0f); 113 114 if(newOffset != oldOffset) { 115 VertexColor* vert = vertexColors; 116 for(int i=0; i<numVertColors; i++) { 117 vert->offsetX = -xOffset/2.0; 118 vert++; 119 } 120 } 121 122 rsgBindProgramVertex(vertBg); 123 rsgBindProgramFragment(fragBg); 124 125 rsgDrawMesh(gBackgroundMesh); 126 127 Particle_t* beam = beamParticles; 128 Particle_t* particle = dotParticles; 129 130 for(int i=0; i<numDotParticles; i++) { 131 132 if(newOffset==oldOffset) { 133 if(beam->position.x/beam->position.z > 0.5) { 134 beam->position.x = -1.0; 135 } 136 if(particle->position.x/particle->position.z > 0.5) { 137 particle->position.x = -1.0; 138 } 139 140 if(beam->position.y > 1.05) { 141 beam->position.y = -1.05; 142 beam->position.x = rsRand(-1.25f, 1.25f); 143 } else { 144 beam->position.y = beam->position.y + 0.000160*beam->position.z; 145 } 146 if(particle->position.y > 1.25) { 147 particle->position.y = -1.25; 148 particle->position.x = rsRand(0.0f, 3.0f); 149 150 } else { 151 particle->position.y = particle->position.y + 0.00022*particle->position.z; 152 } 153 } 154 155 beam->position.x = beam->position.x + 0.0001*beam->position.z; 156 beam->offsetX = newOffset; 157 beam++; 158 particle->offsetX = newOffset; 159 particle->position.x = particle->position.x + 0.0001560*beam->position.z; 160 particle++; 161 } 162 163 rsgBindProgramVertex(vertDots); 164 rsgBindProgramFragment(fragDots); 165 166 rsgBindTexture(fragDots, 0, textureBeam); 167 rsgDrawMesh(beamMesh); 168 169 rsgBindTexture(fragDots, 0, textureDot); 170 rsgDrawMesh(dotMesh); 171 172 oldOffset = newOffset; 173 174 return 66; 175 176 } 177