sorting floats with index

Started by
20 comments, last by cozzie 11 years, 3 months ago

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

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

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?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement