Clockworkcoders Tutorials

 




Vertex Attributes

Introduction

Vertex attributes are used to communicate from "outside" to the vertex shader. Unlike uniform variables, values are provided per vertex (and not globally for all vertices). There are built-in vertex attributes like the normal or the position, or you can specify your own vertex attribute like a tangent or another custom value. Attributes can't be defined in the fragment shader.

Attributes can be defined in the vertex shader using the "attribute" qualifier:

attribute float myAttrib;
Vertex Shader Source Code

 

Built in Vertex Attributes

In many cases the built in vertex attributes are sufficent to use. In the C++ program you can use the regular OpenGL function to set vertex attribute values, for example glVertex3f for the position.

gl_Vertex Position (vec4)
gl_Normal Normal (vec4)
gl_Color Primary color of vertex (vec4)
gl_MultiTexCoord0 Texture coordinate of texture unit 0 (vec4)
gl_MultiTexCoord1 Texture coordinate of texture unit 1 (vec4)
gl_MultiTexCoord2 Texture coordinate of texture unit 2 (vec4)
gl_MultiTexCoord3 Texture coordinate of texture unit 3 (vec4)
gl_MultiTexCoord4 Texture coordinate of texture unit 4 (vec4)
gl_MultiTexCoord5 Texture coordinate of texture unit 5 (vec4)
gl_MultiTexCoord6

Texture coordinate of texture unit 6 (vec4)

gl_MultiTexCoord7 Texture coordinate of texture unit 7 (vec4)
gl_FogCoord Fog Coord (float)

Example: Built-in Vertex Attributes

This example uses the built in attributes gl_Vertex and gl_Color. The color value is used to translate the object (i.e. misuse of color) .

tutorial example

glBegin(GL_TRIANGLES)
   glVertex3f(0.0f, 0.0f, 0.0f);
   glColor3f(0.1,0.0,0.0);
   glVertex3f(1.0f, 0.0f, 0.0f);
   glColor3f(0.0,0.1,0.0);
   glVertex3f(1.0f, 1.0f, 0.0f);
   glColor3f(0.1,0.1,0.0);

glEnd();    
C++ Source Code

void main(void)
{ vec4 a = gl_Vertex + gl_Color;
gl_Position = gl_ModelViewProjectionMatrix * a;
}
Vertex Shader Source Code

void main (void)  
{
gl_FragColor = vec4(0.0,0.0,1.0,1.0);
}
Fragment Shader Source Code

Custom Vertex Attributes

A custom, user-defined attribute can also be defined. The OpenGL function glBindAttribLocation associates the name of the variable with an index.

For example, glBindAttribLocation(ProgramObject, 10, "myAttrib") would bind the attribute "myAttrib" to index 10.

The maximum number of attribute locations is limited by the graphics hardware. You can retrieve the maximum supported number of vertex attributes with glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &n).

Setting attribute values can be done using glVertexAttrib function.

Unfortunately there are certain limitations when using this on NVidia Hardware. According to NVidia:
"GLSL attempts to eliminate aliasing of vertex attributes but this is integral to NVIDIA’s hardware approach and necessary for maintaining compatibility with existing OpenGL applications that NVIDIA customers rely on. NVIDIA’s GLSL implementation therefore does not allow built-in vertex attributes to collide with a generic vertex attributes that is assigned to a particular vertex attribute index with glBindAttribLocation. For example, you should not use gl_Normal (a built-in vertex attribute) and also use glBindAttribLocation to bind a generic vertex attribute named "whatever" to vertex attribute index 2 because gl_Normal aliases to index 2."

In other words, NVidia hardware indices are reserved for built-in attributes:

gl_Vertex 0
gl_Normal 2
gl_Color 3
gl_SecondaryColor 4
gl_FogCoord 5
gl_MultiTexCoord0 8
gl_MultiTexCoord1 9
gl_MultiTexCoord2 10
gl_MultiTexCoord3 11
gl_MultiTexCoord4 12
gl_MultiTexCoord5 13
gl_MultiTexCoord6 14
gl_MultiTexCoord7 15

Example Project

Download: GLSL_Attributes.zip (Visual Studio 8 Project)
(If you create a project/makefile for a different platform/compiler, please send it to: christen(at)clockworkcoders.com and I will put it here.)

 

Previous: Uniform Variables

Next: Varying Variables