Graphics Reference
In-Depth Information
the value(s) to be writen to the atri-
bute variable at that index. All the
glVertexAttrib functions are expected
to be used between glBegin and glEnd ,
just as the built-in atribute seting func-
tions are.
The type of the data val is expected
to match the type specified in the func-
tion name. However, since the vertex
atributes are always stored in an array of
type vec4 , any byte, short, int, unsigned
byte, unsigned short, or unsigned int
will be converted into a standard GLfloat
before it is actually stored.
In the short application code fragment below, we want to assign a vec2
atribute to each vertex of the triangle being drawn. The values to be assigned
to that atribute for the three vertices are u0 and v0 , u1 and v1 , and u2 and v2 .
The role of the glVertexAttrib2f( ) function is to set these values for the
atribute.
Notice that the glVertexAttrib
routine does not take a program
handle as one of its arguments. This
routine sets attribute variables in the
currently active shader program. So, be
sure that you call glUseProgram( ) on
the correct program before setting that
program's variables. (Presumably you
would already have done this because
to use glVertexAttrib( ) functions,
you would be drawing something.)
// in the vertex shader:
in vec2 aUV; // a per-vertex attribute
// in the C / C++ global variables:
GLint UVloc;
// in the C / C++ graphics setup code (after linking the shader
// program):
UVloc = glGetAttribLocation( program, “aUV” );
if( UVloc < 0 )
fprintf( stderr, “Cannot find Attribute variable 'aUV\n” );
// in the C / C++ display callback
if( UVloc > 0 )
{
glBegin( GL_TRIANGLES );
glVertexAttrib2f( UVloc, u0, v0 );
glVertex3f( x0, y0, z0 );
glVertexAttrib2f( UVloc, u1, v1 );
glVertex3f( x1, y1, z1 );
Search WWH ::




Custom Search