Opengl: Use single channel texture as alpha channel to display text

前端 未结 2 728
余生分开走
余生分开走 2021-01-13 23:24

What I\'m trying to do is load a texture into hardware from a single channel data array and use it\'s alpha channel to draw text onto an object. I am using opengl 4.

<
相关标签:
2条回答
  • 2021-01-13 23:38

    I think you swapped the "internal format" and "format" parameters of glTexImage2d(). That is, you told it that you want RGBA in the texture object, but only had RED in the file data rather than vice-versa.

    Try to replace your call with the following:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, texture->x, texture->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(full_texture));
    
    0 讨论(0)
  • 2021-01-13 23:55
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->x, texture->y, 0, GL_RED, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(full_texture));
    

    This is technically legal but never a good idea.

    First, you need to understand what the third parameter to glTexImage2D is. That's the actual image format of the texture. You are not creating a texture with one channel; you're creating a texture with four channels.

    Next, you need to understand what the last three parameters do. These are the pixel transfer parameters; they describe what the pixel data you're giving to OpenGL looks like.

    This command is saying, "create a 4 channel texture, then upload some data to just the red channel. This data is stored as an array of unsigned bytes." Uploading data to only some of the channels of a texture is technically legal, but almost never a good idea. If you're creating a single-channel texture, you should use a single-channel texture. And that means a proper image format.

    Next, things get more confusing:

    new_texture->datasize = texture_width * texture_height*4;
    

    Your use of "*4" strongly suggests that you're creating four-channel pixel data. But you're only uploading one-channel data. The rest of your computations agree with this; you don't seem to ever fill in any data pass full_texture[texture_width * texture_height]. So you're probably allocating more memory than you need.

    One last thing: always use sized internal formats. Never just use GL_RGBA; use GL_RGBA8 or GL_RGBA4 or whatever. Don't let the driver pick and hope it gives you a good one.

    So, the correct upload would be this:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, texture->x, texture->y, 0, GL_RED, GL_UNSIGNED_BYTE, full_texture);
    

    FYI: the reinterpret_cast is unnecessary; even in C++, pointers can implicitly be converted into void*.

    0 讨论(0)
提交回复
热议问题