Public Group

# glDrawPixels(...) and alpha blending c++

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a question about glDrawPixels(...), I can't find any documentation that says it supports alpha blending, though it does support a number of other features , including pixel formats that include alpha values. So I'm looking for a definative yes or no, if yes, could you show me what I'm doing wrong? If no, I have a few follow up questions.

void BasicLibrary::drawImage(float X, float Y, Image *IMG)
{
{return;}

if(IMG-&gt;isAlphaOn())
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glRasterPos2f(TransXpt(X), TransYpt(Y));
glDrawPixels((int)IMG-&gt;xSize, (int)IMG-&gt;ySize, GL_RGBA, GL_UNSIGNED_BYTE, &IMG-&gt;pixelData[0]);
glDisable(GL_BLEND);
}
else
{
glRasterPos2f(TransXpt(X), TransYpt(Y));
glDrawPixels((int)IMG-&gt;xSize, (int)IMG-&gt;ySize, GL_RGB, GL_UNSIGNED_BYTE, &IMG-&gt;pixelData[0]);
}
}


The result of this code is the pixel image drawn to the screen without any blending effects.

##### Share on other sites
According to the Red Book:

"(...)When using glDrawPixels() to write RGBA or color-index information, you may need to control the current drawing buffers with glDrawBuffer(), which, along with glReadBuffer(), is also described in "Selecting Color Buffers for Writing and Reading" in Chapter 10.(...)"

"(...)To paint with a circular brush in blue, repeatedly draw a blue square with glDrawPixels() where the alpha values are largest in the center and taper to zero at the edges of a circle centered in the square. Draw using a blending function that uses alpha of the incoming color and (1-alpha) of the color already at the pixel.(...)"

So it looks that alpha blending is supported by this function.

Forgetting for a while about first quote: maybe your problem is caused by fact, that you read the image without its alpha channel? Dunno, I don't see any obvious errors in your source code.

Btw, in terms of performance, it would be much better to change your Image to OGL texture, change view to ortographical and then render quad with stretched over it texture. I can show you even some code if you want :-)

##### Share on other sites
Ahh thanks!

Yes, my image data isn't read with an alpha channel. However, I add one through some code.

void Image::addAlphaComponent(){  if(alphaOn == true)  {return;}    else  {alphaOn=true;}    int imageSize = xSize * ySize * 3;  int totalSize = xSize * ySize * 4;  int k = 0;  vector<unsigned char> tmp;  tmp.resize(totalSize);    for(int i=0;i<imageSize;i+=3, k+=4)  {    tmp[k + 0] = pixelData[i + 0];    tmp[k + 1] = pixelData[i + 1];    tmp[k + 2] = pixelData[i + 2];        tmp[k + 3] = (unsigned char) 255;  }   pixelData.resize(totalSize);    for(int i=0;i<totalSize;++i)  {    pixelData = tmp;  }}

Basically, I think it works because it doesn't make the image distort or anything. Yes, that code would assign an alpha value that would make it appear solid. I change the alpha value in some other code.

I was thinking about using the image data as a texture, then rendering an quad using said texture. At this point in time though, I wouldn't be storing the texture index. Would be ok if every frame I remake the texture using the image data? I might be manipulating it a lot (i.e adding removing alpha, flipping horizontally/vertically, swapping red blue values, etc) and would I suffer super performance problems doing so?

##### Share on other sites
Quote:
 I was thinking about using the image data as a texture, then rendering an quad using said texture. At this point in time though, I wouldn't be storing the texture index. Would be ok if every frame I remake the texture using the image data? I might be manipulating it a lot (i.e adding removing alpha, flipping horizontally/vertically, swapping red blue values, etc) and would I suffer super performance problems doing so?

It would be OK, but it would be also horribly slow :-) But, AFAIK, you don't need to re-create texture in every frame, since you can *change texture* using glDrawPixels.

Other things:

- why on Earth would you be adding / removing alpha to the same picture every frame? Calling just glEnable / glDisable GL_BLEND wouldn't be sufficent?

- flipping horizontally/vertically is very (and I mean very) simple to do with textures - you get it for free, as well as rotation, stretching, alpha blending (blending only to some extent) etc.

- swapping red blue values - just like in the first paragraph, you could use glReadPixels / glDrawPixels to alter those channels. OTOH, such operation, will be horribly slow - and AFAIK it doesn't matter if you do it on screen or texture.

for(int i=0;i<totalSize;++i)
{
pixelData = tmp;
}

it will be faster if you will use memcpy.

##### Share on other sites
Seems like I need to investigate the capabilities of textures more. If I can make it them do all that, makes my code I wrote for my Image class obsolete, but in a good way. :)

I'm under the impression that memcpy didn't work for vectors because the memory involved isn't necessarily consecutive. Never tried it though. Much to consider, thanks!

##### Share on other sites
No prob :-)

Yes, I suggest to try textures - IMHO they are very easy to work with, have good capabilities and outstanding performance.

In all cases, std :: vectors are implemented internally by normal arrays - and they are contiguous. Read more.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634048
• Total Posts
3015233
×