Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 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 stateFragment(PFBackground) |
| 18 | #pragma stateFragmentStore(PFSBackground) |
| 19 | |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 20 | #define ELLIPSE_RATIO 0.892f |
| 21 | |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 22 | float angle; |
| 23 | float distance; |
| 24 | |
| 25 | void init() { |
| 26 | angle = 0.0f; |
| 27 | distance = 0.0f; |
| 28 | } |
| 29 | |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 30 | void drawSpace(float xOffset, int width, int height) { |
| 31 | bindTexture(NAMED_PFBackground, 0, NAMED_TSpace); |
| 32 | drawQuadTexCoords( |
Romain Guy | 9e6f234 | 2009-08-24 18:40:21 -0700 | [diff] [blame] | 33 | 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 34 | width, 0.0f, 0.0f, 2.0f, 1.0f, |
| 35 | width, height, 0.0f, 2.0f, 0.0f, |
| 36 | 0.0f, height, 0.0f, 0.0f, 0.0f); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void drawLights(float xOffset, int width, int height) { |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 40 | bindProgramVertex(NAMED_PVStars); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 41 | bindProgramFragment(NAMED_PFBackground); |
| 42 | bindTexture(NAMED_PFBackground, 0, NAMED_TLight1); |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 43 | |
| 44 | float scale = 512.0f / width; |
| 45 | float x = -scale + xOffset - scale * 0.05f; |
| 46 | float y = -scale; |
| 47 | |
| 48 | scale *= 2.0f; |
| 49 | |
| 50 | drawQuad(x, y, 0.0f, |
| 51 | x + scale * 1.1f, y, 0.0f, |
| 52 | x + scale * 1.1f, y + scale, 0.0f, |
| 53 | x, y + scale, 0.0f); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 56 | void drawParticles(float xOffset, int width, int height) { |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 57 | bindProgramVertex(NAMED_PVStars); |
| 58 | bindProgramFragment(NAMED_PFStars); |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 59 | bindProgramFragmentStore(NAMED_PFSLights); |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 60 | bindTexture(NAMED_PFStars, 0, NAMED_TFlares); |
| 61 | |
| 62 | float matrix[16]; |
| 63 | matrixLoadTranslate(matrix, 0.0f, 0.0f, 10.0f - 6.0f * distance); |
| 64 | matrixScale(matrix, 6.15f, 6.0f, 1.0f); |
| 65 | matrixRotate(matrix, angle, 1.0f, 0.5f, 0.0f); |
| 66 | vpLoadModelMatrix(matrix); |
| 67 | |
| 68 | // quadratic attenuation |
| 69 | pointAttenuation(0.1f, 0.0f, 0.06f); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 70 | |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 71 | int radius = State->galaxyRadius; |
| 72 | int particlesCount = State->particlesCount; |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 73 | |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 74 | float w = xOffset; |
| 75 | |
| 76 | struct Stars_s *star = Stars; |
| 77 | struct Particles_s *vtx = Particles; |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 78 | |
| 79 | int i = 0; |
| 80 | for ( ; i < particlesCount; i++) { |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 81 | float a = star->angle + star->speed; |
| 82 | float x = star->distance * sinf(a); |
| 83 | float y = star->distance * cosf(a) * ELLIPSE_RATIO; |
| 84 | |
| 85 | vtx->x = star->t * x + star->s * y + w; |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 86 | vtx->y = star->s * x - star->t * y; |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 87 | star->angle = a; |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 88 | star++; |
| 89 | vtx++; |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | uploadToBufferObject(NAMED_ParticlesBuffer); |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 93 | drawSimpleMeshRange(NAMED_ParticlesMesh, 0, particlesCount); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | int main(int index) { |
Jason Sams | 16b673e | 2009-09-03 15:43:30 -0700 | [diff] [blame] | 97 | int width = State->width; |
| 98 | int height = State->height; |
| 99 | |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 100 | float x = lerpf(1.0f, -1.0f, State->xOffset); |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 101 | |
| 102 | drawSpace(x, width, height); |
| 103 | drawParticles(x, width, height); |
| 104 | drawLights(x, width, height); |
Romain Guy | 909dff3 | 2009-09-04 17:54:17 -0700 | [diff] [blame^] | 105 | |
| 106 | // if (angle < 68.0f) { |
| 107 | // angle += 0.4f; |
| 108 | // distance = angle / 68.0f; |
| 109 | // } |
| 110 | |
Romain Guy | e04d27e | 2009-08-21 16:30:52 -0700 | [diff] [blame] | 111 | return 1; |
| 112 | } |