Jump to content
  • Advertisement
Sign in to follow this  
treeway

Index Buffers

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

Hi Ive created a vertex buffer with my cube in it using a specific fvf and it draws fine using the DrawPrimitive method: Creating the vertex Buffer:

MYVERTEX data[]={

	    //FRONT
		{halfWidth,-halfHeight,halfDepth,0,0,1,0xFFFFFFFF,1,0},{-halfWidth,halfHeight,halfDepth,0,0,1,0xFFFFFFFF,0,1},{-halfWidth,-halfHeight,halfDepth,0,0,1,0xFFFFFFFF,0,0}, 
		{halfWidth,halfHeight,halfDepth,0,0,1,0xFFFFFFFF,1,1},{-halfWidth,halfHeight,halfDepth,0,0,1,0xFFFFFFFF,0,1},{halfWidth,-halfHeight,halfDepth,0,0,1,0xFFFFFFFF,1,0},

		//LEFT
		{halfWidth,-halfHeight,halfDepth,1,0,0,0xFFFFFFFF,0,0},{halfWidth,-halfHeight,-halfDepth,1,0,0,0xFFFFFFFF,1,0},{halfWidth,halfHeight,halfDepth,1,0,0,0xFFFFFFFF,0,1},
		{halfWidth,-halfHeight,-halfDepth,1,0,0,0xFFFFFFFF,1,0},{halfWidth,halfHeight,-halfDepth,1,0,0,0xFFFFFFFF,1,1},{halfWidth,halfHeight,halfDepth,1,0,0,0xFFFFFFFF,0,1},
	

		//BACK
		{-halfWidth,-halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,1,0},{halfWidth,halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,0,1},{halfWidth,-halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,0,0},
		{-halfWidth,-halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,1,0},{-halfWidth,halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,1,1},{halfWidth,halfHeight,-halfDepth,0,0,-1,0xFFFFFFFF,0,1},
		
		//RIGHT
		{-halfWidth,-halfHeight,-halfDepth,-1,0,0,0xFFFFFFFF,0,0},{-halfWidth,-halfHeight,halfDepth,-1,0,0,0xFFFFFFFF,1,0,},{-halfWidth,halfHeight,-halfDepth,-1,0,0,0xFFFFFFFF,0,1},
		{-halfWidth,-halfHeight,halfDepth,-1,0,0,0xFFFFFFFF,1,0},{-halfWidth,halfHeight,halfDepth,-1,0,0,0xFFFFFFFF,1,1},{-halfWidth,halfHeight,-halfDepth,-1,0,0,0xFFFFFFFF,0,1},

		
		//TOP
		{-halfWidth,halfHeight,-halfDepth,0,1,0,0xFFFFFFFF,0,1},{-halfWidth,halfHeight,halfDepth,0,1,0,0xFFFFFFFF,0,0},{halfWidth,halfHeight,halfDepth,0,1,0,0xFFFFFFFF,1,0},
		{halfWidth,halfHeight,halfDepth,0,1,0,0xFFFFFFFF,1,0},{halfWidth,halfHeight,-halfDepth,0,1,0,0xFFFFFFFF,1,1},{-halfWidth,halfHeight,-halfDepth,0,1,0,0xFFFFFFFF,0,1},

		//Bottom
		{-halfWidth,-halfHeight,-halfDepth,0,-1,0,0xFFFFFFFF,0,1},{halfWidth,-halfHeight,-halfDepth,0,-1,0,0xFFFFFFFF,1,1},{halfWidth,-halfHeight,halfDepth,0,-1,0,0xFFFFFFFF,1,0},
		{-halfWidth,-halfHeight,halfDepth,0,-1,0,0xFFFFFFFF,0,0},{-halfWidth,-halfHeight,-halfDepth,0,-1,0,0xFFFFFFFF,0,1},{halfWidth,-halfHeight,halfDepth,0,-1,0,0xFFFFFFFF,1,0},
	};

		int vert_count= sizeof(data)/sizeof(MYVERTEX);
		int byte_count= vert_count*sizeof(MYVERTEX);
		g_list_count = vert_count/3;

	
		if(FAILED(CreateVertexBuffer(base->D3dDevice,byte_count,D3DUSAGE_WRITEONLY,MYFVF,D3DPOOL_MANAGED,&vb)))
		{
			base->messageBox(base->hWnd,"Error Creating vertex buffer");
		}

		if(FAILED( vb->Lock(0, //Offset
				    0, //SizeToLock
			        &vb_vertices, //Vertices
			        0)))  //Flags))
		{
			base->messageBox(base->hWnd,"Error Locking vertex buffer");
		}

			memcpy(vb_vertices,data,byte_count);

		vb->Unlock();


Drawing the Vertex Buffer:
	base->D3dDevice->SetFVF(MYFVF);

	//Bind our Vertex Buffer
	SetStreamSource(base->D3dDevice,0,vb,sizeof(MYVERTEX));


        //Render from our Vertex Buffer
	base->D3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,g_list_count); 

okay now i want to use an index buffer as well as a vertex buffer to speed things up. From what I understand, an index buffer stores only indexes of the unique verticies and uses an index for non unique ones. i.e if 1 and 4 are the same it will use 1 twice. So i wrote this code to create an index buffer from my vertex buffer: Creating the Index Buffer:
vector<INDEX*> indices;
		indices.clear();
		uniqueCount = 0;

		for(int i = 0; i < vert_count; i ++)
		{
			INDEX* cur = new INDEX;
			MYVERTEX* currentVertex = &data;
			int indexSize= (int) indices.size();
			bool found = false;
			for(int x = 0; x < indexSize; x ++)
			{
				if(currentVertex->x == indices[x]->vertex->x &&					// Any Vertex with the same co-ordinate is the same
					currentVertex->y == indices[x]->vertex->y && 
					currentVertex->z == indices[x]->vertex->z)
				{
				   found = true;
				   cur->vertex = indices[x]->vertex;
				   cur->index = x;
				   break;
				}
			}
			
			if(! found)
			{
				cur->index = uniqueCount;									// This vertex is unique, add it
				cur->vertex = currentVertex; 
				uniqueCount ++;
			}
			indices.push_back(cur); 
			
			sprintf(base->buffer,"Index : %d = %d \n",i,indices->index);
			OutputDebugString(base->buffer);
		}

		sprintf(base->buffer,"%d Unique Vertices",uniqueCount);
		base->messageBox(base->hWnd,base->buffer,"Look Here");
		
		WORD *indexTable;
		indexTable = new WORD[vert_count];

		ib->Lock( 0, 0, (void**)&indices, 0 );
			
		for(UINT i = 0; i < indices.size(); i++)
		{
			indexTable  = indices->index;
		}	
		ib->Unlock();
		

this seems to work correctly as it tells me there are 6 unique vertices in terms of position only and it outputs a list of all vertices which seems correct. However when i try to draw the index buffer: Trying to draw the Index Buffer:
	base->D3dDevice->SetFVF(MYFVF);
        base->D3dDevice->SetIndices(ib); // Set the index buffer   
	//Bind our Vertex Buffer
	SetStreamSource(base->D3dDevice,0,vb,sizeof(MYVERTEX));

        if(FAILED(base->D3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,g_list_count,0,g_list_count/3)))
				 {
				    base->messageBox(base->hWnd,"Drawing the Index Buffer has failed");
				 }	

i dont see anything rendered but the call to draw the indexed primitive doesnt fail. I have tried turning off culling and setting the render state to wire frame but i still dont see anything. I think it may be to do with the order of the verices in the vertex buffer? how do i know if they are "clockwise" or not? sorry for the ling post thanks

Share this post


Link to post
Share on other sites
Advertisement
You calculate

g_list_count = vert_count/3;


but then divide by three in your draw call as well. Change your draw call to something like

DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,g_list_count*3,0,g_list_count)

(the number of vertices used is g_list_count*3)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!