c - OpenGL ES 2.0 纹理显示为黑色

标签 c graphics opengl-es opengl-es-2.0

我正在为嵌入式设备开发一个使用 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_textureglTexImage2D 函数本身的问题。

最佳答案

您需要将 U 和 V 维度的钳位模式设置为 CLAMP_TO_EDGE,否则纹理不完整,将采样为黑色。

关于c - OpenGL ES 2.0 纹理显示为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040541/

相关文章:

java - 如何在 C/OpenGL 中从整数数组创建位图

c -/proc的内核模块

c - 终止指向指针的指针?

opengl - 延迟渲染 - 使用光体积重建点光源的位置是否有效?

iOS:Core Graphics 是在 OpenGL 之上实现的吗?

javascript - OpenGL将汽车朝圆形路径旋转

c - 学习用 C 语言行走 - 将 5 行 bash shell 脚本翻译为 C

C 在释放内存时将 double 类型的变量转换为 char 类型的变量

android - 如何避免 Android 应用程序的图形内存不足?

image - BlackBerry - 如何调整大小和存储图像?