为什么绑定 2 openGL sampler1d 纹理不起作用?
Why binding 2 openGL sampler1d texture doesn't work?
我想将两个一维纹理传递到我的片段着色器中。
我正在创建两个 sampler1d 纹理,将它们绑定到我的着色器程序,但似乎两个采样器都使用相同的纹理。
当我对 texture1 使用采样 texelFetch 时,它采用了我在 texture2 中定义的错误颜色。
下面的 Shader 程序 returns 当我希望看到红色屏幕时,我看到了绿色屏幕。播放参数我看到 texture1 的 buf 的任何变化都会改变 texture2 的颜色。
有什么解决办法吗?
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture0; //this is default-bound texture0, works fine
uniform sampler1D texture1; //this is bound manually with constant RED color
uniform sampler1D texture2; //this is bound manually with constant GREEN color
void main()
{
vec4 col1 = texelFetch(texture1, 0, 0);
FragColor = col1; //should be Red, but in reality it returns GREEN from texture2 =(
}
这是我将着色器采样器绑定到真实纹理的代码:
GLuint tex1Id;
glGenTextures(1, &tex1Id );
glBindTexture(GL_TEXTURE_1D, tex1Id);
GLuint tex2Id ;
glGenTextures(1, &tex2Id );
glBindTexture(GL_TEXTURE_1D, tex2Id );
//red color for texture1
GLsizei size = 1;
uint8_t* buf = new uint8_t[size * 4];
buf[0] = 255;
buf[1] = 0;
buf[2] = 0;
buf[3] = 100;
//green color for texture1
uint8_t* buf2 = new uint8_t[size * 4];
buf2[0] = 0;
buf2[1] = 255;
buf2[2] = 0;
buf2[3] = 100;
GLint tex1loc = glGetUniformLocation(shader->ID, "texture1");
GLint tex2loc = glGetUniformLocation(shader->ID, "texture2");
glUniform1i(tex1loc, 1); // RED for texture1
glUniform1i(tex2loc, 2); // GREEN for texture2
glActiveTexture(GL_TEXTURE0 + 1); //RED
glBindTexture(GL_TEXTURE_1D, tex1Id);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
glActiveTexture(GL_TEXTURE0 + 2); //GREEN
glBindTexture(GL_TEXTURE_1D, tex2Id);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf2);
delete[] buf;
delete[] buf2;
实际上,两个纹理采样器制服(texture1
和 texture2
)的绑定点都是默认值 (0),因为从未设置过绑定点。 glUniform
在当前安装的程序对象的默认统一块中指定统一变量的值。 uniform location 只是一个数字,用于解决程序中的 uniform,但不指定程序。
由于 tex2Id
是创建的第二个纹理,它绑定到纹理单元 0 和 2。因此两个采样器(texture1
和 texture2
)都在纹理对象 tex2Id
中查找。
在调用 glUniform1i
:
之前,您必须使用 glUseProgram
安装程序
glUseProgram(shader->ID);
glUniform1i(tex1loc, 1); // RED for texture1
glUniform1i(tex2loc, 2); // GREEN for texture2
从 OpenGL 4.1 开始,您可以选择使用 glProgramUniform
:
glProgramUniform1i(shader->ID, tex1loc, 1); // RED for texture1
glProgramUniform1i(shader->ID, tex2loc, 2); // GREEN for texture2
我想将两个一维纹理传递到我的片段着色器中。 我正在创建两个 sampler1d 纹理,将它们绑定到我的着色器程序,但似乎两个采样器都使用相同的纹理。 当我对 texture1 使用采样 texelFetch 时,它采用了我在 texture2 中定义的错误颜色。
下面的 Shader 程序 returns 当我希望看到红色屏幕时,我看到了绿色屏幕。播放参数我看到 texture1 的 buf 的任何变化都会改变 texture2 的颜色。 有什么解决办法吗?
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture0; //this is default-bound texture0, works fine
uniform sampler1D texture1; //this is bound manually with constant RED color
uniform sampler1D texture2; //this is bound manually with constant GREEN color
void main()
{
vec4 col1 = texelFetch(texture1, 0, 0);
FragColor = col1; //should be Red, but in reality it returns GREEN from texture2 =(
}
这是我将着色器采样器绑定到真实纹理的代码:
GLuint tex1Id;
glGenTextures(1, &tex1Id );
glBindTexture(GL_TEXTURE_1D, tex1Id);
GLuint tex2Id ;
glGenTextures(1, &tex2Id );
glBindTexture(GL_TEXTURE_1D, tex2Id );
//red color for texture1
GLsizei size = 1;
uint8_t* buf = new uint8_t[size * 4];
buf[0] = 255;
buf[1] = 0;
buf[2] = 0;
buf[3] = 100;
//green color for texture1
uint8_t* buf2 = new uint8_t[size * 4];
buf2[0] = 0;
buf2[1] = 255;
buf2[2] = 0;
buf2[3] = 100;
GLint tex1loc = glGetUniformLocation(shader->ID, "texture1");
GLint tex2loc = glGetUniformLocation(shader->ID, "texture2");
glUniform1i(tex1loc, 1); // RED for texture1
glUniform1i(tex2loc, 2); // GREEN for texture2
glActiveTexture(GL_TEXTURE0 + 1); //RED
glBindTexture(GL_TEXTURE_1D, tex1Id);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
glActiveTexture(GL_TEXTURE0 + 2); //GREEN
glBindTexture(GL_TEXTURE_1D, tex2Id);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf2);
delete[] buf;
delete[] buf2;
实际上,两个纹理采样器制服(texture1
和 texture2
)的绑定点都是默认值 (0),因为从未设置过绑定点。 glUniform
在当前安装的程序对象的默认统一块中指定统一变量的值。 uniform location 只是一个数字,用于解决程序中的 uniform,但不指定程序。
由于 tex2Id
是创建的第二个纹理,它绑定到纹理单元 0 和 2。因此两个采样器(texture1
和 texture2
)都在纹理对象 tex2Id
中查找。
在调用 glUniform1i
:
glUseProgram
安装程序
glUseProgram(shader->ID);
glUniform1i(tex1loc, 1); // RED for texture1
glUniform1i(tex2loc, 2); // GREEN for texture2
从 OpenGL 4.1 开始,您可以选择使用 glProgramUniform
:
glProgramUniform1i(shader->ID, tex1loc, 1); // RED for texture1
glProgramUniform1i(shader->ID, tex2loc, 2); // GREEN for texture2