Write to Memory

Good day,

If I make a blue rectangle with a certain transparency, and a red rectangle on top of it with the same transparency
I get a purple rectangle where the two rectangles are on top of each other.
(See image below)

Now I need the blue and red rectangle without the purple part.
They should have the same transparency. So my tought was to write this to a buffer without transparency, so the blue rectangle isn’t visible.
And write the buffer with the transparency at once to the screen.

Each rectangle is from a Rectangle-class. The Rectangle-Class holds the function Draw.
This function has the following code


glEnable(GL_SAMPLE_ALPHA_TO_MASK_EXT);
glPushMatrix();
glBegin(GL_QUADS);
glColor4ub(penColor1.red(), penColor1.green(), penColor1.blue(), alpha);
glVertex2f(start.rx(), start.ry());

I looked on the internet but couldn’t find any solution.
I have fully supported OpenGL 2.0

Regards
-Tortin

Simply enabling depth buffer test should be enough :
glEnable(GL_DEPTH_TEST);

Be sure to request a depth buffer at the GL context creation.

I’m unclear on whether or not you want the purple region to be blue, red or black.

Depth testing, as ZbuffeR suggested could do the trick. If you plan on using the depth buffer to cull out one of the quads you should draw your quads from nearest to farthest.

You mean you want boolean operations?

http://thebuildingcoder.typepad.com/blog/2009/02/boolean-operations-for-2d-polygons.html

that link may help you

@bardackx
That’s not what I was looking for

@Aeluned
The Purple part should be either blue or red, depending on what’s drawn later.

@ZbuffeR
Still still trying to make it work.


I see this as glass.
If I place Blue glass with Red glass on top of it it become purple.
Now I need stained glass, so the colors are next to each other instead of overlaying.

With depth testing it would work on reverse order only.

You can’t have “blend with black but not with color”.
If you want to simply erase previous color data, just disable blending, and tweak colors accordingly.

@ZbuffeR
That’s why I first need to write to a buffer / variable. After adding all graphics I have a buffer with solid colors.
With the right overlaying colors, and after that write it at once with transparency to the screen so OpenGL could blend the buffer.
Greetings

EDIT:
I read the colors from an XML-file so manually mixing the colors manually is no option

Well great, you are done then. No need for any blending on the buffer.

So it won’t work?

Ok I think something must have been lost in translation.
Let me recap :

  1. draw your rectangle colors without any blending, and no depth test.
  2. glCopyTexImage2D that into a texture
    http://www.opengl.org/sdk/docs/man/xhtml/glCopyTexImage2D.xml
  3. glClear
  4. glColor4ub(255,255,255,alpha);
  5. render textured quad, with texture created in step 2)
  6. swapbuffers. Admire “stained glass” result

Kind of got the problem.
It work at least.
I use multi sampling on my project. So if I need a transparency it draws a part of the pixels.
When I draw 2 graphics with the same transparency the drawn pixels are exact on top of each other.

After all the buffer wasn’t needed at all,
Everyone thanks of their support.
Greetings