Vertex buffers....what?

Started by
13 comments, last by jsloan 19 years, 7 months ago
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?
Advertisement
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?
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
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
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.
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.
Now i understand, thank you.
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.
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. :)
---PS3dev
Quote:Original post by stro
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.


I don't exactly follow, can you run it by again?
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)); 

This topic is closed to new replies.

Advertisement