OpenGL Shading Language Tutorials

OpenGL Shading Language Extensions


 

OpenGL Shading language requires the following extensions:

GL_ARB_fragment_shader This extension adds functionality to define fragment shader objects. A fragment shader object is a shader object that, when attached to a program object, can be compiled and linked to produce an executable that runs on the fragment processor in OpenGL. The fragment processor is a programmable unit that replaces the OpenGL 1.4 fixed-function texturing, color sum and fog stages. This extension also defines how such an executable interacts with the fixed functionality fragment processing of OpenGL 1.4.
GL_ARB_vertex_shader This extension adds programmable vertex level processing to OpenGL. A vertex shader replaces the transformation, texture
coordinate generation and lighting parts of OpenGL, and it also adds
texture access at the vertex level. Furthermore, management of vertex
shader objects and loading generic attributes are discussed. A vertex
shader object, attached to a program object, can be compiled and linked to produce an executable that runs on the vertex processor in OpenGL. This extension also defines how such an executable interacts with the fixed functionality vertex processing of OpenGL 1.4.
GL_ARB_shader_object This extension adds API calls that are necessary to manage shader
objects and program objects.
GL_ARB_shading_language_100 This extension string indicates that the OpenGL Shading Language,
version 1.00, is supported. (There are no function pointers or definitions actually related to this extension, it shows that the Language Version "1.0.0" is supported)

 

C++ Source code to check and initialize Extensions (using glew)

I don't want to write a tutorial how to setup OpenGL Extensions. The most popular way to access extensions is using Glew (The OpenGL Extension Wrangler Library).

glewInit();

if (GL_TRUE != glewGetExtension((const GLubyte*) "GL_ARB_fragment_shader"))
{
   cout << "GL_ARB_fragment_shader extension is not available!";
}
if (GL_TRUE != glewGetExtension((const GLubyte*) "GL_ARB_vertex_shader"))
{
   cout << "GL_ARB_vertex_shader extension is not available!";
}
if (GL_TRUE != glewGetExtension((const GLubyte*) "GL_ARB_shader_objects"))
{
   cout << "GL_ARB_shader_objects extension is not available!";
}

 

C++ Source: Loading and compiling Shaders (example source)

This code isn't really related to shaders at all, it loads an ASCII file and reserves memory to store it. Of course you don't have to do this, there are other ways to access the source. This is just one way to do it.

// filename: specify filename
// Shadersource: pointer to contents of the ASCII file. Free memory when you don't
//               need it anymore with delete[] ShaderSource

int load(char* filename, GLubyte* Shadersource)
{

ifstream file;
file.open(filename, ios::in);
if(!file) return -1;
unsigned long len = getFileLength(file);
if (len==0) return -2; // "Empty File"
ShaderSource = (GLubyte*) new char[len+1];
if (ShaderSource == 0) return -3; // out of memory
// len isn't always strlen: some characters are stripped in ascii read... // it is important to 0-terminate the real length later, // len is just max possible value... ShaderSource[len] = 0;
unsigned int i=0;
while (file.good())
{
ShaderSource[i++] = file.get(); // get character from file
if (i>len) i=len;
}
ShaderSource[i] = 0; // 0 terminate it at real position!.
file.close(); return 0; }

 

C++ Source: Compiling the Program

For Vertex Shader:

GLhandleARB ProgramObject = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); 

For Fragment Shader

GLhandleARB ProgramObject = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); 

Common Code

GLint length = (GLint) strlen((const char*)ShaderSource);
int compiled = 0;

glShaderSourceARB(ProgramObject, 1, (const GLcharARB **)&ShaderSource, &length); glCompileShaderARB(ProgramObject); glGetObjectParameterivARB(ProgramObject, GL_OBJECT_COMPILE_STATUS_ARB, &compiled);
if (compiled) { // compilation successful! } else { // compilation error! Check compiler log! }


Accessing the compiler Log

If there was a compiler error it is a good idea to check the compiler log and output it.

int CompilerLog(void)
{
 int blen = 0;	
int slen = 0; if (ProgramObject==0) return 0; // not a valid program object glGetObjectParameterivARB(ProgramObject, GL_OBJECT_INFO_LOG_LENGTH_ARB , &blen);
 if (blen > 1)
 {
   if ((compiler_log = (GLcharARB*)malloc(blen)) == NULL) 
   {
     return -3; // out of memory!
   }
   glGetInfoLogARB(ProgramObject, blen, &slen, compiler_log);
   if (compiler_log!=0) cout << "compiler_log: " << compiler_log; 
 }
 free(compiler_log);     
 return 0;
}

 

C++ Code: Attaching to Program Object and linking

Add the fragment and vertex shader(s) to a Program object and link it. You have to call glAttachObjectARB for every fragment and vertex shader you want to use. Adding several vertex or fragment programs is like using #include in C or C++. You can use core functions (for example lighting) in one source and call it in another.

ShaderObject = glCreateProgramObjectARB();
glAttachObjectARB(ShaderObject, ProgramObject);
int linked;
glLinkProgramARB(ShaderObject);
glGetObjectParameterivARB(ShaderObject, GL_OBJECT_LINK_STATUS_ARB, &linked);
if (linked)
{ // congratulations! the program is linked!
}
else
{ // Linker error
}

You can access the Linker log with the "CompilerLog()" function described earlier.

 

List of Functions provided by the extensions.

3D Labs has a very nice online reference of the functions:
3DLabs OpenGL Shading Language Extensions Function Reference.

Functions provided by GL_ARB_fragment_shader

none

Functions provided by GL_ARB_vertex_shader

void        VertexAttrib1fARB(uint index, float v0)
void        VertexAttrib1sARB(uint index, short v0)
void        VertexAttrib1dARB(uint index, double v0)
void        VertexAttrib2fARB(uint index, float v0, float v1)
void        VertexAttrib2sARB(uint index, short v0, short v1)
void VertexAttrib2dARB(uint index, double v0, double v1) void VertexAttrib3fARB(uint index, float v0, float v1, float v2)
void VertexAttrib3sARB(uint index, short v0, short v1, short v2) void VertexAttrib3dARB(uint index, double v0, double v1, double v2) void VertexAttrib4fARB(uint index, float v0, float v1, float v2, float v3) void VertexAttrib4sARB(uint index, short v0, short v1, short v2, short v3) void VertexAttrib4dARB(uint index, double v0, double v1, double v2, double v3) void VertexAttrib4NubARB(uint index, ubyte x, ubyte y, ubyte z, ubyte w) void VertexAttrib1fvARB(uint index, const float *v) void VertexAttrib1svARB(uint index, const short *v) void VertexAttrib1dvARB(uint index, const double *v) void VertexAttrib2fvARB(uint index, const float *v) void VertexAttrib2svARB(uint index, const short *v) void VertexAttrib2dvARB(uint index, const double *v) void VertexAttrib3fvARB(uint index, const float *v) void VertexAttrib3svARB(uint index, const short *v) void VertexAttrib3dvARB(uint index, const double *v) void VertexAttrib4fvARB(uint index, const float *v) void VertexAttrib4svARB(uint index, const short *v) void VertexAttrib4dvARB(uint index, const double *v) void VertexAttrib4ivARB(uint index, const int *v) void VertexAttrib4bvARB(uint index, const byte *v) void VertexAttrib4ubvARB(uint index, const ubyte *v) void VertexAttrib4usvARB(uint index, const ushort *v) void VertexAttrib4uivARB(uint index, const uint *v) void VertexAttrib4NbvARB(uint index, const byte *v) void VertexAttrib4NsvARB(uint index, const short *v) void VertexAttrib4NivARB(uint index, const int *v) void VertexAttrib4NubvARB(uint index, const ubyte *v) void VertexAttrib4NusvARB(uint index, const ushort *v) void VertexAttrib4NuivARB(uint index, const uint *v) void VertexAttribPointerARB(uint index, int size, enum type, boolean normalized, sizei stride, const void *pointer)
void EnableVertexAttribArrayARB(uint index)
void DisableVertexAttribArrayARB(uint index)
void BindAttribLocationARB(handleARB programObj, uint index, const charARB *name)
void GetActiveAttribARB(handleARB programObj, uint index, sizei maxLength,
sizei *length, int *size, enum *type, charARB *name)
GLint GetAttribLocationARB(handle programObj, const charARB *name)
void GetVertexAttribdvARB(uint index, enum pname, double *params) void GetVertexAttribfvARB(uint index, enum pname, float *params) void GetVertexAttribivARB(uint index, enum pname, int *params) void GetVertexAttribPointervARB(uint index, enum pname, void **pointer)

 

Functions provided by GL_ARB_shader_object

void        DeleteObjectARB(handleARB obj)
handleARB   GetHandleARB(enum pname)
void DetachObjectARB(handleARB containerObj, handleARB attachedObj)
handleARB CreateShaderObjectARB(enum shaderType)
void ShaderSourceARB(handleARB shaderObj, sizei count, const charARB **string, const int *length) void CompileShaderARB(handleARB shaderObj)
handleARB CreateProgramObjectARB(void)
void AttachObjectARB(handleARB containerObj, handleARB obj)
void LinkProgramARB(handleARB programObj)
void UseProgramObjectARB(handleARB programObj)
void ValidateProgramARB(handleARB programObj)
void Uniform1fARB(int location, float v0) void Uniform2fARB(int location, float v0, float v1)
void Uniform3fARB(int location, float v0, float v1, float v2)
void Uniform4fARB(int location, float v0, float v1, float v2, float v3)
void Uniform1iARB(int location, int v0)
void Uniform2iARB(int location, int v0, int v1)
void Uniform3iARB(int location, int v0, int v1, int v2)
void Uniform4iARB(int location, int v0, int v1, int v2, int v3)
void Uniform1fvARB(int location, sizei count, float *value)
void Uniform2fvARB(int location, sizei count, float *value)
void Uniform3fvARB(int location, sizei count, float *value)
void Uniform4fvARB(int location, sizei count, float *value)
void Uniform1ivARB(int location, sizei count, int *value)
void Uniform2ivARB(int location, sizei count, int *value)
void Uniform3ivARB(int location, sizei count, int *value)
void Uniform4ivARB(int location, sizei count, int *value)
void UniformMatrix2fvARB(int location, sizei count, boolean transpose, float *value)
void UniformMatrix3fvARB(int location, sizei count, boolean transpose, float *value)
void UniformMatrix4fvARB(int location, sizei count, boolean transpose, float *value)
void GetObjectParameterfvARB(handleARB obj, enum pname, float *params)
void GetObjectParameterivARB(handleARB obj, enum pname, int *params)
void GetInfoLogARB(handleARB obj, sizei maxLength, sizei *length, charARB *infoLog)
void GetAttachedObjectsARB(handleARB containerObj, sizei maxCount, sizei *count, handleARB *obj)
int GetUniformLocationARB(handleARB programObj, const charARB *name)
void GetActiveUniformARB(handleARB programObj, uint index, sizei maxLength, sizei *length, int *size, enum *type, charARB *name) void GetUniformfvARB(handleARB programObj, int location, float *params)
void GetUniformivARB(handleARB programObj, int location, int *params)
void GetShaderSourceARB(handleARB obj, sizei maxLength, sizei *length, charARB *source)

 

More Information

OpenGL Extension Registry: http://oss.sgi.com/projects/ogl-sample/registry/

 

 

 

© 2003-2004 by Martin Christen. All Rights Reserved.
christen@clockworkcoders.com