简体   繁体   English

无法在 OpenGL ES2 中显示 RGB 纹理

[英]Cannot get RGB texture to display in OpenGL ES2

I can get 1 channel of the texture to display with this code:我可以使用以下代码显示纹理的 1 个通道:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, image_width, image_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, image_grayscale);
    point bbox[4] = {
        { -0.5, 0.5, 0, 0 },
        { 0.5, 0.5, 1, 0 },
        { -0.5, -0.5, 0, 1 },
        { 0.5, -0.5, 1, 1 },
    };
    glBufferData(GL_ARRAY_BUFFER, sizeof bbox, bbox, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glVertexAttribPointer(uniform_color, 4, GL_FLOAT, GL_FALSE, 0, bbox);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

But when I change to但是当我改变为

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, button_image_width, button_image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_rgb);

I get a solid color instead of the image我得到纯色而不是图像

Below is the fragment shader下面是片段着色器

    GLbyte fShaderStr[] =  
       "varying vec2 texpos;                                                \n"
       "uniform sampler2D tex;                                              \n"
       "uniform vec4 color;                                                 \n"
       "void main()                                                         \n"
       "{                                                                   \n"
       "   gl_FragColor = vec4(1, 1, 1, texture2D(tex, texpos).a) * color;  \n"
       "}         

How can I draw an RGB texture?如何绘制RGB纹理?

When you do当你做

glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, image_width, image_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, image_grayscale);

the the result is a texture image, with an alpha channel only.结果是一个纹理图像,只有一个 alpha 通道。 texture2D return 0 for the red, green and blue channel. texture2D为红色、绿色和蓝色通道返回 0。 The alpha channel returns the corresponding value to the texture coordinate. alpha 通道返回纹理坐标对应的值。
Because of that in the fragment shader the alpha channel of the sampler is read:因为在片段着色器中,采样器的 alpha 通道被读取:
(See Swizzling ) (见 调和

texture2D(tex, texpos).a

But when you do但是当你这样做时

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, button_image_width, button_image_height, 0, >GL_RGB, GL_UNSIGNED_BYTE, image_rgb)

Then the texture image has 3 color channels, but the alpha channel is 1 for all texels.然后纹理图像有 3 个颜色通道,但所有纹素的 alpha 通道为 1。

You have to adapt the fragment shader for the 2nd case:您必须为第二种情况调整片段着色器:

gl_FragColor = vec4(texture2D(tex, texpos).rgb, 1.0);

or或者

gl_FragColor = texture2D(tex, texpos);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM