Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to render Sprites in OpenGl (transparency question)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 wizardpc   Members   -  Reputation: 282

Like
0Likes
Like

Posted 19 December 2012 - 12:19 PM

So my question is more about rendering a certain color transparent in an image texture. The images are RGB, and I wish to render everything except pink (255,0,255).

Is it easy to do something like this with OpenGL?

Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2343

Like
2Likes
Like

Posted 19 December 2012 - 12:45 PM

This form of transparency is known as colour key. You could find some answers perhaps by googling "opengl colour key".

As an approach, it's very much out of favour and is a bit awkward on modern graphics pipelines. Basically, your options are to hack something into your pixel shader to detect and reject pixels with the magenta colour, or to preprocess your texture (either offline, or as you load it) to add an alpha channel to your texture (an alpha channel is a fourth colour channel which is often used for transparency). I'd recommend adding the alpha channel.

#3 radioteeth   Prime Members   -  Reputation: 1102

Like
1Likes
Like

Posted 19 December 2012 - 02:11 PM

I agree with C0lumbo, except that I would generate the alpha-channel when loading the texture image.. Load the image so you have raw RGB values from it, then set up to create a 4-channel RGBA texture, and then loop through all the pixels in the source image RGB, checking for RGB 255,0,255.. If you encounter pink, set the RGBA to 0,0,0,0, black with zero opacity, this will produce black edges instead of pink ones where your texture should be transparent (if you're using any filtering other than GL_NEAREST when rendering your texture).. Then just make sure you enable GL_BLEND, and your glBlendFunc is set to GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA.

#4 wizardpc   Members   -  Reputation: 282

Like
0Likes
Like

Posted 19 December 2012 - 03:33 PM

Thanks for the replies, So I guess there is no way when rendering it with an OpenGL call. I will do the work when I load the texture. I'm using GLFW's Texture loading, so I'm guessing this will be a bit of work.

#5 wizardpc   Members   -  Reputation: 282

Like
0Likes
Like

Posted 19 December 2012 - 03:51 PM

Thanks for the correct term to google "color key". In my research I found an easy way to do it with a shader. Is it better for me to manipulate the tex data, or is it better to just send the texture to the shader and let it handle the transparency. This would only be for my 2D sprites. And I'm using shaders for all my other rendering in my game.

#6 radioteeth   Prime Members   -  Reputation: 1102

Like
0Likes
Like

Posted 19 December 2012 - 09:47 PM

Use a shader if you want to keep things simple.

#7 wizardpc   Members   -  Reputation: 282

Like
0Likes
Like

Posted 20 December 2012 - 06:24 AM

Use a shader if you want to keep things simple.


I did, and with your advice of using GL_NEAREST, it works perfectly and was very simple like you said. +1




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS