Mesh Z Order Rendering

Started by
5 comments, last by FirstDouchaEver 17 years, 11 months ago
Hopefully somebody can help me wit this problem. I have a mesh (.x file) of a tree. The branches are made of two triangles and the texture of a branch+leaves are mapped to them. I can see what the problem is but have no idea of how to approach a solution. The problem is with the rendering and the ZOrder. My pixel shader code is as follows: float4 TreePixelShader(VertexShaderInput In) : COLOR0 { float4 RGBA; RGBA = tex2D(BaseTextureSampler, In.TexCoord_XY); if((RGBA.r == 0.0f) && (RGBA.g == 0.0f) && (RGBA.b == 0.0f)) RGBA.a = 0.0f; // Make BLACK transparent return RGBA; }; When rendering the tree leaves, the triangles on the front (near camera for example) are rendered first. The alpha parts (Black on the texture) render pefectly rendering the background. The problem is other leaves or other trees that are rendered after this one. These don't get rendered as the triangle has already plotted the background. I've tried a couple of RenderStates, switching ON/OFF Z Order, but this then affects the main landscape. I'm not sure what other Renderstates do or how to allow these triangles to render last.. the camera wil always move so oder will change is you go behid the tree for example.. I will have hundreds of trees so recalculating every frame would seem to be a nightmare. This link shows a picture of the problem in order to illustrate it better. http://homepage.ntlworld.com/doucha/wedding/uni/tree%20rendering.jpg This is obviously not normally a problem so hopefully there's a simple enough solution to do with Z Buffers and renderstates etc. Any help or advice would be appreciated . Cheers
Advertisement
When doing alpha blending, there really isn't much you can do but sort back to front and render in that order. It would probably be acceptable to just sort by tree location with respect to distance to the camera ...the problem in your screenshot of course would still be there, but greatly reduced.

Another option is to go with alpha testing instead of blending, this would remove the need for sorting but makes objects with alpha more aliased.

If you do want to go with sorting then I have personally gotten very good results with a radix sort of squared distances to objects with alpha.

All the best,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
ergh!

Thanks for that I feared sucha thing - my problem is I have f intite amount to implement this and eveything else for my project - and don't know where to begin.. well I do know now, thanks :)

This I can see would sort ender order of trees - however what about the render order of the triangles within the mesh? Or within an X File.. as the leaves in front would block leaves behind, the same way as my screenshot shows the entire tree blced.. so would I need to sort the triangles of the mesh also?

And one last thing if you don't mind, what do I need to do to do Alpha Testing instead of Blending is it a Renderstate? SOrry I'm only just begining with this stuff really.

Cheers
The render states you need to pay attention to for alpha testing are D3DRENDERSTATE_ALPHATESTENABLE , D3DRENDERSTATE_ALPHAREF and D3DRENDERSTATE_ALPHAFUNC

When blending ...for the actual triangles of the tree mesh that have an alpha texture, yes ideally you should sort those too. A rough sort is all that is needed, just so you can only see the blending problems if you are actually looking for them.

It is also a good idea to only have the alpha blending/testing renderstates enabled when you are rendering polys that have alpha (I am not sure what kind of overhead it would have if you were to leave the renderstates enabled for all your opaque stuff too but i'd imagine it would have enough of an effect that it'd be best to turn the renderstates off).

Rendering lots of alpha polys can quickly destroy your fillrate so for alpha testing it may (profiling is always a good idea in these situations) even be a good idea to sort and draw those front-to-back [wink]

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Thanks alot I shall stay up until all hours getting this sorted, hopefully :(
If you want to avoid sorting and still get the alpha blending look check out this method.
Thanks all for the advice - I used the AlphaTest method and it looks fine to me - I think I can see the antialiasing that you're on about but my tree textures are a bit lame anyway - it's certainly a trade off I can live with and has saved uge amounts of effort and time not having to implement a sort.

Cheers again, you saved my ass on this :)

This topic is closed to new replies.

Advertisement