(C++) Add your own cube node

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
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

(C++) Add your own cube node

Post by rogerborg »

I was asked about how to get the mesh from a cube node created by ISceneManager::addCubeSceneNode(), and as far as I can see, you can't, because it creates an internal class, CCubeSceneNode, which has no interface that exposes its mesh.

So, short of modifying the engine (to make CCubeSceneNode an IMeshSceneNode and return an IMeshSceneNode from addCubeSceneNode()), I'd do it with something like this:

Code: Select all

	f32 cubeSize = 5.f;
	video::SColor cubeColour(255,255,255,255);

	SMeshBuffer * buffer = new SMeshBuffer();
	u16 u[36] = {   0,2,1,   0,3,2,   1,5,4,   1,2,5,   4,6,7,   4,5,6, 
            7,3,0,   7,6,3,   9,5,2,   9,8,5,   0,11,10,   0,10,7};

	buffer->Indices.set_used(36);
	for (s32 i=0; i<36; ++i)
		buffer->Indices[i] = u[i];

	buffer->Vertices.set_used(12);
	buffer->Vertices[0]  = video::S3DVertex(0,0,0, -1,-1,-1, cubeColour, 0, 1); 
	buffer->Vertices[1]  = video::S3DVertex(1,0,0,  1,-1,-1, cubeColour, 1, 1); 
	buffer->Vertices[2]  = video::S3DVertex(1,1,0,  1, 1,-1, cubeColour, 1, 0); 
	buffer->Vertices[3]  = video::S3DVertex(0,1,0, -1, 1,-1, cubeColour, 0, 0); 
	buffer->Vertices[4]  = video::S3DVertex(1,0,1,  1,-1, 1, cubeColour, 0, 1); 
	buffer->Vertices[5]  = video::S3DVertex(1,1,1,  1, 1, 1, cubeColour, 0, 0); 
	buffer->Vertices[6]  = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 1, 0); 
	buffer->Vertices[7]  = video::S3DVertex(0,0,1, -1,-1, 1, cubeColour, 1, 1); 
	buffer->Vertices[8]  = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 0, 1); 
	buffer->Vertices[9]  = video::S3DVertex(0,1,0, -1, 1,-1, cubeColour, 1, 1); 
	buffer->Vertices[10] = video::S3DVertex(1,0,1,  1,-1, 1, cubeColour, 1, 0); 
	buffer->Vertices[11] = video::S3DVertex(1,0,0,  1,-1,-1, cubeColour, 0, 0); 

	buffer->BoundingBox.reset(0,0,0); 

	for (int i=0; i<12; ++i)
	{
		buffer->Vertices[i].Pos -= core::vector3df(0.5f, 0.5f, 0.5f);
		buffer->Vertices[i].Pos *= cubeSize;
		buffer->BoundingBox.addInternalPoint(buffer->Vertices[i].Pos);
	}

	SMesh * cubeMesh = new SMesh();
	cubeMesh->addMeshBuffer(buffer);

	ISceneNode * cubeSceneNode = smgr->addMeshSceneNode(cubeMesh);

... then set up cubeSceneNode with any properties that you need.
de3000
Posts: 20
Joined: Tue Sep 19, 2006 4:57 pm
Location: South Africa

I don't care if this thread is old

Post by de3000 »

Thanx rogerborg :D I was just about to post a question about this.

and yes i do know this thread is old but is nice to say thanx :P
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

And it's nice to be thanked.

Is this the part where we kiss? :P
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yeah, go for it and show some video of it :P
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

No way, that's a DMCA takedown waiting to happen. ;)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Note that if you want to texture each face you'll need 24 vertices - fewer depending on how many corners can share texcoords.
de3000
Posts: 20
Joined: Tue Sep 19, 2006 4:57 pm
Location: South Africa

Post by de3000 »

rogerborg wrote:And it's nice to be thanked.

Is this the part where we kiss? :P
Kiss? :shock: ummm :? NO! lol :lol:

ANYWAY....

im using Jirr wrapper and well mesh buffers aren't friendly just yet. lol
Helderash
Posts: 49
Joined: Sun Mar 25, 2007 8:41 pm

Post by Helderash »

Would there be any way to loop around the section setting vertices, e.g.

buffer->Vertices[0] = video::S3DVertex(0,0,0, -1,-1,-1, cubeColour, 0, 1);

to automatically generate cubes from a larger number of vertices? I'm working on some deformation experiments, and need to form a large cube from a number of smaller elements (smaller cubes). I'm just terrified of having to assign them one after another, especially when I plan to be using over 1000 vertices in some cases.

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

Post by hybrid »

Best thing would be to check the CSphereSceneNode and copy over the node handling to CubeSceneNode. It should allow to define the vertical and horizontal vertices, defaulting to 1 for backward compatibility. Then upload the new class and we will add it to Irrlicht :)
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

So if I wanted to look at this cube from the inside, eg, skybox, all I would need to do is invert the Indice direction vector numbers? (eg -1,-1,-1 to 1,1,1)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

No, that would just change the normal that is used for lighting calculations. Actually the normals that are provided in this code are invalid as they should be normalized.

Anyways, you would need to reorder the indices in the index buffer if you wanted to see the box from the inside. Either that or disable backface culling on the material for the box.

Travis
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Yeah I just times them by a negative number and it was sweet.
(The vertex positions)

Cheers
Cyberdrill
Posts: 18
Joined: Wed Feb 27, 2008 2:32 pm
Location: Russia, Moscow

Vertices...

Post by Cyberdrill »

Please, tell how Vertices for Mesh Buffer were defined!? I try, but can't find any regularities... And how defined their normales?... And why there are 12 vertices? :) (cube has only 8 geometric vertices...) I'm really panic now :))))
Post Reply