Questions about transparents meshes (alpha blending and z order) [solved]

Started by
11 comments, last by riruilo 16 years, 11 months ago
Hi friends. I need your ideas, please. I would like to implement transparent objets in my very small engine, OpenGL of course. I know I should draw firstly all my opaque objects, and after that, my transparent objects. But! from back to front, and that is my problem. How can I implement it. My idea is to take in account just one vertex per one mesh (not every triangles), my mesh class has a GetFirstVertx method. After do a traverse I can use glGetCurrentMatrix to retrieve the currrent matrix when I am in that object, but how can I get Z? If I get that my idea is to implement a method called SetZorder and GetZorder. After get Z, I think it should be easy, just order all my meshes and render them from back to front. Can anyone help me with my implementation questions? Thank you very much. [Edited by - riruilo on May 8, 2007 10:30:08 AM]
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Advertisement
Forget about a so accurate sort. Besides killing your CPU, it still won't give correct results. Solving blend is a per-fragment problem.

If you REALLY need "pixel-perfect" transparency, you should look at order independant transparency and depth peeling.

In general, there are little subsets in which this is necessary. A per-OBB test would be enough.

Previously "Krohm"

First you need a method that gives you the translation from model-space to world-space (the objects position).

Just sort by the distance from the camera to the objects position.
i.e. v = cam.position - obj.position;d = sqrt(v.x*v.x + v.y*v.y + v.z*v.z);then sort on d
btw u dont need to call the sqrt for the above function (esp if youre calling it for a lot of objs)
Just be careful that if you sort by bounds/object centers, then a (large) object can be visible and have its center behind the camera (such as a large window) - in the distance from camera method above this will result in incorrect ordering. You may wish to consider adding testing againt the camera near plane to see if it is behind, and allow your algorithm to sort on negative numbers too.
Thanks for your replies.

But I have 2 questions.

First all, I am Opengl beginner ( and english), maybe my questions are a bit stupid.

I have one arbitrary vertex of my model, in local coordinates, that vertex is transformed by modelview matrix. What operation should I do to get the transformed vertex?

And just one question more. (possibly I am wrong)

Before render my scene, I use glrotate and gltranslate with opposite values to move my world, that is to move the camera, but actually I am moving my world, not my camera. So, should I use this:

v = cam.position - obj.position;
d = sqrt(v.x*v.x + v.y*v.y + v.z*v.z);

Simply I have no camera.

Thanks a lot, I appreciate a lot your help.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Quote:Original post by riruilo
Thanks for your replies.

But I have 2 questions.

First all, I am Opengl beginner ( and english), maybe my questions are a bit stupid.

I have one arbitrary vertex of my model, in local coordinates, that vertex is transformed by modelview matrix. What operation should I do to get the transformed vertex?

And just one question more. (possibly I am wrong)

Before render my scene, I use glrotate and gltranslate with opposite values to move my world, that is to move the camera, but actually I am moving my world, not my camera. So, should I use this:

v = cam.position - obj.position;
d = sqrt(v.x*v.x + v.y*v.y + v.z*v.z);

Simply I have no camera.

Thanks a lot, I appreciate a lot your help.
I would think the easiest approach would be to simply transform the position of each object into camera/view space, and then sort by z value.

All you would really need would be:
depth = dot(object.pos - camera.pos, camera.forward);
You should have your camera position available (since as you state, you're calling glTranslate() with the negative of this vector).

Ideally you should have the forward vector available as well (or be able to derive it), but if you're relying exclusively on glRotate*() this may not be the case.

Another option would be to query the modelview matrix (which at any time represents a transformation from local space to view space), apply it to your object origins, and sort based on the resulting z values.

FYI, this sort of thing is a lot easier if you use an external math library, rather than relying exclusively on OpenGL's transform functions.
Thanks jyk

A math library? for what? for instance....

An other question, I have a transformation matrix and one vertex, what should I do to get the transformed vertex.

Thanks a lot.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Quote:A math library? for what? for instance...
If you're asking why a math library can be useful when working with OpenGL, the answer (in short) is that it allows you to do things that are either difficult or impossible to do solely though OpenGL transform functions.
Quote:An other question, I have a transformation matrix and one vertex, what should I do to get the transformed vertex.
This is a good example :) OpenGL does not provide a means of 'manually' transforming geometry, or querying the geometry data after transformation, but if you have a decent math library available, this becomes quite easy. (You can do it indirectly via OpenGL, but you'd have to query the modelview matrix at the appropriate time, and then apply the transformation 'by hand' to the vertex in question.)
Thanks jyk, you are very helpful for me.



Just one question, I promise this is the LAST.

I have the modelview matrix using glGetMatrix(modelview) and one vertex, how can I apply that transformation BY HAND to that vertex? or which theory should I know ( I´m beginnger)

By the way, I need just that operation so maybe use a library for that is not useful, I think ( maybe I am wrong )

Thanks.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.

This topic is closed to new replies.

Advertisement