package com.example.opengltrying; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.opengl.GLES20; import android.opengl.Matrix; import android.opengl.GLSurfaceView; import android.os.SystemClock; import android.provider.Settings.System; import android.view.MotionEvent; public class MyRenderer implements GLSurfaceView.Renderer { private static final float TOUCH_SCALE_FACTOR = 180.0f / 320.0f; public volatile float mAngle = 0.0f; //private Triangle mTriangle; private Square mSquare; private MulticoloredTriangle mTriangle; private TexturedSquare mTexSquare; private TexturedCube mTexCube; private float[] mProjMatrix = new float[16]; private float[] mViewMatrix = new float[16]; private float[] mMVPMatrix = new float[16]; private float[] mRotationMatrix = new float[16]; private float[] mViewProjMatrix = new float[16]; private volatile Context mContext; private int mWidth; private int mHeight; private float mPreviousX = 0.0f; private float mPreviousY = 0.0f; public MyRenderer( final Context context, final int width, final int height ) { mContext = context; mWidth = width; mHeight = height; } @Override public void onDrawFrame(GL10 unused) { // Redraw background color GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // Set the background frame color GLES20.glClearColor(0.21f, 0.23f, 0.52f, 1.0f); // Set the camera position Matrix.setLookAtM( mViewMatrix, 0, 0.0f, 0.0f, 10.0f, // camera position 0.0f, 0.0f, 0.0f, // camera target 0.0f, 1.0f, 0.0f ); // camera up vector // Calculate model * view * projection matrix Matrix.multiplyMM( mMVPMatrix, 0, mProjMatrix, 0, mViewMatrix, 0 ); // Get rotation angle based on system time //long time = SystemClock.uptimeMillis() % 4000L; //mAngle = 0.090f * ((int) time); // Create rotation matrix around -Z axis Matrix.setRotateM( mRotationMatrix, 0, mAngle, 0.0f, -1.0f, 0.0f ); // Update WVP matrix with given rotation angle Matrix.multiplyMM( mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0 ); Matrix.multiplyMM( mViewProjMatrix, 0, mProjMatrix, 0, mViewMatrix, 0); // Draw triangle //mTriangle.draw( mMVPMatrix ); //mSquare.draw( mMVPMatrix ); //mTriangle.draw( mMVPMatrix ); //mTexSquare.draw( mMVPMatrix ); mTexCube.draw( mViewProjMatrix ); } @Override public void onSurfaceChanged(GL10 unused, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; // Define projection matrix //Matrix.perspectiveM( mProjMatrix, 0, 45.0f, ratio, 0.0f, 100.0f ); float nearZ = 0.1f; float farZ = 100.0f; float fov = 60.0f; // degrees, try also 45, or different number if you like float top = (float) (Math.tan(fov * ((float) Math.PI) / 360.0f) * nearZ); float bottom = -top; float left = ratio * bottom; float right = ratio * top; Matrix.frustumM( mProjMatrix, 0, left, right, bottom, top, nearZ, farZ ); int a = 10; } @Override public void onSurfaceCreated(GL10 unused, EGLConfig config) { // Set the background frame color GLES20.glClearColor(0.721f, 0.23f, 0.52f, 1.0f); // Initialize triangle //mTriangle = new Triangle(); // Initialize square mSquare = new Square(); //mTriangle = new MulticoloredTriangle(); mTexSquare = new TexturedSquare( mContext ); mTexCube = new TexturedCube( mContext ); } public static int LoadShader( int type, String shaderCode ) { // Create a vertex shader type (GLES20.GL_VERTEX_SHADER) // or a fragment shader type (GLES20.GL_FGARMENT_SHADER) int shader = GLES20.glCreateShader( type ); // Add the source code to the shader and compile it GLES20.glShaderSource( shader, shaderCode ); GLES20.glCompileShader( shader ); return shader; } public void onTouchEvent( MotionEvent e ) { // MotionEvent reports input details from the touch screen // and other input controls. In this case, you are only // interested in events where the touch position changed. float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - mPreviousX; float dy = y - mPreviousY; // reverse direction of rotation above the mid-line if (y > mHeight / 2) { dx = dx * -1 ; } // reverse direction of rotation to left of the mid-line if (x < mWidth / 2) { dy = dy * -1 ; } mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; } mPreviousX = x; mPreviousY = y; } }