Best way to Create a Grid

Started by
8 comments, last by ChazH 9 years, 4 months ago

Hello,

So, recently I have decided to start playing around with creating a level editor in my spare time and in the level editor I was thinking about having a grid. I am looking to mimic the type of gird that is used in UDK. I was wondering what would be the best way to create a grid? I was thinking that I could create a line list vertex buffer but that seems rather slow. I could also go with a single quad and use a shader to render the grid as well. What would be the best solution to create a grid that looks like the grid in UDK? Any suggestions?

Thank you,
Chaz
Advertisement
Why would a line list be slow?
The reason I said a line list was slow was mainly due to the time it takes to build all of the verts. The wider and taller the grid the longer it takes. When I tried to setup a grid that was 1000 x 1000 it took a while to setup ( might just be due to the laptop that I am developing on ). Would a line list be the best way to setup a grid like UDK?
Thank you,
Chaz

Why 1000x1000? Was the udk grid really that big? ( My memory has a habit of dissapearing into the void )

And maybe you dont need such a huge grid, just translate the grid based on the camera position ( whilst having some sort of a snapping method ), then you'll always have the grid around.

A 10x10 linegrid will be extremely cheap, however if you persist on the 1000x1000 perhaps a geometry shader might of interest? Im not very experienced in the performance remarks of the geometry shader so perhaps somebody with more experience can step in.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Why not create the vertex/index buffers for the grid *once*, and then reuse them from then on? If the grid is evenly spaced, the only thing you need to do when rendering it is alter its transform.

Even a 1000x1000 line grid is only 3996 vertices, if each individual line goes all the way across (you're not making a vertex for every intersection, are you?!).
Who needs buffers anyway ? wink.png



cbuffer Parameters
{
	uint Rows;
	matrix WorldViewProjection;
}

void LineGridVS(uint vid: SV_VertexID,
	out float4 color: COLOR,
	out float4 position: SV_Position)
{
    // which row ?
    uint n = vid / 2;	
    // line start or end (column coordinate)
    position.x = (vid % 2) * Rows;	
    // row coordinate (modulo needed for later flip to work)	
    position.z = n % (Rows + 1);
    position.y = 0;
    position.w = 1;
    // center around origin
    position.xz -= (Rows / 2);		
    color = float4(1,0,0,1);
    // rows or columns ? flip x and z (and also give a different color)
    if(n > Rows)
    {
        position.xz = position.zx;
        color = float4(0,1,0,1);
    }
    position = mul(position, WorldViewProjection);		
}
Don't bind neither vertex nor index buffer nor input layout. Call with context->Draw((rows + 1) * 4, 0); (Edit: Line list topology). Anything else (centering around camera and snapping) can be done with the transformation, as pointed out.

Nice weekend y'all!

Edit2: Minor corrections.

Who needs buffers anyway ? wink.png




cbuffer Parameters
{
	uint Rows;
	matrix WorldViewProjection;
}

void LineGridVS(uint vid: SV_VertexID,
	out float4 color: COLOR,
	out float4 position: SV_Position)
{
    // which row ?
    uint n = vid / 2;	
    // line start or end (column coordinate)
    position.x = (vid % 2) * Rows;	
    // row coordinate (modulo needed for later flip to work)	
    position.z = n % (Rows + 1);
    position.y = 0;
    position.w = 1;
    // center around origin
    position.xz -= (Rows / 2);		
    color = float4(1,0,0,1);
    // rows or columns ? flip x and z (and also give a different color)
    if(n > Rows)
    {
        position.xz = position.zx;
        color = float4(0,1,0,1);
    }
    position = mul(position, WorldViewProjection);		
}
Don't bind neither vertex nor index buffer nor input layout. Call with context->Draw((rows + 1) * 4, 0); (Edit: Line list topology). Anything else (centering around camera and snapping) can be done with the transformation, as pointed out.

Nice weekend y'all!

Edit2: Minor corrections.

That's a easy way to get a grid on the screen, but it may take more GPU effort than is really needed. It is usually sufficient just to make a grid and store it in some vertex/index buffers, and then you can easily transform the vertices and simply rasterize the geometry. It will eliminate the vertex work being done to generate the lines, at the cost of a little bit of bandwidth.

Thank you for the suggestions. The idea of a small grid and just keep updating the transforms seems like a solid option. I will have to give that a try later this week. Last night I had an idea and haven't had a chance to implement it yet and was wondering if there were some possible downsides to it. Currently with the way I am building my grid with a line list is that there are 4 verts for each square ( currently not using an index buffer, stupid me, I know ). So, if the grid is 100 x 100 then I would be using 4,000 verts. Seems kind of crazy. Anyway, I am thinking that I could easily just create a grid by having 400 verts instead of 4,000. I believe, I could do this by just setting the positions based on the width or height of the grid. For instance, if the grid was 100 x 100, then a vert for the x row would be -50, z ( vert 1 ) and 50, z ( vert 2 ). Then do the same thing for the z axis. Does anyone for see any issues with this method?

Thank you,
Chaz H

Thank you,
Chaz

No issues at all. It's exactly what Nypyren hinted at (and what my vertex shader does procedurally). Go for it.

Edit: I wonder if a indexed draw is worth it, since you only share 4 vertices :wink:

Thanks for the help. It worked great.

Thank you,
Chaz

This topic is closed to new replies.

Advertisement