MedicalVisualization

OpenGL GLSL Shader Documentation

OpenGL GLSL Shader | | OpenGL GLSL Shader Example

The GLSL shader language definition is available online at OpenGL.org:

The glVertex frame work provides easy access to GLSL as follows:

To compile and link a GLSL shader from a vertex and a fragment shader:

GLuint id = lglCompileGLSLProgram("#version 120\n ...");

To load a GLSL shader from a file:

GLuint id = lglLoadGLSLProgram(filename);

The simplest shader possible is:

#version 120
attribute vec4 vertex_position;
uniform mat4 mvp;
void main()
{
   gl_Position = mvp * vertex_position;
}
---
#version 120
uniform vec4 color;
void main()
{
   gl_FragColor = color;
}

To use a GLSL shader:

lglUseProgram(id);

Given a uniform float “v” in the GLSL shader, we pass a value to the variable in shader as follows:

lglUniformf("v", ...);

Given a uniform vec4 “v” in the GLSL shader, we pass a vector value to the variable in the shader as follows:

lglUniformfv("v", vec4(...));

Texture objects can be created with the lglCreateTexmap functions of the frame work:

GLuint texid2D = lglCreateTexmap2D(width, height, type, data);
GLuint texid3D = lglCreateTexmap3D(width, height, depth, type, data);

Then the texture objects can be enabled (meaning that texture color is modulating vertex colors):

lglTexture2D(texid2D);
lglTexture3D(texid3D);

The returned texture object ids can be passed to GLSL by means of a so called uniform sampler:

lglSampler2D("tex2D", texid2D);
lglSampler3D("tex3D", texid3D);

Then the texture objects can be accessed with the sampler “tex2D” resp. “tex3D” in the GLSL fragment shader:

uniform sampler2D tex2D;
vec4 texcol = texture2D(tex2D, texcoords);

More details and examples are available in the glVertex documentation.

OpenGL GLSL Shader | | OpenGL GLSL Shader Example

Options: