a simple grid class

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

a simple grid class

Post by mohaps »

Image
I was getting tired of a blank screen for my test progies .. so i took the two minutes and wrote a grid class... for drawing XZ grid

the shot is the grid class quickly hacked on top of the Tutorial 1 codebase...

Here is the code.

Code: Select all


class Grid
{
public:
	Grid():center(0,0,0),sizeHalf(1000),gridSpacing(50)
	{
		mtl.Lighting = false;
		mtl.AmbientColor = mtl.DiffuseColor = mtl.EmissiveColor = video::SColor(255,0,200,0);
	}
	virtual ~Grid()
	{
	}
	void draw(scene::ISceneManager* smgr)
	{
		core::vector3df leftMost = center;
		leftMost.X -= sizeHalf;
		leftMost.Z -= sizeHalf;

		core::vector3df rightMost = center;
		rightMost.X += sizeHalf;
		rightMost.Z += sizeHalf;

		smgr->getVideoDriver()->setMaterial(mtl);
		//cout<<"
		for(f32 x = 0; x <= 2 * sizeHalf; x+= gridSpacing)
		{
			vector3df start = leftMost;
			start.X += x ;

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

			//cout<<"Drawing Line From : "<<start.X<<","<<start.Y<<","<<start.Z<<" to "<<end.X<<","<<end.Y<<","<<end.Z<<endl;
			smgr->getVideoDriver()->draw3DLine(start,end,mtl.AmbientColor);
		}
		//cout<<endl;
		
		for(f32 z = 0; z <= 2 * sizeHalf; z+= gridSpacing)
		{
			vector3df start = leftMost;
			start.Z += z ;

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

			smgr->getVideoDriver()->draw3DLine(start,end,mtl.AmbientColor);
		}
	}

	core::vector3df center;
	f32 sizeHalf; 
	f32 gridSpacing;
	SMaterial mtl;
};
sample usage

Code: Select all

driver->beginScene(true, true, SColor(20,0,0,0));

		smgr->drawAll();
		grid.draw(smgr);
		guienv->drawAll();

		driver->endScene();
[/code]
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Ah, that's useful. Could need that too sometimes, until now I always created a skybox instead. :)
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Yea that's useful. But you might also want to set the transformation of the driver (besides the material) before drawing the grid (each time). Otherwise it might not render correctly if other nodes with transformations are present.

http://irrlicht.sourceforge.net/phpBB2/ ... line#24583

Maybe a "GridSceneNode" would be a good idea as well.
It is like it is. And because it is like it is, things are like they are.
mohaps
Posts: 248
Joined: Tue Jun 08, 2004 1:54 pm
Location: Shrewsbury MA
Contact:

Post by mohaps »

yeah

Code: Select all

driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
should do the trick in the scene node
---
Saurav Mohapatra
author, artist and bona fide geek

web: http://www.mohaps.com
email: mohaps AT gmail DOT com
etcaptor
Posts: 871
Joined: Fri Apr 09, 2004 10:32 pm
Location: Valhalla
Contact:

Post by etcaptor »

jox wrote
then it might not give you the expected result or even draw nothing.
driver->draw3DLine(vector3df(0,0,0), vector3df(0,10,0), SColor(0,0,0,0));

to fix this you can set the transform and material of the driver before drawing lines, for example:
True, I occur this problem in Irrlicht. Will try jox's issue about it.
About gridSceneNode - this will not difficulty to make it since we have mohap's grid class. But I think that mohap will make it instead some of us :) like author of grid class.
ImageImage
Site development -Rock and metal online
--- etcaptor.com ------freenetlife.com
TheRLG
Posts: 372
Joined: Thu Oct 07, 2004 11:20 pm

Post by TheRLG »

Last edited by TheRLG on Tue Jul 08, 2008 7:05 pm, edited 1 time in total.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

I would love to see that RLG.
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Sweet

Post by dudMaN »

With the transform matrix fix, this is awesome! needed something like this for my game :)

-dudMan
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Yeah that's pretty nice, even more then nice.
I'll use it for sure.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

Put the class info in a file called grid.h
then put the

Code: Select all

grid.draw(smgr);
into the spot the example shows, but its stating grid is undeclared?
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Post by dudMaN »

humbrol, declare a Grid then named grid :/

-dudMan
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

been a long week of learning alot of new stuff, the example above shows the class name as Grid but the call as grid.draw();

tried Grid.draw() with the same results,, forgive this poor noob.. im diving in deep and trying to learn.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

"Grid" is the class, "grid" would be an instance.
Learn some OOP basics.
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

Grid* grid;
Grid* whatever;
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Virion wrote:Grid* grid;
Grid* whatever;
and..

Code: Select all

grid->draw();
whatever->draw();
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply