Jump to content
• ### What is your GameDev Story?

• Advertisement

# Check if a triangles is visible

This topic is 3827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

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

#### Share this post

##### Share on other sites
Might it be sufficient just to examine the normal of the triangle to determine if it's back facing?

#### Share this post

##### Share on other sites
Quote:
 Original post by enigmagameHi 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

##### Share on other sites
Quote:
 Original post by V-manWhy 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

##### 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

##### 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

##### 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

##### Share on other sites
Quote:
 Original post by Lord_EvilEspecially doing occlusion culling for single triangles using occlusion queries is wasteful

I'm sure he already knows that.

#### Share this 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

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 10
• 11
• 13
• 9
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634089
• Total Posts
3015423
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!