1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.graphics.spritetext;
18 
19 import android.opengl.Matrix;
20 
21 import javax.microedition.khronos.opengles.GL10;
22 
23 /**
24  * A utility that projects
25  *
26  */
27 class Projector {
Projector()28     public Projector() {
29         mMVP = new float[16];
30         mV = new float[4];
31         mGrabber = new MatrixGrabber();
32     }
33 
setCurrentView(int x, int y, int width, int height)34     public void setCurrentView(int x, int y, int width, int height) {
35         mX = x;
36         mY = y;
37         mViewWidth = width;
38         mViewHeight = height;
39     }
40 
project(float[] obj, int objOffset, float[] win, int winOffset)41     public void project(float[] obj, int objOffset, float[] win, int winOffset) {
42         if (!mMVPComputed) {
43             Matrix.multiplyMM(mMVP, 0, mGrabber.mProjection, 0, mGrabber.mModelView, 0);
44             mMVPComputed = true;
45         }
46 
47         Matrix.multiplyMV(mV, 0, mMVP, 0, obj, objOffset);
48 
49         float rw = 1.0f / mV[3];
50 
51         win[winOffset] = mX + mViewWidth * (mV[0] * rw + 1.0f) * 0.5f;
52         win[winOffset + 1] = mY + mViewHeight * (mV[1] * rw + 1.0f) * 0.5f;
53         win[winOffset + 2] = (mV[2] * rw + 1.0f) * 0.5f;
54     }
55 
56     /**
57      * Get the current projection matrix. Has the side-effect of
58      * setting current matrix mode to GL_PROJECTION
59      * @param gl
60      */
getCurrentProjection(GL10 gl)61     public void getCurrentProjection(GL10 gl) {
62         mGrabber.getCurrentProjection(gl);
63         mMVPComputed = false;
64     }
65 
66     /**
67      * Get the current model view matrix. Has the side-effect of
68      * setting current matrix mode to GL_MODELVIEW
69      * @param gl
70      */
getCurrentModelView(GL10 gl)71     public void getCurrentModelView(GL10 gl) {
72         mGrabber.getCurrentModelView(gl);
73         mMVPComputed = false;
74     }
75 
76     private MatrixGrabber mGrabber;
77     private boolean mMVPComputed;
78     private float[] mMVP;
79     private float[] mV;
80     private int mX;
81     private int mY;
82     private int mViewWidth;
83     private int mViewHeight;
84 }
85