1 #pragma version(1) 2 3 #pragma rs java_package_name(com.android.galaxy4) 4 #include "rs_graphics.rsh" 5 #pragma stateVertex(parent); 6 #pragma stateStore(parent); 7 8 typedef struct __attribute__((packed, aligned(4))) Particle { 9 float3 position; 10 uchar4 color; 11 } Particle_t; 12 13 typedef struct VpConsts { 14 rs_matrix4x4 MVP; 15 } VpConsts_t; 16 VpConsts_t *vpConstants; 17 18 19 // hold clouds 20 Particle_t *spaceClouds; 21 22 // hold bg stars 23 Particle_t *bgStars; 24 25 rs_mesh spaceCloudsMesh; 26 rs_mesh bgStarsMesh; 27 28 rs_program_vertex vertSpaceClouds; 29 rs_program_vertex vertBgStars; 30 rs_program_fragment fragSpaceClouds; 31 rs_program_fragment fragBgStars; 32 rs_program_vertex vertBg; 33 rs_program_fragment fragBg; 34 35 rs_allocation textureSpaceCloud; 36 rs_allocation textureFGStar; 37 rs_allocation textureBg; 38 39 static int gGalaxyRadius = 250; 40 41 float xOffset; 42 43 #define PI 3.1415f 44 #define TWO_PI 6.283f 45 46 /** 47 * Helper function to generate the stars. 48 */ 49 static float randomGauss() { 50 float x1; 51 float x2; 52 float w = 2.f; 53 54 while (w >= 1.0f) { 55 x1 = rsRand(2.0f) - 1.0f; 56 x2 = rsRand(2.0f) - 1.0f; 57 w = x1 * x1 + x2 * x2; 58 } 59 60 w = sqrt(-2.0f * log(w) / w); 61 return x1 * w; 62 } 63 64 static float mapf(float minStart, float minStop, float maxStart, float maxStop, float value) { 65 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart)); 66 } 67 68 69 70 void positionParticles(){ 71 rsDebug("************************&&&&&&&&&&&&&&& Called positionBGStars", rsUptimeMillis()); 72 73 float width = rsgGetWidth(); 74 float height = rsgGetHeight(); 75 76 float scale = gGalaxyRadius / (width * 0.5f); 77 78 // space clouds 79 Particle_t* particle = spaceClouds; 80 int size = rsAllocationGetDimX(rsGetAllocation(spaceClouds)); 81 for(int i=0; i<size; i++){ 82 83 float d = fabs(randomGauss()) * gGalaxyRadius * 0.5f + rsRand(64.0f); 84 85 d = mapf(-4.0f, gGalaxyRadius + 4.0f, 0.0f, scale, d); 86 87 float id = d / gGalaxyRadius; 88 float z = randomGauss() * 0.4f * (1.0f - id); 89 90 if (d > gGalaxyRadius * 0.15f) { 91 z *= 0.6f * (1.0f - id); 92 } else { 93 z *= 0.72f; 94 } 95 96 particle->position.x = rsRand(TWO_PI); 97 particle->position.y = d; 98 particle->position.z = z/5.0f; 99 particle->color = rsPackColorTo8888(1.0f, 0.0f, 1.0f); 100 particle++; 101 } 102 103 // bg stars 104 size = rsAllocationGetDimX(rsGetAllocation(bgStars)); 105 particle = bgStars; 106 for(int i=0; i<size; i++){ 107 float d = fabs(randomGauss()) * gGalaxyRadius * 0.5f + rsRand(64.0f); 108 109 d = mapf(-4.0f, gGalaxyRadius + 4.0f, 0.0f, scale, d); 110 111 float id = d / gGalaxyRadius; 112 float z = randomGauss() * 0.4f * (1.0f - id); 113 114 if (d > gGalaxyRadius * 0.15f) { 115 z *= 0.6f * (1.0f - id); 116 } else { 117 z *= 0.72f; 118 } 119 120 particle->position.x = rsRand(TWO_PI); 121 particle->position.y = d; 122 particle->position.z = z/5.0f; 123 particle->color = rsPackColorTo8888(1.0f, 0.0f, 1.0f); 124 particle++; 125 } 126 127 128 } 129 130 static void drawBg(int width, int height){ 131 rsgBindTexture(fragBg, 0, textureBg); 132 rsgDrawRect(0.0f, 0.0f, width, height, 0.0f); 133 } 134 135 int root(){ 136 float width = rsgGetWidth(); 137 float height = rsgGetHeight(); 138 139 140 rsgClearColor(0.0f, 0.f, 0.f, 0.5f); 141 142 // bg 143 rsgBindProgramVertex(vertBg); 144 rsgBindProgramFragment(fragBg); 145 drawBg(width, height); 146 147 148 // space cloud 149 rsgBindProgramVertex(vertSpaceClouds); 150 int size = rsAllocationGetDimX(rsGetAllocation(spaceClouds)); 151 Particle_t *particle = spaceClouds; 152 153 for(int i=0; i<size; i++){ 154 particle->position.x -= .065; 155 particle++; 156 } 157 rsgBindProgramFragment(fragSpaceClouds); 158 rsgBindTexture(fragSpaceClouds, 0, textureSpaceCloud); 159 rsgBindTexture(fragSpaceClouds, 1, textureFGStar); 160 rsgDrawMesh(spaceCloudsMesh); 161 162 163 164 // bg stars 165 rsgBindProgramVertex(vertBgStars); 166 size = rsAllocationGetDimX(rsGetAllocation(bgStars)); 167 particle = bgStars; 168 169 for(int i=0; i<size; i++){ 170 particle->position.x -= .007; 171 particle++; 172 } 173 rsgBindProgramFragment(fragBgStars); 174 rsgDrawMesh(bgStarsMesh); 175 176 177 return 40; 178 } 179 180