纹理单位重叠?渲染错误的纹理

时间:2018-10-05 01:35:27

标签: c++ opengl glsl framebuffer uniform

我想在片段着色器中使用4个纹理。一个是立方体贴图,一个是在帧缓冲区上的渲染纹理,最后两个是从图像创建的。

当我尝试在片段着色器中调用它们时,无论调用的是哪一个,我都会得到与调用的不同或相同的信息(取决于我尝试修复它时的不同迭代)。

这就是我的想法

GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);

GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
GLenum DrawBuffers[1]={GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);

if ( glCheckFramebufferStatus ( GL_FRAMEBUFFER ) == GL_FRAMEBUFFER_COMPLETE )
{
  glViewport(0, 0, 1024, 1024);
  glClearColor( 0.4f, 0.4f, 0.4f, 1.0f );
  glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

  ngl::ShaderLib* shader = ngl::ShaderLib::instance();
  (*shader)["Normal"]->use();
  m_transform.setPosition(0.0f, 0.0f, 0.0f);
  loadMatricesToShader();
  //drawing the quad
  prim->draw("tex");
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,1024,768);

// grab an instance of the shader manager
ngl::ShaderLib* shader = ngl::ShaderLib::instance();
( *shader )[ "PBR" ]->use();

glUniform1i(renderedTexture, 1);

//initialize environment map
initEnvironment();

initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(m_glossMapTex, 2);
initTexture(3, m_textMap, "images/alege.png");
glUniform1i(m_textMap, 3);

还有initTexture和initEnvironment

void NGLScene::initTexture(const GLuint& texUnit, GLuint &texId, const char *filename) {

glActiveTexture(GL_TEXTURE0 + texUnit);

// Load up the image using NGL routine
ngl::Image img(filename);

glGenTextures(1, &texId);

glBindTexture(GL_TEXTURE_2D, texId);

glTexImage2D (
            GL_TEXTURE_2D,    
            0,                
            img.format(),   
            img.width(),      
            img.height(),     
            0,                
            GL_RGB,          
            GL_UNSIGNED_BYTE, 
            img.getPixels()); 

// Set up parameters for our texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

void NGLScene::initEnvironment() {

glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
glActiveTexture (GL_TEXTURE0);
glGenTextures (1, &m_envTex);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_envTex);

initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, "images/sky_zneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, "images/sky_zpos.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, "images/sky_ypos.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, "images/sky_yneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, "images/sky_xneg.png");
initEnvironmentSide(GL_TEXTURE_CUBE_MAP_POSITIVE_X, "images/sky_xpos.png");

glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_AUTO_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLfloat anisotropy;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &anisotropy);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);

// Set our cube map texture to on the shader so we can use it
ngl::ShaderLib *shader=ngl::ShaderLib::instance();
shader->use("PBR");
shader->setUniform("envMap", 0);
}

void NGLScene::initEnvironmentSide(GLenum target, const char *filename) {
// Load up the image using NGL routine
ngl::Image img(filename);

glTexImage2D (
  target,          
  0,                
  img.format(),     
  img.width(),     
  img.height(),    
  0,               
  GL_RGBA,         
  GL_UNSIGNED_BYTE, 
  img.getPixels()   
);
}

我感觉我在纹理单位和设置制服方面的知识方面存在差距,或者片段着色器中的制服可能存在问题。

1 个答案:

答案 0 :(得分:1)

在您的代码中有一个误解,即如何使用glUniform1i。如果将值分配给制服,则必须通过制服位置索引来标识制服。参见Uniform (GLSL)

glUniform1i的拳头参数必须是制服的位置,而不是命名的纹理对象。

可以通过Layout Qualifier

在着色器中显式设置制服的位置

例如

GLSL

layout(location = 7) uniform sampler2D u_gloss;

C ++

initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(7, 2); // uniform location 7

如果布局限定符未设置制服的位置,则在链接程序时会自动设置制服的位置。您可以通过glGetUniformLocation来请求此位置:

例如

GLSL

uniform sampler2D u_gloss;

C ++

GLuint program_obj = ... ; // ( *shader )[ "PBR" ]->???
GLint gloss_location = glGetUniformLocation(program_obj , "u_gloss");

initTexture(2, m_glossMapTex, "images/gloss.png");
glUniform1i(gloss_location, 2);