Advertisement
chrisvarns

GLES20 cube

Dec 17th, 2011
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. HelloOpenGLES20Activity.java
  2. ---------------------------------------------------------------------------------------
  3.  
  4.  
  5. package com.varnz.helloopengles20;
  6.  
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.opengl.GLSurfaceView;
  10. import android.os.Bundle;
  11.  
  12. public class HelloOpenGLES20Activity extends Activity {
  13.    
  14.     private GLSurfaceView mGLSurfaceView;
  15.    
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.        
  20.         //Create a GLSurfaceView instance and set it
  21.         //as the ContentView for this activity.
  22.         mGLSurfaceView = new HelloOpenGLES20SurfaceView(this);
  23.         setContentView(mGLSurfaceView);
  24.     }
  25.    
  26.     @Override
  27.     protected void onPause() {
  28.         super.onPause();
  29.         //The following call pauses the rendering thread.
  30.         //If your OpenGL application is memory intensive,
  31.         //you should consider de-allocating objects that
  32.         //consume significant memory here.
  33.         mGLSurfaceView.onPause();
  34.     }
  35.    
  36.     @Override
  37.     protected void onResume() {
  38.         super.onResume();
  39.         //The following call resumes a paused rendering thread.
  40.         //If you de-allocated graphic objects for onPause()
  41.         //this is a good place to re-allocate them.
  42.         mGLSurfaceView.onResume();
  43.     }
  44. }
  45.  
  46. class HelloOpenGLES20SurfaceView extends GLSurfaceView {
  47.    
  48.     public HelloOpenGLES20SurfaceView(Context context){
  49.         super(context);
  50.        
  51.         //Create an OpenGL ES 2.0 context.
  52.         setEGLContextClientVersion(2);
  53.         //Set the Renderer for drawing on the GLSurfaceView
  54.         setRenderer(new HelloOpenGLES20Renderer());
  55.     }
  56. }
  57.  
  58.  
  59.  
  60. HelloOpenGLES20Renderer.java
  61. ---------------------------------------------------------------------------------------
  62.  
  63. package com.varnz.helloopengles20;
  64.  
  65. import java.nio.ByteBuffer;
  66. import java.nio.ByteOrder;
  67. import java.nio.FloatBuffer;
  68. import java.nio.ShortBuffer;
  69.  
  70. import javax.microedition.khronos.egl.EGLConfig;
  71. import javax.microedition.khronos.opengles.GL10;
  72.  
  73. import android.opengl.GLES20;
  74. import android.opengl.GLSurfaceView;
  75. import android.opengl.Matrix;
  76.  
  77. public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer {
  78.    
  79.     private FloatBuffer cubeVertBuffer,
  80.                         cubeColourBuffer;
  81.    
  82.     private ShortBuffer cubeIndexBuffer;
  83.    
  84.     private int mProgram,
  85.                 maPositionHandle,
  86.                 maColourHandle,
  87.                 muMVPMatrixHandle;
  88.    
  89.     private float[] mMVPMatrix = new float[16];
  90.     private float[] mMMatrix = new float[16];
  91.     private float[] mVMatrix = new float[16];
  92.     private float[] mProjMatrix = new float[16];
  93.    
  94.     private final String vertexShaderCode =
  95.             "uniform mat4 uMVPMatrix;   \n" +
  96.             "attribute vec4 vPosition;  \n" +
  97.             "attribute vec4 vColour;    \n" +
  98.             "varying vec4 fColour;      \n" +
  99.             "void main(){               \n" +
  100.             "   fColour = vColour;      \n" +
  101.             "   gl_Position = uMVPMatrix * vPosition;\n" +
  102.             "}                          \n";
  103.    
  104.     private final String fragmentShaderCode =
  105.             "precision mediump float;   \n" +
  106.             "varying vec4 fColour;      \n" +
  107.             "void main(){               \n" +
  108.             "   gl_FragColor = fColour; \n" +
  109.             //" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
  110.             "}                          \n";
  111.    
  112.     private int loadShader(int type, String shaderCode){
  113.         //Create a vertex shader type
  114.         //or a fragment shader type
  115.         int shader = GLES20.glCreateShader(type);
  116.        
  117.         //add the source code to the shader and compile it
  118.         GLES20.glShaderSource(shader, shaderCode);
  119.         GLES20.glCompileShader(shader);
  120.        
  121.         return shader;
  122.     }
  123.    
  124.     private void initShapes(){
  125.        
  126.         float[] cubeCoords = {
  127.                 //X, Y, Z
  128.                 -1, -1, -1,
  129.                  1, -1, -1,
  130.                  1,  1, -1,
  131.                 -1,  1, -1,
  132.                 -1, -1,  1,
  133.                  1, -1,  1,
  134.                  1,  1,  1,
  135.                 -1,  1,  1
  136.         };
  137.        
  138.         float[] cubeColours = {
  139.                 0, 0, 0, 1,
  140.                 0, 0, 1, 1,
  141.                 0, 1, 0, 1,
  142.                 0, 1, 1, 1,
  143.                 1, 0, 0, 1,
  144.                 1, 0, 1, 1,
  145.                 1, 1, 0, 1,
  146.                 1, 1, 1, 1
  147.         };
  148.        
  149.         short[] cubeIndices = {            
  150.                 0, 4, 5,
  151.                 0, 5, 1,
  152.                 1, 5, 6,
  153.                 1, 6, 2,
  154.                 2, 6, 7,
  155.                 2, 7, 3,
  156.                 3, 7, 4,
  157.                 3, 4, 0,
  158.                 4, 7, 6,
  159.                 4, 6, 5,
  160.                 3, 0, 1,
  161.                 3, 1, 2
  162.         };
  163.        
  164.         cubeVertBuffer = ByteBuffer.allocateDirect(cubeCoords.length * 4)
  165.                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
  166.         cubeVertBuffer.put(cubeCoords).position(0);
  167.        
  168.         cubeColourBuffer = ByteBuffer.allocateDirect(cubeColours.length * 4)
  169.                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
  170.         cubeColourBuffer.put(cubeColours).position(0);
  171.        
  172.         cubeIndexBuffer = ByteBuffer.allocateDirect(cubeIndices.length * 4)
  173.                 .order(ByteOrder.nativeOrder()).asShortBuffer();
  174.         cubeIndexBuffer.put(cubeIndices).position(0);
  175.     }
  176.    
  177.     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
  178.        
  179.         //Set the background frame color
  180.         GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1);
  181.         GLES20.glEnable(GLES20.GL_DEPTH_TEST);
  182.         GLES20.glDepthFunc(GLES20.GL_LEQUAL);
  183.         initShapes();
  184.        
  185.         int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
  186.         int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
  187.        
  188.         mProgram = GLES20.glCreateProgram();
  189.         GLES20.glAttachShader(mProgram, vertexShader);
  190.         GLES20.glAttachShader(mProgram, fragmentShader);
  191.         GLES20.glLinkProgram(mProgram);
  192.        
  193.         //get handle to the vertex shaders vPos member
  194.         maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
  195.         maColourHandle = GLES20.glGetAttribLocation(mProgram, "vColour");
  196.         muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
  197.        
  198.         Matrix.setIdentityM(mMMatrix, 0);
  199.         Matrix.rotateM(mMMatrix, 0, -40, 1, -1, 0);
  200.        
  201.     }
  202.    
  203.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  204.         GLES20.glViewport(0, 0, width, height);
  205.        
  206.         float ratio = (float) width / height;
  207.        
  208.         Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 1, 10);
  209.         Matrix.setLookAtM(mVMatrix, 0, 0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  210.        
  211.     }
  212.    
  213.     public void onDrawFrame(GL10 unused) {
  214.        
  215.         //Redraw background color
  216.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  217.        
  218.         //Add program
  219.         GLES20.glUseProgram(mProgram);
  220.        
  221.         //Prepare the cube data
  222.         GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 0, cubeVertBuffer);
  223.         GLES20.glEnableVertexAttribArray(maPositionHandle);
  224.         GLES20.glVertexAttribPointer(maColourHandle, 4, GLES20.GL_FLOAT, false, 0, cubeColourBuffer);
  225.         GLES20.glEnableVertexAttribArray(maColourHandle);
  226.        
  227.         //Rotate cube
  228.         Matrix.rotateM(mMMatrix, 0, 3f, 6f, 2.7f, 3.5f);
  229.        
  230.         //Set up MVP
  231.         Matrix.setIdentityM(mMVPMatrix, 0);
  232.         Matrix.multiplyMM(mMVPMatrix, 0, mMMatrix, 0, mMVPMatrix, 0);
  233.         Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMVPMatrix, 0);
  234.         Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
  235.        
  236.         GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
  237.  
  238.         //Draw the cube
  239.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, cubeIndexBuffer);
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement