1: package com.programmez.opengl.exercice1; 2: 3: import java.nio.ByteBuffer; 4: import java.nio.ByteOrder; 5: import java.nio.IntBuffer; 6: 7: import javax.microedition.khronos.opengles.GL10; 8: 9: import android.content.Context; 10: import android.graphics.Bitmap; 11: import android.graphics.BitmapFactory; 12: import android.opengl.GLUtils; 13: 14: class GLCube { 15: private final IntBuffer mVertexBuffer; 16: private final IntBuffer mTextureBuffer; 17: 18: 19: public GLCube() { 20: 21: int one = 65536; 22: int half = one / 2; 23: int vertices[] = { 24: // FRONT 25: -half, -half, half, half, -half, half, 26: -half, half, half, half, half, half, 27: // BACK 28: -half, -half, -half, -half, half, -half, 29: half, -half, -half, half, half, -half, 30: // LEFT 31: -half, -half, half, -half, half, half, 32: -half, -half, -half, -half, half, -half, 33: // RIGHT 34: half, -half, -half, half, half, -half, 35: half, -half, half, half, half, half, 36: // TOP 37: -half, half, half, half, half, half, 38: -half, half, -half, half, half, -half, 39: // BOTTOM 40: -half, -half, half, -half, -half, -half, 41: half, -half, half, half, -half, -half, }; 42: 43: 44: 45: int texCoords[] = { 46: // FRONT 47: 0, one, one, one, 0, 0, one, 0, 48: // BACK 49: one, one, one, 0, 0, one, 0, 0, 50: // LEFT 51: one, one, one, 0, 0, one, 0, 0, 52: // RIGHT 53: one, one, one, 0, 0, one, 0, 0, 54: // TOP 55: one, 0, 0, 0, one, one, 0, one, 56: // BOTTOM 57: 0, 0, 0, one, one, 0, one, one, }; 58: 59: 60: 61: // Buffers to be passed to gl*Pointer() functions must be 62: // direct, i.e., they must be placed on the native heap 63: // where the garbage collector cannot move them. 64: // 65: // Buffers with multi-byte data types (e.g., short, int, 66: // float) must have their byte order set to native order 67: ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 68: vbb.order(ByteOrder.nativeOrder()); 69: mVertexBuffer = vbb.asIntBuffer(); 70: mVertexBuffer.put(vertices); 71: mVertexBuffer.position(0); 72: 73: 74: 75: // ... 76: ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4); 77: tbb.order(ByteOrder.nativeOrder()); 78: mTextureBuffer = tbb.asIntBuffer(); 79: mTextureBuffer.put(texCoords); 80: mTextureBuffer.position(0); 81: 82: } 83: 84: 85: public void draw(GL10 gl) { 86: gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer); 87: gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer); 88: 89: 90: 91: gl.glColor4f(1, 1, 1, 1); 92: gl.glNormal3f(0, 0, 1); 93: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); 94: gl.glNormal3f(0, 0, -1); 95: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); 96: 97: gl.glColor4f(1, 1, 1, 1); 98: gl.glNormal3f(-1, 0, 0); 99: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); 100: gl.glNormal3f(1, 0, 0); 101: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); 102: 103: gl.glColor4f(1, 1, 1, 1); 104: gl.glNormal3f(0, 1, 0); 105: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); 106: gl.glNormal3f(0, -1, 0); 107: gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); 108: } 109: 110: 111: static void loadTexture(GL10 gl, Context context, int resource) { 112: Bitmap bmp = BitmapFactory.decodeResource( 113: context.getResources(), resource); 114: GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); 115: gl.glTexParameterx(GL10.GL_TEXTURE_2D, 116: GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 117: gl.glTexParameterx(GL10.GL_TEXTURE_2D, 118: GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 119: bmp.recycle(); 120: } 121: 122: } 123: