Jump to content
  • Advertisement
Sign in to follow this  
jsloan

Vertex buffers....what?

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

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


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


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 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!