# Vertex buffers....what?

## Recommended Posts

Im having some trouble grasping the vertex buffer\index buffer concept. For instance right now i have a plain jane 2d square that im rendering like this:
        //square 1
// vertex 0
box[0].x = 0;
box[0].y = 40;

//vertex 1
box[1].x = 0;
box[1].y = 0;

//vertex 2
box[2].x = 40;
box[2].y = 40;

//vertex 3
box[3].x = 40;
box[3].y = 0;

box[0].diffuse = 0x00FF0000;
box[1].diffuse = 0x00FF0000;
box[2].diffuse = 0x000000FF;
box[3].diffuse = 0x000000FF;


Now i need to have around 289 of these 'squares' to make up my grid. But what i dont understand is how im actually using 6 vertices than 4. I only have 4 vertices?

##### Share on other sites
I'm not quite sure what your question is. Do you want to know how to use an index buffer with the provided vertex data or what?

##### Share on other sites
each vertice needs and x and a y coord.....your displaying 4 sets of x and y coords......if that isnt what u mean then please elaborate ur question doesnt really make sense

##### Share on other sites
I apologize if i was unclear, an example of how to make one with my example would help, but i was wondering how an index buffer comes into play with squares, because i dont see how im using duplicate vertexes i.e. 6 instead of 4.

##### Share on other sites
An index buffer isn't really usefull in this situation.

An index buffer stores a list of indices in a vertex buffer which allows you to save each vertex only once in a vertex buffer and then refer to each of them more than once in the much cheaper to use index buffer.

From the verts you provided before a square would look like this using a triangle list:

1  +         1 +----+ 3    | \          \   |   |   \          \ |0  +----+ 2         + 2

This means that you have six entries in your index buffer, one for each corner of each tri but only define four verts in your vertex buffer.

##### Share on other sites
Now i understand, thank you.

##### Share on other sites
One moe question if you will :)

If i add say 20 more squares to my static array is it possible to have it render 21 squares instead of one big object? I have tried to add 2 squares or 8 vertex entries but it doesnt seem to work :?(

i also set my primative number to 4 without any success.

##### Share on other sites
Getting index & vertex buffers to work properly when you set them up manually can be a bit of a bitch. Do you actually need to use index buffers? Like, what kind of speed requirements do you have, vs the size of your finished 3D structure? If you don't need blistering speed, I'd recommend just sticking to vertex buffers, keeps things simpler. :)

##### Share on other sites
Quote:
 Original post by stroAn index buffer stores a list of indices in a vertex buffer which allows you to save each vertex only once in a vertex buffer and then refer to each of them more than once in the much cheaper to use index buffer.

I don't exactly follow, can you run it by again?

##### Share on other sites
Well as silly at it may sound i let myself get talked into converting my direct draw game over to D3D, needless to say so far its been a bloody pain in the ass.

This is(WAS) a simple 2d grid game with very simple visuals, i dont need speed but i would like an efficient way of drawing the 289 or so squares, this is how im currently doing it...

bool LRRender::InitVertex(){//	1  +         1 +----+ 3 //	   | \          \   |//	   |   \          \ |//	0  +----+ 2         + 2	//log vertex positions	freopen("vertex.txt","w",stdout);	int vtx = 0;	int obj = 0;	char buffer[255];	for (int i=0;i<17;i++)	{		for (int j=0;j<17;j++)		{			//v0			gridster.ConvertFromSimplePos(&i,&j); //conversion			grid[vtx].x = i;			grid[vtx].y = j+40;			grid[vtx].z = 0.0f;			grid[vtx].rhw = 1.0f;			grid[vtx].diffuse = 0x00FF0000;			gridster.ConvertToSimplePos(&i,&j); //conversion			sprintf(buffer,"---- Object:%d ---- \n Vertex:%d \n posX:%f \n posY:%f \n\n",obj,vtx,grid[vtx].x,grid[vtx].y);			fprintf(stdout,buffer);			ZeroMemory(buffer,sizeof(buffer));			vtx++;			//v1			gridster.ConvertFromSimplePos(&i,&j); //conversion			grid[vtx].x = i;			grid[vtx].y = j;			grid[vtx].z = 0.0f;			grid[vtx].rhw = 1.0f;			grid[vtx].diffuse = 0x00FF0000;			gridster.ConvertToSimplePos(&i,&j); //conversion			sprintf(buffer,"Vertex:%d \n posX:%f \n posY:%f \n\n",vtx,grid[vtx].x,grid[vtx].y);			fprintf(stdout,buffer);			ZeroMemory(buffer,sizeof(buffer));			vtx++;			//v2			gridster.ConvertFromSimplePos(&i,&j); //conversion			grid[vtx].x = i+40;			grid[vtx].y = j+40;			grid[vtx].z = 0.0f;			grid[vtx].rhw = 1.0f;			grid[vtx].diffuse = 0x00FF0000;			gridster.ConvertToSimplePos(&i,&j); //conversion			sprintf(buffer,"Vertex:%d \n posX:%f \n posY:%f \n\n",vtx,grid[vtx].x,grid[vtx].y);			fprintf(stdout,buffer);			ZeroMemory(buffer,sizeof(buffer));			vtx++;			//v3			gridster.ConvertFromSimplePos(&i,&j); //conversion			grid[vtx].x = i+40;			grid[vtx].y = j;			grid[vtx].z = 0.0f;			grid[vtx].rhw = 1.0f;			grid[vtx].diffuse = 0x00FF0000;			gridster.ConvertToSimplePos(&i,&j); //conversion			sprintf(buffer,"Vertex:%d \n posX:%f \n posY:%f \n\n",vtx,grid[vtx].x,grid[vtx].y);			fprintf(stdout,buffer);			ZeroMemory(buffer,sizeof(buffer));			vtx++;			obj++; //1 object done		}	}	//vertex buffer	//index buffer	return true;}

Although the logic is correct it still does not appear to be rending properly as its treating it as ONE object and not 289 individual squares.

Rendering with this command:
 pd3dd->DrawPrimitiveUP(D3DPT_TRIANGLELIST,289,grid,sizeof(vertex));

##### Share on other sites
Perhaps i should rephrase the question, how would i get the DrawPrimitiveUP() function to recognize my array as more than one object in this case 289 squares?

 Silly me, i forgot each object is actually 2 primtives so it would be 289*2. But it still does not render properly. :?(

pd3dd->DrawPrimitiveUP(D3DPT_TRIANGLELIST,578,grid,sizeof(vertex));

##### Share on other sites
An index buffer is a simple concept. You use it to reduce the number of distinct vertices you send to the rendering pipeline - to reduce duplicity. It is only useful, however, when you actually have shared vertices. For instance, a 3D cube has eight vertices, but 12 triangles. Each triangle consists of 3 vertices, so somehow 8 vertices must become 36. An index buffer solves this problem by "fooling" the rendering pipeline into believing it has received more vertices than it actually has, in essence saying "here's vertex 24" and then handing over vertex 4 - perhaps for the third time.

Your real challenge will be texturing, specifically setting up your texture coordinates.

##### Share on other sites
Thanks for cleaing that up!

Perhaps you can help me straighten out how i can get the DrawPrimitiveUP() to draw my squares individually instead of essentially just one as it seems to be doing. I posted my code above, any help would be appreacited :)

##### Share on other sites
Quote:
 Original post by jsloanPerhaps you can help me straighten out how i can get the DrawPrimitiveUP() to draw my squares individually instead of essentially just one as it seems to be doing. I posted my code above, any help would be appreacited :)
DrawPrimitive* functions render a sequence; they consider the data passed to them to be monolithic. If you want individual rendering, you're either going to have to call DrawPrimitive* several times (use a loop) or call DrawIndexedPrimitive.

##### Share on other sites
Thanks, i was reading up on the DrawInDexedPrimitiveUP function here is the prototype:

HRESULT DrawIndexedPrimitiveUP(          D3DPRIMITIVETYPE PrimitiveType,    UINT MinVertexIndex,    UINT NumVertexIndices,    UINT PrimitiveCount,    const void *pIndexData,    D3DFORMAT IndexDataFormat,    CONST void* pVertexStreamZeroData,    UINT VertexStreamZeroStride);

im assuming i DO have to create an index buffer in order to do this?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982193

• 9
• 24
• 10
• 9
• 13