Texture Sampling

From OpenGL Wiki
Revision as of 19:40, 29 November 2008 by V-man (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In order to setup texture sampling, after you have compiled your shader, you need to get the location for the samplers.
For getting a shader's uniform's location, the shader doesn't need to be bound when you call glGetUniformLocation.
Let's assume you have a sampler called MyDiffuseTexture, MyEnvironmentMap, MyGlossMap in your FS

 uniform sampler2D MyDiffuseTexture;
 uniform samplerCube MyEnvironmentMap;
 uniform sampler2D MyGlossMap;

In your code, get the locations

 the_location0 = glGetUniformLocation(ProgramObject, "MyDiffuseTexture");
 the_location1 = glGetUniformLocation(ProgramObject, "MyEnvironmentMap");
 the_location2 = glGetUniformLocation(ProgramObject, "MyGlossMap");

By default, each of those uniforms are 0. If you use the shader as is, an error will be raised. You would need to call glGetError(). The state of the shader is also consider invalid.

 int isValid;
 glGetProgramiv(ProgramObject, GL_VALIDATE_STATUS, &isValid);

So, to set up those uniforms, bind the shader and call glUniform1i since they are considered as integers

 //Bind to tex unit 0
 glUniform1i(the_location0, 0);
 //Bind to tex unit 1
 glUniform1i(the_location1, 1);
 //Bind to tex unit 2
 glUniform1i(the_location2, 2);

To bind your texture, use glBindTexture

 glBindTexture(GL_TEXTURE_2D, tex[0]);
 glBindTexture(GL_TEXTURE_CUBE_MAP, tex[1]);
 glBindTexture(GL_TEXTURE_2D, tex[2]);