FileDocCategorySizeDatePackage
OBJViewer.javaAPI DocAndroid 1.5 API9721Wed May 06 22:41:08 BST 2009com.android.objviewer

OBJView

public class OBJView extends android.view.View

Fields Summary
private static final float
PI
private static final float
TWO_PI
private static final float
PI_OVER_TWO
private float[]
lightModelAmbient
private android.graphics.Paint
paint
private android.graphics.glutils.GLView
glView
private boolean
initialized
private android.graphics.OpenGLContext
mGLContext
private long
mNextTime
private float
mRotAngle
private float
mRotVelocity
private float
mTiltAngle
private float
mCenterX
private float
mCenterY
private float
mCenterZ
private float
mScale
private float[]
mLightDir
private static final int
INVALIDATE
private final android.os.Handler
mHandler
Constructors Summary
public OBJView(android.content.Context context)


       
        super(context);

        mGLContext = new OpenGLContext(OpenGLContext.DEPTH_BUFFER);

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

        requestFocus();
    
Methods Summary
private voidinit(javax.microedition.khronos.opengles.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);
    
protected voidonDraw(android.graphics.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);
    
public booleanonKeyDown(int keyCode, android.view.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;
    
public voidreset()

        initialized = false;

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

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