Home | History | Annotate | Download | only in holospiral
      1 // Copyright (C) 2010 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 
     17 #pragma rs java_package_name(com.android.wallpaper.holospiral)
     18 
     19 #include "rs_graphics.rsh"
     20 
     21 #define FOV 60.0f
     22 #define SPIRAL_ROTATE_SPEED 15.0f
     23 #define INNER_ROTATE_SPEED 1.5f
     24 #define OUTER_ROTATE_SPEED 0.5f
     25 
     26 // Vertex Shaders
     27 rs_program_vertex gPVBackground;
     28 rs_program_vertex gPVGeometry;
     29 
     30 // Fragment Shaders
     31 rs_program_fragment gPFBackground;
     32 rs_program_fragment gPFGeometry;
     33 
     34 // Blending and Depth testing
     35 rs_program_store gPSBackground;
     36 rs_program_store gPSGeometry;
     37 
     38 // Meshes
     39 rs_mesh gInnerGeometry;
     40 rs_mesh gOuterGeometry;
     41 rs_mesh gBackgroundMesh;
     42 
     43 // Matrices
     44 static rs_matrix4x4 gProjectionMatrix;
     45 static rs_matrix4x4 gTransformedModelView;
     46 
     47 // Textures
     48 rs_allocation gPointTexture;
     49 
     50 // Misc fields
     51 float gXOffset;
     52 float gNearPlane;
     53 float gFarPlane;
     54 
     55 // User defined data types
     56 typedef struct VertexShaderConstants_s {
     57     rs_matrix4x4 modelViewProj;
     58     float maxPointSize;
     59     float farPlane;
     60 } VertexShaderConstants;
     61 VertexShaderConstants* gVSConstants;
     62 
     63 typedef struct VertexColor_s {
     64     float3 position;
     65     float4 color;
     66 } VertexColor;
     67 VertexColor* VertexColor_s_dummy;
     68 
     69 static float lastTime;
     70 static float gInnerRotateAngle;
     71 static float gOuterRotateAngle;
     72 static float gWidth;
     73 static float gHeight;
     74 
     75 static float modulo360(float val) {
     76     static const INVERSE_360 = 1.0f / 360.0f;
     77     int multiplier = (int)(val * INVERSE_360);
     78     return val - (multiplier * 360.0f);
     79 }
     80 
     81 static void drawBackground() {
     82     rsgBindProgramVertex(gPVBackground);
     83     rsgBindProgramFragment(gPFBackground);
     84     rsgBindProgramStore(gPSBackground);
     85 
     86     rsgDrawMesh(gBackgroundMesh);
     87 }
     88 
     89 static void drawGeometry(float dt) {
     90     rsgBindProgramVertex(gPVGeometry);
     91     rsgBindProgramFragment(gPFGeometry);
     92     rsgBindProgramStore(gPSGeometry);
     93 
     94     rsgBindTexture(gPFGeometry, 0, gPointTexture);
     95 
     96     rs_matrix4x4 modelView;
     97     rs_matrix4x4 rotateModelView;
     98 
     99     rsMatrixLoad(&modelView, &gTransformedModelView);
    100     rsMatrixRotate(&modelView, gXOffset * -SPIRAL_ROTATE_SPEED, 0.0f, 1.0f, 0.0f);
    101 
    102     rsMatrixLoad(&rotateModelView, &modelView);
    103     {
    104         rsMatrixRotate(&rotateModelView, -gOuterRotateAngle, 0.0f, 0.0f, 1.0f);
    105         rsMatrixLoadMultiply(&gVSConstants->modelViewProj, &gProjectionMatrix, &rotateModelView);
    106 
    107         // Wrap the rotation so we don't go past 360
    108         gOuterRotateAngle = modulo360(gOuterRotateAngle + (dt * OUTER_ROTATE_SPEED));
    109 
    110         rsgDrawMesh(gOuterGeometry);
    111     }
    112 
    113     rsMatrixLoad(&rotateModelView, &modelView);
    114     {
    115         rsMatrixRotate(&rotateModelView, gInnerRotateAngle, 0.0f, 0.0f, 1.0f);
    116         rsMatrixLoadMultiply(&gVSConstants->modelViewProj, &gProjectionMatrix, &rotateModelView);
    117 
    118         // Wrap the rotation so we don't go past 360
    119         gInnerRotateAngle = modulo360(gInnerRotateAngle + (dt * INNER_ROTATE_SPEED));
    120 
    121         rsgDrawMesh(gInnerGeometry);
    122     }
    123 }
    124 
    125 void resize(float width, float height) {
    126     gWidth = width;
    127     gHeight = height;
    128     gVSConstants->farPlane = gFarPlane;
    129     rsMatrixLoadPerspective(&gProjectionMatrix, FOV, gWidth / gHeight, gNearPlane, gFarPlane);
    130 }
    131 
    132 void init() {
    133     gInnerRotateAngle = 0.0f;
    134     gOuterRotateAngle = 0.0f;
    135 
    136     gXOffset = 0.0f;
    137     lastTime = rsUptimeMillis();
    138 
    139     // Pre-calculate some fixed transformations
    140     rsMatrixLoadIdentity(&gTransformedModelView);
    141     rsMatrixTranslate(&gTransformedModelView, -3.0f, -5.0f, -18.0f);
    142     rsMatrixRotate(&gTransformedModelView, 20.0f, 0.0f, 1.0f, 0.0f);
    143     rsMatrixRotate(&gTransformedModelView, -10.0f, 1.0f, 0.0f, 0.0f);
    144 }
    145 
    146 int root(void) {
    147     float now = rsUptimeMillis();
    148     float elapsed = (now - lastTime) * 0.001f;
    149     lastTime = now;
    150 
    151     drawBackground();
    152     drawGeometry(elapsed);
    153 
    154     // Around 14 FPS
    155     return 70;
    156 }
    157