Home | History | Annotate | Download | only in fbotest
      1 // Copyright (C) 2011 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.fbotest)
     18 
     19 #include "rs_graphics.rsh"
     20 
     21 rs_program_vertex gPVBackground;
     22 rs_program_fragment gPFBackground;
     23 
     24 rs_allocation gTGrid;
     25 
     26 rs_program_store gPFSBackground;
     27 
     28 rs_font gItalic;
     29 rs_allocation gTextAlloc;
     30 
     31 rs_allocation gOffscreen;
     32 rs_allocation gOffscreenDepth;
     33 rs_allocation gReadBackTest;
     34 
     35 typedef struct MeshInfo {
     36     rs_mesh mMesh;
     37     int mNumIndexSets;
     38     float3 bBoxMin;
     39     float3 bBoxMax;
     40 } MeshInfo_t;
     41 
     42 MeshInfo_t *gMeshes;
     43 
     44 static float3 gLookAt;
     45 
     46 static float gRotateX;
     47 static float gRotateY;
     48 static float gZoom;
     49 
     50 static float gLastX;
     51 static float gLastY;
     52 
     53 void onActionDown(float x, float y) {
     54     gLastX = x;
     55     gLastY = y;
     56 }
     57 
     58 void onActionScale(float scale) {
     59 
     60     gZoom *= 1.0f / scale;
     61     gZoom = max(0.1f, min(gZoom, 500.0f));
     62 }
     63 
     64 void onActionMove(float x, float y) {
     65     float dx = gLastX - x;
     66     float dy = gLastY - y;
     67 
     68     if (fabs(dy) <= 2.0f) {
     69         dy = 0.0f;
     70     }
     71     if (fabs(dx) <= 2.0f) {
     72         dx = 0.0f;
     73     }
     74 
     75     gRotateY -= dx;
     76     if (gRotateY > 360) {
     77         gRotateY -= 360;
     78     }
     79     if (gRotateY < 0) {
     80         gRotateY += 360;
     81     }
     82 
     83     gRotateX -= dy;
     84     gRotateX = min(gRotateX, 80.0f);
     85     gRotateX = max(gRotateX, -80.0f);
     86 
     87     gLastX = x;
     88     gLastY = y;
     89 }
     90 
     91 void init() {
     92     gRotateX = 0.0f;
     93     gRotateY = 0.0f;
     94     gZoom = 50.0f;
     95     gLookAt = 0.0f;
     96 }
     97 
     98 void updateMeshInfo() {
     99     rs_allocation allMeshes = rsGetAllocation(gMeshes);
    100     int size = rsAllocationGetDimX(allMeshes);
    101     gLookAt = 0.0f;
    102     float minX, minY, minZ, maxX, maxY, maxZ;
    103     for (int i = 0; i < size; i++) {
    104         MeshInfo_t *info = (MeshInfo_t*)rsGetElementAt(allMeshes, i);
    105         rsgMeshComputeBoundingBox(info->mMesh,
    106                                   &minX, &minY, &minZ,
    107                                   &maxX, &maxY, &maxZ);
    108         info->bBoxMin = (float3){minX, minY, minZ};
    109         info->bBoxMax = (float3){maxX, maxY, maxZ};
    110         gLookAt += (info->bBoxMin + info->bBoxMax)*0.5f;
    111     }
    112     gLookAt = gLookAt / (float)size;
    113 }
    114 
    115 static void renderAllMeshes() {
    116     rs_allocation allMeshes = rsGetAllocation(gMeshes);
    117     int size = rsAllocationGetDimX(allMeshes);
    118     gLookAt = 0.0f;
    119     float minX, minY, minZ, maxX, maxY, maxZ;
    120     for (int i = 0; i < size; i++) {
    121         MeshInfo_t *info = (MeshInfo_t*)rsGetElementAt(allMeshes, i);
    122         rsgDrawMesh(info->mMesh);
    123     }
    124 }
    125 
    126 static void drawDescription() {
    127     uint width = rsgGetWidth();
    128     uint height = rsgGetHeight();
    129     int left = 0, right = 0, top = 0, bottom = 0;
    130 
    131     rsgBindFont(gItalic);
    132 
    133     rsgMeasureText(gTextAlloc, &left, &right, &top, &bottom);
    134     rsgDrawText(gTextAlloc, 2 -left, height - 2 + bottom);
    135 }
    136 
    137 static void renderOffscreen(bool useDepth) {
    138 
    139     rsgBindColorTarget(gOffscreen, 0);
    140     if (useDepth) {
    141         rsgBindDepthTarget(gOffscreenDepth);
    142         rsgClearDepth(1.0f);
    143     } else {
    144         rsgClearDepthTarget();
    145     }
    146     rsgClearColor(0.8f, 0.0f, 0.0f, 1.0f);
    147 
    148     rsgBindProgramVertex(gPVBackground);
    149     rs_matrix4x4 proj;
    150     float aspect = (float)rsAllocationGetDimX(gOffscreen) / (float)rsAllocationGetDimY(gOffscreen);
    151     rsMatrixLoadPerspective(&proj, 30.0f, aspect, 1.0f, 100.0f);
    152     rsgProgramVertexLoadProjectionMatrix(&proj);
    153 
    154     rsgBindProgramFragment(gPFBackground);
    155     rsgBindProgramStore(gPFSBackground);
    156     rsgBindTexture(gPFBackground, 0, gTGrid);
    157 
    158     rs_matrix4x4 matrix;
    159     rsMatrixLoadIdentity(&matrix);
    160     // Position our models on the screen
    161     rsMatrixTranslate(&matrix, gLookAt.x, gLookAt.y, gLookAt.z - gZoom);
    162     rsMatrixRotate(&matrix, gRotateX, 1.0f, 0.0f, 0.0f);
    163     rsMatrixRotate(&matrix, gRotateY, 0.0f, 1.0f, 0.0f);
    164     rsgProgramVertexLoadModelMatrix(&matrix);
    165 
    166     renderAllMeshes();
    167 
    168     // Render into the frambuffer
    169     rsgClearAllRenderTargets();
    170 }
    171 
    172 static void drawOffscreenResult(int posX, int posY, rs_allocation texture) {
    173     // display the result
    174     rs_matrix4x4 proj, matrix;
    175     rsMatrixLoadOrtho(&proj, 0, rsgGetWidth(), rsgGetHeight(), 0, -500, 500);
    176     rsgProgramVertexLoadProjectionMatrix(&proj);
    177     rsMatrixLoadIdentity(&matrix);
    178     rsgProgramVertexLoadModelMatrix(&matrix);
    179     rsgBindTexture(gPFBackground, 0, texture);
    180     float startX = posX, startY = posY;
    181     float width = 256, height = 256;
    182     rsgDrawQuadTexCoords(startX, startY, 0, 0, 1,
    183                          startX, startY + height, 0, 0, 0,
    184                          startX + width, startY + height, 0, 1, 0,
    185                          startX + width, startY, 0, 1, 1);
    186 }
    187 
    188 int root(void) {
    189 
    190     rsgClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    191     rsgClearDepth(1.0f);
    192 
    193     renderOffscreen(true);
    194     drawOffscreenResult(0, 0, gOffscreen);
    195 
    196 
    197     uint32_t w = rsAllocationGetDimX(gOffscreen);
    198     uint32_t h = rsAllocationGetDimY(gOffscreen);
    199     uint32_t numElements = w*h;
    200 
    201     rsgAllocationSyncAll(gOffscreen, RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET);
    202 
    203     rsAllocationCopy2DRange(gReadBackTest, 0, 0, 0,
    204                             RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, w, h,
    205                             gOffscreen, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
    206 
    207     rsgAllocationSyncAll(gReadBackTest);
    208     drawOffscreenResult(0, 300, gReadBackTest);
    209 
    210     rsgBindProgramVertex(gPVBackground);
    211     rs_matrix4x4 proj;
    212     float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
    213     rsMatrixLoadPerspective(&proj, 30.0f, aspect, 1.0f, 100.0f);
    214     rsgProgramVertexLoadProjectionMatrix(&proj);
    215 
    216     rsgBindProgramFragment(gPFBackground);
    217     rsgBindProgramStore(gPFSBackground);
    218     rsgBindTexture(gPFBackground, 0, gTGrid);
    219 
    220     rs_matrix4x4 matrix;
    221     rsMatrixLoadIdentity(&matrix);
    222     // Position our models on the screen
    223     rsMatrixTranslate(&matrix, gLookAt.x, gLookAt.y, gLookAt.z - gZoom);
    224     rsMatrixRotate(&matrix, gRotateX, 1.0f, 0.0f, 0.0f);
    225     rsMatrixRotate(&matrix, gRotateY, 0.0f, 1.0f, 0.0f);
    226     rsgProgramVertexLoadModelMatrix(&matrix);
    227 
    228     renderAllMeshes();
    229 
    230     drawDescription();
    231 
    232     return 0;
    233 }
    234