Add or change TEXTURE of any FACE of a Custom Scene Node.

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
Danny Gilbert
Posts: 54
Joined: Thu Oct 25, 2007 1:38 pm
Location: Montreal, Canada

Add or change TEXTURE of any FACE of a Custom Scene Node.

Post by Danny Gilbert »

I have the following to create a custom scene node (a cube as an exemple). This is a classic.... (tutorial 3 modified)

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 = u;

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.Pos -= core::vector3df(0.5f, 0.5f, 0.5f);
buffer->Vertices.Pos *= cubeSize;
buffer->BoundingBox.addInternalPoint(buffer->Vertices.Pos);
}

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

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


How I can set the texture of each faces ?
I find how to set one material and one texture for the entire cube but not for each face.

Thank you !
vi-wer
Posts: 93
Joined: Sun May 20, 2007 7:15 pm
Location: Germany
Contact:

Post by vi-wer »

The material is applied to all faces in the meshbuffer. To set a different texture for each face you either have to create a meshbuffer for each face, so it has its own material, or use the customscenenode from the tutorial (this one does look like the one from the tutorial :wink: ) and always set another material before using drawPrimitives to draw one face. For your code the first way is probably faster to add then the second.
Danny Gilbert
Posts: 54
Joined: Thu Oct 25, 2007 1:38 pm
Location: Montreal, Canada

Mesh buffer and NODES.

Post by Danny Gilbert »

Thanks for your reply.

If a use one MESH for each face (and each one has it's material), I will
need to create ONE NODE for each of this mesh....

The problem is that I want only one node for the "cube".
In my game, each of my nodes will represent a CORRIDOR (puzzle piece of a 3D labyrinth). Each corridor is composed of WALLS, TOP and FLOOR.
So I created it manually with vertices and mesh buffer and I want to set
the texture for the faces.

If I create one mesh buffer for each face (to set the texture), I think that I will create one node for each face. This is what I had last week and it works... but I want more Performance (less NODES) by creating bigger structure like the cube.

Maybe I don't understand something.

Do you have any Idea ? Where I am Wrong ?
vi-wer
Posts: 93
Joined: Sun May 20, 2007 7:15 pm
Location: Germany
Contact:

Post by vi-wer »

You don't have to create a scenenode for each face. You only need to add all meshbuffers to one mesh and set different materials to it. You can set the material while creating the faces or if you've created the scene node and then call node->getMaterial( material num here).Textures[0] = texture. Like this:

Code: Select all

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

SMeshBuffer * buffer = new SMeshBuffer();
SMeshBuffer * buffer2 = new SMeshBuffer();
u16 u[6] = { 0,2,1, 0,3,2};
u16 u2[6] = {0,11,10, 0,10,7};

for (s32 i=0; i<6; ++i)
{
  buffer->Indices.push_back( u[i] );
  buffer2->Indices.push_back( u2[i] );
}

// set material per buffer, use this or the one at the end of this code
buffer->Material.Textures[0] = texture1
buffer2->Material.Textures[0] = texture2

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);

buffer2->Vertices[0] = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 1, 0);
buffer2->Vertices[1] = video::S3DVertex(0,0,1, -1,-1, 1, cubeColour, 1, 1);
buffer2->Vertices[2] = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 0, 1);
buffer2->Vertices[3] = video::S3DVertex(0,1,0, -1, 1,-1, cubeColour, 1, 1);
buffer2->Vertices[4] = video::S3DVertex(1,0,1, 1,-1, 1, cubeColour, 1, 0);
buffer2->Vertices[5] = video::S3DVertex(1,0,0, 1,-1,-1, cubeColour, 0, 0);

/* bounding box reset for both bufferrs here*/
buffer->BoundingBox.reset(0,0,0);
// ......

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

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

//Set material per scene node, material count should be the same as buffer count
cubeSceneNode->getMaterial(0).Textures[0] = texture1
cubeSceneNode->getMaterial(1).Textures[0] = texture2
Danny Gilbert
Posts: 54
Joined: Thu Oct 25, 2007 1:38 pm
Location: Montreal, Canada

thank you...

Post by Danny Gilbert »

Wow ! Thank you very much. This is what I want to know.
I suggest to improve the Tutorial 3 with the code you send to me.

.... I will try it and do something very clean and give it as a SNIPPET
on this Web site.

I am sure that it is very useful for anybody.

Thanks again.
yuko
Posts: 6
Joined: Mon Apr 03, 2006 4:54 pm

Post by yuko »

Did you finally made a snippet??
It would be very useful for me because I have a very similar problem and I dont understand that code...
thanks!
Post Reply