Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.opengltrying;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- import android.content.Context;
- import android.opengl.GLES20;
- import android.opengl.Matrix;
- import android.os.SystemClock;
- public class TexturedCube {
- private FloatBuffer mVertexBuffer;
- private ShortBuffer mIndexBuffer;
- private float mVerticesData[] = {
- // Front face
- -1.0f, 1.0f, 1.0f,
- 0.0f, 0.0f,
- -1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 0.0f,
- -1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, -1.0f, 1.0f,
- 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 0.0f,
- // Right face
- 1.0f, 1.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- 1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, 1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- // Back face
- 1.0f, 1.0f, -1.0f,
- 0.0f, 0.0f,
- 1.0f, -1.0f, -1.0f,
- 0.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- 1.0f, -1.0f, -1.0f,
- 0.0f, 1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- // Left face
- -1.0f, 1.0f, -1.0f,
- 0.0f, 0.0f,
- -1.0f, -1.0f, -1.0f,
- 0.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f, 0.0f,
- -1.0f, -1.0f, -1.0f,
- 0.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f, 0.0f,
- // Top face
- -1.0f, 1.0f, -1.0f,
- 0.0f, 0.0f,
- -1.0f, 1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- -1.0f, 1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 0.0f,
- // Bottom face
- 1.0f, -1.0f, -1.0f,
- 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, 0.0f,
- 1.0f, -1.0f, 1.0f,
- 0.0f, 1.0f,
- -1.0f, -1.0f, 1.0f,
- 1.0f, 1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, 0.0f
- };
- // Number of coordinates per vertex in this array
- private final int FLOATS_PER_VERTEX = 5;
- private final int BYTES_PER_FLOAT = 4;
- private final int BYTES_PER_SHORT = 2;
- private final int mVertexPositionDataSize = 3;
- private final int mVertexTextureCoordsDataSize = 2;
- private short mIndices[] = { 0, 1, 2, 0, 2, 3 };
- private final String mVertexShaderCode =
- "uniform mat4 uWVPMatrix;" +
- "attribute vec4 aPosition;" +
- "attribute vec2 aTexCoord;" +
- "varying vec2 vTexCoord;" +
- "void main() {" +
- " vTexCoord = aTexCoord;" +
- " gl_Position = aPosition * uWVPMatrix;" +
- "}";
- private final String mFragmentShaderCode =
- "precision mediump float;" +
- "varying vec2 vTexCoord;" +
- "uniform sampler2D uTexture;" +
- "void main() {" +
- " gl_FragColor = texture2D( uTexture, vTexCoord );" +
- //" gl_FragColor = vec4( 0.1f, 0.12f, 0.52f, 1.0f );" +
- "}";
- private int mProgram;
- private int mPositionHandle;
- private int mTexCoordHandle;
- private final int mVertexStrideBytes = FLOATS_PER_VERTEX * BYTES_PER_FLOAT; // bytes per vertex
- private final int mVerticesCount = mVerticesData.length / FLOATS_PER_VERTEX;
- private int mWVPMatrixHandle;
- private int mTextureHandle;
- private int mTexture;
- private float[] mModelMatrix = new float[16];
- private float[] mMVPMatrix = new float[16];
- public TexturedCube( final Context context ) {
- Matrix.setIdentityM( mModelMatrix, 0 );
- // Initialize vertex byte buffer for shape coordinates
- ByteBuffer bb = ByteBuffer.allocateDirect(
- // ( # of coordinate values * 4 bytes per float )
- mVerticesData.length * BYTES_PER_FLOAT );
- bb.order( ByteOrder.nativeOrder() );
- mVertexBuffer = bb.asFloatBuffer();
- mVertexBuffer.put( mVerticesData );
- mVertexBuffer.position( 0 );
- // Initialize byte buffer for the draw list
- ByteBuffer dlb = ByteBuffer.allocateDirect(
- // ( # of coordinate values * 2 bytes per short )
- mIndices.length * BYTES_PER_SHORT );
- dlb.order( ByteOrder.nativeOrder() );
- mIndexBuffer = dlb.asShortBuffer();
- mIndexBuffer.put( mIndices );
- mIndexBuffer.position( 0 );
- // Configure rendering
- int vertexShader = MyRenderer.LoadShader( GLES20.GL_VERTEX_SHADER,
- mVertexShaderCode );
- int fragmentShader = MyRenderer.LoadShader( GLES20.GL_FRAGMENT_SHADER,
- mFragmentShaderCode );
- mProgram = GLES20.glCreateProgram();
- GLES20.glAttachShader( mProgram, vertexShader );
- GLES20.glAttachShader( mProgram, fragmentShader );
- GLES20.glLinkProgram( mProgram );
- // Load texture resource
- mTexture = GFXUtils.LoadTexture( context, R.drawable.brick );
- }
- public void draw( float[] viewProjMatrix ) {
- // Add program to OpenGL ES environment
- GLES20.glUseProgram( mProgram );
- int err = GLES20.glGetError();
- // Get handle to vertex shader's vPosition member
- mPositionHandle = GLES20.glGetAttribLocation( mProgram, "aPosition" );
- err = GLES20.glGetError();
- mTexCoordHandle = GLES20.glGetAttribLocation( mProgram, "aTexCoord" );
- err = GLES20.glGetError();
- // Set the array offset to the start of position data
- mVertexBuffer.position( 0 );
- // Prepare the position coordinate data
- GLES20.glVertexAttribPointer( mPositionHandle, mVertexPositionDataSize,
- GLES20.GL_FLOAT, false,
- mVertexStrideBytes, mVertexBuffer );
- err = GLES20.glGetError();
- // Enable a handle to position chunk of data
- GLES20.glEnableVertexAttribArray( mPositionHandle );
- // Set the array offset to the start of color data
- mVertexBuffer.position( 0 + mVertexPositionDataSize );
- // Prepare the color coordinate data
- GLES20.glVertexAttribPointer( mTexCoordHandle, mVertexTextureCoordsDataSize,
- GLES20.GL_FLOAT, false,
- mVertexStrideBytes, mVertexBuffer );
- // Enable a handle to color chunk of data
- GLES20.glEnableVertexAttribArray( mTexCoordHandle );
- // Get handle to the vertex shader's uWVPMatrix member
- mWVPMatrixHandle = GLES20.glGetUniformLocation( mProgram, "uWVPMatrix" );
- // Set the transformation matrix
- // Get rotation angle based on system time
- long time = SystemClock.uptimeMillis() % 4000L;
- float angle = 0.0090f * ((int) time);
- // Create rotation matrix around -Z axis
- float[] rotationMatrix = new float[16];
- Matrix.setRotateM( rotationMatrix, 0, angle, 0.0f, -1.0f, 0.0f );
- Matrix.multiplyMM( mModelMatrix, 0, rotationMatrix, 0, mModelMatrix, 0 );
- Matrix.multiplyMM( mMVPMatrix, 0, viewProjMatrix, 0, mModelMatrix, 0 );
- GLES20.glUniformMatrix4fv( mWVPMatrixHandle, 1, false, mMVPMatrix, 0 );
- mTextureHandle = GLES20.glGetUniformLocation( mProgram, "uTexture" );
- GLES20.glActiveTexture( GLES20.GL_TEXTURE0 );
- GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, mTexture );
- GLES20.glUniform1i( mTextureHandle, 0);
- // Draw the triangle
- GLES20.glDrawArrays( GLES20.GL_TRIANGLES, 0, mVerticesCount );
- // Disable vertex position chunk of data in array
- //GLES20.glDisableVertexAttribArray( mPositionHandle );
- // Disable vertex color chunk of data in array
- //GLES20.glDisableVertexAttribArray( mColorHandle );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement