OpenGL Color Blending

So I’ve almost finished my tile based colored lighting system for my game, but I’m having an issue when using the color blending.

To set the RGB levels for each tile, I use the following code which is based off my own ‘Buffer Object’ class:

(‘bo’ is an instance of the buffer object class)


bo.bindImage(i);
bo.setDepth(Tile.yPosToDepth(y) - 1.0f); //This line is not important to this issue (At least I don't think it is!)
bo.setRGB(r0, g0, b0);
bo.setUV(txmax, tymin);
bo.addVertex(x + tileSize, y + tileSize);
bo.setRGB(r1, g1, b1);
bo.setUV(txmin, tymin);
bo.addVertex(x, y + tileSize);
bo.setRGB(r3, g3, b3);
bo.setUV(txmin, tymax);
bo.addVertex(x, y);
bo.setRGB(r2, g2, b2);
bo.setUV(txmax, tymax);
bo.addVertex(x + tileSize, y);

This gets compiled exactly as you’d expect it to into a VBO. However the colour blending is not working properly when I use textures as shown in this image:

As you can see, the blue only shows up on the grey texture, not on the green.
However when I replace all my tiles with white textures I get the following result:

So the colour blending is working correctly!??!

Please note that green and blue lights are working completely fine, it just seems to be an issue with the blue channel.

Here are my texture loading parameters if it is of any help:


GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, b);

Also note, that GL_BLEND is enabled, in the images, the player’s shirt is green but that is still blending correctly and the random squares are rain, so just ignore them!

Thanks in advance! :smiley:

What blend func are you using?


GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

Is this the wrong one?

Looks correct. Are you sure your glow/light texture isn’t being hidden by the depth test?

Is it a problem with one channel being not represented even slightly so it doesn’t get shown? Instead of straight blue do something like vec3(0.02, 0.02, 1.0) and see what you get.

What order do you draw your objects on to the screen? I suspect you have an issue here

Depending on the order you draw things in the source and destination colors will be different

[quote]Is it a problem with one channel being not represented even slightly so it doesn’t get shown? Instead of straight blue do something like vec3(0.02, 0.02, 1.0) and see what you get.
[/quote]
It seems that the closer the other channels get to 0, the less visible the lighting effect is on non-grey textures. I found when making light colour: (0.3f, 0.3f, 1.0f) in RGB, the light showed up somewhat, but the blue didn’t seem to be blending with the green.

[quote]What order do you draw your objects on to the screen? I suspect you have an issue here

Depending on the order you draw things in the source and destination colors will be different
[/quote]
I render all of the tiles on the map, then the entities. But I don’t see why this should make a difference.

[quote]Looks correct. Are you sure your glow/light texture isn’t being hidden by the depth test?
[/quote]
There is no texture for a light, it is simply represented as a color offset to the original texture of the tile.

I render all of the tiles on the map, then the entities. But I don’t see why this should make a difference.
[/quote]
Have a look at http://www.openglsuperbible.com/2013/08/20/is-order-independent-transparency-really-necessary/, you will see what I mean by the order of things. The blend function you have is order dependant. Which order do you draw your grey / green and blue tiles?

I’m still failing to see how this makes a difference, as the color gets applied directly as a color offset to the texture as if it were written like this:


GL11.glColor4f(r, g, b, 1.0f);
GL11.glVertex2f(x, y);

The article you linked discusses the ordering of physical geometry in the scene whereas the light itself is just an applied color offset