纹理采样时的sample值的设置问题

zzllvlv 2020-12-14 09:55:55
在红宝书第六章纹理视图的源代码中,init函数如下:
void Init()
{
ShaderInfo si[] = { { GL_VERTEX_SHADER, "TextureView.vert" },{ GL_FRAGMENT_SHADER, "TextureView.frag" },{ GL_NONE, NULL } };
Program = LoadShaders(si);

glGenVertexArrays(1, &vert);
glBindVertexArray(vert);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Verts), Verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(Verts->position)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);


float texData[] = { 1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 1.0 ,0.0, 1.0 };
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexStorage2D(GL_TEXTURE_2D, 2, GL_RGBA8, 2, 2);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_FLOAT, texData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

vglImageData image;
texCube = vglLoadTexture("../../media/test.dds", 0, &image);
e = glGetError();

glGenTextures(6, texPanel);
glTextureView(texPanel[0], GL_TEXTURE_2D, texCube, image.internalFormat, 0, image.mipLevels, 0, 1);
e = glGetError();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texPanel[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
vglUnloadImage(&image);

glClearColor(0.2f, 0.1f, 0.3f, 1.0f);
glUseProgram(Program);
Sleep(30);
}

display函数如下:
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vert);
vmath::mat4 ModelView = vmath::mat4::identity();
for (int i = 0; i< 1; ++i)
{
for (int j = 0; j < 1; j++)
{
ModelView = vmath::translate(0.63f*i, -0.63f*j, 0.0f);
glUniformMatrix4fv(1, 1, GL_FALSE, ModelView);
//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(Program,"tex"),0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}
glutSwapBuffers();
glutPostRedisplay();
}
而片元着色器中代码很简单,就是:
#version 430 core

in vec2 TexCoord;

layout(location = 0) uniform sampler2D tex;

out vec4 fColor;

void main()
{
fColor = texture(tex, TexCoord);
}
事实上,在display函数中,无论我将sampler2D 的采样器tex通过glUniform1i设置为0,或者1,显示的都是绑定在GL_TEXTURE1上的texPanel[0]的图像。只有在display中再次将tex绑定到GL_TEXTURE0(将注释掉的两行重新写入),才能正确根据glUniform1i的值进行采样,请问这是为什么?
...全文
3657 回复 打赏 收藏 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
相关推荐
发帖
OpenAPI

2245

社区成员

OpenAPI
社区管理员
  • OpenAPI社区
加入社区
帖子事件
创建了帖子
2020-12-14 09:55
社区公告
暂无公告