• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Florian22222

Sorting alpha-blended objects

8 posts in this topic

Hi!

I am currently programming a little engine for terrain rendering with trees and plants.
I´ve just built in trees and grass already. But i have a huge problem with alpha blended objects(both grass and trees needs them).

Are there any techniques to sort the object by the distance to the camera so I can draw them from back to front? If, are there any helpfull render states or do i have to do it in vertex shader or something like this?

I hope you can help me, that i can get on with my current work ;)
0

Share this post


Link to post
Share on other sites
You do it on the CPU side - sort your meshes by distance, then issue the draw commands for those meshes in that order
0

Share this post


Link to post
Share on other sites
Thank you!

Ok can you give me a little code snippet how i compute the distance to the camera?
Do i have to use the worldMatrix for each object?

like this:
[CODE]
D3DXVECTOR vObjPos = (D3DXVECTOR) *m_World.41;
D3DXVECTOR3 vDistance = vObjPos - camera.vEye;
float fDistance = length(vObjPos);
[/CODE]

Isn´t this to inefficient for 100 leaves on a tree or 100 grass tufts?

EDIT: How can i do that with the LPD3DXMESH class?
0

Share this post


Link to post
Share on other sites
[quote name='IceBreaker23' timestamp='1325280320' post='4898247']

like this:
[CODE]
D3DXVECTOR vObjPos = (D3DXVECTOR) *m_World.41;
D3DXVECTOR3 vDistance = vObjPos - camera.vEye;
float fDistance = length(vObjPos);
[/CODE]

Isn´t this to inefficient for 100 leaves on a tree or 100 grass tufts?
[/quote]

What's inefficient about it? How long does it take on your hardware?

Computing the lengthSquared might be slightly cheaper as it'll avoid a divide, and you can sort by that just the same
1

Share this post


Link to post
Share on other sites
[quote name='rdragon1' timestamp='1325280541' post='4898249']
[quote name='IceBreaker23' timestamp='1325280320' post='4898247']
like this:
[CODE]
D3DXVECTOR vObjPos = (D3DXVECTOR) *m_World.41;
D3DXVECTOR3 vDistance = vObjPos - camera.vEye;
float fDistance = length(vObjPos);
[/CODE]

Isn´t this to inefficient for 100 leaves on a tree or 100 grass tufts?
[/quote]

What's inefficient about it? How long does it take on your hardware?

Computing the lengthSquared might be slightly cheaper as it'll avoid a divide, and you can sort by that just the same
[/quote]
Or a hidden square root.

Your sorting algorithm matters more than the specific operation. Or, O (n log n) is going to just be faster than O (n^2), for most cases. More than that, premature optimization is a root of evil - Optimize once you've profiled the code and know where the slow-downs are. Otherwise, drive yourself mad, you will. :)
1

Share this post


Link to post
Share on other sites
[quote name='IceBreaker23' timestamp='1325280320' post='4898247']
Isn´t this to inefficient for 100 leaves on a tree or 100 grass tufts?

[/quote]

In general you're probably not going to be able to properly sort the individual leaves of a tree, it's just not practical. It's a lot of items to sort, and more importantly it would mean a draw call per leaf (unless you dynamically generated an index buffer, which brings its own problems). Usually people will just coarsely sort by object and try to minimize the artifacts on things on like leaves (or they just live with them). For certain categories of meshes it's possible to create a pre-sorted index buffer that will always render the triangles of that mesh in back-to-front order with respect to itself. However this won't work for a lot of cases, particularly cases where triangles intersect or overlap. In fact any kind of sorting will [i]always[/i] fail when triangles intersect.

As an alternative, you can try using something other than traditional blending. Such as...[list=1]
[*]Alpha testing
[*]Alpha-to-coverage
[*][url="http://www2.disney.co.uk/cms_res/blackrockstudio/pdf/Foliage_Rendering_in_Pure.pdf"]Post-process blending[/url]
[/list]
There are also methods for achieving order-independent transparency, but in general these tend to be too expensive for games.
1

Share this post


Link to post
Share on other sites
Hello
I'm currently[b] experimenting[/b] an algorithm to sort objects per distance very fastly, here's a description :

The main structure of the sorting object is an hash table ;for instance, let's say the depth range (in view space) is [0,1000], and there are 1000 entries in the hash table : the first entry covers [0,1] depth range, the second covers [1,2] depth range and so on. So when just storing objects , we already presort them in subsets . Each entry of the hash table contains a subset of objects that now must be sorted. I use then std::qsort() on each table entry. (I cut into chunks the entries and put a worker thread on each chunk)

Results : I sort 1.000.000 int objects in 8 ms (average)
CPU : Phenom 4 Cores
RAM : 3 GB
OS : Vista 32 bit
config for this result : 1024 entries in the table, 4 threads (one on each core) (the computation time seems more stable with 8 threads) ; depth range : [0,1000]

(It needed 100 ms for a 'single entry hash table' with one thread ; ie pure std::qsort() sort)

My plan is to sort transparent faces ( I ignore the intersection case and the "A over B ; B over C ; C over A" overlap case )
Today, my transparent faces all use united color so I will build a (pre sized at cooking) unique buffer containing all these faces, so I'd need only one draw call for transparency

But that's just a [i]personal experiment[/i], I think it can work mostly because there's quite few transparent primitives and because they all use the same layout/technique.

I can post/send my code for this if you're interested or just curious

For what it worthes [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Nico
1

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  
Followers 0