Difference between revisions of "Generic Vertex Attribute - examples"

From OpenGL Wiki
Jump to: navigation, search
m
m
Line 1: Line 1:
With GL 2.0, a new way to specify your vertex information became available : glVertexAttribPointer<br>
+
With GL 2.0, a new way to specify your vertex information became available: glVertexAttribPointer<br>
 
You could of course continue to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.<br>
 
You could of course continue to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.<br>
 
If you create a GL 3.0 forward context, you won't be able to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.<br>
 
If you create a GL 3.0 forward context, you won't be able to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.<br>
<b>You must use the generic version in this case : glVertexAttribPointer</b><br>
+
<b>You must use the generic version in this case: glVertexAttribPointer</b><br>
 
<br>
 
<br>
 
One of the requirements is to use shaders. Here is a GLSL example.
 
One of the requirements is to use shaders. Here is a GLSL example.
Line 60: Line 60:
 
<br>
 
<br>
 
<br>
 
<br>
and the vertex structure would like this : <br>
+
and the vertex structure would look like this: <br>
 
   struct MyVertex
 
   struct MyVertex
 
   {
 
   {

Revision as of 09:24, 30 May 2009

With GL 2.0, a new way to specify your vertex information became available: glVertexAttribPointer
You could of course continue to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.
If you create a GL 3.0 forward context, you won't be able to use glVertexPointer, glTexCoordPointer, glNormalPointer, glColorPointer.
You must use the generic version in this case: glVertexAttribPointer

One of the requirements is to use shaders. Here is a GLSL example.

 //Vertex Shader
 uniform mat4 ProjectionModelviewMatrix;
 uniform mat4 ModelviewMatrix;
 attribute vec4 InVertex;  //w will be set to 1.0 automatically
 attribute vec2 InTexCoord0;
 attribute vec3 InNormal;
 varying vec2 OutTexCoord0;
 //-------------------
 void main()
 {
   gl_Position = ProjectionModelviewMatrix * InVertex;
   OutTexCoord0 = InTexCoord0;
   vec3 normal = vec3(ModelviewMatrix * vec4(InNormal, 0.0);
   //Do lighting computation
   XXXXX
 }

Once you compile and link your GLSL shader, you can query the attrib locations :
vertexLoc = glGetAttribLocation(MyShader, "InVertex");
texCoord0Loc = glGetAttribLocation(MyShader, "InTexCoord0");
normalLoc = glGetAttribLocation(MyShader, "InNormal");

The alternative way is to specify the locations yourself :
glBindAttribLocation(MyShader, 0, "InVertex");
glBindAttribLocation(MyShader, 1, "InNormal");
glBindAttribLocation(MyShader, 2, "InTexCoord0");
but in the case you must link the shader AFTER those glBindAttribLocation calls :
glLinkProgram(MyShader);

and don't forget to check for errors.
int linked;
glGetProgramiv(MyShader, GL_LINK_STATUS, &linked);

int maxLength;
glGetProgramiv(MyShader, GL_INFO_LOG_LENGTH, &maxLength);
maxLength = maxLength + 1;
uchar *pLinkInfoLog = new uchar[maxLength];
glGetProgramInfoLog(MyShader, maxLength, &maxLength, pLinkInfoLog);

For rendering, VBO is recommended. See the VBO page for more info :
http://www.opengl.org/wiki/General_OpenGL

glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);

//Vertices, XYZ, FLOAT. We give GL_FALSE since we don't want normalization
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
//Normals, XYZ, FLOAT.
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
//TexCoord0, XY, FLOAT.
glVertexAttribPointer(texCoord0Loc, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);

Also, don't forget to enable the arrays that you need and disable the arrays that you don't.
Enable is done with glEnableVertexAttrib(index) and disable is done with glDisableVertexAttrib(index).


and the vertex structure would look like this:

 struct MyVertex
 {
   float x, y, z;
   float nx, ny, nz;
   float s0, t0;
 };