Vertices.java
Go to the documentation of this file.
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    //--Constants--//
00013    final static int POSITION_CNT_2D = 2;              // Number of Components in Vertex Position for 2D
00014    final static int POSITION_CNT_3D = 3;              // Number of Components in Vertex Position for 3D
00015    final static int COLOR_CNT = 4;                    // Number of Components in Vertex Color
00016    final static int TEXCOORD_CNT = 2;                 // Number of Components in Vertex Texture Coords
00017    final static int NORMAL_CNT = 3;                   // Number of Components in Vertex Normal
00018 
00019    final static int INDEX_SIZE = Short.SIZE / 8;      // Index Byte Size (Short.SIZE = bits)
00020 
00021    //--Members--//
00022    // NOTE: all members are constant, and initialized in constructor!
00023    final GL10 gl;                                     // GL Instance
00024    final boolean hasColor;                            // Use Color in Vertices
00025    final boolean hasTexCoords;                        // Use Texture Coords in Vertices
00026    final boolean hasNormals;                          // Use Normals in Vertices
00027    public final int positionCnt;                      // Number of Position Components (2=2D, 3=3D)
00028    public final int vertexStride;                     // Vertex Stride (Element Size of a Single Vertex)
00029    public final int vertexSize;                       // Bytesize of a Single Vertex
00030    final IntBuffer vertices;                          // Vertex Buffer
00031    final ShortBuffer indices;                         // Index Buffer
00032    public int numVertices;                            // Number of Vertices in Buffer
00033    public int numIndices;                             // Number of Indices in Buffer
00034    final int[] tmpBuffer;                             // Temp Buffer for Vertex Conversion
00035 
00036    //--Constructor--//
00037    // D: create the vertices/indices as specified (for 2d/3d)
00038    // A: gl - the gl instance to use
00039    //    maxVertices - maximum vertices allowed in buffer
00040    //    maxIndices - maximum indices allowed in buffer
00041    //    hasColor - use color values in vertices
00042    //    hasTexCoords - use texture coordinates in vertices
00043    //    hasNormals - use normals in vertices
00044    //    use3D - (false, default) use 2d positions (ie. x/y only)
00045    //            (true) use 3d positions (ie. x/y/z)
00046    public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords, boolean hasNormals)  {
00047       this( gl, maxVertices, maxIndices, hasColor, hasTexCoords, hasNormals, false );  // Call Overloaded Constructor
00048    }
00049    public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoords, boolean hasNormals, boolean use3D)  {
00050       this.gl = gl;                                   // Save GL Instance
00051       this.hasColor = hasColor;                       // Save Color Flag
00052       this.hasTexCoords = hasTexCoords;               // Save Texture Coords Flag
00053       this.hasNormals = hasNormals;                   // Save Normals Flag
00054       this.positionCnt = use3D ? POSITION_CNT_3D : POSITION_CNT_2D;  // Set Position Component Count
00055       this.vertexStride = this.positionCnt + ( hasColor ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 ) + ( hasNormals ? NORMAL_CNT : 0 );  // Calculate Vertex Stride
00056       this.vertexSize = this.vertexStride * 4;        // Calculate Vertex Byte Size
00057 
00058       ByteBuffer buffer = ByteBuffer.allocateDirect( maxVertices * vertexSize );  // Allocate Buffer for Vertices (Max)
00059       buffer.order( ByteOrder.nativeOrder() );        // Set Native Byte Order
00060       this.vertices = buffer.asIntBuffer();           // Save Vertex Buffer
00061 
00062       if ( maxIndices > 0 )  {                        // IF Indices Required
00063          buffer = ByteBuffer.allocateDirect( maxIndices * INDEX_SIZE );  // Allocate Buffer for Indices (MAX)
00064          buffer.order( ByteOrder.nativeOrder() );     // Set Native Byte Order
00065          this.indices = buffer.asShortBuffer();       // Save Index Buffer
00066       }
00067       else                                            // ELSE Indices Not Required
00068          indices = null;                              // No Index Buffer
00069 
00070       numVertices = 0;                                // Zero Vertices in Buffer
00071       numIndices = 0;                                 // Zero Indices in Buffer
00072 
00073       this.tmpBuffer = new int[maxVertices * vertexSize / 4];  // Create Temp Buffer
00074    }
00075 
00076    //--Set Vertices--//
00077    // D: set the specified vertices in the vertex buffer
00078    //    NOTE: optimized to use integer buffer!
00079    // A: vertices - array of vertices (floats) to set
00080    //    offset - offset to first vertex in array
00081    //    length - number of floats in the vertex array (total)
00082    //             for easy setting use: vtx_cnt * (this.vertexSize / 4)
00083    // R: [none]
00084    public void setVertices(float[] vertices, int offset, int length)  {
00085       this.vertices.clear();                          // Remove Existing Vertices
00086       int last = offset + length;                     // Calculate Last Element
00087       for ( int i = offset, j = 0; i < last; i++, j++ )  // FOR Each Specified Vertex
00088          tmpBuffer[j] = Float.floatToRawIntBits( vertices[i] );  // Set Vertex as Raw Integer Bits in Buffer
00089       this.vertices.put( tmpBuffer, 0, length );      // Set New Vertices
00090       this.vertices.flip();                           // Flip Vertex Buffer
00091       this.numVertices = length / this.vertexStride;  // Save Number of Vertices
00092       //this.numVertices = length / ( this.vertexSize / 4 );  // Save Number of Vertices
00093    }
00094 
00095    //--Set Indices--//
00096    // D: set the specified indices in the index buffer
00097    // A: indices - array of indices (shorts) to set
00098    //    offset - offset to first index in array
00099    //    length - number of indices in array (from offset)
00100    // R: [none]
00101    public void setIndices(short[] indices, int offset, int length)  {
00102       this.indices.clear();                           // Clear Existing Indices
00103       this.indices.put( indices, offset, length );    // Set New Indices
00104       this.indices.flip();                            // Flip Index Buffer
00105       this.numIndices = length;                       // Save Number of Indices
00106    }
00107 
00108    //--Bind--//
00109    // D: perform all required binding/state changes before rendering batches.
00110    //    USAGE: call once before calling draw() multiple times for this buffer.
00111    // A: [none]
00112    // R: [none]
00113    public void bind()  {
00114       gl.glEnableClientState( GL10.GL_VERTEX_ARRAY ); // Enable Position in Vertices
00115       vertices.position( 0 );                         // Set Vertex Buffer to Position
00116       gl.glVertexPointer( positionCnt, GL10.GL_FLOAT, vertexSize, vertices );  // Set Vertex Pointer
00117 
00118       if ( hasColor )  {                              // IF Vertices Have Color
00119          gl.glEnableClientState( GL10.GL_COLOR_ARRAY );  // Enable Color in Vertices
00120          vertices.position( positionCnt );            // Set Vertex Buffer to Color
00121          gl.glColorPointer( COLOR_CNT, GL10.GL_FLOAT, vertexSize, vertices );  // Set Color Pointer
00122       }
00123 
00124       if ( hasTexCoords )  {                          // IF Vertices Have Texture Coords
00125          gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );  // Enable Texture Coords in Vertices
00126          vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) );  // Set Vertex Buffer to Texture Coords (NOTE: position based on whether color is also specified)
00127          gl.glTexCoordPointer( TEXCOORD_CNT, GL10.GL_FLOAT, vertexSize, vertices );  // Set Texture Coords Pointer
00128       }
00129 
00130       if ( hasNormals )  {
00131          gl.glEnableClientState( GL10.GL_NORMAL_ARRAY );  // Enable Normals in Vertices
00132          vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 ) );  // Set Vertex Buffer to Normals (NOTE: position based on whether color/texcoords is also specified)
00133          gl.glNormalPointer( GL10.GL_FLOAT, vertexSize, vertices );  // Set Normals Pointer
00134       }
00135    }
00136 
00137    //--Draw--//
00138    // D: draw the currently bound vertices in the vertex/index buffers
00139    //    USAGE: can only be called after calling bind() for this buffer.
00140    // A: primitiveType - the type of primitive to draw
00141    //    offset - the offset in the vertex/index buffer to start at
00142    //    numVertices - the number of vertices (indices) to draw
00143    // R: [none]
00144    public void draw(int primitiveType, int offset, int numVertices)  {
00145       if ( indices != null )  {                       // IF Indices Exist
00146          indices.position( offset );                  // Set Index Buffer to Specified Offset
00147          gl.glDrawElements( primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices );  // Draw Indexed
00148       }
00149       else  {                                         // ELSE No Indices Exist
00150          gl.glDrawArrays( primitiveType, offset, numVertices );  // Draw Direct (Array)
00151       }
00152    }
00153 
00154    //--Unbind--//
00155    // D: clear binding states when done rendering batches.
00156    //    USAGE: call once before calling draw() multiple times for this buffer.
00157    // A: [none]
00158    // R: [none]
00159    public void unbind()  {
00160       if ( hasColor )                                 // IF Vertices Have Color
00161          gl.glDisableClientState( GL10.GL_COLOR_ARRAY );  // Clear Color State
00162 
00163       if ( hasTexCoords )                             // IF Vertices Have Texture Coords
00164          gl.glDisableClientState( GL10.GL_TEXTURE_COORD_ARRAY );  // Clear Texture Coords State
00165 
00166       if ( hasNormals )                               // IF Vertices Have Normals
00167          gl.glDisableClientState( GL10.GL_NORMAL_ARRAY );  // Clear Normals State
00168    }
00169 
00170    //--Draw Full--//
00171    // D: draw the vertices in the vertex/index buffers
00172    //    NOTE: unoptimized version! use bind()/draw()/unbind() for batches
00173    // A: primitiveType - the type of primitive to draw
00174    //    offset - the offset in the vertex/index buffer to start at
00175    //    numVertices - the number of vertices (indices) to draw
00176    // R: [none]
00177    public void drawFull(int primitiveType, int offset, int numVertices)  {
00178       gl.glEnableClientState( GL10.GL_VERTEX_ARRAY ); // Enable Position in Vertices
00179       vertices.position( 0 );                         // Set Vertex Buffer to Position
00180       gl.glVertexPointer( positionCnt, GL10.GL_FLOAT, vertexSize, vertices );  // Set Vertex Pointer
00181 
00182       if ( hasColor )  {                              // IF Vertices Have Color
00183          gl.glEnableClientState( GL10.GL_COLOR_ARRAY );  // Enable Color in Vertices
00184          vertices.position( positionCnt );            // Set Vertex Buffer to Color
00185          gl.glColorPointer( COLOR_CNT, GL10.GL_FLOAT, vertexSize, vertices );  // Set Color Pointer
00186       }
00187 
00188       if ( hasTexCoords )  {                          // IF Vertices Have Texture Coords
00189          gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );  // Enable Texture Coords in Vertices
00190          vertices.position( positionCnt + ( hasColor ? COLOR_CNT : 0 ) );  // Set Vertex Buffer to Texture Coords (NOTE: position based on whether color is also specified)
00191          gl.glTexCoordPointer( TEXCOORD_CNT, GL10.GL_FLOAT, vertexSize, vertices );  // Set Texture Coords Pointer
00192       }
00193 
00194       if ( indices != null )  {                       // IF Indices Exist
00195          indices.position( offset );                  // Set Index Buffer to Specified Offset
00196          gl.glDrawElements( primitiveType, numVertices, GL10.GL_UNSIGNED_SHORT, indices );  // Draw Indexed
00197       }
00198       else  {                                         // ELSE No Indices Exist
00199          gl.glDrawArrays( primitiveType, offset, numVertices );  // Draw Direct (Array)
00200       }
00201 
00202       if ( hasTexCoords )                             // IF Vertices Have Texture Coords
00203          gl.glDisableClientState( GL10.GL_TEXTURE_COORD_ARRAY );  // Clear Texture Coords State
00204 
00205       if ( hasColor )                                 // IF Vertices Have Color
00206          gl.glDisableClientState( GL10.GL_COLOR_ARRAY );  // Clear Color State
00207    }
00208 
00209    //--Set Vertex Elements--//
00210    // D: use these methods to alter the values (position, color, textcoords, normals) for vertices
00211    //    WARNING: these do NOT validate any values, ensure that the index AND specified
00212    //             elements EXIST before using!!
00213    // A: x, y, z - the x,y,z position to set in buffer
00214    //    r, g, b, a - the r,g,b,a color to set in buffer
00215    //    u, v - the u,v texture coords to set in buffer
00216    //    nx, ny, nz - the x,y,z normal to set in buffer
00217    // R: [none]
00218    void setVtxPosition(int vtxIdx, float x, float y)  {
00219       int index = vtxIdx * vertexStride;              // Calculate Actual Index
00220       vertices.put( index + 0, Float.floatToRawIntBits( x ) );  // Set X
00221       vertices.put( index + 1, Float.floatToRawIntBits( y ) );  // Set Y
00222    }
00223    void setVtxPosition(int vtxIdx, float x, float y, float z)  {
00224       int index = vtxIdx * vertexStride;              // Calculate Actual Index
00225       vertices.put( index + 0, Float.floatToRawIntBits( x ) );  // Set X
00226       vertices.put( index + 1, Float.floatToRawIntBits( y ) );  // Set Y
00227       vertices.put( index + 2, Float.floatToRawIntBits( z ) );  // Set Z
00228    }
00229    void setVtxColor(int vtxIdx, float r, float g, float b, float a)  {
00230       int index = ( vtxIdx * vertexStride ) + positionCnt;  // Calculate Actual Index
00231       vertices.put( index + 0, Float.floatToRawIntBits( r ) );  // Set Red
00232       vertices.put( index + 1, Float.floatToRawIntBits( g ) );  // Set Green
00233       vertices.put( index + 2, Float.floatToRawIntBits( b ) );  // Set Blue
00234       vertices.put( index + 3, Float.floatToRawIntBits( a ) );  // Set Alpha
00235    }
00236    void setVtxColor(int vtxIdx, float r, float g, float b)  {
00237       int index = ( vtxIdx * vertexStride ) + positionCnt;  // Calculate Actual Index
00238       vertices.put( index + 0, Float.floatToRawIntBits( r ) );  // Set Red
00239       vertices.put( index + 1, Float.floatToRawIntBits( g ) );  // Set Green
00240       vertices.put( index + 2, Float.floatToRawIntBits( b ) );  // Set Blue
00241    }
00242    void setVtxColor(int vtxIdx, float a)  {
00243       int index = ( vtxIdx * vertexStride ) + positionCnt;  // Calculate Actual Index
00244       vertices.put( index + 3, Float.floatToRawIntBits( a ) );  // Set Alpha
00245    }
00246    void setVtxTexCoords(int vtxIdx, float u, float v)  {
00247       int index = ( vtxIdx * vertexStride ) + positionCnt + ( hasColor ? COLOR_CNT : 0 );  // Calculate Actual Index
00248       vertices.put( index + 0, Float.floatToRawIntBits( u ) );  // Set U
00249       vertices.put( index + 1, Float.floatToRawIntBits( v ) );  // Set 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 );  // Calculate Actual Index
00253       vertices.put( index + 0, Float.floatToRawIntBits( x ) );  // Set X
00254       vertices.put( index + 1, Float.floatToRawIntBits( y ) );  // Set Y
00255       vertices.put( index + 2, Float.floatToRawIntBits( z ) );  // Set Z
00256    }
00257 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07