Raymond Pang's Home

GLSL (OpenGL Shading Language)

Introduction

OpenGL Shading Language (GLSL) is defined by the Architectural Review Board of OpenGL, a new standard to the OpenGL 2.0 which includes the GPU based Shading into the traditional OpenGL pipeline.

The state-of-the-art Graphics board has 2 major programmable shaders -- Vertex Shader and Fragment Shader, for details, please refer to the Nvidia's web site here.

Comparing with the currently using high level shading language for OpenGL -- Cg , GLSL is not vendor specific and will not require additional Toolkit. It is natively supported in OpenGL. If you are using Nvidia Geforce Series, you can enjoy GLSL by installing driver release 75 series or later.

This document will intend to concentrate on the difference of Cg and GLSL, so as to give a quick learning guide to those Cg experts. For beginner of Shader Language, I will recommend to learn GLSL starting from the Orange Book or lighthouse3dGLSL Tutorial (see reference section).

GLSL vs Cg

Setup in OpenGL

In GLSL, there are difference of Shader Object and Shader Program. This is analogous to our familiar C/C++ .obj and .exe .

A Shader Object can be either a vertex shader or a fragment shader compiled object code. By giving the shader source code, we can compile a shader object.

A Shader Program is created by attaching shader objects and then link up all together to form a workable Shader. I think it is the major difference compare with Cg. In Cg, we simply don't have the difference of Shader Object and Program.

We will require 3 Steps to create a Shader Object :

GLhandle glCreateShaderObject(GLenum shaderType);
void glShaderSource(GLhandle shader, int numOfStrings, const char **strings, int *lenOfStrings);
void glCompileShader(GLhandle program);

and 4 Steps for Shader Program :

GLhandle glCreateProgramObject(void);
void glAttachObject(GLhandle program, GLhandle shader);
void glLinkProgram(GLhandle program);
void glUseProgramObject(GLhandle prog);

The flow of creating Shader Object and Program :

Here is a side by side comparison of GLSL and Cg :

GLSL
Cg

 

Context = cgCreateContext();
F=glCreateShaderObject(GL_FRAGMENT_SHADER_ARB);
FragmentProgram = cgCreateProgramFromFile(Context, CG_SOURCE, "vertexShader.cg",
VertexProfile,
NULL, NULL);
glShaderSource(f, 1, &ff,NULL);
cgGLLoadProgram(FragmentProgram );
glCompileShader(f);

 

p = glCreateProgramObject();

 

glAttachObject(p,f);

 

glLinkProgram(p);

 

glUseProgramObject(p);
cgGLBindProgram(FragmentProgram);

Communication with OpenGL (Passing Parameters from application )

Basically, GLSL has two types of variable qualifiers for communication with the application:

  • Uniform -- common across all shaders
  • Attribute -- unique to each vertex

For the Uniform case , we can call :

GLint glUniform{1,2,3,4}fv(GLint location, GLsizei count, GLfloat *v);
GLint glUniformMatrix{2,3,4}fv(GLint location, GLsizei count, GLboolean transpose, GLfloat *v);

where the location can retrive by calling :

GLint glGetUniformLocation(GLuint program, const GLchar *name)

 

For specifing Attribute , we have :

GLint glGetAttribLocation(GLhandle program,char *name);
GLint glVertexAttrib{1,2,3,4}fv(GLint location, GLfloat *v);

where the location get from :

GLint glGetAttribLocation(GLhandle program,char *name);

Here is another side by side comparison of GLSL and Cg for passing parameters :

GLSL
Cg
glUniform2f(getUniformLocation(VertexProg, Direction"), 0.3, 0.2);
glUniform3f(getUniformLocation(VertexProg, Kd"), 0.85, 0.86, 0.84);
glUniform3fv(getUniformLocation(VertexProg,InvMV"),1,GL_FALSE, invMV);
VertexColorParam = cgGetNamedParameter(VertexProgram, "IN.VertexColor");
ModelViewProjParam = cgGetNamedParameter(VertexProgram, "ModelViewProj");
glBindAttribLocation(VertProg,1,Opacity);
glBindAttribLocation(VertProg, VELOCITY_ARRAY, "Velocity");

 

Inside Draw Loop (after glBegin):
glVertexAttrib1f(1,opacity);
glVertexAttribPointer(VELOCITY_ARRAY,  3, GL_FLOAT, GL_FALSE, 0, velocities);

 

Correspondence between OpenGL and GLSL :

A third kind of variables allowed in GLSL is "varying", which allow values to be passed between vertex and fragment shaders. Default varying variables includes vertex projected position (gl_Position), color (gl_Color) and etc. User defined varying variable is also allowed, the variables have to be defined in both vertex and fragment shaders. For example :

varying float Opacity;

 

Syntax Difference between GLSL and Cg

There are only minor difference I can find between GLSL and Cg. They are very similar include those function call.

1. Variable naming

vec4 <> float4
mat4 <> float4x4

2. No Sematic link required

For Example, in Cg , we need somethink like float4 position : POSITION, but in GLSL we are provide with a set of standard names (gl_position).

Sample GLSL Shader

Here provides a sample GLSL Shader from the Orange Box Sample "brick":

uniform vec3 LightPosition;
const float SpecularContribution = 0.3;
const float DiffuseContribution  = 1.0 - SpecularContribution;
varying float LightIntensity;
varying vec2  MCposition;
void main(void)
{
    vec3 ecPosition = vec3 (gl_ModelViewMatrix * gl_Vertex);
    vec3 tnorm      = normalize(gl_NormalMatrix * gl_Normal);
    vec3 lightVec   = normalize(LightPosition - ecPosition);
    vec3 reflectVec = reflect(-lightVec, tnorm);
    vec3 viewVec    = normalize(-ecPosition);
    float diffuse   = max(dot(lightVec, tnorm), 0.0);
    float spec      = 0.0;
    if (diffuse > 0.0)
    {
        spec = max(dot(reflectVec, viewVec), 0.0);
        spec = pow(spec, 16.0);
    }
    LightIntensity  = DiffuseContribution * diffuse +
                      SpecularContribution * spec;
    MCposition      = gl_Vertex.xy;
    gl_Position     = ftransform();
}

Conclusion

GLSL as a standard high level shading language in OpenGL , Cg as a transitional or vendor specific high level shading language, I have no comment which one is better. However, GLSL should be a trend for the industry and acadermic in the coming years.

Hope you can enjoy the Orange !

Last Update : 11/25/2005

Other References

OpenGL Shading Language Official Documents
http://www.opengl.org/documentation/oglsl.html

GLSL Tutorial from lighthouse3d
http://www.lighthouse3d.com/opengl/glsl/

Book:
OpenGL shading language (Orange Book) / Randi J. Rost ; with contributions by John M. Kessenich and Barthold Lichtenbelt.
Publisher : Boston : Addison-Wesley, c2004.

Power Point Slide of this page:
GLSL.ppt


Home    About Myself    Publication    Links    Raybase
© 2005 Raymond Pang