EditorGridSystem         A dynamically changing grid system for editors and the like

Introduction

I have created a dynamic grid system to use in our Ogre-based world editor. Because Ogre is such a great engine and has such a great community, we decided to give this piece of code to the community. You can find the original forum post here.

Orthographic View

When using an orthographic camera (top, left, right, ...), the grid system has the following features:

  • Grid is automatically updated when necessary (and only when necessary)
  • Grid lines are automatically inserted / removed when the pixel spacing between the lines reaches a certain treshold
  • Major grid lines (full alpha) and minor grid lines (alpha-faded according to their pixel spacing)
  • Options: grid color, division (in how many lines a grid is divided when zooming in), render layer (behind or in front of all objects)

Perspective Cameras

When using a perspective camera, we use a simple 3D grid for now.

TODO

  • Finish support for an arbitrary orthographic camera direction (what to do with axes lines, ...)
  • Implement dynamic splitting and merging of grid cells in perspective view (based on what?)
  • Implement mini-axes rendering (so you can easily see the camera rotation)
  • Implement mini-scale rendering (like on a road map, to easily see unit sizes)
  • Support for multiple grids?
  • Add example code to move objects over one axis or a two-axes plane, so the object stays under the mouse cursor in screen space

Usage Information

To use this, you just create an instance of the class, providing it with an ogre scene manager and viewport. Then you just enable it (ViewportGrid::enable and such).

You can set some options:

  • grid colour: the color of the grid lines. Minor grid lines are alpha-faded according to their pixel spacing. Zero coordinate grid lines automatically get the color of the orthogonal axis, if the camera is pointed along one of these (red for X, green for Y, blue for Z).
  • Division level: this determines when major grid lines are drawn (defaults to 10). It also has an influence on when grid lines are split or merged (they are split into 'division' new lines when zooming in, and 'division' lines are merged when zooming out)
  • Render layer: you can determine where the grid is drawn in orthographic view: behind or in front of all objects. Defaults to behind, and ignored in perspective view (there, the grid is an actual 3d grid on the XZ-plane)
  • Perspective size: the size (width and height) of the grid in perspective mode. Defaults to 100 units.
  • Scale rendering (not implemented yet): set to true if you want scaling info to be rendered on an overlay (like on maps, so you can see how many units one grid is)
  • Mini axes rendering (not implemented yet): set to to true if you want mini-axes to be rendered in an overlay (so you can easily see the direction of your camera, even when the zero-gridlines aren't visible)


It just takes information about the camera attached to the viewport to calculate the grid lines. The actual movement of the camera is up to you. In orthographic view, we always start with the camera positioned 10000 units from the origin, use a fixed FOV, and change the near clipping plane to zoom in/out. Panning is done by moving the camera in its up/right plane.

Updates

  • (2010-02-24) Changed the license to MIT (copied from OGRE)

Source Code

[+] ViewportGrid.h

[+] ViewportGrid.cpp