Jump to content

  • Log In with Google      Sign In   
  • Create Account


sorting floats with index


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#21 cozzie   Members   -  Reputation: 1594

Like
0Likes
Like

Posted 09 January 2013 - 04:31 PM

Thanks Bob, I really appreciate your help.

I tried it out right away and it looks like it's working, last steps to get the right data on the right place will definitely work out.

 

Here's the code I have now and the output.

I'll go work on getting the arrays in my class, to prevent all the memory allocation for each rendered frame.

 

Thanks again.

 

	int *order = new int[mNrD3dMeshesBlended]; 
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) order[oc] = oc; 

	int *indices = new int[mNrD3dMeshesBlended];
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) indices[oc] = mMeshIndexBlended[oc];

	int *dists = new int[mNrD3dMeshesBlended];
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) dists[oc] = mD3dMeshes[mMeshIndexBlended[oc]].mDistToCam;

	std::sort(order, order+mNrD3dMeshesBlended, [&](int a, int b) { return dists[a]<dists[b]; });

Unsorted output:

 0 Unsorted: 1 ,dist to cam: 95.6556
1 Unsorted: 3 ,dist to cam: 105.594
2 Unsorted: 5 ,dist to cam: 152.151
3 Unsorted: 7 ,dist to cam: 46.3681
4 Unsorted: 9 ,dist to cam: 39.37
 

Sorted:

Sorted: 4  39
Sorted: 3  46
Sorted: 0  95
Sorted: 1  105
Sorted: 2  152



Sponsor:

#22 cozzie   Members   -  Reputation: 1594

Like
0Likes
Like

Posted 09 January 2013 - 05:07 PM

Yeeeehaaa, all done.

Here's the result:

 

void CD3dscene::SortBlendedMeshes(D3DXVECTOR3 pCamPosition)
{
	for(oc=0;oc<mNrD3dMeshesBlended;++oc)
	{
		mD3dMeshes[mMeshIndexBlended[oc]].mDistToCam = CoordToCoordDist(pCamPosition, mD3dMeshes[mMeshIndexBlended[oc]].mWorldPos);
	}

	int *order = new int[mNrD3dMeshesBlended]; 
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) order[oc] = oc; 

	for(oc=0;oc<mNrD3dMeshesBlended;++oc) mMeshIndexTemp[oc] = mMeshIndexBlended[oc];
	for(oc=0;oc<mNrD3dMeshesBlended;++oc) mMeshDistToCam[oc] = mD3dMeshes[mMeshIndexBlended[oc]].mDistToCam;

	std::sort(order, order+mNrD3dMeshesBlended, [&](int a, int b) { return mMeshDistToCam[a]>mMeshDistToCam[b]; });

	for(oc=0;oc<mNrD3dMeshesBlended;++oc) mMeshIndexBlended[oc] = mMeshIndexTemp[order[oc]];

	SafeDelete(order);
}

 

The only thing I doubt is if I also should make the order int array a class member, since it's created (and released/deleted to) each frame.

What do you think?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS