History: MOGRE GeneratingAGrid
Source of version: 2 (current)
Copy to clipboard
Sometimes its useful to have a 3D grid placed on the XZ plane as base of reference for your scene. This short code would show you how to construct the 3D grid using a ((ManualObject)) centered at the origin.
{CODE(wrap="1", colors="c#")}private void CreateGrid(int numcols, int numrows, float unitsize)
{
ManualObject grid = mSceneMgr.CreateManualObject("grid");
grid.Begin("BaseWhiteNoLighting", RenderOperation.OperationTypes.OT_LINE_LIST);
float width = (float)numcols * unitsize;
float depth = (float)numrows * unitsize;
Vector3 center = new Vector3(-width / 2.0f, 0, -depth / 2.0f);
for (int i = 0; i < numrows; ++i)
{
Vector3 s, e;
s.x = 0.0f;
s.z = i * unitsize;
s.y = 0.0f;
e.x = width;
e.z = i * unitsize;
e.y = 0.0f;
grid.Position(s + center);
grid.Position(e + center);
}
grid.Position(new Vector3(0.0f, 0.0f, numrows * unitsize) + center);
grid.Position(new Vector3(width, 0.0f, numrows * unitsize) + center);
for (int i = 0; i < numcols; ++i)
{
Vector3 s, e;
s.x = i * unitsize;
s.z = depth;
s.y = 0.0f;
e.x = i * unitsize;
e.z = 0.0f;
e.y = 0.0f;
grid.Position(s + center);
grid.Position(e + center);
}
grid.Position(new Vector3(numcols * unitsize, 0.0f, 0.0f) + center);
grid.Position(new Vector3(numcols * unitsize, 0.0f, depth) + center);
grid.End();
mSceneMgr.RootSceneNode.AttachObject(grid);
}{CODE}
The grid resolution depends on the parameter numrows and numcols. The unitsize parameter tells how big each cell is in world units.
This also constructs the grid with the predefined / hard coded "BaseWhiteNoLighting" material.
{img src="img/wiki_up/Grid.png" alt="Grid.png"}