OpenGL GLSL Shader Example

OpenGL GLSL Shader Documentation | | TFs With GLSL Dependent Texturing

To compile a GLSL shader we use the glVertex frame work.

In this frame work the two vertex and fragment shaders are concatenated with the “---“ separating token. So the compiliation of the previous example GLSL shader is achieved as follows:

#include <glvertex.h>

const char shader[] =
 "#version 120\n"
 "attribute vec4 vertex_position;\n"
 "attribute vec4 vertex_color;\n"
 "uniform mat4 mvp;\n"
 "varying vec4 frag_color;\n"
 "void main()\n"
 "   frag_color = vertex_color;\n"
 "   gl_Position = mvp * vertex_position;\n"
 "#version 120\n"
 "varying vec4 frag_color;\n"
 "void main()\n"
 "   gl_FragColor = frag_color;\n"

GLuint id = lglCompileGLSLProgram(shader);

Note: due to changes of the OpenGL 3.2 standard over OpenGL 2.1, the input registers gl_Vertex, gl_Color etc. are replaced by attribute vectors within the glVertex frame work. The following uniforms and attributes are available:

  • uniform vec4 color: constant color
  • uniform mat4 mv: model-view matrix
  • uniform mat4 mvit: inverse transpose model-view matrix
  • uniform mat4 mvp: combined model-view-projection matrix
  • attribute vec4 vertex_position: incoming vertices
  • attribute vec4 vertex_color: incoming vertex color attribute
  • attribute vec3 vertex_normal: incoming vertex normal attribute
  • attribute vec4 vertex_texcoord: incoming vertex texture coordinate attribute

To try the above GLSL example, open the GLSL shader editor of the frame work:

./myqtapp --glsleditor

OpenGL GLSL Shader Documentation | | TFs With GLSL Dependent Texturing