Help with blending puzzle blocks

Started by
14 comments, last by tacostev 13 years, 11 months ago
Hi all. I've been struggling with this blending issue for a little while and I am sure you guys can point me in the right direction. I'm currently building a puzzle game with different colored blocks. I want to color each block with a different color and then blend in a texture. I can do a glBlendFunc(GL_ONE, GL_ONE) with a solid color and the texture but it's not quite the effect I want. What I'd really like do is color the polygon with a solid color and then throw the texture on top of that as a decal (imagine the texture is a drawing of a four screws or bolts). I think what I need to do is have the two textures, the actual texture and it's mask. With that I could do something like:

glBlendFunc(GL_ONE,GL_ZERO);

//draw the solid color to the verts.

//Load the mask texture
glBlendFunc(GL_DST_COLOR, GL_ZERO);

//draw the mask by blending the source with the destination (white maintains the original color, black removes it)

//Load the actual texture
glBlendFunc(GL_ONE, GL_ONE);

//draw the texture normally


Am I going about this in the right way or am I completely off? I feel like decaling models that are already colored (or even textured) is a pretty common thing but I haven't seen any solid posts on how to do it.
Advertisement
If you want to decal a colored object then blending is not really what you want to be messing with. Blending is for transparency or combining objects from different draw calls.

It is certainly possible to have a texture apply on top of a colored object. Do you have any familiarity with using shaders? I don't have much memory of how alpha textures work in the old fixed function pipeline, but I'm certain that it should be possible. I think if you just use the alpha channel of your decal texture it might do what you want by default, but you might need to enable it with some other state variable that I can't remember.

Make the alpha zero in places where you don't want a texture, and have a value in places where you want your texture to appear. This is essentially the "mask" texture you mentioned, although you can combine it into the fourth channel of the original texture instead of having a second texture.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Try this: GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
@Karwosts I'm kind of curious about this. Does a texture contain alpha data on a per pixel level as well as color data? For instance in a png? I know I can apply an alpha to each vertex of the texture but I wasn't sure if the image itself stored alpha. I'm using the same masking texture NeHe technique from a few years ago so I'm willing to try something new.

Haven't done anything with shaders yet, I should mention this project is using OpenGLES on an IPhone so that limits me in various ways.


@szecs Would this do what I'm looking for with one glDrawArrays call, one glTexCoordPointer call, and one glColorPointer call? That would be incredible.

Do either of you know of a good texture editor I should be using? Thanks for the responses!
No, two passes. First with color and no texture, than with texture. How many tiles do you want to draw? I mean are you sure the performance is an issue?

Draw all the non-textured colored tiles in the first pass, than draw the textured tiles in the second pass.

_____________

I use Photoshop 7
I'll be rendering up to 6 rings of tiles, 20 per ring, so 120 tiles as most. If that's a problem with 2 or 3 passes per tile I can get it down to half of that by rendering only tiles on the top hemisphere (my block structure creates a circle with only the top half on screen at a time).

Currently with 1 to 2 passes it runs fine so I'm not worried about this being a performance issue at it at all.
Er.. did you try that I suggested? It seems that you already use the two pass thing with wrong blending mode. So it would be 2 seconds to modify.
Oh sorry, I'm a work and I can't try it out. I'm posting to get some idea things to try when I get home. I'll post here when I get the results.
Yes, you can store the alpha per-pixel inside the texture. I don't know exactly how to create this in whatever image software you're using, but if you have two separate images (3 channel color + mask) you can combine them in CPU before you upload the data.

Instead of calling glTexImage2D with a GL_RGB type, you will want a GL_RGBA type (four channels). Then instead of uploading (RGB)(RGB)(RGB) pixel data, you'll upload a fourth data component per pixel (RGBA)(RGBA)(RGBA).

Unfortunately I can't remember at the moment how fixed pipeline treats the alpha component, if it uses it to cause transparency, or if it controls the texture blending.


[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Sorry, my bad.

Anyway do you know this alpha layer stuff? Or texturing and blending are new to you?

This topic is closed to new replies.

Advertisement