0

I'm on an OpenGL project.

I have some objects (just say 2) made of the same transparent material (alpha = 0.2, for example). The two objects intersect.

How can I make the intersection part look the same as other part (without border, no different color), so the too objects will look like as one?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Huynh
  • 111
  • 1
  • 10

1 Answers1

1

I'm not sure if you would really want to do it. I will answer anyway, but first let me tell you why I think you don't want that.

In real life, imagine a red stained glass and a blue one. If you look at them in a way that they partially overlap, the overlapping part clearly has a different color (purple). If you get 2 red glasses and look at them so that they have overlap, the overlapping part is more red. That's exactly what is happening in your OpenGL program.

Now in general, when you have multiple transparent objects, you need to sort them based on their distance from your eye and the direction you are looking at. Then you draw them from farthest to closest. This is not a simple task by itself! Think of 2 objects that cross.

One way of achieving what you want is to sort the transparent objects, but draw from closest object to the farthest. This way, you practically don't allow transparency on the same pixel to be done twice. Not a good idea.

Another way would be to do something very specific to these objects of special kind. I say special kind because apparently two of them overlapping doesn't make any changes! You can do what you want by drawing to the stencil buffer instead of the draw buffer, then draw a rectangle with the color you want over the whole screen, but matching only that stencil.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Thank you for your quick response, Shahbaz. I am modeling the cell division process [link]http://stackoverflow.com/questions/10393433/opengl-cell-division-effect . The way I am doing it is to create two identical cells then move them far apart. So I really want that when the two cells are not separated, they look as one. User should be able to move around the scene, so the first option seems hard to follow. Could you please give a link where I can try out the second option (using stencil buffer)? – Huynh May 01 '12 at 13:27
  • Anyone suggest other approach to model the cell division process or similar processes involving object replication? – Huynh May 01 '12 at 16:01
  • I learned stencil buffers from the book "The OpenGL bible", so I don't know which sources on the internet are good, but I can tell you, there are a LOT of them. Just google it and you should be able to find something good. – Shahbaz May 01 '12 at 22:11
  • Also, why are your cells transparent? – Shahbaz May 01 '12 at 22:11
  • So you can look inside them and see their organs. – Huynh May 02 '12 at 02:29
  • If a cell is on top of another, by all means the overlapping part should have a different color. It would all seem unreal otherwise. From your other question, my guess is that you are having problem with cell division. I am guessing your cell division has overlapping parts that create this artifact. If that is true, my suggestion is to work on your cell division so cells wouldn't actually overlap (which is the case in reality) – Shahbaz May 02 '12 at 13:46