Archived

This topic is now archived and is closed to further replies.

Index buffer help very much needed

This topic is 5813 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

ok, I have this problem with getting my index buffer to display properly on screen, it seems to use the totally wrong vertex coords.
            
	int iNumVertices	= (iResX+1) * (iResY+1);
	int iNumFaces		= iResX * iResY * 2;
	int iNumIndices		= iResX * 6 * iResY;
	//

	// Create vertex buffer

	if( FAILED( pDevice->CreateVertexBuffer( iNumVertices * sizeof(MAPCUSTOMVERTEX),
                                             D3DUSAGE_WRITEONLY, D3DFVF_MAPCUSTOMVERTEX,
                                             D3DPOOL_DEFAULT, &g_pVB ) ) )
	return E_FAIL;
	//

	// If lock succeeds, copy vertex array to the vertex buffer and unlock vertex buffer

	MAPCUSTOMVERTEX *pVertices;
	if(FAILED( g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0)))return E_FAIL;
	//

	// Fill vertex array with positon,color and normal information

	for (z=0; z<iResY+1; z++){
		for (x=0; x<iResX+1; x++){
			pVertices[x + z*(iResX+1)].xyz		= D3DXVECTOR3((float)x,0.0f,(float)z);
			pVertices[x + z*(iResX+1)].color	= 0xff7faa00;
			pVertices[x + z*(iResX+1)].vn		= D3DXVECTOR3(0.0f, 1.0f, 0.0f);
		}
	}

	g_pVB->Unlock();
	//

	// Create index buffer

	if( FAILED( pDevice->CreateIndexBuffer( iNumIndices * sizeof(WORD),
                                            D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
                                            D3DPOOL_DEFAULT, &g_IB ) ) )
    return E_FAIL;
	//

	// Lock index buffer

	WORD* pIndices;
	if( FAILED( g_IB->Lock( 0, 0,(BYTE**)&pIndices,0))) return E_FAIL;
	//

	// Fill index list

	for (y=0; y<iResY;y++){
		for (x=0; x<iResX;x++){
			int i = (x + y*iResX)*6;
			*pIndices++ = (WORD)(x + 0 +  y    * (iResX+1));	// 0, 0

			*pIndices++ = (WORD)(x + 0 + (y+1) * (iResX+1));	// 0, 1

			*pIndices++ = (WORD)(x + 1 +  y    * (iResX+1));	// 1, 0


			*pIndices++ = (WORD)(x + 0 + (y+1) * (iResX+1));	// 0, 1

			*pIndices++ = (WORD)(x + 1 + (y+1) * (iResX+1));	// 1, 1

			*pIndices++ = (WORD)(x + 1 +  y    * (iResX+1));	// 1, 0

		}
	}
	//

	// now unlock the index buffer so that we can use it

	g_IB->Unlock();
//

/* This bit is from the function being called every frame */
		pDevice->SetStreamSource( 0, g_pVB, sizeof(D3DFVF_MAPCUSTOMVERTEX) );

		pDevice->SetIndices( g_IB, 0 );

		pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, (iResX+1)*(iResY+1), 0, iResX*iResY*2);
            
This is the log from my program, showing what I belive to be correct vertex and index data: ------------------------------------------------------ CDFMap constructor, iResX=1, iResY=1 iNumVertices: 4 iNumFaces: 2 iNumIndices: 6 pVertices[0] = {0, 0, 0} pVertices[1] = {1, 0, 0} pVertices[2] = {0, 0, 1} pVertices[3] = {1, 0, 1} pIndices[0] = 0 pIndices[1] = 2 pIndices[2] = 1 pIndices[3] = 2 pIndices[4] = 3 pIndices[5] = 1 DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 4, 0, 2) DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 4, 0, 2) .... etc ------------------------------------------------------ Also, I get this when I run the app: "Direct3D8: (INFO) :Failed to create driver indexbuffer", but I dont think it's related because I get the same error while running the DirectX samples in debug mode, and their index buffers work fine. If you see some mistake I've made, don't hesitate to point it out to me! Any help on this would be very very much appreciated, I'm so stuck atm. Edit: I've included a screenshot, available from below. It shows one of the 2 triangles being drawn incorrectly. The 2 triangles in the bottom left corner I made in photoshop to illustrate how it's supposed to look, while the big triangle in the center of the screen is the one actually drawn. One more thing: As I rotate the primitives, rendered using the index buffer, I get alot of flicker on the triangles. Screenshot of problem Edited by - Starwolf on January 12, 2002 9:11:46 AM

Share this post


Link to post
Share on other sites
At first look, have you set the vertex shader? Add it somewhere where you are setting the vertex stream source. Don''t know if that would be the problem, however.

Jim Adams

Share this post


Link to post
Share on other sites
The ships on the linked image is being drawn correctly (they are meshes loaded from .x files), so I suppose my projection, view, world transformations and depth buffer should be okay I guess.. or is that not a given?

Share this post


Link to post
Share on other sites