Difference between revisions of "Example/Texture Shader Binding"

From OpenGL Wiki
Jump to: navigation, search
(Centralizing example)
 
 
Line 2: Line 2:
  
 
<source lang=cpp>
 
<source lang=cpp>
//Initial program setup.
+
// Initial program setup.
glLinkProgram(program); //Initial link
+
glLinkProgram(program); // Initial link
  
 
GLint baseImageLoc = glGetUniformLocation(program, "baseImage");
 
GLint baseImageLoc = glGetUniformLocation(program, "baseImage");
Line 10: Line 10:
  
 
glUseProgram(program);
 
glUseProgram(program);
glUniform1i(baseImageLoc, 0); //Texture unit 0 is for base images.
+
glUniform1i(baseImageLoc, 0); // Texture unit 0 is for base images.
glUniform1i(normalMapLoc, 2); //Texture unit 2 is for normal maps.
+
glUniform1i(normalMapLoc, 2); // Texture unit 2 is for normal maps.
glUniform1i(shadowMapLoc, 4); //Texture unit 4 is for shadow maps.
+
glUniform1i(shadowMapLoc, 4); // Texture unit 4 is for shadow maps.
  
//When rendering an objectwith this program.
+
// When rendering an objectwith this program.
 
glActiveTexture(GL_TEXTURE0 + 0);
 
glActiveTexture(GL_TEXTURE0 + 0);
 
glBindTexture(GL_TEXTURE_2D, object1BaseImage);
 
glBindTexture(GL_TEXTURE_2D, object1BaseImage);
Line 20: Line 20:
 
glActiveTexture(GL_TEXTURE0 + 2);
 
glActiveTexture(GL_TEXTURE0 + 2);
 
glBindTexture(GL_TEXTURE_2D, object1NormalMap);
 
glBindTexture(GL_TEXTURE_2D, object1NormalMap);
glBindSampler(2, linearFiltering); //Same filtering as before
+
glBindSampler(2, linearFiltering); // Same filtering as before
 
glActiveTexture(GL_TEXTURE0 + 4);
 
glActiveTexture(GL_TEXTURE0 + 4);
 
glBindTexture(GL_TEXTURE_2D, shadowMap);
 
glBindTexture(GL_TEXTURE_2D, shadowMap);
glBindSampler(4, depthComparison); //Special sampler for depth comparisons.
+
glBindSampler(4, depthComparison); // Special sampler for depth comparisons.
  
//Render stuff
+
// Render stuff
 
glDraw*();
 
glDraw*();
  
//Render another object with some different textures.
+
// Render another object with some different textures.
 
glActiveTexture(GL_TEXTURE0 + 0);
 
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, object2BaseImage); //Use the same sampler as before.
+
glBindTexture(GL_TEXTURE_2D, object2BaseImage); // Use the same sampler as before.
 
glActiveTexture(GL_TEXTURE0 + 2);
 
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, object2NormalMap); //Use the same sampler as before.
+
glBindTexture(GL_TEXTURE_2D, object2NormalMap); // Use the same sampler as before.
  
//Use the same shadow map, so no need to unbind/bind.
+
// Use the same shadow map, so no need to unbind/bind.
//Render stuff
+
// Render stuff
 
glDraw*();
 
glDraw*();
 
</source>
 
</source>

Latest revision as of 10:49, 3 January 2018

This example shows how to associate textures with shader variables when rendering.

// Initial program setup.
glLinkProgram(program); // Initial link

GLint baseImageLoc = glGetUniformLocation(program, "baseImage");
GLint normalMapLoc = glGetUniformLocation(program, "normalMap");
GLint shadowMapLoc = glGetUniformLocation(program, "shadowMap");

glUseProgram(program);
glUniform1i(baseImageLoc, 0); // Texture unit 0 is for base images.
glUniform1i(normalMapLoc, 2); // Texture unit 2 is for normal maps.
glUniform1i(shadowMapLoc, 4); // Texture unit 4 is for shadow maps.

// When rendering an objectwith this program.
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, object1BaseImage);
glBindSampler(0, linearFiltering);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, object1NormalMap);
glBindSampler(2, linearFiltering); // Same filtering as before
glActiveTexture(GL_TEXTURE0 + 4);
glBindTexture(GL_TEXTURE_2D, shadowMap);
glBindSampler(4, depthComparison); // Special sampler for depth comparisons.

// Render stuff
glDraw*();

// Render another object with some different textures.
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, object2BaseImage); // Use the same sampler as before.
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, object2NormalMap); // Use the same sampler as before.

// Use the same shadow map, so no need to unbind/bind.
// Render stuff
glDraw*();