Index Buffers

Started by
5 comments, last by Kawahee 19 years, 10 months ago
Can anybody tell me what index buffers are used for? I''m trying to use ID3DXMesh to hold my world data, so i can use stuff like D3DXIntersect() to make my life easier. However, i can''t seem to get it to display. And I think it''s because i''m ignoring the index buffer. the vertex buffer and attribute buffers are fine, i''ve written code to handle filling them, but what''s with the index buffer? Any help would be much appreciated.
I''m a signature virus, copy me into your signature and help me spread!
Advertisement
Hi,

Index buffers contain vertex numbers describing a primitive. You can look it up in the help file given with the SDK:
DirectX Graphics, Programming Guide, Getting Started, Direct3D Resources, Index Buffers.

Good luck,
kp
------------------------------------------------------------Neo, the Matrix should be 16-byte aligned for better performance!
Thankyou - but just one more question.

why does ID3DXMesh have a vertex buffer AND an index buffer? and do i need to fill both to get it to work?

[edited by - Kawahee on June 8, 2004 4:50:40 PM]
I''m a signature virus, copy me into your signature and help me spread!
An index buffer is a buffer full of indices. An index is not a vertex. An index is...well, an index into the buffer.

in the following code:

for(int i = 0; i < 10; ++i){    foo[i].Bar();}


foo is the buffer, and i is the index.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

The purpose of an index buffer is to be able to reuse vertecies with out having to store them twice.
the vertex buffer stores all of the unique vertecies.
the index buffer references the vertecies to make up polygons.

Think of triangle fans versus triangle lists, it''s the general idea.
hrm....

ok, i think i''ve got this

say i have a 3 vertices that make up a triangle, do i set the index buffer to 1, 2, 3?

and if i had 4 making a square, and did it with 2x triangles, would i do this:

1, 2, 3, 1, 2, 4?

thanks for all your help
I''m a signature virus, copy me into your signature and help me spread!
ok... i've fiddled around with it and i still can't get it to work.

i'm hoping you guys can find something wrong with my code, because i'm at a loss to find anything.

#include "directx.h"#include "system.h"struct CUSTOMVERTEX{    FLOAT x, y, z; // The transformed position for the vertex.    DWORD diffuse;        // The vertex color.};#define D3DFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)MSG							msg;LPD3DXMESH					gpMesh					=	NULL;CUSTOMVERTEX				gcVertices[3]			=	{															{ 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },															{ 0.0f, 1.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },															{ 1.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },														};int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShowCmd){	char*					pcBuffer	=	new char[1024];	cWindow					Window;	cDirect3D9				Direct3D;	cList<char*>			List;	Window.Init(hInstance, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), "Hello");	List.AddNode("Window Init OK");	ShowWindow(Window.GethWnd(), SW_NORMAL);	List.AddNode("Window Shown OK");	Direct3D.Init(Window.GethWnd());	List.AddNode("Direct3D Init OK");	HRESULT hr = D3DXCreateMeshFVF(1, 3, D3DXMESH_DYNAMIC, D3DFVF, Direct3D.GetDevice(), &gpMesh);	if(FAILED(hr))		return(hr);	void*					pVertices;	if(FAILED(gpMesh->LockVertexBuffer(NULL, (void**)&pVertices)))		return(1);	memcpy(pVertices, gcVertices, sizeof(gcVertices));	gpMesh->UnlockVertexBuffer();	DWORD* pID;	gpMesh->LockAttributeBuffer(NULL, &pID);	*pID = 0;	++pID;	*pID = 0;	++pID;	*pID = 0;	++pID;	gpMesh->UnlockAttributeBuffer();	WORD* pIndex;	gpMesh->LockIndexBuffer(NULL, (void**)&pIndex);	*pIndex = 1;	++pIndex;	*pIndex = 2;	++pIndex;	*pIndex = 3;	++pIndex;	gpMesh->UnlockIndexBuffer();	ZeroMemory(&msg, sizeof(MSG));	while(msg.message != WM_QUIT)	{		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		Direct3D.BeginScene();		Direct3D.Clear(D3DCOLOR_RGBA(0, 0, 0, 255));		List.GotoStart();		sprintf(pcBuffer, "%s", *List.GetData());		while(!List.IsEnd())		{			List.GotoNext();			sprintf(pcBuffer, "%s\n\r%s", pcBuffer, *List.GetData());		}		D3DMATERIAL9 mMaterial;		mMaterial.Diffuse.r = 1.0f;		mMaterial.Diffuse.g = 1.0f;		mMaterial.Diffuse.b = 1.0f;		mMaterial.Diffuse.a = 1.0f;		mMaterial.Ambient = mMaterial.Diffuse;		Direct3D.GetDevice()->SetMaterial(&mMaterial);		Direct3D.GetDevice()->SetTexture(0, NULL);		gpMesh->DrawSubset(0);		Direct3D.Text(pcBuffer);		Direct3D.EndScene();		Direct3D.Present();	}	if(gpMesh != NULL)		gpMesh->Release();	delete[] pcBuffer;	return(0);} 


[edited by - Kawahee on June 9, 2004 3:01:24 AM]
I''m a signature virus, copy me into your signature and help me spread!

This topic is closed to new replies.

Advertisement