Advertisement
Guest User

opengltrying/MyRenderer

a guest
Jul 13th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. package com.example.opengltrying;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5.  
  6. import android.content.Context;
  7. import android.opengl.GLES20;
  8. import android.opengl.Matrix;
  9. import android.opengl.GLSurfaceView;
  10. import android.os.SystemClock;
  11. import android.provider.Settings.System;
  12. import android.view.MotionEvent;
  13.  
  14. public class MyRenderer implements GLSurfaceView.Renderer {
  15.    
  16.     private static final float TOUCH_SCALE_FACTOR = 180.0f / 320.0f;
  17.  
  18.     public volatile float mAngle = 0.0f;
  19.    
  20.     //private Triangle mTriangle;
  21.     private Square mSquare;
  22.     private MulticoloredTriangle mTriangle;
  23.     private TexturedSquare mTexSquare;
  24.     private TexturedCube mTexCube;
  25.     private float[] mProjMatrix = new float[16];
  26.     private float[] mViewMatrix = new float[16];
  27.     private float[] mMVPMatrix = new float[16];
  28.     private float[] mRotationMatrix = new float[16];
  29.     private float[] mViewProjMatrix = new float[16];
  30.    
  31.     private volatile Context mContext;
  32.     private int mWidth;
  33.     private int mHeight;
  34.  
  35.     private float mPreviousX = 0.0f;
  36.     private float mPreviousY = 0.0f;
  37.  
  38.     public MyRenderer( final Context context, final int width, final int height ) {
  39.         mContext = context;
  40.         mWidth = width;
  41.         mHeight = height;
  42.     }
  43.  
  44.     @Override
  45.     public void onDrawFrame(GL10 unused) {
  46.         // Redraw background color
  47.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  48.         // Set the background frame color
  49.         GLES20.glClearColor(0.21f, 0.23f, 0.52f, 1.0f);
  50.        
  51.         // Set the camera position
  52.         Matrix.setLookAtM( mViewMatrix, 0,
  53.                            0.0f, 0.0f, 10.0f,       // camera position
  54.                            0.0f, 0.0f, 0.0f,    // camera target
  55.                            0.0f, 1.0f, 0.0f );  // camera up vector
  56.        
  57.         // Calculate model * view * projection matrix
  58.         Matrix.multiplyMM( mMVPMatrix, 0, mProjMatrix, 0, mViewMatrix, 0 );
  59.        
  60.         // Get rotation angle based on system time
  61.         //long time = SystemClock.uptimeMillis() % 4000L;
  62.         //mAngle = 0.090f * ((int) time);
  63.        
  64.         // Create rotation matrix around -Z axis
  65.         Matrix.setRotateM( mRotationMatrix, 0, mAngle, 0.0f, -1.0f, 0.0f );
  66.        
  67.         // Update WVP matrix with given rotation angle
  68.         Matrix.multiplyMM( mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0 );
  69.        
  70.         Matrix.multiplyMM( mViewProjMatrix, 0, mProjMatrix, 0, mViewMatrix, 0);
  71.        
  72.         // Draw triangle
  73.         //mTriangle.draw( mMVPMatrix );
  74.         //mSquare.draw( mMVPMatrix );
  75.         //mTriangle.draw( mMVPMatrix );
  76.         //mTexSquare.draw( mMVPMatrix );
  77.         mTexCube.draw( mViewProjMatrix );
  78.     }
  79.  
  80.     @Override
  81.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  82.         GLES20.glViewport(0, 0, width, height);
  83.        
  84.         float ratio = (float) width / height;
  85.        
  86.         // Define projection matrix
  87.         //Matrix.perspectiveM( mProjMatrix, 0, 45.0f, ratio, 0.0f, 100.0f );
  88.         float nearZ = 0.1f;
  89.         float farZ = 100.0f;
  90.         float fov = 60.0f; // degrees, try also 45, or different number if you like
  91.         float top = (float) (Math.tan(fov * ((float) Math.PI) / 360.0f) * nearZ);
  92.         float bottom = -top;
  93.         float left = ratio * bottom;
  94.         float right = ratio * top;
  95.         Matrix.frustumM( mProjMatrix, 0, left, right, bottom, top, nearZ, farZ );
  96.         int a = 10;
  97.     }
  98.  
  99.     @Override
  100.     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
  101.         // Set the background frame color
  102.         GLES20.glClearColor(0.721f, 0.23f, 0.52f, 1.0f);
  103.        
  104.         // Initialize triangle
  105.         //mTriangle = new Triangle();
  106.         // Initialize square
  107.         mSquare = new Square();
  108.         //mTriangle = new MulticoloredTriangle();
  109.         mTexSquare = new TexturedSquare( mContext );
  110.         mTexCube = new TexturedCube( mContext );
  111.     }
  112.    
  113.     public static int LoadShader( int type, String shaderCode ) {
  114.         // Create a vertex shader type (GLES20.GL_VERTEX_SHADER)
  115.         // or a fragment shader type (GLES20.GL_FGARMENT_SHADER)
  116.         int shader = GLES20.glCreateShader( type );
  117.        
  118.         // Add the source code to the shader and compile it
  119.         GLES20.glShaderSource( shader, shaderCode );
  120.         GLES20.glCompileShader( shader );
  121.        
  122.         return shader;
  123.     }
  124.    
  125.     public void onTouchEvent( MotionEvent e ) {
  126.         // MotionEvent reports input details from the touch screen
  127.         // and other input controls. In this case, you are only
  128.         // interested in events where the touch position changed.
  129.  
  130.         float x = e.getX();
  131.         float y = e.getY();
  132.  
  133.         switch (e.getAction()) {
  134.             case MotionEvent.ACTION_MOVE:
  135.  
  136.                 float dx = x - mPreviousX;
  137.                 float dy = y - mPreviousY;
  138.  
  139.                 // reverse direction of rotation above the mid-line
  140.                 if (y > mHeight / 2) {
  141.                     dx = dx * -1 ;
  142.                 }
  143.  
  144.                 // reverse direction of rotation to left of the mid-line
  145.                 if (x < mWidth / 2) {
  146.                     dy = dy * -1 ;
  147.                 }
  148.  
  149.                 mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
  150.         }
  151.  
  152.         mPreviousX = x;
  153.         mPreviousY = y;
  154.     }
  155.    
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement