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.
<
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->x, texture->y, 0, GL_RED, GL_UNSIGNED_BYTE, reinterpret_cast(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*
.