Dynamic grid for editor (SOLVED)
by Eero Karvonen · in Torque 2D Beginner · 07/02/2014 (8:32 am) · 15 replies
How would you implement a dynamic coordinate-grid, such as the one found in the commercial TGB's scene editor?
I don't know if that particular case was done in C++ or script, but I would like to do it fully in script.
I don't know if that particular case was done in C++ or script, but I would like to do it fully in script.
#2
Here, on the background:
example screenshot
07/02/2014 (9:44 am)
I mean the grid that's all over the place and dynamically draws more or less subgrid depending on the zooming level.Here, on the background:
example screenshot
#3
07/02/2014 (9:56 am)
Ohhhhh, that. I'll have to look that up.
#4
For anyone with TGB source, it all happens in
07/02/2014 (11:00 am)
I've verified the TGB source and it's pretty simple, each frame calculate the window size and zoom levels, adjusts the grid spacing and draws lines at those coordinates using dgl draw commands, which are pretty low-level.Quote:
dglDrawLine( currStep.mX, windowWorldMin.mY, (currStep.mX), windowWorldMax.mY, mGridColor );
For anyone with TGB source, it all happens in
Quote:
LevelBuilderSceneEdit::onRenderBackground
#5
07/02/2014 (11:19 am)
Thanks! I thought it must be like that. More complicated to do the same thing in script, though, since there are no direct drawing calls available.
#6
However, with T2D's rendering scheme and batching system, if you want to draw anything on screen, you need to either hijack the scenewindow's render loop or create a new type of object and hooking up its rendering code.
Either solution is not that complicated but would take more than the time I could spend on it today.
Ideal solution imho would be to just add a drawGrid function to scenewindow's rendering loop.
07/02/2014 (2:55 pm)
I tested it out quickly, thinking "It shouldn't be harder than simply exposing dglDrawLine to script!".However, with T2D's rendering scheme and batching system, if you want to draw anything on screen, you need to either hijack the scenewindow's render loop or create a new type of object and hooking up its rendering code.
Either solution is not that complicated but would take more than the time I could spend on it today.
Ideal solution imho would be to just add a drawGrid function to scenewindow's rendering loop.
#7
The AngleToy shows how dynamic lines can be drawn with sprites and some vector math. If I was approaching this task and wanted a script solution, I would try making a grid with a rectilinear CompositeSprite and dynamically changing the grid spacing using the sprite size and quantity. Tempted to try that out now...
07/02/2014 (3:18 pm)
Would using dgl be the best solution now that we have batching in T2D MIT? The AngleToy shows how dynamic lines can be drawn with sprites and some vector math. If I was approaching this task and wanted a script solution, I would try making a grid with a rectilinear CompositeSprite and dynamically changing the grid spacing using the sprite size and quantity. Tempted to try that out now...
#8
My thinking is that a Grid could simply be one of the debug modes, just like "collision" or "joints". and be rendered first/behind everything instead of after/above everything. + a few helper functions to adjust spacing, max resolution, etc.
Come on, admit that the following looks sexy =>
07/02/2014 (3:49 pm)
The one drawback I see from the CompositeSprite solution is that it requires a scene in order to be displayed - all kinds of weird configs might make your grid fail to show up.My thinking is that a Grid could simply be one of the debug modes, just like "collision" or "joints". and be rendered first/behind everything instead of after/above everything. + a few helper functions to adjust spacing, max resolution, etc.
Come on, admit that the following looks sexy =>
Quote:Scene.setDebugOn("grid");
#9
07/02/2014 (4:13 pm)
Quote:Quote:Scene.setDebugOn("grid");That's...actually a really good suggestion. Count me in as a person requesting this feature.
#10
The debug modes are pretty interesting - they don't use dgl at all to render, the OpenGL drawing options are all available in the DebugDraw class.
While a visual debug grid is nice, the most useful functionality was the snap to grid option from the TGB level editor. One step at a time though!
07/03/2014 (3:49 am)
@Simon - I like your suggestion too, even though it also requires a scene to display. :-)The debug modes are pretty interesting - they don't use dgl at all to render, the OpenGL drawing options are all available in the DebugDraw class.
While a visual debug grid is nice, the most useful functionality was the snap to grid option from the TGB level editor. One step at a time though!
#11
DebugDraw uses OpenGL calls, dgl is just an OpenGL wrapper which Torque uses.
Here`s what dglDrawLine really does
Some functions in the engine completely bypass dgldrawline and implement the gl functions directly.
07/03/2014 (4:09 am)
@Mike : Yeah, I went to sleep thinking "My solution also requires a scene...DOH!".DebugDraw uses OpenGL calls, dgl is just an OpenGL wrapper which Torque uses.
Here`s what dglDrawLine really does
glBegin(GL_LINES); glVertex2f((F32)x1 + 0.5f, (F32)y1 + 0.5f); glVertex2f((F32)x2 + 0.5f, (F32)y2 + 0.5f); glEnd();
Some functions in the engine completely bypass dgldrawline and implement the gl functions directly.
#12
It kinda works, the left and bottom edge are not happy and disappear when zooming in but aside from that, it's there.
%he main purpose here was just to give a leg up to anyone who might want to pursue this.
- Grid appears beneath all objects
- Grid is set via MyScene.setDebugOn("grid"); Easy to toggle on and off.
- No Grid Snap
- There are NO script hooks to set the grid size, grid color, snap values or anything else except "turn grid on or off".
I've noticed that the TGB code has to make several windowToScene and SceneToWorld and localToWorld coordinate conversions, whereas T2D only has to do this once.
Here is the github branch for those who want to push it further; I really don't think that'll be me as I don't have any use for a grid system. :)
Grid Branch
07/03/2014 (12:56 pm)
Of course I had to try it in T2D MIT.It kinda works, the left and bottom edge are not happy and disappear when zooming in but aside from that, it's there.
%he main purpose here was just to give a leg up to anyone who might want to pursue this.
- Grid appears beneath all objects
- Grid is set via MyScene.setDebugOn("grid"); Easy to toggle on and off.
- No Grid Snap
- There are NO script hooks to set the grid size, grid color, snap values or anything else except "turn grid on or off".
I've noticed that the TGB code has to make several windowToScene and SceneToWorld and localToWorld coordinate conversions, whereas T2D only has to do this once.
Here is the github branch for those who want to push it further; I really don't think that'll be me as I don't have any use for a grid system. :)
Grid Branch
#13
07/03/2014 (1:12 pm)
NEAT! Thanks Simon.
#14
There's some odd behavior though, since I show the same scene in two windows with different cameras which you probably didn't try out, but I declare this case solved, until someone needs the feature more polished. It forks for me well enough. :)
07/03/2014 (1:56 pm)
Thank you, Simon! I incorporated your test in my editor and it helps a lot with what I (will) need it for.There's some odd behavior though, since I show the same scene in two windows with different cameras which you probably didn't try out, but I declare this case solved, until someone needs the feature more polished. It forks for me well enough. :)
#15
As you have noted, I haven't tested it out in gazillion scenarios, just wanted to copy over from TGB so that people who need it might at least have a good head start.
Glad to be of service! I wish I had as much talent for puns as you, though.
07/03/2014 (2:11 pm)
Yeah the grid is Scene-based, thus will display in all scenewindows showing the same scene. It should be scenewindow based, ideally.As you have noted, I haven't tested it out in gazillion scenarios, just wanted to copy over from TGB so that people who need it might at least have a good head start.
Glad to be of service! I wish I had as much talent for puns as you, though.
Community Manager Michael Perry
ZombieShortbus