Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HelloOpenGLES20Activity.java
- ---------------------------------------------------------------------------------------
- package com.varnz.helloopengles20;
- import android.app.Activity;
- import android.content.Context;
- import android.opengl.GLSurfaceView;
- import android.os.Bundle;
- public class HelloOpenGLES20Activity extends Activity {
- private GLSurfaceView mGLSurfaceView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //Create a GLSurfaceView instance and set it
- //as the ContentView for this activity.
- mGLSurfaceView = new HelloOpenGLES20SurfaceView(this);
- setContentView(mGLSurfaceView);
- }
- @Override
- protected void onPause() {
- super.onPause();
- //The following call pauses the rendering thread.
- //If your OpenGL application is memory intensive,
- //you should consider de-allocating objects that
- //consume significant memory here.
- mGLSurfaceView.onPause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- //The following call resumes a paused rendering thread.
- //If you de-allocated graphic objects for onPause()
- //this is a good place to re-allocate them.
- mGLSurfaceView.onResume();
- }
- }
- class HelloOpenGLES20SurfaceView extends GLSurfaceView {
- public HelloOpenGLES20SurfaceView(Context context){
- super(context);
- //Create an OpenGL ES 2.0 context.
- setEGLContextClientVersion(2);
- //Set the Renderer for drawing on the GLSurfaceView
- setRenderer(new HelloOpenGLES20Renderer());
- }
- }
- HelloOpenGLES20Renderer.java
- ---------------------------------------------------------------------------------------
- package com.varnz.helloopengles20;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLES20;
- import android.opengl.GLSurfaceView;
- import android.opengl.Matrix;
- public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer {
- private FloatBuffer cubeVertBuffer,
- cubeColourBuffer;
- private ShortBuffer cubeIndexBuffer;
- private int mProgram,
- maPositionHandle,
- maColourHandle,
- muMVPMatrixHandle;
- private float[] mMVPMatrix = new float[16];
- private float[] mMMatrix = new float[16];
- private float[] mVMatrix = new float[16];
- private float[] mProjMatrix = new float[16];
- private final String vertexShaderCode =
- "uniform mat4 uMVPMatrix; \n" +
- "attribute vec4 vPosition; \n" +
- "attribute vec4 vColour; \n" +
- "varying vec4 fColour; \n" +
- "void main(){ \n" +
- " fColour = vColour; \n" +
- " gl_Position = uMVPMatrix * vPosition;\n" +
- "} \n";
- private final String fragmentShaderCode =
- "precision mediump float; \n" +
- "varying vec4 fColour; \n" +
- "void main(){ \n" +
- " gl_FragColor = fColour; \n" +
- //" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
- "} \n";
- private int loadShader(int type, String shaderCode){
- //Create a vertex shader type
- //or a fragment shader type
- int shader = GLES20.glCreateShader(type);
- //add the source code to the shader and compile it
- GLES20.glShaderSource(shader, shaderCode);
- GLES20.glCompileShader(shader);
- return shader;
- }
- private void initShapes(){
- float[] cubeCoords = {
- //X, Y, Z
- -1, -1, -1,
- 1, -1, -1,
- 1, 1, -1,
- -1, 1, -1,
- -1, -1, 1,
- 1, -1, 1,
- 1, 1, 1,
- -1, 1, 1
- };
- float[] cubeColours = {
- 0, 0, 0, 1,
- 0, 0, 1, 1,
- 0, 1, 0, 1,
- 0, 1, 1, 1,
- 1, 0, 0, 1,
- 1, 0, 1, 1,
- 1, 1, 0, 1,
- 1, 1, 1, 1
- };
- short[] cubeIndices = {
- 0, 4, 5,
- 0, 5, 1,
- 1, 5, 6,
- 1, 6, 2,
- 2, 6, 7,
- 2, 7, 3,
- 3, 7, 4,
- 3, 4, 0,
- 4, 7, 6,
- 4, 6, 5,
- 3, 0, 1,
- 3, 1, 2
- };
- cubeVertBuffer = ByteBuffer.allocateDirect(cubeCoords.length * 4)
- .order(ByteOrder.nativeOrder()).asFloatBuffer();
- cubeVertBuffer.put(cubeCoords).position(0);
- cubeColourBuffer = ByteBuffer.allocateDirect(cubeColours.length * 4)
- .order(ByteOrder.nativeOrder()).asFloatBuffer();
- cubeColourBuffer.put(cubeColours).position(0);
- cubeIndexBuffer = ByteBuffer.allocateDirect(cubeIndices.length * 4)
- .order(ByteOrder.nativeOrder()).asShortBuffer();
- cubeIndexBuffer.put(cubeIndices).position(0);
- }
- public void onSurfaceCreated(GL10 unused, EGLConfig config) {
- //Set the background frame color
- GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1);
- GLES20.glEnable(GLES20.GL_DEPTH_TEST);
- GLES20.glDepthFunc(GLES20.GL_LEQUAL);
- initShapes();
- int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
- int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
- mProgram = GLES20.glCreateProgram();
- GLES20.glAttachShader(mProgram, vertexShader);
- GLES20.glAttachShader(mProgram, fragmentShader);
- GLES20.glLinkProgram(mProgram);
- //get handle to the vertex shaders vPos member
- maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
- maColourHandle = GLES20.glGetAttribLocation(mProgram, "vColour");
- muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
- Matrix.setIdentityM(mMMatrix, 0);
- Matrix.rotateM(mMMatrix, 0, -40, 1, -1, 0);
- }
- public void onSurfaceChanged(GL10 unused, int width, int height) {
- GLES20.glViewport(0, 0, width, height);
- float ratio = (float) width / height;
- Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 1, 10);
- Matrix.setLookAtM(mVMatrix, 0, 0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- }
- public void onDrawFrame(GL10 unused) {
- //Redraw background color
- GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
- //Add program
- GLES20.glUseProgram(mProgram);
- //Prepare the cube data
- GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 0, cubeVertBuffer);
- GLES20.glEnableVertexAttribArray(maPositionHandle);
- GLES20.glVertexAttribPointer(maColourHandle, 4, GLES20.GL_FLOAT, false, 0, cubeColourBuffer);
- GLES20.glEnableVertexAttribArray(maColourHandle);
- //Rotate cube
- Matrix.rotateM(mMMatrix, 0, 3f, 6f, 2.7f, 3.5f);
- //Set up MVP
- Matrix.setIdentityM(mMVPMatrix, 0);
- Matrix.multiplyMM(mMVPMatrix, 0, mMMatrix, 0, mMVPMatrix, 0);
- Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMVPMatrix, 0);
- Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
- GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
- //Draw the cube
- GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, cubeIndexBuffer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement