elect
elect

Reputation: 7190

Rendering two textures with blending and alpha test

what I am looking for is the following... I have a circle on a square image, alpha is 0 at the corners

http://dl.dropbox.com/u/1401029/circle.png

and also a square shadow, alpha is 0 everywhere else

http://dl.dropbox.com/u/1401029/image.png

Well, I would like to have as final result a blending of these two renders, plus tha shadow not being rendered outside the circle

How could I achieve that? :)

Edit: this is my code so far (t2 circle, t1 shadow)

gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
gl.glEnable(GL2.GL_ALPHA_TEST);
gl.glAlphaFunc(GL2.GL_GREATER, 0.1f);
    t2.enable(gl);     
    t2.bind(gl);

    t2.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
    t2.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);

    a = 2.0f;
    gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0, 0);
        gl.glVertex3f(-a, -a, 0);

        gl.glTexCoord2f(0, 1);
        gl.glVertex3f(-a, a, 0);

        gl.glTexCoord2f(1, 1);
        gl.glVertex3f(a, a, 0);

        gl.glTexCoord2f(1, 0);
        gl.glVertex3f(a, -a, 0);
    gl.glEnd();   

    t2.disable(gl);
gl.glDisable(GL2.GL_ALPHA_TEST);

gl.glDisable(GL2.GL_DEPTH_TEST);

    gl.glEnable(GL2.GL_BLEND);
    gl.glBlendFunc (GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);

    t1.enable(gl);
    t1.bind(gl);

    t1.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
    t1.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);

    gl.glBegin(GL2.GL_QUADS);
        gl.glTexCoord2f(0, 0);
        gl.glVertex3f(-a, -a, 0);

        gl.glTexCoord2f(0, 1);
        gl.glVertex3f(-a, a, 0);

        gl.glTexCoord2f(1, 1);
        gl.glVertex3f(a, a, 0);

        gl.glTexCoord2f(1, 0);
        gl.glVertex3f(a, -a, 0);
    gl.glEnd();   

    t1.disable(gl);
    gl.glDisable(GL2.GL_BLEND);
gl.glEnable(GL2.GL_DEPTH_TEST);

But it's not working so far, the shadow get completely rendered on the circle, where am I wrong?

Upvotes: 1

Views: 2252

Answers (1)

Ani
Ani

Reputation: 10896

Very simple, you will need to use multitexturing and modulate the second texture like so. This like lightmapping, so the blending function (before the pixel gets blended with the framebuffer contents) is

output.rgb = Texture1.rgb * Texture2.rgb * 2;
output.a = Texture1.a * Texture2.a;

In Shawn Hargreaves' words,

"The easiest way to think about this operation is that the first texture defines the basic color of the object, which can then be adjusted by the second texture:

  • Where the second texture contains 50% grey, the color is unchanged
  • Where it is darker than 50%, the color is darkened
  • Where it is brighter than 50%, the color becomes brighter
  • If the second texture is not monochrome, the color is tinted accordingly"

This will make the circle texture the "mask" for the second texture (or vice versa).

Hope this helps!

Upvotes: 1

Related Questions