And I thought having understood mesh generation...

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

And I thought having understood mesh generation...

Post by Oddmonger »

Hello,

I'd like to generate a simple mesh.
It's a hexagon.
This code compile without error with

Code: Select all

g++ 04-hexagons.cpp -o 04-hexagons.elf -I/usr/include/irrlicht -L/usr/X11R6/lib -lX11 -lXxf86vm -lIrrlicht -lpng -ljpeg -lGL -Wall
Obviously, i've screwed up something, because it shows nothing at all.
I don't think it's a camera problem : I've shaked the mouse with motivation, nothing seems to hide "behind" me.

I'd like to have some advices.
Thanks.

Code: Select all

#include <irrlicht.h>
#include <math.h>

irr::IrrlichtDevice *device=0;
irr::video::IVideoDriver *driver=0;
irr::scene::ISceneManager *scenemgr=0;
irr::scene::ICameraSceneNode *cam=0;

#define PI 3.14159265

void start_irr()
{
  /* createDevice has optionnals parameters(vsync, fullscreen, bpp..) */
  device = irr::createDevice(irr::video::EDT_OPENGL, irr::core::dimension2d<irr::s32>(800,600), 32 );
  if (!device) { printf("Impossible to create device!\n"); exit(0); }
  device->setWindowCaption(L"Hexagon test");

  driver = device->getVideoDriver();
  if (!driver) printf("Driver error\n");

  scenemgr = device->getSceneManager();
}

void stop_irr()
{
  device->drop();
}


irr::scene::SMesh * create_hexagon(int x, int y, int z, int diameter)
{
  irr::scene::SMesh *mesh; //mesh (Smesh = implémentation of IMesh)
  irr::scene::SMeshBuffer *mesh_buffer; //its buffer
#define NB_TRIANGLES 4 //nbr of triangles which mesh is made of.
  
  irr::video::SColor hexagonColor(255,255,255,255);

  mesh = new irr::scene::SMesh();
  //SMeshBuffer == CMeshBuffer: material, and 3 buffers: Vertices, Indices, BoundingBox
  mesh_buffer = new irr::scene::SMeshBuffer();

  //now, we define indices of the mesh
   mesh_buffer->Vertices.set_used(6); //vertices for a hexagon
   mesh_buffer->Indices.set_used(NB_TRIANGLES*3); //indices for hexagon

   //we build hexagon with the vertices list
   irr::u16 liste_sommets[NB_TRIANGLES*3] =
     { 0,4,5, 0,1,4, 1,3,4, 1,2,3 };
   for (irr::s32 i=0; i<NB_TRIANGLES*3; ++i)
     mesh_buffer->Indices[i] = liste_sommets[i];

  for (int i=0; i<6; i++) {
   //S3DVertex: pos(x,y,z) normal(x,y,z) color(c) texture_coords(u,v)
    mesh_buffer->Vertices[i] = irr::video::S3DVertex (
          x+cos(PI/3*i)*diameter/2, z, y+sin(PI/3*i)*diameter/2,
          0,0,0, //no normals today
          hexagonColor,
          0,0 //no tex coordinates either.
          );
  }

  mesh->addMeshBuffer(mesh_buffer);
  return mesh;
/* this is my hexagon. It's not very nice, but it's mine.
  0     1
   -----
 5/     \2
  \_____/
  4     3
*/
}

static void main_loop()
{

  //hexagon
  irr::scene::SMesh *mesh_hexagon;
  irr::scene::ISceneNode *scenenode_hexagon;
  mesh_hexagon = create_hexagon(1.0,1.0,1.0,2.0);
  scenenode_hexagon = scenemgr->addMeshSceneNode(mesh_hexagon);
  scenenode_hexagon->setPosition(irr::core::vector3df(1.0,1.0,-1.0));

  cam = scenemgr->addCameraSceneNodeFPS (0, 100.0f, 300.0f, 0, 0, 0, false);
  //with fps cam, we better not show the cursor.
  device->getCursorControl ()-> setVisible (false);

  
  while(device->run() && driver)
  {
    driver->beginScene(true, true, irr::video::SColor(255,0,0,255));
    scenemgr->drawAll();
    driver->endScene();

    device->sleep(20);
  }
}


int main()
{
  start_irr();
  main_loop();
  stop_irr();
  return 0;
}
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Just guessing here-
1) Is it facing the wrong way round? Back faces are not drawn, the back of a triangle in Irrlicht goes clockwise.
2) Your bounding box is not set, call recalculateBoundingBox on your mesh and buffers, or turn automatic culling off on your node.

To debug your mesh, try setting node->setDebugDataVisible and look at the bounding boxes
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Could be a bit too small?

Plus it's being created at 0,0,0 and so is the camera... so you're effectively inside it so it's likely it's just not getting rendered or isn't visible because it's too close or something.
Image Image Image
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

create_hexagon() wants ints so it works with

Code: Select all

  mesh_hexagon = create_hexagon(1,1,1,2);
Change these 2 lines to see it directly

Code: Select all

  scenenode_hexagon->setPosition(irr::core::vector3df(0.f,0.f,1.f)); 

  cam = scenemgr->addCameraSceneNodeFPS ();
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Yeah those should really be floats, otherwise there will be rounding problems unless you do proper casting.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Oddmonger
Posts: 24
Joined: Tue Oct 04, 2005 7:01 pm
Location: Near Paris

Post by Oddmonger »

Oh... so much answers in such little time lapse.
So it was my camera after all. (and the precision problem in my function).

Thank you :D

Of course, as you noticed, i did parameters mistake (int and not float in "create_hexagon" function.)
I would have bet that "-Wall" parameter in compilation would point this...

I should write a little axis function (for showing axis on the screen), as all is upside down quickly.

I didn't thought either of the "face order" problem (clockwise, anti-clockwise). Though it's not a problem here, perhaps it could be one with some parameters like "backface culling". I don't know for the moment, but i'll try not forget this.

Thank again.
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

-Wall finds it with GCC 3.x. You are probably using GCC 4.X.

Did you notice the camera parameters were from Irrlicht 1.4 (speed is now /1000 in 1.5).

;) Here is code I'm using to see how Irrlicht really works (see where is (0,0,0), where the cam is looking at etc) try it! Basic, but essential

Code: Select all

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;

class CArrows
{
 ISceneNode* arrow[3];

public:
 CArrows::CArrows(IrrlichtDevice* device)
 {
  const wchar_t* s[] = {L"X", L"Y", L"Z"};

  const vector3df v[3] = {vector3df(90, 90, 0),
                          vector3df(0, 90, 0),
                          vector3df(90, 0, 0)};

  const vector3df t[3] = {vector3df(90, 0, 0),
                          vector3df(0, 90, 0),
                          vector3df(0, 0, 90)};

  const SColor c[3] = {SColor(255,255,  0,  0),
                       SColor(255,  0,255,  0),
                       SColor(255,  0,  0,255)};

  irr::gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
  ISceneManager* smgr = device->getSceneManager();

  smgr->addTextSceneNode(font, L"0,0,0", c[0], NULL, vector3df(0, 0, 0), -1);

  for (u8 n=0; n<3; n++)
  {
    arrow[n] = smgr->addAnimatedMeshSceneNode(smgr->addArrowMesh("arrow",
                         SColor(255, 255,100,25), SColor(255, 255,255,255)), NULL);

    arrow[n]->setScale(vector3df(100, 100, 100));
    arrow[n]->setRotation(v[n]);
    smgr->addTextSceneNode(font, s[n], c[n], NULL, t[n], -1);
    arrow[n]->setMaterialFlag(video::EMF_LIGHTING, false);
  }
 }


 CArrows::~CArrows()
 {
  for (u8 n=0; n<3; n++)
    arrow[n]->remove();
 }

};


int main()
{
  IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d<s32>(600,400), 16, false);
  if (!device) return 0;

  IVideoDriver* driver = device->getVideoDriver();
  ISceneManager* smgr  = device->getSceneManager();
  device->getCursorControl()->setVisible(false);
  device->setWindowCaption(L"arrows");

  scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 100.0f, .1f); 
  cam->setPosition(core::vector3df(0,0,-200)); 
  //cam->setTarget(core::vector3df(0,0,1)); 

  CArrows* arrows = new CArrows(device);

  u32 then=0;

  while(device->run() && device->isWindowActive()) 
  {
    if (then < device->getTimer()->getTime())
    {
      driver->beginScene(true, true, 0); 
      smgr->drawAll();
      driver->endScene();
      then = device->getTimer()->getTime() + 30;
    }
  }
 
  delete CArrows;
  device->closeDevice();

  return 0;
}
Post Reply