OpenGL - 不显示简单的 2D 纹理
OpenGL - Simple 2D Texture Not Being Displayed
我使用 glfw 和 glew 编写了一个非常简单的 OpenGL 程序。它可以正常编译并运行,但是我传递给片段着色器的简单纹理没有显示。
唯一的几何图形是一个四边形。
我只想显示单一的纯色,即纹理的第一个元素。我知道片段着色器完全可以工作,因为我可以显示没有纹理的纯色:
#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;
in vec2 texCoord;
uniform sampler2D tex;
void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}
然而,当我使用纹理采样器时,我什么也得不到,就像采样器只采样 0(只改变 main()
):
void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}
我正在以我认为正确的方式填充和生成纹理(TEX_SIZE
定义为 #define TEX_SIZE (32*32)
):
//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};
//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);
这是整个绘制循环:
//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);
//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);
if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}
glfwSwapBuffers(window);
glfwPollEvents();
}
为什么会这样?我完全卡住了。
编辑:根据建议更改了数组初始化:
//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}
没有变化。输出仍然全黑。
GL_TEXTURE_MIN_FILTER
的初始值为GL_NEAREST_MIPMAP_LINEAR
。但是您不生成 mipmap。这导致贴图不完整.
OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205
A texture is said to be complete if all the texture images and texture parameters
required to utilize the texture for texture application are consistently defined.
... a texture is complete unless any of the following
conditions hold true:
- The minification filter requires a mipmap (is neither
NEAREST
nor LINEAR
),
and the texture is not mipmap complete.
更改纹理缩小过滤器 (glTexParameteri
) 以解决您的问题:
例如
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
我使用 glfw 和 glew 编写了一个非常简单的 OpenGL 程序。它可以正常编译并运行,但是我传递给片段着色器的简单纹理没有显示。
唯一的几何图形是一个四边形。
我只想显示单一的纯色,即纹理的第一个元素。我知道片段着色器完全可以工作,因为我可以显示没有纹理的纯色:
#version 320 es
mediump out vec4 color;
precision mediump float;
precision mediump sampler2D;
in vec2 texCoord;
uniform sampler2D tex;
void main() {
color = vec4(0.5, 0.75, 0.5, 1.0);
}
然而,当我使用纹理采样器时,我什么也得不到,就像采样器只采样 0(只改变 main()
):
void main() {
color = vec4(texture(tex, vec2(0, 0)).rgb, 1);
}
我正在以我认为正确的方式填充和生成纹理(TEX_SIZE
定义为 #define TEX_SIZE (32*32)
):
//Texture Data
GLfloat texData[TEX_SIZE * 3] = {1.0};
//Create texture
GLuint tex;
glGenTextures(1, &tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGB, GL_FLOAT, texData);
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_WRAP_R, GL_REPEAT);
这是整个绘制循环:
//Render loop
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS
&& !glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
//Texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(glGetUniformLocation(programID, "tex"), 0);
//Draw the quad
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, gIndices);
glDisableClientState(GL_VERTEX_ARRAY);
if (glGetError()) {
puts((char *)glewGetErrorString(glGetError()));
}
glfwSwapBuffers(window);
glfwPollEvents();
}
为什么会这样?我完全卡住了。
编辑:根据建议更改了数组初始化:
//Texture Data
GLfloat texData[TEX_SIZE * 3];
for (int i = 0; i < TEX_SIZE * 3; i++) {
texData[i] = 1.0;
}
没有变化。输出仍然全黑。
GL_TEXTURE_MIN_FILTER
的初始值为GL_NEAREST_MIPMAP_LINEAR
。但是您不生成 mipmap。这导致贴图不完整.
OpenGL ES 3.2 Specification; 8.17 Texture Completeness; page 205
A texture is said to be complete if all the texture images and texture parameters required to utilize the texture for texture application are consistently defined.
... a texture is complete unless any of the following conditions hold true:
- The minification filter requires a mipmap (is neither
NEAREST
norLINEAR
), and the texture is not mipmap complete.
更改纹理缩小过滤器 (glTexParameteri
) 以解决您的问题:
例如
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);