Jump to content
  • Advertisement
Sign in to follow this  

Index Buffers

This topic is 4327 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[]={






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

			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");



Drawing the Vertex Buffer:

	//Bind our Vertex Buffer

        //Render from our Vertex Buffer

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;
		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;
			if(! found)
				cur->index = uniqueCount;									// This vertex is unique, add it
				cur->vertex = currentVertex; 
				uniqueCount ++;
			sprintf(base->buffer,"Index : %d = %d \n",i,indices->index);

		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;

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->SetIndices(ib); // Set the index buffer   
	//Bind our Vertex Buffer

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


(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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!