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

## Recommended Posts

riruilo    218
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]

##### Share on other sites
Krohm    5031
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.

##### Share on other sites
Hodgman    51336
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

##### Share on other sites
zedz    291
btw u dont need to call the sqrt for the above function (esp if youre calling it for a lot of objs)

##### Share on other sites
Rompa    307
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.

##### Share on other sites
riruilo    218

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.

##### Share on other sites
jyk    2094
Quote:
 Original post by riruiloThanks 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[i] = dot(object[i].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.

##### Share on other sites
riruilo    218
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.

##### Share on other sites
jyk    2094
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.)

##### Share on other sites
riruilo    218
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.

##### Share on other sites
jyk    2094
Quote:
 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)
To multiply a vector by a matrix (representing an affine transform) using OpenGL conventions:
x' = m[0] * x + m[4] * y + m[8] * z + m[12];y' = m[1] * x + m[5] * y + m[9] * z + m[13];z' = m[2] * x + m[6] * y + m[10] * z + m[14];
To learn more about what's going on here, Google 'matrix math', 'vector math', 'matrix multiplication', 'matrix vector multiplication', and similar terms. Or, just consult a good reference on linear algebra.
Quote:
 By the way, I need just that operation so maybe use a library for that is not useful, I think ( maybe I am wrong )
The more 3-d programming you do, the more difficult it will become to get by without a math library. However, you can probably squeeze by without one for the time being if what you're doing isn't too involved.

##### Share on other sites
riruilo    218
Thanks. Problem solved.

##### Share on other sites
riruilo    218
Hi friends! It works nice. Thanks a lot.

But just one thing:

I only use
glGetMatrix(modelview) and one vertex to calculate a transformed vertex with

z' = m[2] * x + m[6] * y + m[10] * z + m[14];

I dont use any camera position, because there is no camera in opengl, and when aI transform my "virtual" camera, its transformations are stored in modelview, so I didnt use:

depth[i] = dot(object[i].pos - camera.pos, camera.forward);

A screeshoot, look at lights and window panes (glass):

## Create an account

Register a new account

• ### Similar Content

• I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.

• There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window.
I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.

Thanks
• By cebugdev
hi all,

i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
thank you, and looking forward to positive replies.

• I have a few beginner questions about tesselation that I really have no clue.
The opengl wiki doesn't seem to talk anything about the details.

What is the relationship between TCS layout out and TES layout in?
How does the tesselator know how control points are organized?
e.g. If TES input requests triangles, but TCS can output N vertices.
What happens in this case?
http://www.informit.com/articles/article.aspx?p=2120983
the isoline example TCS out=4, but TES in=isoline.
And gl_TessCoord is only a single one.
So which ones are the control points?
How are tesselator building primitives?
• By Orella
I've been developing a 2D Engine using SFML + ImGui.
Here you can see an image
The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine.
I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui.
3D Editor preview
But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
If you can provide code will be better. And if you want me to provide any specific code tell me.
Thanks!

• 14
• 20
• 35
• 13
• 39