我正在为嵌入式设备开发一个使用 OpenGL ES 2.0 的应用程序。
这是我的片段着色器:
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
gl_FragColor = texture2D(s_texture, v_texCoord);
}
我正确地设置了纹理。出于某种原因,调用 glTexImage2D
没有产生我正在寻找的结果。纹理完全是黑色的,而不是用我提供的数据填充。
这就是我创建纹理的方式:
GLuint textureId;
// 2x2 Image, 3 bytes per pixel (R, G, B)
GLubyte pixels[6 * 3] =
{
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
255, 255, 0, // Yellow
0, 255, 255,
255, 0, 255
};
// Use tightly packed data
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
// Generate a texture object
glGenTextures ( 1, &textureId );
// Bind the texture object
glBindTexture ( GL_TEXTURE_2D, textureId );
// Load the texture
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels );
// Set the filtering mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
然后,我像这样绑定(bind)纹理:
samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, textureId );
// Set the sampler texture unit to 0
glUniform1i ( samplerLoc, 0 );
我在调试时确认顶点和纹理坐标已绑定(bind)并传递给着色器。因此,它必须是 s_texture
或 glTexImage2D
函数本身的问题。
最佳答案
您需要将 U 和 V 维度的钳位模式设置为 CLAMP_TO_EDGE
,否则纹理不完整,将采样为黑色。
关于c - OpenGL ES 2.0 纹理显示为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040541/