00001 package uk.co.blogspot.fractiousg.texample;
00002
00003 import java.nio.ByteBuffer;
00004 import java.nio.ByteOrder;
00005 import java.nio.IntBuffer;
00006 import java.nio.ShortBuffer;
00007
00008 import javax.microedition.khronos.opengles.GL10;
00009
00010 public class Vertices {
00011
00012
00013 final static int POSITION_CNT_2D = 2;
00014 final static int POSITION_CNT_3D = 3;
00015 final static int COLOR_CNT = 4;
00016 final static int TEXCOORD_CNT = 2;
00017 final static int NORMAL_CNT = 3;
00018
00019 final static int INDEX_SIZE = Short.SIZE / 8;
00020
00021
00022
00023 final GL10 gl;
00024 final boolean hasColor;
00025 final boolean hasTexCoords;
00026 final boolean hasNormals;
00027 public final int positionCnt;
00028 public final int vertexStride;
00029 public final int vertexSize;
00030 final IntBuffer vertices;
00031 final ShortBuffer indices;
00032 public int numVertices;
00033 public int numIndices;
00034 final int[] tmpBuffer;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords, boolean hasNormals) {
00047 this( gl, maxVertices, maxIndices, hasColor, hasTexCoords, hasNormals, false );
00048 }
00049 public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords, boolean hasNormals, boolean use3D) {
00050 this.gl = gl;
00051 this.hasColor = hasColor;
00052 this.hasTexCoords = hasTexCoords;
00053 this.hasNormals = hasNormals;
00054 this.positionCnt = use3D ? POSITION_CNT_3D : POSITION_CNT_2D;
00055 this.vertexStride = this.positionCnt + ( hasColor ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 ) + ( hasNormals ? NORMAL_CNT : 0 );
00056 this.vertexSize = this.vertexStride * 4;
00057
00058 ByteBuffer buffer = ByteBuffer.allocateDirect( maxVertices * vertexSize );
00059 buffer.order( ByteOrder.nativeOrder() );
00060 this.vertices = buffer.asIntBuffer();
00061
00062 if ( maxIndices > 0 ) {
00063 buffer = ByteBuffer.allocateDirect( maxIndices * INDEX_SIZE );
00064 buffer.order( ByteOrder.nativeOrder() );
00065 this.indices = buffer.asShortBuffer();
00066 }
00067 else
00068 indices = null;
00069
00070 numVertices = 0;
00071 numIndices = 0;
00072
00073 this.tmpBuffer = new int[maxVertices * vertexSize / 4];
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 public void setVertices(float[] vertices, int offset, int length) {
00085 this.vertices.clear();
00086 int last = offset + length;
00087 for ( int i = offset, j = 0; i < last; i++, j++ )
00088 tmpBuffer[j] = Float.floatToRawIntBits( vertices[i] );
00089 this.vertices.put( tmpBuffer, 0, length );
00090 this.vertices.flip();
00091 this.numVertices = length / this.vertexStride;
00092
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 public void setIndices(short[] indices, int offset, int length) {
00102 this.indices.clear();
00103 this.indices.put( indices, offset, length );
00104 this.indices.flip();
00105 this.numIndices = length;
00106 }
00107
00108
00109
00110
00111
00112
00113 public void bind() {
00114 gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
00115 vertices.position( 0 );
00116 gl.glVertexPointer( positionCnt, GL10.GL_FLOAT, vertexSize, vertices );
00117
00118 if ( hasColor ) {
00119 gl.glEnableClientState( GL10.GL_COLOR_ARRAY );
00120 vertices.position( positionCnt );
00121 gl.glColorPointer( COLOR_CNT, GL10.GL_FLOAT, vertexSize, vertices );
00122 }
00123
00124 if ( hasTexCoords ) {
00125 gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
00126 vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) );
00127 gl.glTexCoordPointer( TEXCOORD_CNT, GL10.GL_FLOAT, vertexSize, vertices );
00128 }
00129
00130 if ( hasNormals ) {
00131 gl.glEnableClientState( GL10.GL_NORMAL_ARRAY );
00132 vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 ) );
00133 gl.glNormalPointer( GL10.GL_FLOAT, vertexSize, vertices );
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144 public void draw(int primitiveType, int offset, int numVertices) {
00145 if ( indices != null ) {
00146 indices.position( offset );
00147 gl.glDrawElements( primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices );
00148 }
00149 else {
00150 gl.glDrawArrays( primitiveType, offset, numVertices );
00151 }
00152 }
00153
00154
00155
00156
00157
00158
00159 public void unbind() {
00160 if ( hasColor )
00161 gl.glDisableClientState( GL10.GL_COLOR_ARRAY );
00162
00163 if ( hasTexCoords )
00164 gl.glDisableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
00165
00166 if ( hasNormals )
00167 gl.glDisableClientState( GL10.GL_NORMAL_ARRAY );
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 public void drawFull(int primitiveType, int offset, int numVertices) {
00178 gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
00179 vertices.position( 0 );
00180 gl.glVertexPointer( positionCnt, GL10.GL_FLOAT, vertexSize, vertices );
00181
00182 if ( hasColor ) {
00183 gl.glEnableClientState( GL10.GL_COLOR_ARRAY );
00184 vertices.position( positionCnt );
00185 gl.glColorPointer( COLOR_CNT, GL10.GL_FLOAT, vertexSize, vertices );
00186 }
00187
00188 if ( hasTexCoords ) {
00189 gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
00190 vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) );
00191 gl.glTexCoordPointer( TEXCOORD_CNT, GL10.GL_FLOAT, vertexSize, vertices );
00192 }
00193
00194 if ( indices != null ) {
00195 indices.position( offset );
00196 gl.glDrawElements( primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices );
00197 }
00198 else {
00199 gl.glDrawArrays( primitiveType, offset, numVertices );
00200 }
00201
00202 if ( hasTexCoords )
00203 gl.glDisableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
00204
00205 if ( hasColor )
00206 gl.glDisableClientState( GL10.GL_COLOR_ARRAY );
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 void setVtxPosition(int vtxIdx, float x, float y) {
00219 int index = vtxIdx * vertexStride;
00220 vertices.put( index + 0, Float.floatToRawIntBits( x ) );
00221 vertices.put( index + 1, Float.floatToRawIntBits( y ) );
00222 }
00223 void setVtxPosition(int vtxIdx, float x, float y, float z) {
00224 int index = vtxIdx * vertexStride;
00225 vertices.put( index + 0, Float.floatToRawIntBits( x ) );
00226 vertices.put( index + 1, Float.floatToRawIntBits( y ) );
00227 vertices.put( index + 2, Float.floatToRawIntBits( z ) );
00228 }
00229 void setVtxColor(int vtxIdx, float r, float g, float b, float a) {
00230 int index = ( vtxIdx * vertexStride ) + positionCnt;
00231 vertices.put( index + 0, Float.floatToRawIntBits( r ) );
00232 vertices.put( index + 1, Float.floatToRawIntBits( g ) );
00233 vertices.put( index + 2, Float.floatToRawIntBits( b ) );
00234 vertices.put( index + 3, Float.floatToRawIntBits( a ) );
00235 }
00236 void setVtxColor(int vtxIdx, float r, float g, float b) {
00237 int index = ( vtxIdx * vertexStride ) + positionCnt;
00238 vertices.put( index + 0, Float.floatToRawIntBits( r ) );
00239 vertices.put( index + 1, Float.floatToRawIntBits( g ) );
00240 vertices.put( index + 2, Float.floatToRawIntBits( b ) );
00241 }
00242 void setVtxColor(int vtxIdx, float a) {
00243 int index = ( vtxIdx * vertexStride ) + positionCnt;
00244 vertices.put( index + 3, Float.floatToRawIntBits( a ) );
00245 }
00246 void setVtxTexCoords(int vtxIdx, float u, float v) {
00247 int index = ( vtxIdx * vertexStride ) + positionCnt + ( hasColor ? COLOR_CNT : 0 );
00248 vertices.put( index + 0, Float.floatToRawIntBits( u ) );
00249 vertices.put( index + 1, Float.floatToRawIntBits( v ) );
00250 }
00251 void setVtxNormal(int vtxIdx, float x, float y, float z) {
00252 int index = ( vtxIdx * vertexStride ) + positionCnt + ( hasColor ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 );
00253 vertices.put( index + 0, Float.floatToRawIntBits( x ) );
00254 vertices.put( index + 1, Float.floatToRawIntBits( y ) );
00255 vertices.put( index + 2, Float.floatToRawIntBits( z ) );
00256 }
00257 }