Check if a triangles is visible

Started by
10 comments, last by zedz 15 years, 9 months ago
Hi to all! The topic title is my question. For example, if the model is a cube and the camera is in front of this model, the only two triangle visible are the two triangles facing the camera. I would a method where i pass the triangles vertices, and he return true if the triangle is visible, false otherwise. The cube is a simple model, this method must be applied to a generic model, for example a teapot. Thanks for the help!
Advertisement
There is not an easy solution for this problem AFAIK as a triangle might be entirely obscured except for a single pixel, you can use GL_ARB_occlusion_query but of course you should cull away most of the triangles before doing this. Also you would have to draw your geometry front to back.

Hope this helps.
Might it be sufficient just to examine the normal of the triangle to determine if it's back facing?
Quote:Original post by enigmagame
Hi to all!
The topic title is my question. For example, if the model is a cube and the camera is in front of this model, the only two triangle visible are the two triangles facing the camera.
I would a method where i pass the triangles vertices, and he return true if the triangle is visible, false otherwise.
The cube is a simple model, this method must be applied to a generic model, for example a teapot.
Thanks for the help!


Why do you want to know if a triangle is not facing the viewer?
Anyway, what you need to do is compute face normals for each triangle. Transform the normal by the inverse transpose of the view (modelview) matrix.
Do a dot product with {0, 0, 1}
if result<=0 then triangle is not facing viewer.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by V-man
Why do you want to know if a triangle is not facing the viewer?

In reality I want know if a triangle is visible in a current camere view. There aren't triangles outside the camera frustum, but a triangle can be occlued or partially occluded, by other geometry (other triangles), in this case the triangle isn't visible or is partially visible.
So if the model is a cube and the camera is in front of this model, I can see only two triangles, otherwise if the camera is rotated and increased (in height) I can see six triangles.
Then what you want is what Mr. Cableguy suggested: GL_ARB_occlusion_query. He didn't mention that this extension and its counterparts will indicate the number of pixels drawn (meaning if only one pixel from the triangle made it to the final scene you'll know about it).

Quote:
Also you would have to draw your geometry front to back.


Hmm, now I'm questioning my memory...so if a number of pixels from the triangle are rendered successfully, but then overwritten in the color buffer later (hence fully occluding the object), the extension doesn't pick up on it?
Quote:Original post by isenthalpic53
Then what you want is what Mr. Cableguy suggested: GL_ARB_occlusion_query. He didn't mention that this extension and its counterparts will indicate the number of pixels drawn (meaning if only one pixel from the triangle made it to the final scene you'll know about it).

Quote:
Also you would have to draw your geometry front to back.


Hmm, now I'm questioning my memory...so if a number of pixels from the triangle are rendered successfully, but then overwritten in the color buffer later (hence fully occluding the object), the extension doesn't pick up on it?


Occlusion queries return the number of fragments that get to screen. If you want to know if an object is hidden, you have to render other stuff in your scene first, then render that particular object.

And if you do the Query test, you shouldn't query for the result immedialty. You should query on the next frame. The Query test happens in parallel.
And if you are going to query too often, like every frame, it might even drag down performance. You might want to query every 5 frames or something.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Just to chime in here: Maybe I didn't read the posts carefully enough but I have the impression that he wants something on the lines of backface culling ("which triangles don't face the camera?") and/or occlusion culling ("triangles partially visible").

As stated before, occlusion culling can be done via hardware occlusion queries or doing a hierarchical z-test manually.

Backface culling is done by OpenGL if enabled but could also be achieved by analyzing the (projected) normal of the face, i.e. if the projected normal's z-component is zero or positive (pointing away from the camera) the triangle isn't visible.

The question for enigmagame is: WHY do you need this information? If it's for culling you should let OpenGL do the job. Especially doing occlusion culling for single triangles using occlusion queries is wasteful since this is already done by the z-test. If using occlusion queries you should use a simple representation like a cube and cull the entire object if the query fails.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Quote:Original post by Lord_Evil
Especially doing occlusion culling for single triangles using occlusion queries is wasteful


I'm sure he already knows that.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by Lord_Evil
......I have the impression that he wants something on the lines of backface culling ("which triangles don't face the camera?")



Maybe Ive got the wrong idea but he doesnt say in his post that he actually wants that information. He only wants to know which triangles are visible.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!

This topic is closed to new replies.

Advertisement