Difference between revisions of "Generic Vertex Attribute - examples"

From OpenGL Wiki
Jump to: navigation, search
m
m
Line 22: Line 22:
 
   }
 
   }
  
Once you compile and link your GLSL shader, you can query the attrib locations :<br>
+
Once you compile and link your GLSL shader, you can query the attrib locations :
vertexLoc = glGetAttribLocation(MyShader, "InVertex");<br>
+
  vertexLoc = glGetAttribLocation(MyShader, "InVertex");
texCoord0Loc = glGetAttribLocation(MyShader, "InTexCoord0");<br>
+
  texCoord0Loc = glGetAttribLocation(MyShader, "InTexCoord0");
normalLoc = glGetAttribLocation(MyShader, "InNormal");<br>
+
  normalLoc = glGetAttribLocation(MyShader, "InNormal");
<br>
+
 
The alternative way is to specify the locations yourself :<br>
+
The alternative way is to specify the locations yourself :
glBindAttribLocation(MyShader, 0, "InVertex");<br>
+
  glBindAttribLocation(MyShader, 0, "InVertex");
glBindAttribLocation(MyShader, 1, "InNormal");<br>
+
  glBindAttribLocation(MyShader, 1, "InNormal");
glBindAttribLocation(MyShader, 2, "InTexCoord0");<br>
+
  glBindAttribLocation(MyShader, 2, "InTexCoord0");
but in the case you must link the shader AFTER those glBindAttribLocation calls :<br>
+
 
glLinkProgram(MyShader);<br>
+
but in this case you must link the shader AFTER those glBindAttribLocation calls :
<br>
+
  glLinkProgram(MyShader);
and don't forget to check for errors.<br>
+
 
int linked;<br>
+
and don't forget to check for errors.
glGetProgramiv(MyShader, GL_LINK_STATUS, &linked);<br>
+
  int linked;
<br>
+
  glGetProgramiv(MyShader, GL_LINK_STATUS, &linked);
int maxLength;<br>
+
  //Make sure linked==TRUE
glGetProgramiv(MyShader, GL_INFO_LOG_LENGTH, &maxLength);<br>
+
 
maxLength = maxLength + 1;<br>
+
  //If linked==FALSE, the log contains information on what went wrong
uchar *pLinkInfoLog = new uchar[maxLength];<br>
+
  int maxLength;
glGetProgramInfoLog(MyShader, maxLength, &maxLength, pLinkInfoLog);<br>
+
  glGetProgramiv(MyShader, GL_INFO_LOG_LENGTH, &maxLength);
<br>
+
  maxLength = maxLength + 1;
For rendering, VBO is recommended. See the VBO page for more info :<br>
+
  uchar *pLinkInfoLog = new uchar[maxLength];
http://www.opengl.org/wiki/General_OpenGL<br>
+
  glGetProgramInfoLog(MyShader, maxLength, &maxLength, pLinkInfoLog);
<br>
+
 
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);<br>
+
For rendering, of course, there are many methods in OpenGL : immediate mode, display list, standard vertex arrays, VBO.<br>
<br>
+
VBO is always recommended for modern programs.<br>
//Vertices, XYZ, FLOAT. We give GL_FALSE since we don't want normalization<br>
+
See the VBO page for more info : http://www.opengl.org/wiki/General_OpenGL<br>
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);<br>
 
//Normals, XYZ, FLOAT.<br>
 
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);<br>
 
//TexCoord0, XY, FLOAT.<br>
 
glVertexAttribPointer(texCoord0Loc, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);<br>
 
 
<br>
 
<br>
 +
  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 (Also called ST. Also called UV), 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.<br>
 
Also, don't forget to enable the arrays that you need and disable the arrays that you don't.<br>
 
Enable is done with glEnableVertexAttrib(index) and disable is done with glDisableVertexAttrib(index).<br>
 
Enable is done with glEnableVertexAttrib(index) and disable is done with glDisableVertexAttrib(index).<br>
 
<br>
 
<br>
 
<br>
 
<br>
and the vertex structure would look like this: <br>
+
and the vertex structure would look like this since in this example, we only have VNT (vertex, normal and texcoord0): <br>
 
   struct MyVertex
 
   struct MyVertex
 
   {
 
   {
Line 67: Line 71:
 
     float s0, t0;
 
     float s0, t0;
 
   };
 
   };
 +
 +
Additional : glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &MaxVertexAttribs) tells you the maximum number that the implementation supports and this is typically 16.

Revision as of 14:55, 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 this 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);
 //Make sure linked==TRUE
 //If linked==FALSE, the log contains information on what went wrong
 int maxLength;
 glGetProgramiv(MyShader, GL_INFO_LOG_LENGTH, &maxLength);
 maxLength = maxLength + 1;
 uchar *pLinkInfoLog = new uchar[maxLength];
 glGetProgramInfoLog(MyShader, maxLength, &maxLength, pLinkInfoLog);

For rendering, of course, there are many methods in OpenGL : immediate mode, display list, standard vertex arrays, VBO.
VBO is always recommended for modern programs.
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 (Also called ST. Also called UV), 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 since in this example, we only have VNT (vertex, normal and texcoord0):

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

Additional : glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &MaxVertexAttribs) tells you the maximum number that the implementation supports and this is typically 16.