Changeset 14249


Ignore:
Timestamp:
01/15/13 11:25:39 (12 years ago)
Author:
ltnguyen
Message:

add rotation and pitch zoom

Location:
issm/trunk-jpl/src/android/ISSM/src/com/example/issm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/android/ISSM/src/com/example/issm/ISSM.java

    r14210 r14249  
    252252        mGLView = new MyGLSurfaceView(this, buff, size, colorMap);
    253253        frame.addView(mGLView);
    254         frame.addView(colorBar);
     254        //frame.addView(colorBar);
    255255    }
    256256//------------------------------------------------------------------------------------
  • issm/trunk-jpl/src/android/ISSM/src/com/example/issm/MyGLRenderer.java

    r14210 r14249  
    3030{
    3131        public volatile float mAngle;
    32        
     32        public volatile float mAngleX; /* NEW STUFF*/
     33        public volatile float mAngleY;
     34       
     35       
     36    private FloatBuffer[] triangleVert;
     37       
     38        private float[][] mScaleMatrix;  // scaling /* NEW STUFF*/
     39        private float[][] mRotXMatrix;   // rotation x
     40        private float[][] mRotYMatrix;   // rotation x
     41
    3342        /**
    3443         * Store the model matrix. This matrix is used to move models from object space (where each model can be thought
     
    5261        private int mMatrixHandle;
    5362       
    54     private FloatBuffer[] triangleVert;
    55    
    5663    private float[][] triangleData2DArr;
    5764   
     
    7885    private final int mColorDataSize = 4;       
    7986
     87        // scaling /* NEW STUFF*/
     88        float scaleX = 1.0f;
     89        float scaleY = 1.0f;
     90        float scaleZ = 1.0f;
     91
     92        public float eyeX;
     93        public float eyeY;
     94        public float eyeZ;
     95       
    8096    public MyGLRenderer(float[][] vertices)
    8197    {   
    8298        triangleData2DArr = new float[vertices.length][21];
     99 
    83100        triangleVert = new FloatBuffer[vertices.length];
     101        mScaleMatrix  = new float[triangleVert.length][16];  // scaling  /* NEW STUFF*/
     102        mRotXMatrix  = new float[triangleVert.length][16];       // rotation x
     103        mRotYMatrix  = new float[triangleVert.length][16];       // rotation x
    84104       
    85105        for (int i = 0; i < vertices.length; i++)
     
    88108                        triangleData2DArr[i][j] = vertices[i][j];
    89109        }
    90                        
     110           
    91111            // initialize vertex byte buffer for shape coordinates
    92112        for (int i = 0; i < vertices.length; i++)
     
    98118    }
    99119   
     120        public void changeScale(float scale) /* NEW STUFF*/
     121        {
     122                if (scaleX * scale > 3.0f) return;
     123                  scaleX *= scale; scaleY *= scale; scaleZ *= scale;
     124               
     125                //scaleX += scale;scaleY += scale;scaleZ += scale;
     126                //scaleX = scale;scaleY = scale;scaleZ = scale;
     127                //scaleX /= 100;scaleY /= 100;scaleZ /= 100;
     128        }
     129
     130        public void defaultScale()
     131        {
     132                scaleX = 1f;
     133                scaleY = 1f;
     134                scaleZ = 1f;
     135        }
     136       
    100137    public void onSurfaceCreated(GL10 unused, EGLConfig config)
    101138    {
    102139        // Set the background frame color
    103         GLES20.glClearColor(0.0f, 0.8f, 1.0f, 1.0f);
     140        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    104141       
    105142                // Position the eye behind the origin.
    106                 final float eyeX = 0.0f;
    107                 final float eyeY = 0.0f;
    108                 final float eyeZ = 1.5f;
     143                eyeX = 0.0f;
     144                eyeY = 0.0f;
     145                eyeZ = 1.5f;
    109146
    110147                // We are looking toward the distance
     
    123160                mViewMatrix = new float[triangleVert.length][16];
    124161               
     162               
    125163                for (int i = 0; i < triangleVert.length; i++)
    126164                        Matrix.setLookAtM(mViewMatrix[i], 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
     165                        //Matrix.setLookAtM(mVMatrix[i], 0, 0, 0, -5.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
    127166       
    128167                final String vertexShader =
     
    134173                          + "{                              \n"
    135174                          + "   v_Color = a_Color;          \n"         // Pass the color through to the fragment shader.
     175                         
    136176                                                                                                                // It will be interpolated across the triangle.
    137177                          + "   gl_Position = u_MVPMatrix   \n"         // gl_Position is a special variable used to store the final position.
     
    197237                GLES20.glBindAttribLocation(mProgram, 0, "a_Position");
    198238                GLES20.glBindAttribLocation(mProgram, 1, "a_Color");
    199 
     239               
    200240                // Link the two shaders together into a program.
    201241                GLES20.glLinkProgram(mProgram);
     
    214254                for (int i = 0; i < triangleVert.length; i++)
    215255                {
    216                         mMatrixHandle = GLES20.glGetUniformLocation(mProgram, "u_MVPMatrix");        
     256                        mMatrixHandle = GLES20.glGetUniformLocation(mProgram, "u_MVPMatrix");       
    217257                mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_Position");
    218258                mColorHandle = GLES20.glGetAttribLocation(mProgram, "a_Color");
     
    222262        GLES20.glUseProgram(mProgram);
    223263    }
    224    
    225264    public void onSurfaceChanged(GL10 unused, int width, int height)
    226265    {
     
    236275                for (int i = 0; i < triangleVert.length; i++)
    237276                        Matrix.frustumM(mProjectionMatrix[i], 0, -ratio*0.7f, ratio*1.0f, 0.0f, 1.0f, 1.0f, 14f);
    238                 //Matrix.frustumM(m, offset, left, right, bottom, top, near, far)
    239277    }
    240278   
     
    247285        // Draw the triangle facing straight on.
    248286        for (int i = 0; i < triangleVert.length; i++)
    249                 Matrix.setIdentityM(mModelMatrix[i], 0);
     287        {
     288                //Matrix.setIdentityM(mModelMatrix[i], 0); /* NEW STUFF*/
     289                Matrix.setIdentityM(mScaleMatrix[i], 0);
     290                Matrix.scaleM(mScaleMatrix[i], 0, scaleX, scaleY, scaleZ);
     291               
     292                // Rotation along x
     293                Matrix.setRotateM(mRotXMatrix[i], 0, this.mAngleY, -1.0f, 0.0f, 0.0f);
     294                Matrix.setRotateM(mRotYMatrix[i], 0, this.mAngleX, 0.0f, 1.0f, 0.0f); /* NEW STUFF*/
     295               
     296                Matrix.setLookAtM(mViewMatrix[i], 0, eyeX, eyeY, eyeZ, 0.0f, 0.0f, -5.0f, 0.0f, 1.0f, 0.0f);
     297       
     298        }
    250299               
    251300        mMVPMatrix = new float[triangleVert.length][16];
     
    273322                GLES20.glEnableVertexAttribArray(mColorHandle);
    274323               
    275                 //Matrix.setRotateM(mModelMatrix[i], 0, mAngle, 0, 0, -1.0f);
     324                Matrix.setRotateM(mRotXMatrix[i], 0, this.mAngleY, -1.0f, 0.0f, 0.0f); /* NEW STUFF*/
     325                Matrix.setRotateM(mRotYMatrix[i], 0, this.mAngleX, 0.0f, 1.0f, 0.0f);
     326
     327                float tempMatrix[][] = new float[triangleVert.length][16]; /* NEW STUFF*/
     328                Matrix.multiplyMM(tempMatrix[i], 0, mRotYMatrix[i], 0, mRotXMatrix[i], 0);
     329                Matrix.multiplyMM(mModelMatrix[i], 0, mScaleMatrix[i], 0, tempMatrix[i], 0);
    276330
    277331                        // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
     
    283337                Matrix.multiplyMM(mMVPMatrix[i], 0, mProjectionMatrix[i], 0, mMVPMatrix[i], 0);
    284338               
    285                 GLES20.glUniformMatrix4fv(mMatrixHandle, 1, false, mMVPMatrix[i], 0);
    286                
     339                GLES20.glUniformMatrix4fv(mMatrixHandle, 1, false, mMVPMatrix[i], 0);   
     340
    287341                // Draw the triangle
    288342                GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
  • issm/trunk-jpl/src/android/ISSM/src/com/example/issm/MyGLSurfaceView.java

    r14246 r14249  
    66import android.content.Context;
    77import android.opengl.GLSurfaceView;
     8import android.util.FloatMath;
    89import android.view.MotionEvent;
    910
     
    1718        final int firstVelocity = 9;
    1819        ColorMap colorMap;
     20       
     21        // pinch to zoom
     22        float oldDist = 100.0f;
     23        float newDist;
     24       
     25        int mode = 0;
    1926    public MyGLSurfaceView(Context context, DoubleBuffer db, int size, ColorMap colorMap)
    2027    {
     
    152159   
    153160    private float mPreviousY;
     161 // touch events
     162        private final int NONE = 0;
     163        private final int DRAG = 0;
     164        private final int ZOOM = 0;
    154165    @Override
    155     public boolean onTouchEvent(MotionEvent e) {
    156         // MotionEvent reports input details from the touch screen
    157         // and other input controls. In this case, you are only
    158         // interested in events where the touch position changed.
    159 
    160         float x = e.getX();
    161         float y = e.getY();
    162 
    163         switch (e.getAction()) {
    164             case MotionEvent.ACTION_MOVE:
    165 
    166                 float dx = x - mPreviousX;
    167                 float dy = y - mPreviousY;
    168 
    169                 // reverse direction of rotation above the mid-line
    170                 if (y > getHeight() / 2) {
    171                   dx = dx * -1 ;
    172                 }
    173 
    174                 // reverse direction of rotation to left of the mid-line
    175                 if (x < getWidth() / 2) {
    176                   dy = dy * -1 ;
    177                 }
    178 
    179                 mRend.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;  // = 180.0f / 320
    180                 requestRender();
    181         }
    182 
    183         mPreviousX = x;
    184         mPreviousY = y;
    185         return true;
    186     }
     166    public boolean onTouchEvent(MotionEvent e) /* NEW STUFF*/
     167        {
     168                float x = e.getX();
     169                float y = e.getY();
     170                switch (e.getAction()) {
     171                        case MotionEvent.ACTION_DOWN:                   // one touch: drag
     172                      mode = DRAG;
     173                      //mRend.eyeX = e.getX();
     174                      //mRend.eyeY = e.getY;
     175                      break;
     176                     
     177                        case MotionEvent.ACTION_POINTER_DOWN:   // two touches: zoom
     178                                oldDist = spacing(e);
     179                                if (oldDist > 10.0f)
     180                                        mode = ZOOM; // zoom
     181                                break;
     182                               
     183                        case MotionEvent.ACTION_UP:             // no mode
     184                                mode = NONE;
     185                                oldDist = 100.0f;
     186                                break;
     187                               
     188                        case MotionEvent.ACTION_POINTER_UP:             // no mode
     189                                mode = NONE;
     190                                oldDist = 100.0f;
     191                                break;
     192                               
     193                        case MotionEvent.ACTION_MOVE:                                           // rotation
     194                                if (e.getPointerCount() > 1 && mode == ZOOM)
     195                                {
     196                                        newDist = spacing(e);
     197                                        if (newDist > 10.0f) {
     198                                                float scale = newDist/oldDist; // scale
     199                                               
     200                                                // scale in the renderer
     201                                                mRend.changeScale(scale);
     202                                                oldDist = newDist;
     203                                        }
     204                                }
     205                                else if (mode == DRAG)
     206                                {
     207                                        float dx = x - mPreviousX;
     208                                        float dy = y - mPreviousY;
     209                                       
     210                                        mRend.mAngleX += dx * TOUCH_SCALE_FACTOR;
     211                                        mRend.mAngleY += dy * TOUCH_SCALE_FACTOR;
     212
     213                                        requestRender();
     214                                }
     215                                break;
     216                }
     217                mPreviousX = x;
     218                mPreviousY = y;
     219                return true;
     220        }
     221       
     222        private float spacing(MotionEvent event) /* NEW STUFF*/
     223        {
     224                float x = event.getX(0) - event.getX(1);
     225                float y = event.getY(0) - event.getY(1);
     226                return FloatMath.sqrt(x * x + y * y);
     227        }
    187228}
     229
Note: See TracChangeset for help on using the changeset viewer.