File: OBJViewer.java

package info (click to toggle)
android-platform-development 7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,092 kB
  • sloc: ansic: 161,291; java: 15,681; cpp: 7,721; xml: 6,419; python: 5,456; sh: 1,748; lisp: 261; ruby: 183; asm: 132; perl: 88; makefile: 22
file content (334 lines) | stat: -rw-r--r-- 9,721 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.objviewer;

import android.app.Activity;
import android.content.AssetManager;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.OpenGLContext;
import android.graphics.Paint;
import android.graphics.glutils.GLView;
import android.graphics.glutils.Object3D;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

class OBJView extends View {

    // Mathematical constants
    private static final float PI = (float)Math.PI;
    private static final float TWO_PI = (float)(2.0*Math.PI);
    private static final float PI_OVER_TWO = (float)(Math.PI/2.0);

    // Ambient light to apply
    // private float[] lightModelAmbient = { 0.0f, 0.0f, 0.0f, 1.0f };
    private float[] lightModelAmbient = { 0.2f, 0.2f, 0.2f, 1.0f };

    // Paint object for drawing the FPS display
    private Paint paint = new Paint();

    // GLView object to manage drawing    
    private GLView glView = new GLView();

    private boolean         initialized = false;

    private OpenGLContext   mGLContext;

    // Next time to draw
    private long            mNextTime;

    // View transformation controlled by UI
    private float           mRotAngle = 0.0f;
    private float           mRotVelocity = 1.0f;
    private float           mTiltAngle = 0.0f;

    // Object bounds
    private float           mCenterX = 0.0f;
    private float           mCenterY = 0.0f;
    private float           mCenterZ = 0.0f;
    private float           mScale   = 1.0f;

    // Light direction
    private float[] mLightDir = { 0.0f, 0.0f, 1.0f, 0.0f };

    public OBJView(Context context) {
        super(context);

        mGLContext = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);

        Message msg = mHandler.obtainMessage(INVALIDATE);
        mNextTime = SystemClock.uptimeMillis() + 100;
        mHandler.sendMessageAtTime(msg, mNextTime);

        requestFocus();
    }

    public void reset() {
        initialized = false;

        mRotAngle = 0.0f;
        mRotVelocity = 1.0f;
        mTiltAngle = 0.0f;

        mCenterX = 0.0f;
        mCenterY = 0.0f;
        mCenterZ = 0.0f;
        mScale   = 1.0f;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Hand the key to the GLView object first
        if (glView.processKey(keyCode)) {
            return true;
        }

        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                mRotVelocity -= 1.0f;
                break;

            case KeyEvent.KEYCODE_DPAD_RIGHT:
                mRotVelocity += 1.0f;
                break;

            case KeyEvent.KEYCODE_DPAD_CENTER:
                mRotVelocity = 0.0f;
                break;

            case KeyEvent.KEYCODE_DPAD_UP:	
                mTiltAngle -= 360.0f/24.0f;
                break;

            case KeyEvent.KEYCODE_DPAD_DOWN:
                mTiltAngle += 360.0f/24.0f;
                break;

            case KeyEvent.KEYCODE_U:
                OBJViewer.nextObject();
                reset();
                break;

            default:
                return super.onKeyDown(keyCode, event);
        }

        return true;
    }

    private void init(GL10 gl) {
        glView.reset();

        paint.setColor(0xffffffff);

        gl.glEnable(gl.GL_DEPTH_TEST);

        gl.glDisable(gl.GL_SCISSOR_TEST);

        // Some quality settings...
        gl.glEnable(gl.GL_DITHER);

        gl.glShadeModel(gl.GL_SMOOTH);

        gl.glEnable(gl.GL_CULL_FACE);
        gl.glFrontFace(gl.GL_CCW);

        gl.glClearColor(0, 0, 0, 1);

        gl.glLightModelf(gl.GL_LIGHT_MODEL_TWO_SIDE, 0);
        gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, lightModelAmbient, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        GL10 gl = (GL10)mGLContext.getGL();
        mGLContext.makeCurrent(this);

        if (!initialized) {
            init(gl);
            initialized = true;

            // Load the object
            Object3D obj = OBJViewer.getObject();

            // Compute a scale factor and translation to bring it
            // into view
            mCenterX = (obj.getBoundsMinX() + obj.getBoundsMaxX())/2.0f;
            mCenterY = (obj.getBoundsMinY() + obj.getBoundsMaxY())/2.0f;
            mCenterZ = (obj.getBoundsMinZ() + obj.getBoundsMaxZ())/2.0f;
            float spanX = obj.getBoundsMaxX() - obj.getBoundsMinX();
            float spanY = obj.getBoundsMaxY() - obj.getBoundsMinY();
            float spanZ = obj.getBoundsMaxZ() - obj.getBoundsMinZ();
            float maxSpan = Math.max(spanX, spanY);
            maxSpan = Math.max(maxSpan, spanZ);
            mScale = 2.0f/maxSpan;
        }

        int w = getWidth();
        int h = getHeight();
        gl.glViewport(0, 0, w, h);

        float ratio = (float)w/h;
        glView.setAspectRatio(ratio);

        // Clear buffers
        gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);

        // Set up the projection and view
        glView.setProjection(gl);
        glView.setView(gl);

        // Set up lighting
        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glEnable(gl.GL_LIGHTING);
        gl.glEnable(gl.GL_LIGHT0);
        gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, mLightDir, 0);
        glView.setLights(gl, gl.GL_LIGHT0);

        // Rotate the viewpoint around the model
        gl.glRotatef(mTiltAngle, 1, 0, 0);
        gl.glRotatef(mRotAngle, 0, 1, 0);

        // Scale object to fit in [-1, 1]
        gl.glScalef(mScale, mScale, mScale);

        // Center the object at the origin
        gl.glTranslatef(-mCenterX, -mCenterY, -mCenterZ);

        // Increment the rotation angle
        mRotAngle += mRotVelocity;
        if (mRotAngle < 0.0f) {
            mRotAngle += 360.0f;
        }
        if (mRotAngle > 360.0f) {
            mRotAngle -= 360.0f;
        }

        // Draw the object
        Object3D object = OBJViewer.getObject();
        object.draw(gl);

        // Allow GL to complete
        mGLContext.post();

        // Draw GLView messages and/or FPS
        glView.showMessages(canvas);
        glView.setNumTriangles(object.getNumTriangles());
        glView.showStatistics(canvas, w);
    }

    private static final int INVALIDATE = 1;

    private final Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == INVALIDATE) {
                invalidate();
                msg = obtainMessage(INVALIDATE);
                long current = SystemClock.uptimeMillis();
                if (mNextTime < current) {
                    mNextTime = current + 20;
                }
                sendMessageAtTime(msg, mNextTime);
                mNextTime += 20;
            }
        }
    };
}


public class OBJViewer extends Activity {

    private static Object3D object = null;

    private static List<String> objectFiles = new ArrayList<String>();
    private static int objectIndex = 0;

    static {
        objectFiles.add("world.gles");
        objectFiles.add("al.gles");
        objectFiles.add("apple.gles");
        objectFiles.add("dolphins.gles");
        objectFiles.add("f16.gles");
        objectFiles.add("flowers.gles");
        objectFiles.add("porsche.gles");
        objectFiles.add("rosevase.gles");
        objectFiles.add("shuttle.gles");
        objectFiles.add("soccerball.gles");
    }

    private int readInt16(InputStream is) throws Exception {
        return is.read() | (is.read() << 8);
    }

    public static Object3D getObject() {
        return object;
    }

    public static void nextObject() {
        try {
            objectIndex = (objectIndex + 1) % objectFiles.size();
            object.load(objectFiles.get(objectIndex));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Get rid of the title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // Make sure we're not drawing a background
        setTheme(R.style.Theme);
        setContentView(new OBJView((Context)getApplication()));

        if (object == null) {
            try {
                final AssetManager am = getAssets();
                this.object = new Object3D() {
                    public InputStream readFile(String filename)
                    throws IOException {
                        return am.open(filename);

                    }
                };
                object.load(objectFiles.get(0));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override protected void onResume() {
        super.onResume();
    }

    @Override protected void onStop() {
        super.onStop();
    }
}