Example/Uniform Location Query Old: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
Categorization.
Flfirsla (talk | contribs)
No edit summary
 
Line 7: Line 7:
uniform mat4 someMatrix;
uniform mat4 someMatrix;
...
...
//Code must use these uniforms to be active.
// Code must use these uniforms to be active.
</source>
</source>


C++ code:
C++ code:
<source lang=cpp>
<source lang=cpp>
GLuint program = //Compile and link program.
GLuint program = // Compile and link program.
GLint someVectorLoc = glGetUniformLocation(program, "someVector");
GLint someVectorLoc = glGetUniformLocation(program, "someVector");
assert(someVectorLoc != -1); //If fails, then the uniform is inactive.
assert(someVectorLoc != -1); // If fails, then the uniform is inactive.
GLint someMatrixLoc = glGetUniformLocation(program, "someMatrix");
GLint someMatrixLoc = glGetUniformLocation(program, "someMatrix");
assert(someMatrixLoc != -1); //If fails, then the uniform is inactive.
assert(someMatrixLoc != -1); // If fails, then the uniform is inactive.
...
...
glUseProgram(program);
glUseProgram(program);
glUniform3f(someVectorLoc, 3.0f, 3.0f, 4.0f);
glUniform3f(someVectorLoc, 3.0f, 3.0f, 4.0f);
GLfloat matrix[16];
GLfloat matrix[16];
//Fill `matrix` with data. Non-transposed matrix data.
// Fill `matrix` with data. Non-transposed matrix data.
glUniformMatrix4fv(someMatrixLoc, 1, GL_FALSE, matrix);
glUniformMatrix4fv(someMatrixLoc, 1, GL_FALSE, matrix);
</source>
</source>
<noinclude>[[Category:Example Code]]</noinclude>
<noinclude>[[Category:Example Code]]</noinclude>

Latest revision as of 11:16, 3 January 2018

Querying and using uniform locations, under the old introspection API.

Vertex shader code:

...
uniform vec3 someVector;
uniform mat4 someMatrix;
...
// Code must use these uniforms to be active.

C++ code:

GLuint program = // Compile and link program.
GLint someVectorLoc = glGetUniformLocation(program, "someVector");
assert(someVectorLoc != -1); // If fails, then the uniform is inactive.
GLint someMatrixLoc = glGetUniformLocation(program, "someMatrix");
assert(someMatrixLoc != -1); // If fails, then the uniform is inactive.
...
glUseProgram(program);
glUniform3f(someVectorLoc, 3.0f, 3.0f, 4.0f);
GLfloat matrix[16];
// Fill `matrix` with data. Non-transposed matrix data.
glUniformMatrix4fv(someMatrixLoc, 1, GL_FALSE, matrix);