1 // Copyright (C) 2009 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #pragma version(1) 16 #pragma stateVertex(PVBackground) 17 #pragma stateRaster(parent) 18 #pragma stateStore(PFSBackground) 19 20 #define RSID_POINTS 1 21 22 void dumpState() { 23 24 // debugF("@@@@@ yrot: ", State->yRotation); 25 26 } 27 28 29 void drawVU(float* ident) { 30 31 int i; 32 33 float mat1[16]; 34 float scale = 0.0041; 35 36 matrixLoadMat(mat1,ident); 37 matrixRotate(mat1, 0.f, 0.f, 0.f, 1.f); 38 matrixScale(mat1, scale, scale, scale); 39 vpLoadModelMatrix(mat1); 40 41 bindProgramFragment(NAMED_PFBackgroundMip); 42 43 // draw the background image (416x233) 44 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_background); 45 drawQuadTexCoords( 46 -208.0f, -33.0f, 600.0f, // space 47 0.09375f, 0.9551f, // texture 48 208, -33.0f, 600.0f, // space 49 0.90625, 0.9551f, // texture 50 208, 200.0f, 600.0f, // space 51 0.90625, 0.0449f, // texture 52 -208.0f, 200.0f, 600.0f, // space 53 0.09375f, 0.0449f); // texture 54 55 // draw the peak indicator light (56x58) 56 if (State->mPeak > 0) { 57 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_peak_on); 58 } else { 59 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_peak_off); 60 } 61 drawQuadTexCoords( 62 140.0f, 70.0f, 600.0f, // space 63 0.0625f, 0.953125, // texture 64 196, 70.0f, 600.0f, // space 65 0.9375f, 0.953125, // texture 66 196, 128.0f, 600.0f, // space 67 0.9375f, 0.046875, // texture 68 140.0f, 128.0f, 600.0f, // space 69 0.0625f, 0.046875); // texture 70 71 72 73 // Draw the needle (88x262, center of rotation at 44,217 from top left) 74 75 // set matrix so point of rotation becomes origin 76 matrixLoadMat(mat1,ident); 77 matrixTranslate(mat1, 0.f, -57.0f * scale, 0.f); 78 matrixRotate(mat1, State->mAngle - 90.f, 0.f, 0.f, 1.f); 79 matrixScale(mat1, scale, scale, scale); 80 vpLoadModelMatrix(mat1); 81 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_needle); 82 drawQuadTexCoords( 83 -44.0f, -102.0f+57.f, 600.0f, // space 84 .15625f, 0.755859375f, // texture 85 44.0f, -102.0f+57.f, 600.0f, // space 86 0.84375f, 0.755859375f, // texture 87 44.0f, 160.0f+57.f, 600.0f, // space 88 0.84375f, 0.244140625f, // texture 89 -44.0f, 160.0f+57.f, 600.0f, // space 90 0.15625f, 0.244140625f); // texture 91 92 93 // restore matrix 94 matrixLoadMat(mat1,ident); 95 matrixRotate(mat1, 0.f, 0.f, 0.f, 1.f); 96 matrixScale(mat1, scale, scale, scale); 97 vpLoadModelMatrix(mat1); 98 99 // erase the part of the needle we don't want to show 100 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_black); 101 drawQuad(-100.f, -55.f, 600.f, 102 -100.f, -105.f, 600.f, 103 100.f, -105.f, 600.f, 104 100.f, -55.f, 600.f); 105 106 107 // draw the frame (472x290) 108 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_frame); 109 drawQuadTexCoords( 110 -236.0f, -60.0f, 600.0f, // space 111 0.0390625f, 0.783203125f, // texture 112 236, -60.0f, 600.0f, // space 113 0.9609375f, 0.783203125f, // texture 114 236, 230.0f, 600.0f, // space 115 0.9609375f, 0.216796875f, // texture 116 -236.0f, 230.0f, 600.0f, // space 117 0.0390625f, 0.216796875f); // texture 118 119 120 } 121 122 int fadeoutcounter = 0; 123 int fadeincounter = 0; 124 int wave1pos = 0; 125 int wave1amp = 0; 126 int wave2pos = 0; 127 int wave2amp= 0; 128 int wave3pos = 0; 129 int wave3amp= 0; 130 int wave4pos = 0; 131 int wave4amp= 0; 132 float idle[4096]; 133 int waveCounter = 0; 134 int lastuptime = 0; 135 float autorotation = 0; 136 137 #define FADEOUT_LENGTH 100 138 #define FADEOUT_FACTOR 0.95f 139 #define FADEIN_LENGTH 15 140 141 void makeIdleWave(float *points) { 142 int i; 143 // show a number of superimposed moving sinewaves 144 float amp1 = sinf(0.007 * wave1amp) * 120 * 1024; 145 float amp2 = sinf(0.023 * wave2amp) * 80 * 1024; 146 float amp3 = sinf(0.011 * wave3amp) * 40 * 1024; 147 float amp4 = sinf(0.031 * wave4amp) * 20 * 1024; 148 for (i = 0; i < 256; i++) { 149 float val = sinf(0.013 * (wave1pos + i * 4)) * amp1 150 + sinf(0.029 * (wave2pos + i * 4)) * amp2; 151 float off = sinf(0.005 * (wave3pos + i * 4)) * amp3 152 + sinf(0.017 * (wave4pos + i * 4)) * amp4; 153 if (val < 2.f && val > -2.f) val = 2.f; 154 points[i*8+1] = val + off; 155 points[i*8+5] = -val + off; 156 } 157 } 158 159 160 void drawWave(float *ident) { 161 float scale = .008f; 162 float mat1[16]; 163 matrixLoadMat(mat1, ident); 164 matrixScale(mat1, scale, scale / 2048.f, scale); 165 matrixTranslate(mat1, 0.f, 81920.f, 350.f); 166 vpLoadModelMatrix(mat1); 167 int i; 168 169 if (State->mIdle) { 170 171 // idle state animation 172 float *points = loadArrayF(RSID_POINTS, 0); 173 if (fadeoutcounter > 0) { 174 // fade waveform to 0 175 for (i = 0; i < 256; i++) { 176 float val = absf(points[i*8+1]); 177 val = val * FADEOUT_FACTOR; 178 if (val < 2.f) val = 2.f; 179 points[i*8+1] = val; 180 points[i*8+5] = -val; 181 } 182 fadeoutcounter--; 183 if (fadeoutcounter == 0) { 184 wave1amp = 0; 185 wave2amp = 0; 186 wave3amp = 0; 187 wave4amp = 0; 188 } 189 } else { 190 // idle animation 191 makeIdleWave(points); 192 } 193 fadeincounter = FADEIN_LENGTH; 194 } else { 195 if (fadeincounter > 0 && fadeoutcounter == 0) { 196 // morph from idle animation back to waveform 197 makeIdleWave(idle); 198 if (waveCounter != State->mWaveCounter) { 199 waveCounter = State->mWaveCounter; 200 float *points = loadArrayF(RSID_POINTS, 0); 201 for (i = 0; i < 256; i++) { 202 float val = absf(points[i*8+1]); 203 points[i*8+1] = (val * (FADEIN_LENGTH - fadeincounter) + idle[i*8+1] * fadeincounter) / FADEIN_LENGTH; 204 points[i*8+5] = (-val * (FADEIN_LENGTH - fadeincounter) + idle[i*8+5] * fadeincounter) / FADEIN_LENGTH; 205 } 206 } 207 fadeincounter--; 208 if (fadeincounter == 0) { 209 fadeoutcounter = FADEOUT_LENGTH; 210 } 211 } else { 212 fadeoutcounter = FADEOUT_LENGTH; 213 } 214 } 215 216 uploadToBufferObject(NAMED_PointBuffer); 217 bindProgramFragment(NAMED_PFBackgroundNoMip); 218 bindTexture(NAMED_PFBackgroundNoMip, 0, NAMED_Tlinetexture); 219 drawSimpleMesh(NAMED_CubeMesh); 220 } 221 222 223 void drawVizLayer(float *ident) { 224 225 int i; 226 227 for (i = 0; i < 6; i++) { 228 if (i & 1) { 229 drawVU(ident); 230 } else { 231 drawWave(ident); 232 } 233 234 matrixRotate(ident, 60.f, 0.f, 1.f, 0.f); 235 } 236 } 237 238 239 int main(int launchID) { 240 241 int i; 242 float ident[16]; 243 int now = uptimeMillis(); 244 int delta = now - lastuptime; 245 lastuptime = now; 246 if (delta > 80) { 247 // Limit the delta to avoid jumps when coming back from sleep. 248 // A value of 80 will make the rotation keep the same speed 249 // until the frame rate drops to 12.5 fps, at which point it 250 // will start slowing down. 251 delta = 80; 252 } 253 autorotation += .3 * delta / 35; 254 while (autorotation > 360.f) autorotation -= 360.f; 255 256 matrixLoadIdentity(ident); 257 matrixRotate(ident, State->mTilt, 1.f, 0.f, 0.f); 258 matrixRotate(ident, autorotation + State->mRotate, 0.f, 1.f, 0.f); 259 260 // draw the reflections 261 matrixTranslate(ident, 0.f, -1.f, 0.f); 262 matrixScale(ident, 1.f, -1.f, 1.f); 263 drawVizLayer(ident); 264 265 // draw the reflecting plane 266 bindProgramFragment(NAMED_PFBackgroundMip); 267 bindTexture(NAMED_PFBackgroundMip, 0, NAMED_Tvumeter_album); 268 drawQuadTexCoords( 269 -1500.0f, -60.0f, 1500.0f, // space 270 0.f, 1.f, // texture 271 1500, -60.0f, 1500.0f, // space 272 1.f, 1.f, // texture 273 1500, -60.0f, -1500.0f, // space 274 1.f, 0.f, // texture 275 -1500.0f, -60.0f, -1500.0f, // space 276 0.f, 0.f); // texture 277 278 // draw the visualizer 279 matrixScale(ident, 1.f, -1.f, 1.f); 280 matrixTranslate(ident, 0.f, 1.f, 0.f); 281 282 drawVizLayer(ident); 283 284 wave1pos++; 285 wave1amp++; 286 wave2pos--; 287 wave2amp++; 288 wave3pos++; 289 wave3amp++; 290 wave4pos++; 291 wave4amp++; 292 293 return 1; 294 } 295