Grid system for editors and such

A place to show off your latest screenshots and for people to comment on them. Only start a new thread here if you have some nice images to show off!
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Grid system for editors and such

Post by JeDi »

Hi,

I'm creating a grid-system for our world editor, and I am going to share it with the community because I think it could be useful for more people. And maybe some people have useful comments or ideas about it...

Only orthographic camera support for now, but that's the hardest case anyway.

Features (orthographic):
- 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)


Screenshots:

(white grid, behind objects)
Image

(white grid, in front of objects)
Image

(black grid)
Image


You can find the current header and implementation files on my webspace. I'll wiki this if some people find it useful


TODO:
- Support other modes than top-view (some values are still hardcoded)
- Perspective view support: user can set the grid radius and color, and the grid is rendered on the Y=0 plane
- Optional mini-axes and scale rendered on an overlay

Edit: This is also on the wiki.

Greetz,
JeDi
Last edited by JeDi on Fri Sep 28, 2012 2:29 pm, edited 2 times in total.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19265
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Thanks for posting. This would be a good Wiki entry, but please can you make sure the licensing conditions are clear in the source files themselves.
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

OK, I'll update the source files, and wiki this when the system is fully functional. I guess LGPL will be the best license? What I want is that when somebody fixes something or adds a feature, the code is given back to the community, and that nobody sells or uses the code as if it was their own. I don't care if the product it is used in is open-source or free. Actually, I'll just use the same licensing as Ogre itself...

I almost finished support for an arbitrary viewing direction in orthographic view (the colored lines at the zero-lines are only rendered when the viewing direction aligns with a world axis). So when that is finished, I only have to do the support for perspective view, which should be easy (although I don't really know how I'm going to do the dynamic merging and splitting of grid lines in that case).

It would be great if some people would try this in their projects (probably editors cause of the orthographic camera). But I guess the topic title isn't very inviting. I should maybe edit it to "Dynamic Grid System".

Edit: I've replaced the files with a newer version, with LGPL licensing in the files (copied from an Ogre source file). I've added some code to make it work with all camera views, but it isn't working (it is in top- or bottom-view). The zero-axis colors are correct now. So only a very stupid bug to fix, and orthograpic view is finished

Edit 2: I did some fixing and cleaning up of the code, and implemented a basic perspective view grid. It should be working in all camera views now. I removed a macro and an unnecessary include file, so it should compile out of the box (only dependent on Ogre). I also create the grid only once now, and update from then (I found out that the number of vertices doesn't have to be the same when updating). Still some TODOs, but it should be usefull now... If only someone would try it out :roll: Anyway, I'll upload the files as soon as possible, but the server seems to be down (there was a power failure at the university).

Greetz,
JeDi
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

OK, the new files are uploaded. The server is back online
User avatar
Sidegame
Gnoblar
Posts: 9
Joined: Wed Jul 25, 2007 5:26 am
Location: University of Tasmania, Australia

Post by Sidegame »

Thanks! This is very cool, we would have likely implemented something like this ourself soon enough, your implementation is very good :)

A little documentation would be handy too :)
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

Well, I think the usage is fairly straigtforward, so the doxygen information should suffice. I'll put some documentation on the wiki though, once I get to it.

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.

I hope it will be useful.
Greetz,
JeDi
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

Post by KungFooMasta »

Thanks for updating this JeDi. I would check it out, but I don't currently have an editor for it. I guess I could stick it in the QuickGUI Demo and play around with it.. but then I'd need to implement camera controls to move around with. Maybe this weekend I can give this a try. :)
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

Post by KungFooMasta »

JeDi, I need a Grid in my application now, and I remembered this thread you posted from a while back.

Few questions/comments:
- From a quick glance at the code, it looks like only 1 grid is produced. We should add two other grids for the yz and yx planes, and have them all at origin.

- Is there any way to enforce a grid line every x ogre units? For example, I want to scale everything such that 1 ogre unit is 1 meter. Minor grid lines at every unit would increase visibility of unit size as displayed in Ogre's scene.

- Have you made any updates to the code between since the your last post on this?

Thanks for any help.
Creator of QuickGUI!
Rackle
Gnome
Posts: 375
Joined: Sat Jul 16, 2005 1:42 am
Location: Montreal

Post by Rackle »

Looks like the source code is no longer available. Could it be possible to post the contents of those files here or in the wiki?

Other sources of information:
http://www.ogre3d.org/wiki/index.php/Line3D
http://www.ogre3d.org/wiki/index.php/DynamicLineDrawing
http://www.ogre3d.org/wiki/index.php/MOGRE_Line_3D
Last edited by Rackle on Tue Feb 26, 2008 3:48 pm, edited 1 time in total.
Brainshack
Greenskin
Posts: 118
Joined: Tue Feb 19, 2008 1:01 pm

Post by Brainshack »

Is this still available somewhere? Unfortunetaly i can't see the screenshots. But this is exactly what i need if i read things right...
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

Hey all,

I'm sorry I didn't replay on a lot of requests and posts the last couple of months, I was really swamped in work.

The code is still available, but it seems the server I put the files on is down at the moment. I should look for another server actually, cause this server is administered by students at our university, to give them the chance to get some experience in server maintenance. I guess the students this year were not a very good choice, cause the server has been down three times already this year...

I still don't have a lot of time to spend on this, but the least I can do is put the up-to-date version of the grid system files online somewhere. I haven't had a look at the code for a while now, so I don't know if a lot has changed. But if anyone has any questions about it, please ask!

I also put some time in properly dragging objects over an axis, in a way that the object stays under the cursor in screen space (projected on a single axis if doing single-axis movement).

I created a wiki page here, I will fill it in right away.

Greetz,
JeDi
Rackle
Gnome
Posts: 375
Joined: Sat Jul 16, 2005 1:42 am
Location: Montreal

Post by Rackle »

JeDi wrote:I'm sorry I didn't replay on a lot of requests and posts the last couple of months, I was really swamped in work.
The new posts are from 20 minutes before yours...that's pretty darn fast reply :wink:

Thank you for posting that code in the Ogre wiki. It seems like a needed sample. Although I'm thinking that I'll need a grid where the cells are colored green (for ok to put item here) and red (for not ok to put item here).
Brainshack
Greenskin
Posts: 118
Joined: Tue Feb 19, 2008 1:01 pm

Post by Brainshack »

Nice to see. Guess in combination with ETM this will make it quite easy to get a basic editor to work. This will really save time to me and others i think. Can't wait to try that out =)

The work you put in this is highly apreciated
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

Hi,

I've rapidly copied all useful information in this post to the wiki. If someone uses this, I would love to have some feedback.

I'll add the object moving code as soon as possible, if someone is interested in it (it involves shooting rays to determine world-space movement according to screen-space mouse movement)

Greetz,
JeDi
Brainshack
Greenskin
Posts: 118
Joined: Tue Feb 19, 2008 1:01 pm

Post by Brainshack »

I'm sure i will try this out asap. I'm a Ogre Newby so it might time some time for me to get into it =) I will give you feedback as soon as possible.
Brainshack
Greenskin
Posts: 118
Joined: Tue Feb 19, 2008 1:01 pm

Post by Brainshack »

As you can see here: http://www.brainshack.net/wordpress/wp- ... /first.jpg

it worked very well, only 2 lines of code where needed to get i running :)

HBut how do i change the orientation of the grid?
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

Hi, glad you got it working.

You don't have to control the grid system, it uses information from the camera of the viewport you provide it. So if you switch your camera to use orthographic projection, the orthogonal grid system will be used automatically.

While the orthogonal system has more advanced features like automatic merging, splitting and alpha-fading of the grid, the grid in perspective mode is just a simple grid plane on Y=0 for now.

So, if pCamera is your camera, try this for top view:

Code: Select all

Ogre::Quaternion orientation;
orientation.FromAxes(Ogre::Vector3::UNIT_X, Ogre::Vector3::NEGATIVE_UNIT_Z, Ogre::Vector3::UNIT_Y);
pCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
pCamera->setOrientation(orientation);
pCamera->setPosition(0,10000,0);
Then, you can pan and zoom the view like this (say m_zoom, m_panX and m_panY are your variables):

Code: Select all

// Reset the camera
pCamera->setPosition(Ogre::Vector3::ZERO);

// Avoid weird clipping plane problems
if(m_zoom < 0.01f) m_zoom = 0.01f;

pCamera->moveRelative(Ogre::Vector3(m_panX, m_panY, 10000));
pCamera->setNearClipDistance(m_zoom);
Greetz,
JeDi
User avatar
IFASS
Gnome
Posts: 387
Joined: Tue Jan 04, 2005 12:07 pm
Location: Netherlands

Post by IFASS »

Hi JeDi, i'm interested in the moving object code, could you explain me what it does exactly?
Image
JeDi
Gnome
Posts: 351
Joined: Thu Oct 21, 2004 1:34 pm
Location: Diepenbeek, Belgium
x 3
Contact:

Post by JeDi »

Well, I should really get some sleep now, but I will quickly paste the code I use for movement here. If you have any questions about it ask them here. I'll answer them first thing in the morning.

Code: Select all

/* 1. Calculate ray intersections with plane (constructed from active axes)
    2. Difference in intersection points is translation in world space
    3. Project translation on single axis if necessary
*/

// Construct the plane to work in (in world space)
Ogre::Vector3 normal;
switch(m_moveSpace)
{
  //! @todo Choose normal pointing to camera for single axis
  case MoveX: normal = Ogre::Vector3::UNIT_Y; break;
  case MoveY: normal = Ogre::Vector3::UNIT_Z; break;
  case MoveZ: normal = Ogre::Vector3::UNIT_X; break;

  case MoveXY: normal = Ogre::Vector3::UNIT_Z; break;
  case MoveXZ: normal = Ogre::Vector3::UNIT_Y; break;
  case MoveYZ: normal = Ogre::Vector3::UNIT_X; break;
}
Ogre::Plane plane(normal, m_pTarget->getWorldPosition());

// Get the width and height of the render window
int width = pCamera->getViewport()->getActualWidth();
int height = pCamera->getViewport()->getActualHeight();

// Shoot first ray (from old screen position)
float normX = (float) winXold / width;
float normY = (float) winYold / height;
Ogre::Ray ray = pCamera->getCameraToViewportRay(normX, normY);
std::pair<bool, float> intersectResult = Ogre::Math::intersects(ray, plane);
Ogre::Vector3 intersection1 = ray.getPoint(intersectResult.second);

// Shoot second ray (from new screen position)
normX = (float) winXnew / width;
normY = (float) winYnew / height;
ray = pCamera->getCameraToViewportRay(normX, normY);
intersectResult = Ogre::Math::intersects(ray, plane);
Ogre::Vector3 intersection2 = ray.getPoint(intersectResult.second);

// Calculate world translation
Ogre::Vector3 translation = intersection2 - intersection1;

// Project one single axis if necessary
switch(m_moveSpace)
{
case MoveX:
  plane.redefine(Ogre::Vector3::UNIT_Z, m_pTarget->getWorldPosition());
  translation = -plane.projectVector(translation);
  break;
case MoveY:
  plane.redefine(Ogre::Vector3::UNIT_X, m_pTarget->getWorldPosition());
  translation = -plane.projectVector(translation);
  break;
case MoveZ:
  plane.redefine(Ogre::Vector3::UNIT_Y, m_pTarget->getWorldPosition());
  translation = -plane.projectVector(translation);
  break;

default:
  break;
}

// Move the target
m_pTarget->translate(translation, Ogre::Node::TS_WORLD);
Greetz,
JeDi
User avatar
wdenslow
Kobold
Posts: 25
Joined: Tue Feb 17, 2009 6:11 pm

Re: Grid system for editors and such

Post by wdenslow »

Sorry to resurrect this.

Just had a quick question. When I'm trying to initialize the grid system I'm getting a few errors- maybe someone could point me in the right direction.

My code:

Code: Select all

ViewportGrid::ViewportGrid(Ogre::SceneManager * mSceneManager;, Ogre::Viewport * mRenderWindow);
		ViewportGrid grid;
		grid.enable();
Errors:

Code: Select all

1>.\ogre\XuOgre\XuOgre.cpp(156) : error C2275: 'Ogre::SceneManager' : illegal use of this type as an expression
1>.\ogre\XuOgre\XuOgre.cpp(156) : error C2275: 'Ogre::Viewport' : illegal use of this type as an expression
1>.\ogre\XuOgre\XuOgre.cpp(157) : error C2512: 'Ogre::ViewportGrid' : no appropriate default constructor available
User avatar
wdenslow
Kobold
Posts: 25
Joined: Tue Feb 17, 2009 6:11 pm

Re: Grid system for editors and such

Post by wdenslow »

Sorry to bump, but does anyone have any ideas?
User avatar
joew
Greenskin
Posts: 113
Joined: Fri Nov 03, 2006 6:03 pm

Re: Grid system for editors and such

Post by joew »

I'm not sure what you're trying to show as that code you posted makes absolutely no sense... even without the syntax error of the semi in the constructor declaration. But why does your code snippet have a constructor declaration, then trying to create a grid on the stack without the params? Are these just random lines from your code?
Last edited by joew on Tue Feb 23, 2010 5:55 pm, edited 1 time in total.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Grid system for editors and such

Post by jacmoe »

wdenslow wrote: My code:

Code: Select all

ViewportGrid::ViewportGrid(Ogre::SceneManager * mSceneManager;, Ogre::Viewport * mRenderWindow);
		ViewportGrid grid;
		grid.enable();
Are you serious? :)
You have a lot to learn about coding in C++.
Read up on classes.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Grid system for editors and such

Post by jacmoe »

I have one serious complaint about this:
It cannot be on the Wiki due to license issues.
We are pretty clear on that all the code is in the Public Domain.
It must be taken down, and hosted somewhere external.
Maybe on Google code?

I can look into hosting this myself, as I created a project called 'ogrepository' on Google Code.

But, JeDi, if you're around:
Either public domain it, or give me the green light to host it as well - but I'd prefer it to be MIT then. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
MrD
Goblin
Posts: 292
Joined: Wed Oct 21, 2009 3:16 pm
Location: England
x 1

Re: Grid system for editors and such

Post by MrD »

jacmoe wrote:but I'd prefer it to be MIT then. :wink:
I'd second this. I'm interested in using this, but since my code is MIT, and this is LGPL, I would have to put this inside its own DLL just to stop my code being infected by the LGPL which is frankly quite annoying. You previously stated that you gave it the same license as OGRE (which at the time was LGPL), but now OGRE has also switched to MIT (I am curious as to how many, if any, OGRE addons will also make this change).
Insimnax Framework - A game framework for OGRE
Post Reply