LWJGL set transparency color? - opengl

I've been playing with LWJGL a little, as a bit of a step up from Pygame. I'm trying to render a sprite and I was wondering if LWJGL has a function similar to Pygame's colorkey that lets you define a color in an image that will be rendered as transparent. Do you have to use an alpha channel in OpenGL?

OpenGL doesn't have any built in color keying support. You'll need to either manually swap your key for alpha on the CPU, or use a custom shader that replaces it on the fly.

Related

make background of sprite transparent in SDL

How to change background color of sprites to transparent by changing alpha after loading it to a SDL_Surface. Are there any functions in SDL which use a floodfill kind of algorithm and change all pixel with a given color to transparent on the outside. I don't want it to happen inside the border of the sprite if the same color is used.
Sample Image:
I would like to make the background blue here transparent before I blit it on the window surface using SDL_BlitSurface.
Only a color key (SDL_SetColorKey()) or a full alpha channel are going to help you here.
Note, that you can provide an alpha channel in your source graphics if you use a format such as PNG. If you only sometimes need/want an alpha, then provide the alpha channel in your source graphics and use SDL_SetSurfaceBlendMode() with SDL_BLENDMODE_NONE to blend without the alpha and SDL_BLENDMODE_BLEND to blend with it.
Both SDL_Surface and SDL_Texture support SDL_BlendMode.
Even if SDL provided another method, such as the fill you mentioned. You wouldn't want to use that. It is more difficult, expensive, and unnecessary overhead. You should stick with best practices here.
You may want to look into SDL_Texture and SDL_Renderer and switch to using a "texture atlas" instead of individual surfaces/textures for each image to maximize performance.

How do I make my object transparent but still show the texture?

I'm trying to render a model in OpenGL. I'm on Day 4 of C++ and OpenGL (Yes, I have learned this quickly) and I'm at a bit of a stop with textures.
I'm having a bit of trouble making my texture alpha work. In this image, I have this character from Spiral Knights. As you can see on the top of his head, there's those white portions.
I've got Blending enabled and my blend function set to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
What I'm assuming here, and this is why I ask this question, is that the texture transparency is working, but the triangles behind the texture are still showing.
How do I make those triangles invisible but still show my texture?
Thanks.
There are two important things to be done when using blending:
You must sort primitives back to front and render in that order (order independent transparency in depth buffer based renderers is still an ongoing research topic).
When using textures to control the alpha channel you must either write a shader that somehow gets the texture's alpha values passed down to the resulting fragment color, or – if you're using the fixed function pipeline – you have to use GL_MODULATE texture env mode, or GL_DECAL with the primitive color alpha value set to 0, or use GL_REPLACE.

How to use a complex OpenGL as background in QGraphicsScene?

I'm trying to create a display with a complex OpenGL image and some spinboxes on the image. Using http://doc.qt.digia.com/qq/qq26-openglcanvas.html I'm able to have a two layers object (inheriting from QGraphicsScene) with a simple OpenGL image as background and the controls on foreground.
So, now I'm trying to display my true OpenGL image as background. This image is created by:
A quad mapped on a structure,
Some small 2D objects represented by 2D textures with alpha channel and specific shaders, drawn on the quad (upper z value)
Some polylines.
With this image I have some strange behavior. The 2D textured objects are drawn with a white background. Some experiments seem to indicate that, in the drawing of this complex OpenGL image the alpha channel is disabled.
I tried different configurations for the QGLWidget used as viewport of the QGraphicsView but without result.
So I need help to be able to create this OpenGL image with the right transparency effects.

Problem with blending in openGL (Color bar example)

Can anyone give some clues as to why when I try to render the color bar quad below
It appears like this:
Here is my rendering code:
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_ONE, GL.GL_ZERO);
gl.glBlendEquation(GL.GL_FUNC_ADD);
gl.glEnable(GL.GL_ALPHA_TEST);
gl.glAlphaFunc(GL.GL_GREATER, 0.01f);
// do the drawing...
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glDisable(GL.GL_ALPHA_TEST);
I'm sure the solution is simple and I'm just having a brainfart but it's just one of those days!
Thanks for the help!
What kind of blending are you trying to perform? To simply draw something without any color mixing or alpha channels you don't even have to play around with GL_BLEND or GL_ALPHA_TEST (leave both disabled). GL_BLEND is used to define how to add different "layers" of color (usually on how to apply alpha channels) while GL_ALPHA_TEST decides what alpha values to respect/ignore. Also check your vertex colors when rendering the quad (try to render a unicolor quad without texture, e.g. using magenta).
However looking at your images I'd guess you somehow disabled drawing to your red color channel (glColorMask()) - although there's yellow, which confuses me.
There was a problem with RGBA getting swapped around when I imported the PNG file.

Making 3d Models Transparent in OpenGl and OpenFrameworks

I'm using c++ OpenFrameworks and ofx3dModelLoader to load in 3ds models that I've already created. This is working great. However I would like to smoothly transition models visually as the enter the frustum's back plane. I have fog working but would really like to fade the models in from transparency.
Is there any way to do this either through openframeworks or opengl?
You could handle this in the fragment shader. Simply change the output of the alpha on the output colour to 0.0.
You can change the alpha component of the texture applied to the objects. You might also search to see if there's a way to change the alpha component of a texture/object without redefining the entire texture.
I am not 100% sure how the 3ds loader works, but with most openframeworks drawing operations, you can alter the transparency of the drawing by setting ofSetColor();
In your case, you would set to (255, 255, 255, x) where x is the amount of transparency that you wanted to achieve.