Sign in to follow this  
Pirosan

Am i accessing the index/vertex buffer correctly?

Recommended Posts

Well, what i am doing is basically checking the area under the camera to see what polygon it is over with the following code:
BOOL bHit;
DWORD dwFace;
float fBary1, fBary2, fDist;
D3DXIntersect(m_pMesh, &pRayPos, &pRayDir, &bHit, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL);
struct intersect intersectO;
//	fvf = m_pMesh->GetFVF();
BYTE *vertices;
BYTE *offsets;

if(bHit){
	m_pMesh->LockIndexBuffer( 0,  &offsets  );
	m_pMesh->LockVertexBuffer(D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK, &vertices);


unsigned int stride = D3DXGetFVFVertexSize(m_pMesh->GetFVF());

// Seek to the first vertex of the given face
vertices += (unsigned char)((stride) * ((offsets[3 * dwFace - 2])));
intersectO.v1 = *reinterpret_cast<D3DXVECTOR3*>(vertices);

vertices += (unsigned char)((stride) * ((offsets[3 * dwFace - 1])));
intersectO.v2 = *reinterpret_cast<D3DXVECTOR3*>(vertices);

vertices += (unsigned char)((stride) * ((offsets[3 * dwFace - 0])));
intersectO.v3 = *reinterpret_cast<D3DXVECTOR3*>(vertices);
intersectO.face = dwFace;
Vtest = intersectO;

m_pMesh->UnlockIndexBuffer();
m_pMesh->UnlockVertexBuffer();
}

I then take the resulting vertex information and render that polygon again... i set it so the rest of the level renders in wireframe and the polygon im standing on to render filled, but it doesnt seem to be working right, i was working with the numbers gotten from the index buffer and it still seems to be accessing the wronge verticies... any ideas?

Share this post


Link to post
Share on other sites
Try this instead for getting the actual vertices.

intersect0.v1 = *reinterpret_cast<D3DXVECTOR3*>(vertices + stride*offsets[3*dwFace+0]);
intersect0.v2 = *reinterpret_cast<D3DXVECTOR3*>(vertices + stride*offsets[3*dwFace+1]);
intersect0.v3 = *reinterpret_cast<D3DXVECTOR3*>(vertices + stride*offsets[3*dwFace+2]);

I think the problem was that you were changing the "vertices" pointer. The index buffer tells you where in your vertex buffer to look. There's no need to change the "vertices" pointer.

neneboricua

Share this post


Link to post
Share on other sites
What is your simulation or game about? Checking if the camera is above each polygon is not very efficient at all. I assume that you are using simple checks already to eliminate this check - bounding spheres or planes etc

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