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