Jump to content
  • Advertisement
Sign in to follow this  
juxie

DirectX index buffer

This topic is 3781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have been trying to retrieve the triangle indices using index buffer to no avail. This is what I have tried.
std::ofstream a(L"test.txt");

LPDIRECT3DINDEXBUFFER9 pIndBuf;
if (SUCCEEDED(g_pMesh->GetIndexBuffer(&pIndBuf))) {
	DWORD numFaces=g_pMesh->GetNumFaces();
	WORD pInd = (WORD) malloc(numFaces*3*sizeof(WORD));
	
	if (SUCCEEDED(pIndBuf->Lock(0,numFaces*3*sizeof(WORD),(VOID **) &pInd,D3DLOCK_DISCARD))) {
		for (int i=0;i<numFaces;i++) {
			for (int j=0;j<3;j++) {
				a << pInd << " ";
				pInd++;
			}
			a << "\n";
		}
		pIndBuf->Unlock();
	}
	pIndBuf->Release();
}


Hope someone could help me on this. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hi,

You didn't tell what kind of (compile) errors you have encountered or where the program fails. I can comment the code a little.



std::ofstream a(L"test.txt");

LPDIRECT3DINDEXBUFFER9 pIndBuf;
if (SUCCEEDED(g_pMesh->GetIndexBuffer(&pIndBuf))) {
DWORD numFaces=g_pMesh->GetNumFaces();

WORD pInd = (WORD) malloc(numFaces*3*sizeof(WORD));

//I think that this line doesn't compile. If it would, you'd end up having
//half of a pointer. pInd should be of type WORD*. I also recommend using
//new instead of malloc. It is clearer. WORD *pInd = new WORD[...];
//One question remains : Why do you allocate memory here?
//Locking an index buffer will give you a pointer to the content of the
//index buffer, you don't need and must not allocate memory for it.
//The pointer to the index buffer content will be stored in pInd. That is the reason it why
//it requires a pointer to a pointer.


if (SUCCEEDED(pIndBuf->Lock(0,numFaces*3*sizeof(WORD),(VOID **) &pInd,D3DLOCK_DISCARD))) {

//Definetely not should you use D3DLOCK_DISCARD if you plan to read the buffer.

for (int i=0;i<numFaces;i++) {
for (int j=0;j<3;j++) {
a << pInd << " ";

//The line should be 'a << *pInd << ";"' after changing the pInd to be a pointer

pInd++;
}
a << "\n";
}
pIndBuf->Unlock();
}
pIndBuf->Release();
}






Otherwise, please write back if you encounter more problems.

Best regards!

Share this post


Link to post
Share on other sites
Sorry for not explaining clearly.
I was trying to retrieve the index buffer of the mesh so that i can get the vertex of each triangle for collision detection purposes.
But while executing the code and dumping the indices to a file, i didn't actually get the correct index at all.
I want to do a collision detection against each triangle in the mesh in this case.
How should I go about getting the info using index buffer?

Thanks

Share this post


Link to post
Share on other sites

Hi,

Well I presented the corrections to the code.

Here's some clarifications:



WORD *pInd = NULL; //don't allocated memory here




For the index buffer locking, I think that D3DLOCK_READONLY would do the thing.

With these modifications, what is the output?

For collision detection purposes, you should make a separate copy of the mesh data to system memory. That is, you shouldn't use D3D buffers for containing the data used for collision detection.

Best regards!

Share this post


Link to post
Share on other sites
Quote:
Original post by kauna

Hi,

Well I presented the corrections to the code.

Here's some clarifications:

*** Source Snippet Removed ***

For the index buffer locking, I think that D3DLOCK_READONLY would do the thing.

With these modifications, what is the output?

For collision detection purposes, you should make a separate copy of the mesh data to system memory. That is, you shouldn't use D3D buffers for containing the data used for collision detection.

Best regards!


Hi,

Yes it does work correctly now. Thanks.
One thing though, you mentioned about making separate copy of the mesh data to system memory, do you mean CloneMesh function?

One more thing, how do I access the vertex using the above index buffer?

Which one is better, to use D3DXIntersect to check collision detection against the mesh or retrieving the index buffer and then perform a collision detection algorithm (assuming I created one correctly)?

Share this post


Link to post
Share on other sites
Quote:
Original post by juxie
One thing though, you mentioned about making separate copy of the mesh data to system memory, do you mean CloneMesh function?
No, nothing to do with Direct3D. Generate or load your mesh data and then store a copy twice - the key point is that graphics data != physics data. Trying to tie the two together might initially seem sensible, but really its just not [smile]

Quote:
Original post by juxie
One more thing, how do I access the vertex using the above index buffer?
Well the index buffer is just that - a buffer of array indices into the vertex buffer. Lock it and step through accordingly [wink]

Quote:
Original post by juxie
Which one is better, to use D3DXIntersect to check collision detection against the mesh or retrieving the index buffer and then perform a collision detection algorithm (assuming I created one correctly)?
It depends on your requirements. D3DX provides a functional if inefficient solution with minimal code on your part. Rolling your own requires more investment from you (in all areas - design, implementation, testing and support) but you stand to gain greater rewards. For example you could make it more optimal via some sort of heirarchical algorithm...

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Quote:
Original post by juxie
One thing though, you mentioned about making separate copy of the mesh data to system memory, do you mean CloneMesh function?
No, nothing to do with Direct3D. Generate or load your mesh data and then store a copy twice - the key point is that graphics data != physics data. Trying to tie the two together might initially seem sensible, but really its just not [smile]

Quote:
Original post by juxie
One more thing, how do I access the vertex using the above index buffer?
Well the index buffer is just that - a buffer of array indices into the vertex buffer. Lock it and step through accordingly [wink]

Quote:
Original post by juxie
Which one is better, to use D3DXIntersect to check collision detection against the mesh or retrieving the index buffer and then perform a collision detection algorithm (assuming I created one correctly)?
It depends on your requirements. D3DX provides a functional if inefficient solution with minimal code on your part. Rolling your own requires more investment from you (in all areas - design, implementation, testing and support) but you stand to gain greater rewards. For example you could make it more optimal via some sort of heirarchical algorithm...

hth
Jack


Hi,

Sorry for being a little slow in understanding it.
I am still not very sure how I should go about doing them.

First, by storing copy twice, do you mean I have 2 copies of vertex data?
For index buffer, do you mean that the index buffer does store the vertex data as well?
Also I think I need to divide the mesh into subsets so that I won't perform collision detection against all the triangle faces.
What should I do?
Can I divide the mesh into subsets and the create bounding boxes for each subsets?

Should I do such as suggested in http://www.mvps.org/directx/articles/mesh_subset.htm?
And then for each subset, I create additional bounding box?

Hope you could advise.

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by juxie
First, by storing copy twice, do you mean I have 2 copies of vertex data?
Yes. This data should be completely and totally seperate from your graphics resources. In fact, there is no reason why it has to be stored as a triangle list, strip or other flat array... you could go all-out and make it a heirarchical ADT.

Quote:
Original post by juxie
For index buffer, do you mean that the index buffer does store the vertex data as well?
No, index buffers can't store anything other than indices.

Quote:
Original post by juxie
Also I think I need to divide the mesh into subsets so that I won't perform collision detection against all the triangle faces.
Unfortunately you still have the wrong idea here. Both the mesh and its subsets are for visualization and not simulation. Quite simply you're using the wrong tool for the job. Subsets are for different materials - visual properties, they're not designed to be groupings for physics and simulations.

Quote:
Original post by juxie
What should I do?
Create other, pure C/C++ resources to contain the geometry data. Then write a collision-detection algorithm against this data set. There are lots of articles discussing ray-primitive intersection tests that should get you started.

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Yes. This data should be completely and totally seperate from your graphics resources. In fact, there is no reason why it has to be stored as a triangle list, strip or other flat array... you could go all-out and make it a heirarchical ADT.

Unfortunately you still have the wrong idea here. Both the mesh and its subsets are for visualization and not simulation. Quite simply you're using the wrong tool for the job. Subsets are for different materials - visual properties, they're not designed to be groupings for physics and simulations.

Create other, pure C/C++ resources to contain the geometry data. Then write a collision-detection algorithm against this data set. There are lots of articles discussing ray-primitive intersection tests that should get you started.


Just to make sure i have understood, instead of using subsets, can i divide the object using an octree and store triangles in the octree according to the space they belong to?

And then for collision detection, I will test using the octree.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!