Problem with Vertex and Index Buffers!

Started by
5 comments, last by Biggles 22 years, 5 months ago
I''m having some trouble with index and vertex buffers and rendering primitives using them. I have a vertex buffer with 8 vertices in it (simple cube) and an index buffer filled with 36 indices to give 12 polygons. I''m rendering them using the usual vertex and index buffer calls:
    	// This sets up the vertex buffer
pDevice->SetStreamSource (0, pVB, sizeof(imBasicVertex));
	pDevice->SetVertexShader (D3DFVF_IMBASICVERTEX);



pDevice->SetMaterial( &Surface );

        // And here we have the index buffer
	pDevice->SetIndices (pIB, 0);
	pDevice->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, NumIndices, 0, NumPolys);
 
When it draws it to screen some polygons don''t appear at all and others are in the wrong place or face the wrong way, etc. This is the structure for my vertex data:
// Basic vertex
struct imBasicVertex
{
    float x, y, z;
    float nx, ny, nz;
	float u,v;
};

#define D3DFVF_IMBASICVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX0)
 
Any ideas what could be going wrong? I checked all the data as it went into the buffers so I know that it is all correct at that stage. Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.
Advertisement
quote:
When it draws it to screen some polygons don''t appear at all and others are in the wrong place or face the wrong way, etc


Firstly, I can only imaging that in some instances the tri''s vertices are indexed incorrectly.

Secondly by default DX triangles are defined in a clock wise fashion, i.e. top to bottom right to bottom left. When DX comes to render the tri if the points (after transformation) are anti-clockwise (i.e. viewed from behind), the tri gets culled.





D.V.
D.V.Carpe Diem
Turn culling off

pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

If everything appears correctly, then your vertices are specified in the wrong order (counterclockwise) or a mix of orders, which you can fix by changing the order to clockwise. If things still do not appear, then something else is wrong with the way you specify your vertices or indices, in which case you need to post more code.
Also make sure your index buffer uses 16-bit indices, not 32-bit.

Jim Adams
I turned culling off and got the same result.
As for 16 bit indices... that could very well be it. They are 32-bit ints at the moment. But I''m sure I set it to use 32bit indices. Don''t tell me that flag isn''t supported.

Here''s more code. This chunk creates the vertex buffer and copies the vertices into it. It also shows the render routine for a mesh:

  IM_RESULT imMesh::CopyVertices (imBasicVertex *Vertices, LPDIRECT3DDEVICE8 pDevice){	imBasicVertex *pVertices;	if (FAILED (pDevice->CreateVertexBuffer(NumVertices * sizeof(imBasicVertex), 0, D3DFVF_IMBASICVERTEX, D3DPOOL_DEFAULT, &pVB)))		return IM_FAIL;	if (FAILED (pVB->Lock(0, 0, (BYTE**)&pVertices, 0)))		return IM_FAIL;	for (int i = 0; i < NumVertices; i++)	{		pVertices[i].x = Vertices[i].x;		pVertices[i].y = Vertices[i].y;		pVertices[i].z = Vertices[i].z;		pVertices[i].nx = Vertices[i].nx;		pVertices[i].ny = Vertices[i].ny;		pVertices[i].nz = Vertices[i].nz;		pVertices[i].u = Vertices[i].u;		pVertices[i].v = Vertices[i].v;	}	pVB->Unlock();	return IM_SUCCESS;}void imMesh::Render (LPDIRECT3DDEVICE8 pDevice){	pDevice->SetStreamSource (0, pVB, sizeof(imBasicVertex));	pDevice->SetVertexShader (D3DFVF_IMBASICVERTEX);	for (int i = 0; i < NumPolysets; i++)		Polysets[i].Render (pDevice);}  


This is the bit that copies indices and actually renders the triangles:

  IM_RESULT imPolyset::CopyIndices (unsigned int *IndsArray, LPDIRECT3DDEVICE8 pDevice){	int *pIndices;	if (FAILED (pDevice->CreateIndexBuffer(NumPolys * 3 * sizeof (int), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &pIB)))		return IM_FAIL;	if (FAILED (pIB->Lock(0, 0, (BYTE**)&pIndices, 0)))		return IM_FAIL;	for (int i = 0; i < NumIndices; i++)		pIndices[i] = (int) IndsArray[i];	pIB->Unlock();	return IM_SUCCESS;}void imPolyset::Render (LPDIRECT3DDEVICE8 pDevice){	// Need material setup stuff here    pDevice->SetMaterial( &Surface );	pDevice->SetIndices (pIB, 0);	pDevice->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, NumIndices, 0, NumPolys);}  


I hope it does support 32 bit indices. Otherwise I''ll have to go change my file format.

Thanks for the help so far.

Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.
Yay! I fixed it! Thanks, Jim Adams, you were right. Changing it to 16 bit values worked. But why don''t 32 bit indices work? Not that it really matters, since if I need 32 bit values the models are probably too complex to draw fast enough.

Now I just need to work out why materials and stuff like that don''t work.

Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.
OK, got a new problem now.
While trying to get texturing to work I found out that I was running Direct3D in retail mode instead of Debug mode. So naturally I turned it on. And now my app doesn''t go at all. All I get is the red-green flashes that tell me something is going seriously wrong somewhere. I turned debug output up high and checked the output messages, and this is what I got every render loop:

Direct3D8: (ERROR) :*** Exception in d:\nt_chk\multimedia\directx\dxg\d3d8\fe\vshader.cpp Line: 592Direct3D8: (ERROR) :Stream 0 stride should match the stride, implied by the current vertex shaderDirect3D8: (ERROR) :DrawIndexedPrimitive failed. 


I took out the u,v part of the vertex structure and the error changed to something about not having enough vertices. So I changed the code given above to manually fill 3 vertices and 3 indices then draw 1 polygon. It worked fine. I added the texture coordinates back in again and it went back to the error I pasted above.

Any ideas? Anyone at all?


--------------------

Never eat anything bigger than your own head.
--------------------Never eat anything bigger than your own head.

This topic is closed to new replies.

Advertisement