Advertisement
Guest User

opengltrying1

a guest
Jul 13th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. package com.example.opengltrying;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import java.nio.ShortBuffer;
  7.  
  8. import android.content.Context;
  9. import android.opengl.GLES20;
  10. import android.opengl.Matrix;
  11. import android.os.SystemClock;
  12.  
  13. public class TexturedCube {
  14.    
  15.     private FloatBuffer mVertexBuffer;
  16.     private ShortBuffer mIndexBuffer;
  17.    
  18.     private float mVerticesData[] = {
  19.             // Front face
  20.             -1.0f, 1.0f, 1.0f,
  21.             0.0f, 0.0f,
  22.             -1.0f, -1.0f, 1.0f,
  23.             0.0f, 1.0f,
  24.             1.0f, 1.0f, 1.0f,
  25.             1.0f, 0.0f,
  26.             -1.0f, -1.0f, 1.0f,
  27.             0.0f, 1.0f,
  28.             1.0f, -1.0f, 1.0f,
  29.             1.0f, 1.0f,
  30.             1.0f, 1.0f, 1.0f,
  31.             1.0f, 0.0f,
  32.  
  33.             // Right face
  34.             1.0f, 1.0f, 1.0f,
  35.             0.0f, 0.0f,    
  36.             1.0f, -1.0f, 1.0f,
  37.             0.0f, 1.0f,
  38.             1.0f, 1.0f, -1.0f,
  39.             1.0f, 0.0f,
  40.             1.0f, -1.0f, 1.0f, 
  41.             0.0f, 1.0f,
  42.             1.0f, -1.0f, -1.0f,
  43.             1.0f, 1.0f,
  44.             1.0f, 1.0f, -1.0f,
  45.             1.0f, 0.0f,
  46.  
  47.             // Back face
  48.             1.0f, 1.0f, -1.0f, 
  49.             0.0f, 0.0f,    
  50.             1.0f, -1.0f, -1.0f,
  51.             0.0f, 1.0f,
  52.             -1.0f, 1.0f, -1.0f,
  53.             1.0f, 0.0f,
  54.             1.0f, -1.0f, -1.0f,
  55.             0.0f, 1.0f,
  56.             -1.0f, -1.0f, -1.0f,
  57.             1.0f, 1.0f,
  58.             -1.0f, 1.0f, -1.0f,
  59.             1.0f, 0.0f,
  60.  
  61.             // Left face
  62.             -1.0f, 1.0f, -1.0f,
  63.             0.0f, 0.0f,
  64.             -1.0f, -1.0f, -1.0f,
  65.             0.0f, 1.0f,
  66.             -1.0f, 1.0f, 1.0f,
  67.             1.0f, 0.0f,
  68.             -1.0f, -1.0f, -1.0f,
  69.             0.0f, 1.0f,
  70.             -1.0f, -1.0f, 1.0f,
  71.             1.0f, 1.0f,
  72.             -1.0f, 1.0f, 1.0f,
  73.             1.0f, 0.0f,
  74.  
  75.             // Top face
  76.             -1.0f, 1.0f, -1.0f,
  77.             0.0f, 0.0f,    
  78.             -1.0f, 1.0f, 1.0f,
  79.             0.0f, 1.0f,
  80.             1.0f, 1.0f, -1.0f,
  81.             1.0f, 0.0f,
  82.             -1.0f, 1.0f, 1.0f,
  83.             0.0f, 1.0f,
  84.             1.0f, 1.0f, 1.0f,
  85.             1.0f, 1.0f,
  86.             1.0f, 1.0f, -1.0f,
  87.             1.0f, 0.0f,
  88.  
  89.             // Bottom face
  90.             1.0f, -1.0f, -1.0f,
  91.             0.0f, 0.0f,    
  92.             1.0f, -1.0f, 1.0f,
  93.             0.0f, 1.0f,
  94.             -1.0f, -1.0f, -1.0f,
  95.             1.0f, 0.0f,
  96.             1.0f, -1.0f, 1.0f,  
  97.             0.0f, 1.0f,
  98.             -1.0f, -1.0f, 1.0f,
  99.             1.0f, 1.0f,
  100.             -1.0f, -1.0f, -1.0f,
  101.             1.0f, 0.0f
  102.     };
  103.    
  104.     // Number of coordinates per vertex in this array
  105.     private final int FLOATS_PER_VERTEX = 5;
  106.    
  107.     private final int BYTES_PER_FLOAT = 4;
  108.    
  109.     private final int BYTES_PER_SHORT = 2;
  110.    
  111.     private final int mVertexPositionDataSize = 3;
  112.    
  113.     private final int mVertexTextureCoordsDataSize = 2;
  114.    
  115.     private short mIndices[] = { 0, 1, 2, 0, 2, 3 };
  116.    
  117.     private final String mVertexShaderCode =
  118.             "uniform mat4 uWVPMatrix;" +
  119.             "attribute vec4 aPosition;" +
  120.             "attribute vec2 aTexCoord;" +
  121.             "varying vec2 vTexCoord;" +
  122.             "void main() {" +
  123.             "  vTexCoord = aTexCoord;" +
  124.             "  gl_Position = aPosition * uWVPMatrix;" +
  125.             "}";
  126.  
  127.     private final String mFragmentShaderCode =
  128.             "precision mediump float;" +
  129.             "varying vec2 vTexCoord;" +
  130.             "uniform sampler2D uTexture;" +
  131.             "void main() {" +
  132.             "  gl_FragColor = texture2D( uTexture, vTexCoord );" +
  133.             //"  gl_FragColor = vec4( 0.1f, 0.12f, 0.52f, 1.0f );" +
  134.             "}";
  135.    
  136.     private int mProgram;
  137.     private int mPositionHandle;
  138.     private int mTexCoordHandle;
  139.    
  140.     private final int mVertexStrideBytes = FLOATS_PER_VERTEX * BYTES_PER_FLOAT; // bytes per vertex
  141.     private final int mVerticesCount = mVerticesData.length / FLOATS_PER_VERTEX;
  142.     private int mWVPMatrixHandle;
  143.     private int mTextureHandle;
  144.     private int mTexture;
  145.    
  146.     private float[] mModelMatrix = new float[16];
  147.     private float[] mMVPMatrix = new float[16];
  148.    
  149.     public TexturedCube( final Context context ) {
  150.        
  151.         Matrix.setIdentityM( mModelMatrix, 0 );
  152.        
  153.         // Initialize vertex byte buffer for shape coordinates
  154.         ByteBuffer bb = ByteBuffer.allocateDirect(
  155.         // ( # of coordinate values * 4 bytes per float )
  156.                 mVerticesData.length * BYTES_PER_FLOAT );
  157.         bb.order( ByteOrder.nativeOrder() );
  158.         mVertexBuffer = bb.asFloatBuffer();
  159.         mVertexBuffer.put( mVerticesData );
  160.         mVertexBuffer.position( 0 );
  161.        
  162.         // Initialize byte buffer for the draw list
  163.         ByteBuffer dlb = ByteBuffer.allocateDirect(
  164.         // ( # of coordinate values * 2 bytes per short )
  165.                 mIndices.length * BYTES_PER_SHORT );
  166.         dlb.order( ByteOrder.nativeOrder() );
  167.         mIndexBuffer = dlb.asShortBuffer();
  168.         mIndexBuffer.put( mIndices );
  169.         mIndexBuffer.position( 0 );
  170.        
  171.         // Configure rendering
  172.         int vertexShader = MyRenderer.LoadShader( GLES20.GL_VERTEX_SHADER,
  173.                                                   mVertexShaderCode );
  174.        
  175.         int fragmentShader = MyRenderer.LoadShader( GLES20.GL_FRAGMENT_SHADER,
  176.                                                     mFragmentShaderCode );
  177.        
  178.         mProgram = GLES20.glCreateProgram();
  179.         GLES20.glAttachShader( mProgram, vertexShader );
  180.         GLES20.glAttachShader( mProgram, fragmentShader );
  181.         GLES20.glLinkProgram( mProgram );
  182.        
  183.         // Load texture resource
  184.         mTexture = GFXUtils.LoadTexture( context, R.drawable.brick );
  185.     }
  186.    
  187.     public void draw( float[] viewProjMatrix ) {
  188.         // Add program to OpenGL ES environment
  189.         GLES20.glUseProgram( mProgram );
  190.        
  191.         int err = GLES20.glGetError();
  192.        
  193.         // Get handle to vertex shader's vPosition member
  194.         mPositionHandle = GLES20.glGetAttribLocation( mProgram, "aPosition" );
  195.         err = GLES20.glGetError();
  196.         mTexCoordHandle = GLES20.glGetAttribLocation( mProgram, "aTexCoord" );
  197.         err = GLES20.glGetError();
  198.        
  199.         // Set the array offset to the start of position data
  200.         mVertexBuffer.position( 0 );
  201.        
  202.         // Prepare the position coordinate data
  203.         GLES20.glVertexAttribPointer( mPositionHandle, mVertexPositionDataSize,
  204.                                       GLES20.GL_FLOAT, false,
  205.                                       mVertexStrideBytes, mVertexBuffer );
  206.         err = GLES20.glGetError();
  207.        
  208.         // Enable a handle to position chunk of data
  209.         GLES20.glEnableVertexAttribArray( mPositionHandle );
  210.        
  211.         // Set the array offset to the start of color data
  212.         mVertexBuffer.position( 0 + mVertexPositionDataSize );
  213.        
  214.         // Prepare the color coordinate data
  215.         GLES20.glVertexAttribPointer( mTexCoordHandle, mVertexTextureCoordsDataSize,
  216.                                       GLES20.GL_FLOAT, false,
  217.                                       mVertexStrideBytes, mVertexBuffer );
  218.        
  219.         // Enable a handle to color chunk of data
  220.         GLES20.glEnableVertexAttribArray( mTexCoordHandle );
  221.        
  222.         // Get handle to the vertex shader's uWVPMatrix member
  223.         mWVPMatrixHandle = GLES20.glGetUniformLocation( mProgram, "uWVPMatrix" );
  224.        
  225.         // Set the transformation matrix
  226.         // Get rotation angle based on system time
  227.         long time = SystemClock.uptimeMillis() % 4000L;
  228.         float angle = 0.0090f * ((int) time);
  229.                
  230.         // Create rotation matrix around -Z axis
  231.         float[] rotationMatrix = new float[16];
  232.         Matrix.setRotateM( rotationMatrix, 0, angle, 0.0f, -1.0f, 0.0f );
  233.         Matrix.multiplyMM( mModelMatrix, 0, rotationMatrix, 0, mModelMatrix, 0 );
  234.         Matrix.multiplyMM( mMVPMatrix, 0, viewProjMatrix, 0, mModelMatrix, 0 );
  235.         GLES20.glUniformMatrix4fv( mWVPMatrixHandle, 1, false, mMVPMatrix, 0 );
  236.        
  237.         mTextureHandle = GLES20.glGetUniformLocation( mProgram, "uTexture" );
  238.        
  239.         GLES20.glActiveTexture( GLES20.GL_TEXTURE0 );
  240.         GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, mTexture );
  241.         GLES20.glUniform1i( mTextureHandle, 0);
  242.        
  243.         // Draw the triangle
  244.         GLES20.glDrawArrays( GLES20.GL_TRIANGLES, 0, mVerticesCount );
  245.        
  246.         // Disable vertex position chunk of data in array
  247.         //GLES20.glDisableVertexAttribArray( mPositionHandle );
  248.        
  249.         // Disable vertex color chunk of data in array
  250.         //GLES20.glDisableVertexAttribArray( mColorHandle );
  251.     }
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement