Jump to content
  • Advertisement
Sign in to follow this  

Rendering an Infinite Grid

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

Forgive me if the topic has been brought up before; I couldn't find any real adequate search terms that found anything useful. Anyway, here's my scenario. I need to have a grid that appears to be infinite along the XZ plane. I have a 3D camera that flies around so the grid needs to adapt to that. The grid needs to be adjustable such that I can set the grid lines to be every unit, every five units, or any other arbitrary scaling. My first attempt was to use a texture to do this. I started by creating a texture that was 64x64 pixels with a white border. I then made a gigantic quad in my world. Then in my shader I tried to pass in the total unit size of the grid, the unit size of the grid spacing, the pixel size of the texture, the pixels/unit scale that I chose (currently 8), and the camera's position. My pixel shader was then trying to use this information to correctly offset the texture coordinates to match up with the world. However I can't seem to get my calculations correct (of converting all the units, pixels, and texture coordinates). Does anyone have or know of any samples or articles on achieving this effect? Or any suggestions of a different approach to the problem? I figured that this textured quad effect was my best bet for an infinite grid, but I'm all up for different ideas if they are quicker to implement.

Share this post

Link to post
Share on other sites
Figured it out :).

Basically what I did was construct a grid using a vertex buffer like one would do for a normal grid. Then I applied a "trick" to the world matrix to get it to follow the camera, but only by increments of the grid's spacing size. This gives the illusion that the grid never moves and since the grid is already drawing at least to the far plane (or close enough) the user can't see the end to see that it's only updating every few units.

For anyone else wanting to do this, here's the basic code I'm using for it (I'm using the XNA framework, but I assume it's easily transferable to native DirectX or OpenGL):

Setting up the grid geometry

// make sure planeSize is even
if (planeSize % 2 != 0)
planeSize += 1;

numLines = planeSize * planeSize;
vd = new VertexDeclaration(viewer.GraphicsDevice, VertexPositionColor.VertexElements);
VertexPositionColor[] verts = new VertexPositionColor[numLines * 2];

for (int i = 0; i < planeSize * 2; i++)
// X
Vector3 p1 = new Vector3(-planeSize, 0f, i - planeSize);
Vector3 p2 = new Vector3(planeSize, 0f, i - planeSize);

// Z
Vector3 p3 = new Vector3(i - planeSize, 0f, -planeSize);
Vector3 p4 = new Vector3(i - planeSize, 0f, planeSize);

verts[(i * 4) + 0] = new VertexPositionColor(p1, Color.Green);
verts[(i * 4) + 1] = new VertexPositionColor(p2, Color.Green);
verts[(i * 4) + 2] = new VertexPositionColor(p3, Color.Green);
verts[(i * 4) + 3] = new VertexPositionColor(p4, Color.Green);

vb = new VertexBuffer(
VertexPositionColor.SizeInBytes * verts.Length,

And then in my draw method (where grid spacing is the units in between each grid line):

// scale to meet the grid spacing and translate only in exact steps
// of the grid spacing
Matrix world =
Matrix.CreateScale(gridSpacing) *
(int)(camera.Position.X / gridSpacing) * gridSpacing,
(int)(camera.Position.Z / gridSpacing) * gridSpacing);

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!