Sign in to follow this  
enigmagame

Check if a triangles is visible

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
From your modelview matrix you can extract the normals for the six planes that define your viewing frustum. A triangle that is visible will have at least one point on the same side of all of the planes.

So test each vertex of each triangle against each of the six planes (use a simple dot product) and quit once you find one vertex that lies within the frustum. If no points lie within the frustum then the triangle is not visible.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
From your modelview matrix you can extract the normals for the six planes that define your viewing frustum. A triangle that is visible will have at least one point on the same side of all of the planes.

So test each vertex of each triangle against each of the six planes (use a simple dot product) and quit once you find one vertex that lies within the frustum. If no points lie within the frustum then the triangle is not visible.

look at the topleft of your screen, im sure u can image a triangle there consisting of all 3 points off the screen, but still part of it is visible

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this