开发者

Creating an OpenGL texture with alpha using NSBitmapImageRep

开发者 https://www.devze.com 2023-03-24 16:41 出处:网络
I am loading a PNG using: theImage = [NSBitmapImageRep imageRepWithContentsOfFile:imagePath]; from which I can successfully create a gl texture and render correctly without any transparency. Howeve

I am loading a PNG using:

theImage = [NSBitmapImageRep imageRepWithContentsOfFile:imagePath];

from which I can successfully create a gl texture and render correctly without any transparency. However, when I switch blending on using:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

The texture renders with the correct transparent background, but the开发者_运维百科 image colours are incorrect.

I have tried several options in the blend function, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_DST_ALPHA, etc.

I was taught maybe I need to reorder the bits in the image data, maybe the channels have been mixed up, but I would not expect it to render correctly when blending is off in that case.

Alternatively, I could use libPNG I guess, but I would like to try using a NSBitmapImageRep if it is possible.


How about supplying a screenshot?


Anyway, your blending function is wrong either way for simple transparency channel blending. It should be either

  • normal alpha: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

or

  • premultiplied alpha: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
0

精彩评论

暂无评论...
验证码 换一张
取 消