OpenGL:Tutorials:Setting up OpenGL for GLSL

From GPWiki
Jump to: navigation, search

An OpenGL Shader Tutorial

This tutorial is intended to demonstrate the coding, loading and application of a OpenGL Shader program. Familiarity with OpenGL and its extension system is assumed. The code contained here is C++, OpenGL Version 1.4 with the use of ARB Extensions. For OpenGL Version 2.0 and higher, the ARB suffix on identifiers may be removed.

Loading the shader functions

OpenGL with versions less than 2.0 do not have the shader functions normally avaliable for use. Instead, the program must load the extension that defines the functions.

The basic functions that should be imported are:

 PFNGLCREATEPROGRAMOBJECTARBPROC  glCreateProgramObjectARB ;
 PFNGLDELETEOBJECTARBPROC         glDeleteObjectARB       ;
 PFNGLUSEPROGRAMOBJECTARBPROC     glUseProgramObjectARB   ;
 PFNGLCREATESHADEROBJECTARBPROC   glCreateShaderObjectARB ;
 PFNGLSHADERSOURCEARBPROC         glShaderSourceARB        ;
 PFNGLCOMPILESHADERARBPROC        glCompileShaderARB       ;
 PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
 PFNGLATTACHOBJECTARBPROC         glAttachObjectARB        ;
 PFNGLGETINFOLOGARBPROC           glGetInfoLogARB          ;
 PFNGLLINKPROGRAMARBPROC          glLinkProgramARB         ;
 PFNGLGETUNIFORMLOCATIONARBPROC   glGetUniformLocationARB  ;
 PFNGLUNIFORM4FARBPROC            glUniform4fARB           ;
 PFNGLUNIFORM3FARBPROC            glUniform3fARB           ;         
 PFNGLUNIFORM1FARBPROC            glUniform1fARB           ;
 PFNGLUNIFORM1IARBPROC            glUniform1iARB           ;
 
 
 PFNGLGETATTRIBLOCATIONARBPROC  glGetAttribLocationARB;
 PFNGLVERTEXATTRIB3FARBPROC     glVertexAttrib3fARB;
 
 PFNGLVERTEXATTRIBPOINTERARBPROC      glVertexAttribPointerARB;
 PFNGLENABLEVERTEXATTRIBARRAYARBPROC  glEnableVertexAttribArrayARB ;
 PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArrayARB;

Defining the GLSL functions

Loading the functions is platform dependant. The code presented here is for windows development. For instructions concerning other operating systems, refer to OpenGL:Extensions.

Loading the shading language extension

The string identifier for GLSL is GL_ARB_shading_language_100. For more information about this extension, see the following.

void loadShaderExtension() {

 //Check to see if the extensionexsists.
 char *ext = (char*)glGetString( GL_EXTENSIONS );  
 
 //If the string does not exist in the GL_EXTENSIONS, we return with an error.
 if( strstr( ext, "GL_ARB_shading_language_100" ) == NULL )
 {
      //This extension string indicates that the OpenGL Shading Language,
      // version 1.00, is supported.
      std::cout<<"Shaders are not supported.\n";
      return;    
  }
 
 //Create all the functions using wglGetProcAddress
 glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)wglGetProcAddress("glCreateProgramObjectARB");
 glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC)wglGetProcAddress("glDeleteObjectARB");
 glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)wglGetProcAddress("glUseProgramObjectARB");
 glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)wglGetProcAddress("glCreateShaderObjectARB");
 glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)wglGetProcAddress("glShaderSourceARB"); 
 glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)wglGetProcAddress("glCompileShaderARB"); 
 glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)wglGetProcAddress("glGetObjectParameterivARB"); 
 glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)wglGetProcAddress("glAttachObjectARB"); 
 glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)wglGetProcAddress("glGetInfoLogARB");
 glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)wglGetProcAddress("glLinkProgramARB");
 
 glGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC)wglGetProcAddress("glGetUniformLocationARB");
 glUniform4fARB = (PFNGLUNIFORM4FARBPROC)wglGetProcAddress("glUniform4fARB");
 glUniform3fARB = (PFNGLUNIFORM3FARBPROC)wglGetProcAddress("glUniform3fARB");
 glUniform1fARB = (PFNGLUNIFORM1FARBPROC)wglGetProcAddress("glUniform1fARB");
 glUniform1iARB = (PFNGLUNIFORM1IARBPROC)wglGetProcAddress("glUniform1iARB");
 glGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)wglGetProcAddress("glGetAttribLocationARB");
 glVertexAttrib3fARB = (PFNGLVERTEXATTRIB3FARBPROC)wglGetProcAddress("glVertexAttrib3fARB");
 
 glVertexAttribPointerARB=(PFNGLVERTEXATTRIBPOINTERARBPROC)wglGetProcAddress("glVertexAttribPointerARB");
 glEnableVertexAttribArrayARB=(PFNGLENABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glEnableVertexAttribArrayARB");
 glDisableVertexAttribArrayARB=( PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glDisableVertexAttribArrayARB");
 

}

Now, the OpenGL shading language is available for use in the program.