Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Sorting transparent objects, qsort()

  1. #1
    Intern Contributor
    Join Date
    Feb 2000
    Location
    Bremen, Bremen, Germany
    Posts
    58

    Sorting transparent objects, qsort()

    Hi !

    I want to draw billboarding bitmap trees. Their background is
    transparent, so I need to sort them to erase the visual artifacts
    that would appear otherwise. To draw them from front to back, I
    need to know their distance from the viewer. If I have 100 trees,
    I need 100 square roots each frame. Not good. This is my first
    problem.

    After I calculated the distances from the viewer, I want to sort them
    so that they are drawn in correct order. I want to use the qsort()
    function for this. This is my second problem. I can't get qsort() to
    accept my callback function:

    int CTreeCollection::Compare(const void *arg1, const void *arg2)
    (My compiler is VC++ 6.0)

    I always get the error "int (const void *,const void *)" can't be
    converted in int "(__cdecl *)(const void *,const void *)". I have no
    idea how to solve this. How should my callback function look ? How shold I
    pass the callback function to qsort() ?

    Can anyone help me to speed up / avoid those square roots every frame ?
    Does anyone know how to use qsort() ?
    Someone out there with a better idea to do all this stuf ?

    Tim

  2. #2
    Member Regular Contributor
    Join Date
    Feb 2000
    Location
    milano, italy
    Posts
    282

    Re: Sorting transparent objects, qsort()

    hi tcs!
    the qsort()!
    i had the same problem when i first tried qsort() for object depth sorting.
    i also tried to make the compare function a member of a class...

    well, you can't make it a member, but that's not a problem.

    the compare function must be a global function, like printf(), to say.

    this is my compare function layout:

    Code :
    #define		SLOT(s)		((Tdmyentityslot *)s)
    //-----------------------------------------------------------------------------
    int __cdecl depthsortfunc( const void *a, const void *b ) {
    	if( SLOT(b)->compare == SLOT(a)->compare ) return 0;
    	if( SLOT(b)->compare >  SLOT(a)->compare ) return 1;
    	return -1;
    }
    #undef		SLOT
    about depth sorting, yes you need a depth value, but there's no need to sort real distances from the camera.
    all you really need is a value related to distance, so simply avoid the sqrt.
    just use the squared distance and everything will work just fine.

    Dolo/\/\ightY




    [This message has been edited by dmy (edited 02-23-2000).]
    Dolo/\/\ightY

  3. #3
    Advanced Member Frequent Contributor cass's Avatar
    Join Date
    Feb 2000
    Location
    Austin, TX, USA
    Posts
    913

    Re: Sorting transparent objects, qsort()

    tcs,

    First thing is, you can sort on distance^2 rather than distance and avoid the square root operation.

    Second, I'd consider using the C++ standard library sort over qsort() since it could probably inline the comparator and avoid a lot of function call overhead.

    For more information on the C++ standard library sort, see:
    http://www.sgi.com/Technology/STL/sort.html

    Hope this helps -
    Cass
    Cass Everitt -- cass@xyzw.us

  4. #4
    Intern Contributor
    Join Date
    Feb 2000
    Location
    Bremen, Bremen, Germany
    Posts
    58

    Re: Sorting transparent objects, qsort()

    Thanks, it works...
    I made it a global function, I already thought this was the reason. The idea with not calculating the distance, but calculating just some kind of sorting value works also great.

    Thanks !

  5. #5
    Junior Member Regular Contributor
    Join Date
    Feb 2000
    Posts
    122

    Re: Sorting transparent objects, qsort()

    hi tcs

    Please could you tell me, how I can get the distance of an object/vertex from the viewer after all that rotations and translations you normally do in a 3D-world with OGL?

    Thanx in advance

  6. #6
    Intern Contributor
    Join Date
    Feb 2000
    Location
    Bremen, Bremen, Germany
    Posts
    58

    Re: Sorting transparent objects, qsort()

    hi !

    simply calculate it ;-))
    There's no other way since opengl is a one way pipeline. I use the described method without square roots, just for depth comparison. If you want't to get the distance, use pythagoras. If you do some rotation stuff, you have to calc that, too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •