Vertices and Indices problem

Started by
4 comments, last by LostSource 17 years, 7 months ago
Lang: DirectX, C++ Compiler: VS2005 I'm currently trying to draw a two sided pyramide (just two triangles connecting and 2 indices). Here is a picture of the pyramide I'm trying to draw. 2 sided pyramide The vertices that I have set are: Vertex* vertices; g_hTriangle->Lock(0, 0, (void**)&vertices, 0); vertices[0] = Vertex( 0.0f, 0.0f,-2.0f); vertices[1] = Vertex(-2.0f, 0.0f, 0.0f); vertices[2] = Vertex( 0.0f, 2.0f, 0.0f); vertices[3] = Vertex( 2.0f, 0.0f, 0.0f); g_hTriangle->Unlock(); and the indices I have set are: WORD* indices; g_hIndexBuffer->Lock(0, 0, (void**)&indices, 0); indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 0; indices[4] = 2; indices[5] = 3; g_hIndexBuffer->Unlock(); The problem is that for some reason the Display() isn't able to draw the two connecting triangles at their indices. Here is the code for the with all the Setup() and Display() functions. Setup()
bool Setup() {
	g_hDevice->CreateVertexBuffer(4 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &g_hTriangle, 0);
	g_hDevice->CreateIndexBuffer(2 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_D16, D3DPOOL_MANAGED, &g_hIndexBuffer, 0);

	Vertex* vertices;
	
	g_hTriangle->Lock(0, 0, (void**)&vertices, 0);
	vertices[0] = Vertex( 0.0f, 0.0f,-2.0f);
	vertices[1] = Vertex(-2.0f, 0.0f, 0.0f);
	vertices[2] = Vertex( 0.0f, 2.0f, 0.0f);
	vertices[3] = Vertex( 2.0f, 0.0f, 0.0f);
	g_hTriangle->Unlock();

	WORD* indices;

	g_hIndexBuffer->Lock(0, 0, (void**)&indices, 0);
	indices[0] = 0; indices[1] = 1; indices[2] = 2;
	indices[3] = 0; indices[4] = 2; indices[5] = 3;
	g_hIndexBuffer->Unlock();

	D3DXVECTOR3 position(0.0f, 5.0f, -8.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMATRIX view;
	D3DXMatrixLookAtLH(&view, &position, &target, &up);
	g_hDevice->SetTransform(D3DTS_VIEW, &view);

	D3DXMATRIX projection;
	D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI * 0.5f, 
		(float)g_nBufferWidth/(float)g_nBufferHeight, 1.0f, 1000.0f);
	g_hDevice->SetTransform(D3DTS_PROJECTION, &projection);

	g_hDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

	return true;
}

Display()
bool Display(bool frame_rate) {
	if(g_hDevice) {
		g_hDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
		g_hDevice->BeginScene();

		g_hDevice->SetStreamSource(0, g_hTriangle, 0, sizeof(Vertex));
		g_hDevice->SetIndices(g_hIndexBuffer);
		g_hDevice->SetFVF(Vertex::FVF);
		g_hDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 6);

		g_hDevice->EndScene();
		g_hDevice->Present(0, 0, 0, 0);
	}
	else
		return false;

	return true;
}

Thank you ahead of time for your help.
Advertisement
Did you remember to disable Culling so all triangles being rendered would appear, regardless of their order?
How do I disable Culling? Sorry, I'm still new to this. = /

Edit: g_hDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)?

Edit2: I tried that and it still didn't work. = / I'm learning this from a book and the book never disables culling. I really wounder what it is that is preventing it from rendering the triangles.

Edit3: I found one problem. The g_hDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 6) function should be g_hDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2); Because I'm only rendering 2 triangles, not 6. = ]

[Edited by - LostSource on September 9, 2006 11:38:01 AM]
Your call to create the index buffer is most certainly failing because you specified D3DFMT_D16 - a depth buffer format. You also aren't making the index buffer large enough to hold the six indices necessary to describe the pyramid.

If you would add error checking to your program or enable the Debug D3D runtime, you'd determine this right away. The DX Forum FAQ has a list of suggestions on how to troubleshoot your app when things don't work as expected. You might want to look at it.

Thank you don! :)

Edit: Agh! I wasn't using D3DFMT_INDEX16.
Thank you don for telling me about that information and my problem with D3DFMT_.

Here is what I got:

DirectX9 Pyramid

This topic is closed to new replies.

Advertisement