[SOLVED] How to adjust a bounding box to a custom mesh 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
Oddmonger
Posts: 24
Joined: Tue Oct 04, 2005 7:01 pm
Location: Near Paris

[SOLVED] How to adjust a bounding box to a custom mesh node?

Post by Oddmonger »

Hello, i ripped code from this forum to create a cube node.

Then, i modify it for expanding the cube on the 3 axes ("scale" factor in the function's parameters).

But the bounding box is still a cube (i can see it in debug mode, when you see bound box in wireframes).

I don't understand, as the code seemed "dynamic" to me (the bounding box should follow the shape modification of the mesh).

So i tried and force modification of the bouding box, but whatever i try, the bounding box remains a cube.

Here's the code i borrowed and modified:

Code: Select all

irr::scene::SMesh * create_mesh(float scale_x,float scale_y,float scale_z)
{
  irr::scene::SMesh *mesh; //the mesh
  irr::scene::SMeshBuffer *mesh_buffer; //its buffer

  mesh = new irr::scene::SMesh();
  mesh_buffer = new irr::scene::SMeshBuffer();

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

   irr::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};

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

   mesh_buffer->Vertices.set_used(12);
  
   mesh_buffer->Vertices[0]  = irr::video::S3DVertex(0,0,0, -1,-1,-1, cubeColour, 0, 1);
   mesh_buffer->Vertices[1]  = irr::video::S3DVertex(1*scale_x,0,0,  1,-1,-1, cubeColour, 1, 1);
   mesh_buffer->Vertices[2]  = irr::video::S3DVertex(1*scale_x,1*scale_y,0,  1, 1,-1, cubeColour, 1, 0);
   mesh_buffer->Vertices[3]  = irr::video::S3DVertex(0,1*scale_y,0, -1, 1,-1, cubeColour, 0, 0);
   mesh_buffer->Vertices[4]  = irr::video::S3DVertex(1*scale_x,0,1*scale_z,  1,-1, 1, cubeColour, 0, 1);
   mesh_buffer->Vertices[5]  = irr::video::S3DVertex(1*scale_x,1*scale_y,1*scale_z,  1, 1, 1, cubeColour, 0, 0);
   mesh_buffer->Vertices[6]  = irr::video::S3DVertex(0,1*scale_y,1*scale_z, -1, 1, 1, cubeColour, 1, 0);
   mesh_buffer->Vertices[7]  = irr::video::S3DVertex(0,0,1*scale_z, -1,-1, 1, cubeColour, 1, 1);
   mesh_buffer->Vertices[8]  = irr::video::S3DVertex(0,1*scale_y,1*scale_z, -1, 1, 1, cubeColour, 0, 1);
   mesh_buffer->Vertices[9]  = irr::video::S3DVertex(0,1*scale_y,0, -1, 1,-1, cubeColour, 1, 1);
   mesh_buffer->Vertices[10] = irr::video::S3DVertex(1*scale_x,0,1*scale_z,  1,-1, 1, cubeColour, 1, 0);
   mesh_buffer->Vertices[11] = irr::video::S3DVertex(1*scale_x,0,0,  1,-1,-1, cubeColour, 0, 0);
   
   for (int i=0; i<12; ++i)
   {
      mesh_buffer->Vertices[i].Pos -= irr::core::vector3df(0.5f, 0.5f, -1.0f);
      mesh_buffer->Vertices[i].Pos *= cubeSize;
//I think the problem is here: (but it should work)
      mesh_buffer->BoundingBox.addInternalPoint(mesh_buffer->Vertices[i].Pos);
   } 

// i also tried this, just for seeing if bounding box would be modified, but nothing happened (bbox is still a cube):   
//  irr::core::aabbox3df bbox(.0,.0,.0,.5,1.,1.);
//  mesh_buffer->setBoundingBox(bbox);

   mesh->addMeshBuffer(mesh_buffer);

   return mesh;
}
Thank you !
Oddmonger
Posts: 24
Joined: Tue Oct 04, 2005 7:01 pm
Location: Near Paris

Post by Oddmonger »

"Hello me, it's me again!" (Megadeth!)

Well i've found out what to do with bounding box.

With bouding box, you cannot modify directly mesh buffers. You have to edit the Imesh, after having included the mesh buffer of course.

That gives :

Code: Select all

   (blah blah and then ...)

   for (int i=0; i<12; ++i)
   {
      mesh_buffer->Vertices[i].Pos -= irr::core::vector3df(0.5f, 0.5f, -1.0f);
      mesh_buffer->Vertices[i].Pos *= cubeSize;
//don't try and change bounding box here!
   } 

   mesh->addMeshBuffer(mesh_buffer);

   //la bounding box ne peut s'ajouter que sur l'objet final.
   for (int i=0; i<12; i++)
      mesh->BoundingBox.addInternalPoint(mesh_buffer->Vertices[i].Pos*3.5);

   return mesh;
}

Hope it will help somebody, i really scratched my head on this one.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

There are recalculate BoundingBox methods...
Post Reply