Home | History | Annotate | Download | only in modelviewer
      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.modelviewer)
     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_matrix4x4 gPostureMatrix;
     32 
     33 typedef struct MeshInfo {
     34     rs_mesh mMesh;
     35     int mNumIndexSets;
     36     float3 bBoxMin;
     37     float3 bBoxMax;
     38 } MeshInfo_t;
     39 
     40 MeshInfo_t *gMeshes;
     41 
     42 static float3 gLookAt;
     43 
     44 static float gRotateX;
     45 static float gRotateY;
     46 static float gZoom;
     47 
     48 static float gLastX;
     49 static float gLastY;
     50 
     51 void onActionDown(float x, float y) {
     52     gLastX = x;
     53     gLastY = y;
     54 }
     55 
     56 void onActionScale(float scale) {
     57 
     58     gZoom *= 1.0f / scale;
     59     gZoom = max(0.1f, min(gZoom, 500.0f));
     60 }
     61 
     62 void onActionMove(float x, float y) {
     63     float dx = gLastX - x;
     64     float dy = gLastY - y;
     65 
     66     if (fabs(dy) <= 2.0f) {
     67         dy = 0.0f;
     68     }
     69     if (fabs(dx) <= 2.0f) {
     70         dx = 0.0f;
     71     }
     72 
     73     gRotateY -= dx;
     74     if (gRotateY > 360) {
     75         gRotateY -= 360;
     76     }
     77     if (gRotateY < 0) {
     78         gRotateY += 360;
     79     }
     80 
     81     gRotateX -= dy;
     82     gRotateX = min(gRotateX, 80.0f);
     83     gRotateX = max(gRotateX, -80.0f);
     84 
     85     gLastX = x;
     86     gLastY = y;
     87 }
     88 
     89 void init() {
     90     gRotateX = 0.0f;
     91     gRotateY = 0.0f;
     92     gZoom = 50.0f;
     93     gLookAt = 0.0f;
     94     rsMatrixLoadIdentity(&gPostureMatrix);
     95 }
     96 
     97 void updateMeshInfo() {
     98     rs_allocation allMeshes = rsGetAllocation(gMeshes);
     99     int size = rsAllocationGetDimX(allMeshes);
    100     gLookAt = 0.0f;
    101     float minX, minY, minZ, maxX, maxY, maxZ;
    102     for (int i = 0; i < size; i++) {
    103         MeshInfo_t *info = (MeshInfo_t*)rsGetElementAt(allMeshes, i);
    104         rsgMeshComputeBoundingBox(info->mMesh,
    105                                   &minX, &minY, &minZ,
    106                                   &maxX, &maxY, &maxZ);
    107         info->bBoxMin = (minX, minY, minZ);
    108         info->bBoxMax = (maxX, maxY, maxZ);
    109         gLookAt += (info->bBoxMin + info->bBoxMax)*0.5f;
    110     }
    111     gLookAt = gLookAt / (float)size;
    112 }
    113 
    114 static void renderAllMeshes() {
    115     rs_allocation allMeshes = rsGetAllocation(gMeshes);
    116     int size = rsAllocationGetDimX(allMeshes);
    117     gLookAt = 0.0f;
    118     float minX, minY, minZ, maxX, maxY, maxZ;
    119     for (int i = 0; i < size; i++) {
    120         MeshInfo_t *info = (MeshInfo_t*)rsGetElementAt(allMeshes, i);
    121         rsgDrawMesh(info->mMesh);
    122     }
    123 }
    124 
    125 void drawDescription() {
    126     uint width = rsgGetWidth();
    127     uint height = rsgGetHeight();
    128     int left = 0, right = 0, top = 0, bottom = 0;
    129 
    130     rsgBindFont(gItalic);
    131 
    132     rsgMeasureText(gTextAlloc, &left, &right, &top, &bottom);
    133     rsgDrawText(gTextAlloc, 2 -left, height - 2 + bottom);
    134 }
    135 
    136 int root(void) {
    137 
    138     rsgClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    139     rsgClearDepth(1.0f);
    140 
    141     rsgBindProgramVertex(gPVBackground);
    142     rs_matrix4x4 proj;
    143     float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
    144     rsMatrixLoadPerspective(&proj, 30.0f, aspect, 1.0f, 100.0f);
    145     rsgProgramVertexLoadProjectionMatrix(&proj);
    146 
    147     rsgBindProgramFragment(gPFBackground);
    148     rsgBindProgramStore(gPFSBackground);
    149     rsgBindTexture(gPFBackground, 0, gTGrid);
    150 
    151     rs_matrix4x4 matrix;
    152     rsMatrixLoadIdentity(&matrix);
    153     // Position our models on the screen
    154     rsMatrixTranslate(&matrix, gLookAt.x, gLookAt.y, gLookAt.z - gZoom);
    155     rsMatrixMultiply(&matrix, &gPostureMatrix);
    156     rsMatrixRotate(&matrix, gRotateX, 1.0f, 0.0f, 0.0f);
    157     rsMatrixRotate(&matrix, gRotateY, 0.0f, 1.0f, 0.0f);
    158     
    159     rsgProgramVertexLoadModelMatrix(&matrix);
    160 
    161     renderAllMeshes();
    162 
    163     drawDescription();
    164 
    165     return 0;
    166 }
    167