OpenGL 3.3 - 我只能绑定到 GL_TEXTURE0
OpenGL 3.3 - I can only bind to GL_TEXTURE0
一段时间以来,我一直在尝试遵循 OpenGL-3.3 教程,但似乎我只能将纹理绑定到 GL_TEXTURE0,否则我会得到一个黑色方块。
示例:
这个有效
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 0);
shader.bind();
glBindVertexArray(vertex_array);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
这不是
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);
shader.bind();
glBindVertexArray(vertex_array);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
详情:
shader.bind
只是调用 glUseProgram
顶点着色器:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 tex_coords;
out vec4 our_color;
out vec2 our_tex_coords;
void main()
{
gl_Position = vec4(position, 1.0f);
our_color = vec4(color, 1.0f);
our_tex_coords = tex_coords;
}
片段着色器:
#version 330 core
out vec4 color;
in vec4 our_color;
in vec2 our_tex_coords;
uniform sampler2D our_texture1;
void main()
{
color = texture(our_texture1, our_tex_coords);
}
纹理创建:
GLuint texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
sf::Image image1;
if (!image1.loadFromFile("../Assets/container.jpg"))
{
error();
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image1.getSize().x, image1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1.getPixelsPtr());
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
据我所知,文件已正确加载。知道哪里出了问题吗?
你的代码看起来不错。
但我认为 shader.bind() 通常应该在 glActiveTexture 之前调用。
进一步提示:如果您希望纹理在使用缩小时消除锯齿,请使用 GL_LINEAR_MIPMAP_LINEAR 而不是 GL_LINEAR。这应该开箱即用,因为您已经生成了 MIPMaps。
此致,
克里斯
较早的答案告诉您如何更改代码以使其工作,但给出了错误的解释。在这个序列中:
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);
shader.bind();
和shader.bind()
调用glUseProgram()
,顺序确实有问题。但与glActiveTexture()
无关,与当前绑定的程序完全无关
glUniform1i()
调用为当前绑定的程序设置了一个统一的值。所以关键是你在glUniform*()
之前调用glUseProgram()
。例如,以下序列将起作用:
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
shader.bind();
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);
一段时间以来,我一直在尝试遵循 OpenGL-3.3 教程,但似乎我只能将纹理绑定到 GL_TEXTURE0,否则我会得到一个黑色方块。
示例:
这个有效
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 0);
shader.bind();
glBindVertexArray(vertex_array);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
这不是
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);
shader.bind();
glBindVertexArray(vertex_array);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
详情:
shader.bind
只是调用 glUseProgram
顶点着色器:
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 tex_coords;
out vec4 our_color;
out vec2 our_tex_coords;
void main()
{
gl_Position = vec4(position, 1.0f);
our_color = vec4(color, 1.0f);
our_tex_coords = tex_coords;
}
片段着色器:
#version 330 core
out vec4 color;
in vec4 our_color;
in vec2 our_tex_coords;
uniform sampler2D our_texture1;
void main()
{
color = texture(our_texture1, our_tex_coords);
}
纹理创建:
GLuint texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
sf::Image image1;
if (!image1.loadFromFile("../Assets/container.jpg"))
{
error();
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image1.getSize().x, image1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1.getPixelsPtr());
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
据我所知,文件已正确加载。知道哪里出了问题吗?
你的代码看起来不错。 但我认为 shader.bind() 通常应该在 glActiveTexture 之前调用。
进一步提示:如果您希望纹理在使用缩小时消除锯齿,请使用 GL_LINEAR_MIPMAP_LINEAR 而不是 GL_LINEAR。这应该开箱即用,因为您已经生成了 MIPMaps。
此致, 克里斯
较早的答案告诉您如何更改代码以使其工作,但给出了错误的解释。在这个序列中:
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);
shader.bind();
和shader.bind()
调用glUseProgram()
,顺序确实有问题。但与glActiveTexture()
无关,与当前绑定的程序完全无关
glUniform1i()
调用为当前绑定的程序设置了一个统一的值。所以关键是你在glUniform*()
之前调用glUseProgram()
。例如,以下序列将起作用:
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture1);
shader.bind();
glUniform1i(glGetUniformLocation(shader.program, "our_texture1"), 1);