Thursday, May 26, 2011

glVertexPointer example c/c++ java

Name

glVertexPointer - define an array of vertex coordinates

C Specification

void glVertexPointer(GLint size,
    GLenum type,
    GLsizei stride,
    const GLvoid * pointer)

Parameters

size
Specifies the number of coordinates per vertex. Must be 2, 3, or 4. The initial value is 4.
type
Specifies the data type of each vertex coordinate in the array. Symbolic constants GL_BYTE, GL_SHORT, and GL_FIXED, are accepted. However, the initial value is GL_FLOAT. The common profile accepts the symbolic constant GL_FLOAT as well.
stride 
Specifies the byte offset between consecutive vertices. If stride is 0, the vertices are understood to be tightly packed in the array. The initial value is 0.
pointer
Specifies a pointer to the first coordinate of the first vertex in the array. The initial value is 0.

Description

glVertexPointer specifies the location and data of an array of vertex coordinates to use when rendering. size specifies the number of coordinates per vertex and type the data type of the coordinates. stride specifies the byte stride from one vertex to the next allowing vertices and attributes to be packed into a single array or stored in separate arrays. (glVertexPointer Single-array storage may be more efficient on some implementations.) When a vertex array is specified, size, type, stride, and pointer are saved as client-side state.
If the vertex array is enabled, it is used when glDrawArrays, or glDrawElements is called. To enable and disable the vertex array, call glEnableClientState and glDisableClientState with the argument GL_VERTEX_ARRAY. The vertex array is initially disabled and isn't accessed when glDrawArrays or glDrawElements is called.
Use glDrawArrays to construct a sequence of primitives (all of the same type) from prespecified vertex and vertex attribute arrays. Use glDrawElements to construct a sequence of primitives by indexing vertices and vertex attributes.

Notes

glVertexPointer is typically implemented on the client side.

Errors

GL_INVALID_VALUE is generated if size is not 2, 3, or 4.
GL_INVALID_ENUM is generated if type is is not an accepted value.
GL_INVALID_VALUE is generated if stride is negative.

Copyright

Copyright © 2003 Silicon Graphics, Inc.
This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

See Also

glColorPointer, glDrawArrays, glDrawElements, glEnable, glNormalPointer, glTexCoordPointer

Example - glVertexPointer

  1. package kr.ac.uos.je;  
  2.   
  3. import java.nio.ByteBuffer;  
  4. import java.nio.ByteOrder;  
  5. import java.nio.FloatBuffer;  
  6.   
  7. import javax.microedition.khronos.egl.EGLConfig;  
  8. import javax.microedition.khronos.opengles.GL10;  
  9.   
  10. import android.opengl.GLSurfaceView.Renderer;  
  11.   
  12. public class GLRenderer implements Renderer{  
  13.   
  14.       
  15. /**  
  16.  * Default Construct  
  17.  */  
  18.     public GLRenderer() {  
  19.         //Dump Vertex to Buffer  
  20.         triangleVertexBuffer = createFloatBuffer(triangle);  
  21.           
  22.     }  
  23.     @Override  
  24.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  25.         //배경을 흰색으로 clear  
  26.         gl.glClearColor(1, 1, 1, 1);  
  27.         //glHint - 특정한 렌더링 기능에 대한 선택적인 제어   
  28.         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onSurfaceChanged(GL10 gl, int width, int height) {  
  33.     }  
  34.   
  35.     private float[] triangle = new float[]{  
  36.             -0.5f, 0, 0,  
  37.             0, 0.5f, 0,  
  38.             0.5f, 0, 0  
  39.     };  
  40.       
  41.     private FloatBuffer triangleVertexBuffer;  
  42.   
  43. /**  
  44.  * Here's Where We Do All The Drawing  
  45.  */  
  46.     @Override  
  47.     public void onDrawFrame(GL10 gl) {  
  48.         //Clear The Screen And The Depth Buffer  
  49.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);  
  50.         //Reset View  
  51.         gl.glLoadIdentity();  
  52.         //Set Triangle Color to RED  
  53.         gl.glColor4f(1, 0, 0, 0.5f);  
  54.         //Set Vertex  
  55.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVertexBuffer);  
  56.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
  57.         //Draw Triangle  
  58.         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);  
  59.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
  60.     }  
  61.       
  62.     protected FloatBuffer createFloatBuffer(float[] array){  
  63.         ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length << 2);  
  64.         byteBuffer.order(ByteOrder.nativeOrder());  
  65.         FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();  
  66.         floatBuffer.put(array);  
  67.         floatBuffer.position(0);  
  68.         return floatBuffer;  
  69.     }  
  70.   
  71. }