Texture blending

Started by Klinenator, November 08, 2010, 02:54:57

Previous topic - Next topic

Klinenator

Im loading textures using slick with this code

selectedTex = TextureLoader.getTexture("PNG",new FileInputStream("/Users/klinenator/Documents/CubeTest/Textures/Selected.png"),GL_NEAREST);

and then later

selectedTex.bind()

how do i blend two textures together so i can have a grass texture then a light color mask over it that will make the grass texture look lighter/darker

jediTofu

//In your OpenGL set up:

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

//Store these variables somewhere:

double red = 1.0; //optionally lower red, green, blue if you want to remove them from texture slightly/fully
double green = 1.0;
double blue = 1.0;
double alpha = 1.0; //1.0 = visible; 0.0 = transparent; 0.5 = half translucent, etc.

//When you draw your texture:

texture.bind();
glColor4d(red,green,blue,alpha); //glColor3f(...) for floats
glBegin(GL_QUADS);
//etc.
glEnd();
cool story, bro

Klinenator

Thank you thats one of the things i wanted to do. How do i make two textures show up on one cube

I have a cube and its grey like stone then i want to overlay some crack marks in it. So i want one texture of just the stone then another of an alpha mask i guess that is just the cracks in the stone

jediTofu

No prob.  I'd assume that you would just draw the cube again but with the different texture:

tex1.bind();
glColor4d(1.0,1.0,1.0,1.0);
glBegin(GL_QUADS);
//...
glEnd(GL_QUADS);

tex2.bind();
glColor4d(1.0,1.0,1.0,0.45);
glBegin(GL_QUADS);
//...
glEnd(GL_QUADS);


Not sure if this is the best way, but worked for me.
You might want to store drawing a cube in an OpenGL display list for speed.  There's some code examples of using a display list on the forums and of course google.
cool story, bro

jediTofu

Also, you might consider drawing the crack marks with OpenGL pixel/line functions instead of using an image...
cool story, bro

Klinenator

Well i want to use it for a bunch of things. Like instead of animating cracks in the cube over time just change the images from 1-10 1 =less cracked 10= more cracked

or for lighting no overlay = bright darker overlay makes the cube darker and darker as time goes on

Klinenator

Might not be the best way but it seems the simplest way

Klinenator

Also i do store them in glCallLists

i initialize the cube in a call list then call it later in the draw function like this

                                treeTex.bind();
                          glCallList(cubeSide);
                          treeTopTex.bind();
                          glCallList(cubeTop);
                          glTranslatef(0,1,0);

this makes a cube with a different texture on the top then moves down one unit and i draw it all over again

Klinenator

With your method arent you technically drawing two cubes for every cube?

jediTofu

Quote from: Klinenator on November 08, 2010, 23:08:09
With your method arent you technically drawing two cubes for every cube?

Yes, but not sure of another alternative, maybe someone will come along and give a more adequate answer.  You could also use Pbuffers or FBOs...you'd basically be drawing the 2 cubes onto a pbuffer or FBO and then drawing that to the display.
cool story, bro

jediTofu

I found this while googling  ;)

http://opengl-doc.com/Sams-OpenGL.SuperBible.Third/0672326019/ch09lev1sec5.html

The only thing is that hardware may not support multitextures (or pbuffers or fbo's for that matter, for my other suggestion).
cool story, bro

Klinenator

If i use an overlay for lighting id have to do it for every cube im not sure but i think it would slow it down drastically to have to draw twice as many cubes. I could be 100% wrong though

jediTofu

Quote from: Klinenator on November 09, 2010, 03:16:20
If i use an overlay for lighting id have to do it for every cube im not sure but i think it would slow it down drastically to have to draw twice as many cubes. I could be 100% wrong though

What about multitextures in the link I provided above?
cool story, bro

Klinenator

Ill have to try and convert that so i can use slick with it

Klinenator

I bind a texture then draw a cube then i want to draw something else after that with just a color and not a texture but it puts the last texture that was bound onto that object

example

dirtTex.bind();
glCallList(cubeSide);
grassTex.bind();
glCallList(cubeTop);
glCallList(cubeBottom);

that last section would also have the grass texture