Lots of Boxes!

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
QJaxun
Posts: 3
Joined: Fri Jul 16, 2010 7:44 am

Lots of Boxes!

Post by QJaxun »

So i've just started playing around with irrlicht and its been a joy to work with so far. Ive got a lot of experience with making 2d games but this is the first time i've really been serious about starting up a 3d project, so forgive me if my question is a stupid one.

Id like to make a game where the levels are entirely made up of cubes, something like the game http://minecraft.net/. The levels will be randomly generated and i need to be able to add and remove cubes during runtime (destructible/constructable terrain).

Ive tried simply adding a separate scene node for each of edit: 200 x 200 = 40,000 not 2000, cubes as a test but obviously take a huge performance hit. I guess im just looking for some advice on how to get started and as im new to 3d programming i don't even know what to search for.

This is how i've tried adding nodes:

Code: Select all

for(int z=0;z<200;z++)
	{
		for(int x=0;x<200;x++)
		{			
			scene::ISceneNode * node = smgr->addCubeSceneNode();

			if (node)
			{
				
				node->setPosition(core::vector3df(x*10,0,z*10));
				node->setMaterialTexture(0, driver->getTexture("../media/t351sml.jpg"));
				node->setMaterialFlag(video::EMF_LIGHTING, false);
			}
		}
	}
Last edited by QJaxun on Fri Jul 16, 2010 8:36 am, edited 1 time in total.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Since you will be using many cubes I would suggest not to use high resolution textures like 512x512. I think 128x128 should be fine.
You should also try to reduce draw calls (idea from other post
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=39154 )
Working on game: Marrbles (Currently stopped).
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Well but he wants to have it destructible so batching is probably not an option.

200x200= 40000 and not 2000, that's why it's so slow :wink:
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
PI
Posts: 176
Joined: Tue Oct 09, 2007 7:15 pm
Location: Hungary

Post by PI »

It's 100% possible. However, you have to use an Octree or Quadtree or Grid or some hybrid or else partitioning method to do that. It's all about selecting those cubes you can see, and hide those you cannot. Also, the block of cubes should be small enough to be possible to quickly rebuild them...

See the pictures:
Image
Image
Image
Image
Image
Image

Note: these screenshots were taken on my crappy laptop; on my main computer the FPS was 1400+, the rebuild time was barely noticable, always < 1ms. The rebuild time could even be lowered, but I've recalculated smooth normals as well. It also uses a Texture Atlas (with a stupid method), so that it renders everything with 1 render call. Or as many render calls as many block of cubes are visible? I don't remember o.O Plus, it deletes all the invisible sides of the cubes and shows only the visible ones.

If you want the code email me at pi24<dot>contact<at>gmail<dot>com! I've made this a long time ago... It's far from being complete, but you might get the idea :)
QJaxun
Posts: 3
Joined: Fri Jul 16, 2010 7:44 am

Post by QJaxun »

Awesome thanks for the replies. Id love to take a look at your example PI (sent you an email).
blizuke
Posts: 8
Joined: Sun Jul 18, 2010 9:51 am

Looking forward for this and/or similar solutions

Post by blizuke »

I've been wishing to find a solution for this for months now.

Please do share this, and/or any similar ways to do it.

It would be greatly appreciated! Thank-you.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

PI wrote:Plus, it deletes all the invisible sides of the cubes and shows only the visible ones.
You didn't write code that does this yourself, did you? That's what backface culling does automatically.
PI
Posts: 176
Joined: Tue Oct 09, 2007 7:15 pm
Location: Hungary

Post by PI »

I've sent you the code via emails guys!
You didn't write code that does this yourself, did you? That's what backface culling does automatically.
Sorry but first I laughed on this! :D
Fancy a big cube that is made out of a lot of small ones. Say 10x10x10, that is 1000 little cubes, each with 6 quads, that is 6000 quad = 12000 triangles. What you're saying is I should keep all these little pieces inside, what no one will ever see. Instead, I delete all the invisible sides, thus I get - to stick with the example - 10x10x6 = 600 visible quads thus 1200 triangles. Sounds a bit better than 12k ain't? :) After that I send the whole thing up to the video card, to get hardware acceleration, plus the backface culling does it job as well. I also select those "block of cubes" can be seen to hide those are unnecessary to render. :)
QJaxun
Posts: 3
Joined: Fri Jul 16, 2010 7:44 am

Post by QJaxun »

Hey PI I have a couple questions for you.

1) Any tips on how to increase the speed of adding cubes. Ive made it add perfect cubes but when building really large maps (like over 64x64x64), even just a flat plane it takes quite awhile to build.

2) Any tips to increase render performance? When rendering a flat plane of boxes of size over 64x64 i get 15-40 fps and was wondering if there is anything i can do to speed things up.

Ive removed all the LDText code and cube selection stuff. Id like to have a view range of like 256 (so a map of like 512x512x128) cubes but with the slow build time and low fps that doesn't seem possible using the current code.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

PI wrote:I've sent you the code via emails guys!
You didn't write code that does this yourself, did you? That's what backface culling does automatically.
Sorry but first I laughed on this! :D
Fancy a big cube that is made out of a lot of small ones. Say 10x10x10, that is 1000 little cubes, each with 6 quads, that is 6000 quad = 12000 triangles. What you're saying is I should keep all these little pieces inside, what no one will ever see. Instead, I delete all the invisible sides, thus I get - to stick with the example - 10x10x6 = 600 visible quads thus 1200 triangles. Sounds a bit better than 12k ain't? :) After that I send the whole thing up to the video card, to get hardware acceleration, plus the backface culling does it job as well. I also select those "block of cubes" can be seen to hide those are unnecessary to render. :)
Alright - I didn't understand what culling you were doing. Obviously doing your own algorithm here would be important for performance.
PI
Posts: 176
Joined: Tue Oct 09, 2007 7:15 pm
Location: Hungary

Post by PI »

QJaxun wrote:Hey PI I have a couple questions for you.

1) Any tips on how to increase the speed of adding cubes. Ive made it add perfect cubes but when building really large maps (like over 64x64x64), even just a flat plane it takes quite awhile to build.

2) Any tips to increase render performance? When rendering a flat plane of boxes of size over 64x64 i get 15-40 fps and was wondering if there is anything i can do to speed things up.

Ive removed all the LDText code and cube selection stuff. Id like to have a view range of like 256 (so a map of like 512x512x128) cubes but with the slow build time and low fps that doesn't seem possible using the current code.
1) You could cut the whole process into phases. Currently it creates new vertex positions; smooths them; calculates raw normals; calculates smooth normals; calculates texture coordinates; then adds cubes to the meshbuffer. As you can see, it can be split into smaller tasks. To make it visible, you could re-build the meshbuffer after a couple of changes. Then, do some other tasks, and re-build it again, ..., etc. You'll have a nice time synchronizing all these tasks, though! :D
I just have re-read you question again, and building such a large amount of cubes... you could then just add those sides that are needed, predefined normals and texture coords, etc. Predefined. Will be faster.
Or, you can optimize the whole stuff even more.

2) 64x64 is not much, you should have much more FPS than that. Does your video card support VBOs? I remember I had 1400+ FPS on my desktop and 150+ on my old laptop (which is really old)! Something is definately going wrong there!
Optimization could help here too. Especially regarding what you can see and what not.

That's all for now.
Cheers,
PI
Post Reply