Jump to content

OpenGL Programming/Advanced/GLSL

From Wikibooks, open books for an open world

Variables

[edit | edit source]

Variables can be passed into GLSL like position and color, but other custom variables are available. Those that change within a glBegin/glEnd pair are called "attributes"; those that are not are called "uniform" [1]. After creating a GLSL program, one gets handles to the variables using

GLint glGetAttribLocation(GLuint program, char *name)

or

GLint glGetUniformLocation(GLuint program, char *name)

The result of this call is then used to set the value of the corresponding variable. Depending on the number of degrees of freedom, one uses

void glUniform1f(GLint location, GLfloat value)
void glUniform2f(GLint location, GLfloat value1, GLfloat value2)
void glUniform3f(GLint location, GLfloat value1, GLfloat value2, GLfloat value3)
void glUniform4f(GLint location, GLfloat value1, GLfloat value2, GLfloat value3, GLfloat value4)

or alternately,

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

For example, in GLSL you might have

uniform float time;
void main(void) {
  ...

then in C/C++ you could say:

GLint time = glGetUniformLocation(myProgram, "time"
...
glUniform1f(time, 10.3);
[edit | edit source]