如何将多个立方体纹理传递到均匀的采样器中

How to pass multiple cube textures into a uniform array of samplers?

本文关键字:采样器 纹理 立方体      更新时间:2023-10-16

说我在着色器中有一个制服,称为:

uniform samplerCube depth_maps[];

我在CPP侧有一系列纹理ID,称为:

vector<GLuint> depth_maps;

是通过绘制到框架附加纹理的绘制而创建的。

对于单个纹理,将信息传递到数组的代码看起来或多或少如下:

    glUseProgram(program);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    GLint loc = glGetUniformLocation(program, "depth_maps");
    if(loc == GL_INVALID_VALUE || loc==GL_INVALID_OPERATION)
    {
        cerr << "Error returned when trying to find depth_map uniform."
            << "nuniform: text"
            << "Error num: " << loc
            << endl;
        return;
    }
    glUniform1i(loc,1);

在此处的gl_texture1中指示此纹理应将其附加到纹理单元1(假设已经连接到gl_texture0的纹理(。

问题n1只有2个保证的纹理单元,此后任何一个取决于实施。

问题n2,即使我有很多,我也只知道如何独立和独立地传递它们(即,我要通过我要通过的每个纹理的上述代码,并为每个纹理定义一个新的制服(

如何将THSI数据作为连续的均匀阵列传递?

说我在着色器中有一个制服,称为:

uniform samplerCube depth_maps[];

那将是一个编译错误。除一个例外,GLSL 中的数组必须(最终(以编译时间确定的大小声明。

但是,假设您已经给了这个数组一个大小(并且您确保将包含纹理图像单元的vector用相同的尺寸(。在统一数组上设置纹理图像单元的工作方式正是设置均匀数组上的任何值的方式。

您可以单独执行每个数组元素的glUniform1i命令。

for(std::size_t i = 0; i < depth_maps.size(); ++i)
{
  std::ostringstream os;
  os << "depth_maps[" << i << "]";
  std::string name = std::move(os).str();
  GLint loc = glGetUniformLocation(program, name.c_str());
  glUniform1i(loc, depth_maps[i]);
}

,也可以一次设置它们。

GLint loc = glGetUniformLocation(program, "depth_maps");
glUniform1iv(loc, depth_maps.size(), depth_maps.data());