7 Replies - 586 Views - Last Post: 08 April 2013 - 06:52 PM Rate Topic: -----

#1 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 01:35 AM

Hey guys, I've been creating a basic binary file format for my meshes and other related data. It's come time to render them to my DirectX rendering pipeline. This is not a help topic, meaning I don't require assistance to what I am working on, I just am curious about a few things.

In the DirectX documentation supplied in the DirectX SDK, I am looking into Index buffers for optimal optimization, and have come to a brain meltdown.

Posted Image

Note: If the image above is not showing here is the direct link:
http://img856.images...rtexbuffers.png

Notice how the Index Buffer goes 0, 1, 2 - 3, 0, 2. I understand the indexing and all that just not this. How come, for the second ▲ the index buffer goes 3, 0, 2 where 3 is a 'real' vertex and 0 and 2 are the 'indexed' vertices. To make that a little more clearer, how come the second ▲ starts with the vertex '3' and not say vertex 0 (0, 2, 3). Has this something to do with culling? if so, (0, 2, 3) is still being rendered clockwise?

Thanks all,

This post has been edited by aaron1178: 07 April 2013 - 01:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: DirectX 9 Vertex and Index Buffers + Culling

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 03:52 AM

It's organized the vertices triangle strip.
Was This Post Helpful? 0
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 04:25 AM

I have another question as well, so I shall post it here rather than necro post :P

I have a vector<D3DXVECTOR3> and am trying to render the vertices but how I copy the data to the vertex buffer is out... heaps, and I'm wondering if you can point me in the right direction with copying vertices from a vector to a LPDIRECT3DVERTEXBUFFER9 buffer.

main.cpp //important code here not full file
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPreviousInstance, LPSTR lpcmdline, int nCmdShow )
{
	MSG Message;

	//mPreMesh();

	g_App.SetD3DStatus(true);
	g_App.InitD3D();

	ifstream in("example.mesh", ios::in | ios::binary);
	SmartPtr<HeaderNode> hNode = new HeaderNode();
	hNode->Read( in );
	SmartPtr<TriShapeDataNode> trishape = new TriShapeDataNode();
	trishape->Read( in );

	LPDIRECT3DVERTEXBUFFER9 mVB1 = NULL;
	LPDIRECT3DINDEXBUFFER9 mIB1 = NULL;
	void* mData;

	g_App.GetDevice()->CreateVertexBuffer( sizeof(trishape->Vertices) , D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &mVB1, NULL );
	mVB1->Lock( 0, sizeof(mData), (void**)&mData, 0 );

	memcpy( mData, &trishape->Vertices[0], sizeof(D3DVERTEX) * trishape->Vertices.size() );


	mVB1->Unlock();
	while( g_App.GetD3DStatus() )
	{
		mMessageLoop(&Message);

		if( g_App.CheckDevice() )
		{
			g_App.RenderS();				

			g_App.GetDevice()->SetStreamSource( 0, mVB1, 0, sizeof(D3DVERTEX) * trishape->Vertices.size() );
			g_App.GetDevice()->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 8 );
			//g_App.GetDevice()->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );

			g_App.RenderE();
		}
	}

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 08:48 AM

It's not really clear from the description nor code snippet you've posted what you really want. How have you filled your vector<D3DXVECTOR3>, and what are you doing with it? Also, isn't this code wrong?

mVB1->Lock( 0, sizeof(mData), (void**)&mData, 0 );



You're not locking the entire buffer because of sizeof(mData). You're only locking four bytes.
Was This Post Helpful? 0
  • +
  • -

#5 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 04:46 PM

Ok, I've modified it so it's sizeof(D3DXVECTOR3) * trishape->Vertices.size() for everything. Like so:

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPreviousInstance, LPSTR lpcmdline, int nCmdShow )
{
	MSG Message;

	//mPreMesh();

	g_App.SetD3DStatus(true);
	g_App.InitD3D();

	ifstream in("example.mesh", ios::in | ios::binary);
	SmartPtr<HeaderNode> hNode = new HeaderNode();
	hNode->Read( in );
	SmartPtr<TriShapeDataNode> trishape = new TriShapeDataNode();
	trishape->Read( in );

	LPDIRECT3DVERTEXBUFFER9 mVB1 = NULL;
	LPDIRECT3DINDEXBUFFER9 mIB1 = NULL;
	void* mData;

	g_App.GetDevice()->CreateVertexBuffer( sizeof(D3DXVECTOR3) * trishape->Vertices.size() , D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &mVB1, NULL );

	mVB1->Lock( 0, sizeof(D3DXVECTOR3) * trishape->Vertices.size(), (void**)&mData, 0 );
	memcpy( mData, &trishape->Vertices, sizeof(D3DXVECTOR3) * trishape->Vertices.size());
	mVB1->Unlock();

	while( g_App.GetD3DStatus() )
	{
		mMessageLoop(&Message);

		if( g_App.CheckDevice() )
		{
			g_App.RenderS();				

			g_App.GetDevice()->SetStreamSource( 0, mVB1, 0, sizeof(D3DVERTEX));
			g_App.GetDevice()->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 );
			//g_App.GetDevice()->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );

			g_App.RenderE();
		}
	}

	return 0;
}



My vector<D3DXVECTOR3> vector is filled from a binary file. (And yes, the values are being read correctly). My whole aim is to render vertices from my binary file, but obviously it's not going so well. I can't find any tutorials on how to render vertices from a std::vector :| I've been trying heaps of solutions and ways that I can think off, but it's not going so well.

The above snippet will render a primitive, except it isn't the correct size, nor the whole mesh. And it seems to be getting stuck towards the right hand side of my window :|

So to your question:

I am trying to render vertices from a binary file that are stored in a std::vector.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 07:47 PM

You don't need the vector structure. What you should be doing is reading the data from the file into memory and accessing it from there.
Was This Post Helpful? 0
  • +
  • -

#7 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 07 April 2013 - 11:09 PM

I've got it working, thanks . I'll get back to you once I've cleaned up the code some more.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: DirectX 9 Vertex and Index Buffers + Culling

Posted 08 April 2013 - 06:52 PM

I still say you don't need the vector. You will find out eventually that it will slow down your rendering noticeably.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1