【问题标题】:OpenGL 4.0 texture bindingOpenGL 4.0 纹理绑定
【发布时间】:2013-09-07 11:12:36
【问题描述】:

我正在尝试将多个纹理绑定到片段着色器中的采样器。加载代码似乎运行良好。 ATI 的 CodeXL 显示纹理加载正确。

但是,当我将模型的纹理绑定到活动纹理 0 和 1 时,我无法让它将值发送到我的着色器。当我将着色器统一标记为 usampler2D 并使用 uvec4 存储颜色时,就像我应该的那样,因为我的纹理是作为无符号字节提供的,我得到一个全白模型。当我将着色器统一更改为 sampler2D 并使用 vec4 存储颜色时,我的 glUniform1i 调用无法再获取着色器变量的位置,因此没有为活动纹理设置任何内容。这导致能够使用漫反射纹理,但我无法获得正常纹理。好的一面是,漫反射纹理是这样在模型上绘制的。

我不确定是什么问题。我已经在网上检查了几个地方试图弄清楚,并且我已经查看了红皮书。我知道我遗漏了一些东西,或者某些状态设置错误,但我似乎找不到它。提前感谢您为解决此问题提供的任何帮助。

纹理创建

int[] testWidth;

testWidth = new int[1];
testWidth[0] = 1000;

// First bind the texture.
bind();

// Make sure that textures are enabled.
// I read that ATI cards need this before MipMapping.
glEnable(GL_TEXTURE_2D);

// Test to make sure we can create a texture like this.
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, format, width, height,
             0, format, GL_UNSIGNED_BYTE, null);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
                         testWidth);
if (testWidth[0] == 0)
{
   message("Could not load texture onto the graphics card.");
}
else
{
   // Not so sure about this part....but it seems to work.
   glPixelStorei(GL_PACK_ALIGNMENT, 1);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   // Load the texture data.
   glTexImage2D(texture_type, 0, format, width, height,
                0, format, GL_UNSIGNED_BYTE, (GLvoid[]?)value);

   // Smaller mipmaps need linear mipmap coords.
   // Larger just uses linear of the main texture.
   glTexParameterf(texture_type, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR_MIPMAP_LINEAR);
   glTexParameterf(texture_type, GL_TEXTURE_MAG_FILTER,
                   GL_LINEAR);

   // Clamp the texture to the edges.
   glTexParameterf(texture_type, GL_TEXTURE_WRAP_S,
                   GL_CLAMP_TO_EDGE);
   glTexParameterf(texture_type, GL_TEXTURE_WRAP_T,
                   GL_CLAMP_TO_EDGE);
   glTexParameterf(texture_type, GL_TEXTURE_WRAP_R,
                   GL_CLAMP_TO_EDGE);

   // Generate the mipmaps. The tex parameter is there
   // for ATI cards. Again, it's something I read online.
   glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
   glGenerateMipmap(texture_type);
}

// Now unbind the texture.
unbind();

纹理绑定

if (currentShader != null)
{
   currentShader.set_uniform_matrix("model_matrix", ref model_matrix,
                                    true);

   if (material != null)
   {
      if (material.diffuse_texture != null)
      {
         glActiveTexture(GL_TEXTURE0);
         material.diffuse_texture.bind();
         currentShader.set_uniform_texture("diffuse_texture",
                                           Constants.DIFFUSE_TEXTURE);
      if (material.normal_testure != null)
      {
         glActiveTexture(GL_TEXTURE1);
         material.normal_texture.bind();
         currentShader.set_uniform_texture("normal_texture",
                                           Constants.NORMAL_TEXTURE);
      }
   }
}

// If there is a renderable then render it.
if (renderable != null)
{
   renderable.render(1.0);
}

if (material != null)
{
   material.unbind();
}

片段着色器

#version 400 core

/**
 * Smooth the inward vertex color. Smooth it so that the fragments
 * which will be in between the vertices as well can get a value close
 * to where they are positioned after being rasterized.
 */
smooth in vec4 vertex_color;

/**
 * Smooth the inward texture coordinates. Smooth it so that the
 * fragments which will be in between the vertices as well can get a
 * value close to where they are positioned after being rasterized.
 */
smooth in vec2 out_texture_coordinate;

/**
 * The color to make this fragment.
 */
out vec4 frag_color;

/**
 * The models diffuse texture. This will be mapped to index 0.
 */
uniform usampler2D diffuse_texture;

/**
 * The models normal texture. This will be mapped to index 1.
 */
uniform usampler2D normal_texture;


/**
 * The starting function of the shader.
 */
void main(void)
{
   uvec4 diffuseColor;
   uvec4 normalModifier;

   diffuseColor = texture(diffuse_texture, out_texture_coordinate);
   normalModifier = texture(normal_texture, out_texture_coordinate);

   // Discard any fragments that have an alpha color less than 0.05.
   if (diffuseColor.a < 1.0)
   {
      // This works as part of depth testing to remove the fragments that
      // are not useful.
      discard;
   }

   frag_color = diffuseColor;
}

统一设置

/**
   * Sets the uniform value for a texture in the shader.
   *
   * @param name The name of the uniform to bind this texture to.
   * This must have already been registered.
   *
   * @param textureUnit The id for the texture unit to bind to the uniform.
   * This is not the texture's id/reference, but the OpenGL texture unit
   * that the reference is bound to.
   * This is set by calling glActiveTexture.
   */
  public void set_uniform_texture(string name, int textureUnit)
  {
     // Check to make sure the uniform was given a location already.
     if (register_uniform(name) == true)
     {
        // Set the data for this uniform then.
        glUniform1i(uniform_mapping.get(name), textureUnit);
     }
     else
     {
        message("Texture was not set. %s", name);
     }
  }

  /**
   * Register a uniform for passing data to the shader program.
   *
   * @return true if the uniform was found with a valid location;
   * otherwise, false.
   *
   * @param name The name for the parameter to get a uniform location for.
   * Use this name for the variable in your shader.
   */
  public bool register_uniform(string name)
  {
     int location;

     // Make sure we didn't already get the location of the uniform value.
     if (uniform_mapping.has_key(name) == false)
     {
        location = Constants.OPENGL_INVALID_INDEX;

        // We have no information about this uniform, so try
        // to get it's location.
        location = glGetUniformLocation(reference, name);

        // The location will 0 or higher if we found the uniform.
        if (location != Constants.OPENGL_INVALID_INDEX)
        {
           uniform_mapping.set(name, location);

           return true;
        }
     }
     else
     {
        // The uniform was previously found and can be used.
        return true;
     }

     debug("Uniform %s not found!!!!!", name);
     return false;
  }

【问题讨论】:

  • 用于纹理的确切格式很重要。此外,还不清楚set_uniform_texture 等函数背后发生了什么。你考虑过generating a log of your OpenGL calls with an appropriate debugging tool吗?
  • @NicolBolas 我的纹理格式是无符号 8 位整数,RGBA 或 RGB 用于普通纹理。我的格式变量将是适当的 GL_RGBA 或 GL_RGB。我将在编辑中显示 set_uniform_texture 函数。就调试而言,我目前正在使用 AMD 的 CodeXL。由于我使用的是 linux gIntercept 将无法正常工作。 gDebugger 无法正确安装,所以我有点卡在 CodeXL 上。这还不错,除非我无法让 CodeXL 转储除 CodeXL-.log 和 CodeXLServers-.log 之外的日志文件。这些文件似乎都没有任何帮助。
  • @NicolBolas 您的建议是正确的。格式确实很重要。我使用 G_RGBA 和 G_RGB 的事实是将纹理转换为浮点采样器。

标签: opengl


【解决方案1】:

将内部格式设置为 GL_RGB/A 意味着您应该使用 sampler2D 而不是 usampler2D,即使原始图像数据最初是作为无符号字节给出的。 编辑 给定的数据在调用 glTexImage2D 时被转换为内部格式(在这种情况下,GL_RGBA 是每个通道 8 位,所以不必发生太多事情)。但是,对于大多数图形应用程序来说,数据需要具有更高的精度,例如在使用非“最近”插值对纹理进行采样时,这就是它通常以浮点数公开的原因。

要绑定多个纹理...

glActiveTexture(GL_TEXTURE0 + firstTextureIndex); //firstTextureIndex should be unique amongst the textures bound for a particular shader
glBindTexture(GL_TEXTURE_2D, myFirstTextureHandle);
glUniform1i(glGetUniformLocation(shaderProgramHandle, "firstSampler"), firstTextureIndex); //note the same index given in the glActiveTexture call. This is also always glUniform1i

然后重复 secondTextureIndex、mySecondTextureHandle、“secondSampler”等。

如果 glGetUniformLocation 没有返回位置,请仔细检查您是否实际使用了它,它会影响着色器输出(或者它会被完全优化)。还要检查常见的拼写错误或缺少“统一”关键字等。

【讨论】:

  • 实际上在调用 glUniform 来设置采样器时,您没有使用与 glActiveTexture 相同的索引。如果您对 glActiveTexture 的调用是 glActiveTexture(GL_TEXTURE0 + i),则相应的采样器设置将是 glUniform1i(…, i)
  • @jozxyqk 我不知道 G_RGBA/G_RGB 自动将类型转换为浮点数。这是最大的问题,所以我接受了你的回答。另一个问题是,正如您所说,正常纹理正在被优化。此外,将 glActiveTexture 与 GL_TEXTURE0 + 我的值一起使用似乎可以解决我的问题。谢谢。
  • @datenwolf 你是对的,我怀疑 glActiveTexture(GL_TEXTURE0 + i) 是我遇到的最大问题。
  • @JasonSmith 最后一件事,GL_RGBA 是每通道 8 位。是的,在 GLSL 中,texture(2d) 调用返回一个插值的 vec4,但 vec4 来自的数据并不总是浮点数。其他内部格式是 GL_RGBA32F,例如 GL_R32F(或 GL_R32UI 用于 unsigned int)。除非你真的需要精度,否则这些纹理的内存会增加四倍。因此,在您的情况下,给出的数据将等同于内部数据(我错误地暗示它不是)。在大多数情况下,您会希望像使用浮点数一样使用它,尤其是对于插值,需要更高的精度。
【解决方案2】:

由于您没有显示Constants 的定义,请确保您的代码具有以下断言:

  if (material.diffuse_texture != null)
  {
     glActiveTexture(GL_TEXTURE0);
     material.diffuse_texture.bind();

     assert(Constants.DIFFUSE_TEXTURE + GL_TEXTURE0 == GL_TEXTURE0);

     currentShader.set_uniform_texture("diffuse_texture",
                                       Constants.DIFFUSE_TEXTURE);
  if (material.normal_testure != null)
  {
     glActiveTexture(GL_TEXTURE1);
     material.normal_texture.bind();

     assert(Constants.NORMAL_TEXTURE + GL_TEXTURE0 == GL_TEXTURE1);

     currentShader.set_uniform_texture("normal_texture",
                                       Constants.NORMAL_TEXTURE);
  }

一个常见的误解是传递给采样器制服的值是给 glActiveTexture 的枚举值。实际上 glActiveTexture 将 GL_TEXTURE0 作为偏移基值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2015-02-26
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多