Jump to content
  • Advertisement

Archived

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

peter86

Index Buffer problem

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

DrawIndexedPrimitive returns D3DERR_INVALIDCALL, when I''m using an index buffer, but it works fine with a vertex buffer. Why? Here''s the code:
  
HRESULT InitGeometry()
{
	HRESULT hr;

	CUSTOMVERTEX g_Vertices[] =
	{
		// Floor

		{ -1.0f, -1.0f,  1.0f, 0xff0000ff, },
		{ -1.0f, -1.0f, -1.0f, 0x00ff0000, },
		{  1.0f, -1.0f, -1.0f, 0x000000ff, },

		{ -1.0f, -1.0f,  1.0f, 0x000000ff, },
		{  1.0f, -1.0f,  1.0f, 0x0000ff00, },
		{  1.0f, -1.0f, -1.0f, 0x000000ff, },

		// Midwall

		{ -1.0f, -1.0f,  1.0f, 0x000000ff, },
		{ -1.0f,  1.0f,  1.0f, 0x00ff0000, },
		{  1.0f,  1.0f,  1.0f, 0x000000ff, },

		{ -1.0f, -1.0f,  1.0f, 0x000000ff, },
		{  1.0f, -1.0f,  1.0f, 0x0000ff00, },
		{  1.0f,  1.0f,  1.0f, 0x000000ff, },

		// Right wall

		{  1.0f, -1.0f,  1.0f, 0x0000ff00, },
		{  1.0f,  1.0f,  1.0f, 0x000000ff, },
		{  1.0f, -1.0f, -1.0f, 0x000000ff, },

		{  1.0f,  1.0f,  1.0f, 0x000000ff, },
		{  1.0f, -1.0f, -1.0f, 0x000000ff, },
		{  1.0f,  1.0f, -1.0f, 0x00ff0000, },


	};

	/*if( FAILED( hr = g_pD3DDevice->CreateIndexBuffer( sizeof(g_Vertices),
													   0, D3DFVF_CUSTOMVERTEX,
													   D3DPOOL_DEFAULT,
													   &g_pVB ) ) )
	{
		return hr;
	}*/

	if( FAILED( hr = g_pD3DDevice->CreateIndexBuffer( sizeof(g_Vertices),
													  0, D3DFMT_INDEX16,
													  D3DPOOL_DEFAULT,
													  &g_pIB ) ) )
	{
		return hr;
	}

	unsigned char* pVertices;
	if( FAILED( hr = g_pIB->Lock( 0, 0, &pVertices, 0 ) ) )
		return hr;
	
	memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );

	if( FAILED( hr = g_pIB->Unlock() ) )
		return hr;


	return S_OK;
}

void Render()
{
	HRESULT hr;

	g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 128 ), 1.0f, 0 );

	g_pD3DDevice->BeginScene();

	SetupMatrices();

	// Render the index buffer contents

	//g_pD3DDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );

	//g_pD3DDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );

	g_pD3DDevice->SetIndices( g_pIB, 0 );
	//g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 6 );

	hr = g_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 6, 0, 6 );
	
	g_pD3DDevice->EndScene();

	g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
	DXTRACE_ERR( "", hr );
}
  

Share this post


Link to post
Share on other sites
Advertisement
I think you''ll have to read some more about index buffers ;-)

1)Create vertices
2)Load vertexbuffer with vertices
3)Create indices(they point to the right vertex).
4)Load indexbuffer with indices.
5)Render

This is for dx8, don''t know about dx9!

Share this post


Link to post
Share on other sites
quote:
Original post by IFooBar
same for dx9




Tought so.

Hey, your link isn't working for me.

[edited by - RiBa on February 9, 2003 3:55:40 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by RiBa
3)Create indices(they point to the right vertex).



How do I create those indices?

Share this post


Link to post
Share on other sites
suppose your vertex buffer is

{ (0,0,0), //0
(1,0,0), //1
(0,1,0), //2
(1,1,0), //3
}

these are the vertices to draw a square if you used only vertex bufffer you would need to repeat 2 vertices, the ones that are shared by the two triangles, but if you have this index buffer:

{0,2,1,2,3,1}

you are specifying the order in witch to draw your vertices ie:
draw 0 then 2nd vertice etc

dunno if it''s clear enough, you should probably read something about this





To be considered a genius you just have to say what everybody knows in a way very few understand

Share this post


Link to post
Share on other sites
Ok, I´ve changed my code now after reading a tutorial on index buffers at www.andypike.com, but I still don't get it to work (DrawIndexedPrimitive returns D3DERR_INVALIDCALL as before).

What wrong with this code?

    
HRESULT InitGeometry()
{
HRESULT hr;

// Create vertices

CUSTOMVERTEX g_Vertices[] =
{
{-1.0f,-1.0f, 1.0f, 0x000000ff, },
{-1.0f,-1.0f,-1.0f, 0x00ff0000, },
{ 1.0f,-1.0f,-1.0f, 0x000000ff, },
{ 1.0f,-1.0f, 1.0f, 0x0000ff00, },
{-1.0f, 1.0f, 1.0f, 0x00ff0000, },
{ 1.0f, 1.0f, 1.0f, 0x000000ff, },
{ 1.0f, 1.0f,-1.0f, 0x00ff0000, },
};

if( FAILED( hr = g_pD3DDevice->CreateVertexBuffer( sizeof(g_Vertices),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&g_pVB ) ) )
{
return hr;
}

VOID* pVertices;
if( FAILED( hr = g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) )
return hr;

memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

g_pVB->Unlock();


// Create indices

WORD g_Indices[] = { 1, 2, 3, 1, 4, 3, 1, 5, 6, 1, 4, 6, 4, 6, 3, 6, 4, 7 };

if( FAILED( hr = g_pD3DDevice->CreateIndexBuffer( sizeof(g_Indices),
0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&g_pIB ) ) )
{
return hr;
}

VOID* pIndices;
if( FAILED( hr = g_pIB->Lock( 0, 0, (BYTE**)&pIndices, 0 ) ) )
return hr;

memcpy( pIndices, g_Indices, sizeof(g_Indices) );

g_pIB->Unlock();


return S_OK;
}

void Render()
{
HRESULT hr;

g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 128 ), 1.0f, 0 );

g_pD3DDevice->BeginScene();

SetupMatrices();

// Render the index buffer contents

g_pD3DDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pD3DDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
g_pD3DDevice->SetIndices( g_pIB, 0 );
//g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 6 );

hr = g_pD3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 7, 0, 6 );

g_pD3DDevice->EndScene();

g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
DXTRACE_ERR( "", hr );
}


[edited by - peter86 on February 10, 2003 2:08:17 PM]

Share this post


Link to post
Share on other sites
Just had a brief look at your code;

The vertices are numbered from 0 to 6, so, in other words,
the seventh vertex doesn''t exist

Share this post


Link to post
Share on other sites
quote:
Original post by RiBa
Just had a brief look at your code;

The vertices are numbered from 0 to 6, so, in other words,
the seventh vertex doesn''t exist




Thanks, it works now

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!