Sign in to follow this  
Steve_Segreto

Sorting Renderables front to back

Recommended Posts

Steve_Segreto    2080
Hi there, I want to sort some 3d renderables front to back from the viewer every frame in a 3rd person RPG game engine. Each of the renderables is a large group of triangles and has a 3-d vector for its "center". The camera is a standard 3d camera with a view matrix. I read somewhere that the "easiest" way to sort these renderables is in view space because then I can just sort based on the z-component. If this is the case, how can I move them to view space for the purposes of the sort?

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
If this is the case, how can I move them to view space for the purposes of the sort?
Just transform the object centers by the camera view matrix.

Since it's really only the z value that's of interest, you could also do the following:
float view_space_z = dot(object.center - camera.position, camera.forward);

Share this post


Link to post
Share on other sites
Zipster    2365
If you have a view matrix, then you just multiply the center by said matrix and it will be in view space. Alternatively, if you have a 3D camera, you can use the location and normalized forward vector directly without needing to transform the whole point. The distance is then d = dot(p,F) - dot(C,F), where 'p' is the test point, 'F' is the normalized forward vector, and 'C' is the camera location.

Are you sorting your renderables from front to back to reduce overdraw? Because sorting isn't technically necessary unless they're transparent. Plus large objects close together don't sort very well because they overlap, and the sort is based on a center point that doesn't represent the whole volume very well. Just some things to think about.

EDIT: Damn jyk and his ninjafoo :)

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