quick and easy 2D Grid function

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

quick and easy 2D Grid function

Post by Midnight »

I'm not sure if this has ever been posted.

It's adapted from my old guice project and some rambling on #irrlicht which you should join if yer not here already.

It's a draw2DGrid function for anyone needing that sort of thing for say a gui editor or something like that. it may be a little more complex then nessesary even but should work easily for anyone.

this is just a modified version of the hello world demo it shouldn't be too difficult to understand what I did here even if you're a bit noobish.

#include <irrlicht.h>


using namespace irr;


using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

IVideoDriver* driver;

//resolution values are normally read from config files
//which is why it's this way for now
s32 ResX = 1024; //width
s32 ResY = 768; //height

s32 GresX;
s32 GresY;

void draw2DGrid(s32 ResX, s32 ResY, s32 GridSize, bool isFullscreen, video::SColor XColor, video::SColor YColor)
{
if(isFullscreen)
{
ResX = driver->getScreenSize().Width;
ResY = driver->getScreenSize().Height;
}

GresX = ResX / GridSize;
GresY = ResY / GridSize;

// you can use 2d line but it's not as precise
for ( s32 x = 0; x < GresX; x++ )
{
driver->draw2DRectangle(XColor,rect<s32>(x*GridSize-1,0,x*GridSize,ResY));
// driver->draw2DLine(position2d<s32>(x*GridSize-1,0), position2d<s32>(x*GridSize,ResY),SColor(255,90,90,90));
}

for ( s32 y = 0; y < GresY; y++ )
{
driver->draw2DRectangle(YColor,rect<s32>(0,y*GridSize-1,ResX,y*GridSize));
// driver->draw2DLine(position2d<s32>(0,y*GridSize-1), position2d<s32>(ResX,y*GridSize),SColor(255,90,90,90));
}
}


int main()
{


IrrlichtDevice *device =
createDevice( video::EDT_DIRECT3D9, dimension2d<s32>(ResX, ResY), 16,
false, false, false, 0);

device->setWindowCaption(L"Midnight's 2D Grid - Irrlicht Engine Demo");

driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();

driver->setFog(video::SColor(200,200,60,60), false, 10.0, 20.0, 0.01, false, true);


guienv->addStaticText(L"Midnight's 2D Grid! This is the Irrlicht Software renderer!",
rect<s32>(10,10,260,22), true);


IAnimatedMesh* mesh = smgr->getMesh("sydney.md2");
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );


if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMaterialFlag(EMF_FOG_ENABLE, true);

node->setMD2Animation ( scene::EMAT_STAND );
node->setMaterialTexture( 0, driver->getTexture("sydney.bmp") );
}

smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

while(device->run())
{
driver->beginScene(true, true, SColor(255,100,101,140));

smgr->drawAll();

//we have to draw the grid before GUI if we want it to be behind the GUI
//if the grid fullscreen flag is set true then it ignores resolution values for easier of use
draw2DGrid(ResX,ResY,16,true,video::SColor(255,255,0,0),video::SColor(255,0,0,255));

guienv->drawAll();

driver->endScene();
}

device->drop();

return 0;
}



enjoy 8)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

hybrid wrote:There's a pretty useful one here http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24938
no hybrid thats a 3D grid scene node for scene editors and stuff

mine is a 2D GUI style grid for things like GUI editors

but the link should be helpful for those who are confused.

*thumbs up*


I'll include this screenshot for a limited time to show exactly what it looks like.

Image
Last edited by Midnight on Thu Apr 24, 2008 9:38 pm, edited 1 time in total.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Hmm, well Midnight, you should consider the process in which sio2 stated for the 3D grid, which can be adapted for use with your 2D grid. There is really no need to draw so many lines.
TheQuestion = 2B || !2B
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Halifax wrote:Hmm, well Midnight, you should consider the process in which sio2 stated for the 3D grid, which can be adapted for use with your 2D grid. There is really no need to draw so many lines.

hmm looking into that now thanks for the tip. 8)



EDIT:

Code: Select all

	// X-axis lines
	for(u32 x = 0; x <= m_size*2; x+= m_spacing)
	{
		vector3df start = leftMost;
		start.X += x ;

		vector3df end = rightMost;
		end.X = start.X;

		// std::cout<<"Drawing Line From : "<<start.X<<","<<start.Y<<","<<start.Z<<" to "<<end.X<<","<<end.Y<<","<<end.Z<<"\n";
		driver->draw3DLine(start,end,m_gridcolor);
	}

	// Z-axis lines
	for(u32 z = 0; z <= m_size*2; z+= m_spacing)
	{
		vector3df start = leftMost;
		start.Z += z ;

		vector3df end = rightMost;
		end.Z = start.Z;

		driver->draw3DLine(start,end,m_gridcolor);
	}
it appears to me there is ABSOLUTELY a need for this many lines. :wink:

could you explain what you mean halifax??
Last edited by Midnight on Thu Apr 24, 2008 9:44 pm, edited 1 time in total.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Also, couldn't you offer one with alternating colors in each direction? red, blue, red, etc. ^^
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, using the scene node with proper transformation right in front of the camera could reduce the number of draw calls (not really the number of lines). But using lots of single draw calls can heavily impact render performance.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Well sio2 explains it in the topic that hybrid posted, it uses a texture, not hundreds of lines:
sio2 wrote:It would be interesting to replace the drawing of 100's or 1000's of lines (which consumer hardware may render as degenerate triangles anyway) with a single alpha-tested texture drawn as a quad, trilinear filtered, with vertex colour modulation to change the line colour. The texture would have the "grid" drawn onto it.
@hybrid: Yeah, and if you wanted to use lines, how would you batch the lines so you only have to use one call? Would it be better just to built a list for an indexed triangle draw, or is there some special way to batch them?
TheQuestion = 2B || !2B
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You use the EPT_LINES primitive. That way you can send two vertices per line segment, arbitrary number of lines.
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

It would be better to render the grid to a texture, then use a simple quad for the grid. I haven't done so with my grid simply because of a lack of time to work on such things. My grid is inefficient, but will handle thousands of lines without murdering the FPS too much.

It's as optimal as I could get it without switching over to a render to texture solution.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Rendering a fullscreen quad that is mostly transparent is expensive in terms of fill rate.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

thanks for the comments about that it's interesting.

well it's a quick and easy solution for testing purposes.
just something I made when I was bored really.
Post Reply