View Full Version : Write to Memory

04-14-2010, 01:41 AM
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

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



04-14-2010, 09:18 AM
Simply enabling depth buffer test should be enough :

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

04-14-2010, 10:19 AM
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.

04-14-2010, 05:12 PM
You mean you want boolean operations?


that link may help you

04-15-2010, 12:49 AM
That's not what I was looking for

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

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.

04-15-2010, 01:21 AM
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.

04-15-2010, 02:21 AM
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.

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

04-15-2010, 02:32 AM
Well great, you are done then. No need for any blending on the buffer.

04-15-2010, 02:41 AM
So it won't work?

04-15-2010, 02:59 AM
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
3) glClear
3) glColor4ub(255,255,255,alpha);
4) render textured quad, with texture created in step 2)
5) swapbuffers. Admire "stained glass" result

04-15-2010, 05:09 AM
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.