Jump to content
  • Advertisement

Archived

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

Kawahee

Index Buffers

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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

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!