Create a custom cube

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
samleo
Posts: 16
Joined: Mon Aug 17, 2015 12:26 am

Create a custom cube

Post by samleo »

Hi, I a new to 3d development and I have some questions. I'm trying to create a custom cube extended from ISceneNode, but it don't shows.
Here my Block code:

Code: Select all

 
#include "block.hpp"
#include <stdio.h>
 
Block::Block (  ): ISceneNode(0,0,0)
{
    type = 0;
    size = 0.0;
    color = SColor(0,0,0,0);
    setVisible(false);
    material.Lighting = false;
}
 
Block::Block ( int type, float size, SColor c, ISceneNode * parent, ISceneManager * smgr, s32 id ): ISceneNode(parent, smgr, id)
{
    init(type, size, c, vector3df(), parent, smgr, id);
}
 
void Block::init (int type, float size, SColor c, vector3df p, ISceneNode * parent, ISceneManager * smgr, s32 id )
{
    this->type = type;
    this->size = size;
    color = c;
    setVisible(false);
    
    initVertex();
    setPosition(p);
    setParent(parent);
    setSceneManager(smgr);
}
 
void Block::initVertex (  )
{
    vector3df p, q;
    vector3df n;
    // vertices' position
    float v[24][3] = 
    {
        // face 0
        {size/2.0f, size/2.0f, size/2.0f}, // vertice 0
        {size/2.0f, size/2.0f, -size/2.0f}, // vertice 1
        {-size/2.0f, size/2.0f, -size/2.0f}, // vertice 2
        {-size/2.0f, size/2.0f, size/2.0f}, // vertice 3
        
        // face 1
        {size/2.0f, -size/2.0f, size/2.0f}, // vertice 4 (4)
        {size/2.0f, -size/2.0f, -size/2.0f}, // vertice 5 (5)
        {size/2.0f, size/2.0f, -size/2.0f}, // vertice 1 (6)
        {size/2.0f, size/2.0f, size/2.0f}, // vertice 0 (7)
        
        // face 2
        {size/2.0f, size/2.0f, -size/2.0f}, // vertice 1 (8)
        {size/2.0f, -size/2.0f, -size/2.0f}, // vertice 5 (9)
        {-size/2.0f, -size/2.0f, -size/2.0f}, // vertice 6 (10)
        {-size/2.0f, size/2.0f, -size/2.0f}, // vertice 2 (11)
        
        // face 3
        {-size/2.0f, size/2.0f, size/2.0f}, // vertice 3 (12)
        {-size/2.0f, size/2.0f, -size/2.0f}, // vertice 2 (13)
        {-size/2.0f, -size/2.0f, -size/2.0f}, // vertice 6 (14)
        {-size/2.0f, -size/2.0f, size/2.0f}, // vertice 7 (15)
        
        // face 4
        {size/2.0f, -size/2.0f, size/2.0f}, // vertice 4 (16)
        {size/2.0f, size/2.0f, size/2.0f}, // vertice 0 (17)
        {-size/2.0f, size/2.0f, size/2.0f}, // vertice 3 (18)
        {-size/2.0f, -size/2.0f, size/2.0f}, // vertice 7 (19)
        
        // face 5
        {size/2.0f, -size/2.0f, -size/2.0f}, // vertice 5 (20)
        {size/2.0f, -size/2.0f, size/2.0f}, // vertice 4 (21)
        {-size/2.0f, -size/2.0f, size/2.0f}, // vertice 7 (22)
        {-size/2.0f, -size/2.0f, -size/2.0f}, // vertice 6 (23)
    };
    
    // coordenadas da textura
    float coord[4][2] =
    {
        {0,0}, 
        {1,0}, 
        {1,1}, 
        {0,1}, 
    };
    
    for (int i = 0; i < 24; i++)
    {
        p.X = v[i][0];
        p.Y = v[i][1];
        p.Z = v[i][2];
        q = p;
        n = p.normalize();
        vertex[i] = S3DVertex(q, n, color, vector2d<f32>(coord[i % 4][0],coord[i % 4][1]));
    }
    
    box.reset(vertex[0].Pos);
    for (int i = 0; i < 24; i++)
        box.addInternalPoint(vertex[i].Pos);
}
 
int Block::getType (  )
{
    return type;
}
 
void Block::setType ( int t )
{
    type = t;
}
 
void Block::setVertexPosition ( vector3df p, int i )
{
    if (i < 0 || i >= 8)
        return;
    
    printf("Rever função\n");
    
    vector3df q = p, r = vertex[i].Pos;
    q.normalize();
    vertex[i].Pos = p;
    vertex[i].Normal = q;
    
    if (r.X != p.X || r.Y != p.Y || r.Z != p.Z)
        change = true;
    
    box.reset(vertex[0].Pos);
    for (int i = 0; i < 8; i++)
        box.addInternalPoint(vertex[i].Pos);
}
 
void Block::setColor ( SColor c, int i )
{
    int j;
    
    if (i < 0 || i >= 8)
    {
        for (j = 0; j < 8; j++)
            vertex[j].Color = c;
    }
    else
    {
        vertex[i].Color = c;
    }
}
 
const aabbox3d<f32> & Block::getBoundingBox (  ) const
{
    return box;
}
 
void Block::OnRegisterSceneNode (  )
{
    if (IsVisible)
        SceneManager->registerNodeForRendering(this);
 
    ISceneNode::OnRegisterSceneNode();
}
 
void Block::render (  )
{
    const u16 indices[] = 
    {
           0,1,2,   2,3,0, // face 0
           4,5,6,   6,7,4, // face 1
          8,9,10, 10,11,8, // face 2
        12,13,14, 14,15,12, // face 3
        16,17,18, 18,19,16, // face 4
        20,21,22, 22,23,20, // face 5
    };
    
    IVideoDriver * driver = SceneManager->getVideoDriver();
    
    driver->setMaterial(material);
    driver->setTransform(ETS_WORLD, AbsoluteTransformation);
    driver->drawVertexPrimitiveList(&vertex[0], 24, &indices[0], 12);
}
 
u32 Block::getMaterialCount (  )
{
    return 1;
}
 
SMaterial& Block::getMaterial ( u32 i )
{
    return material;
}
 
Other question is: may anyone explain to me how irrlicht draw a simple triangle? Or a square/plane?
I searched the forum and I read about anti/clockwise vertex and I don't make idea about this :?
I know draw polygons in opengl and I don't can understand how this works in irrlicht.

EDIT: YES!!! Now I can draw the block, but still have the question above.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Create a custom cube

Post by mongoose7 »

To draw a box in Irrlicht you need to create a mesh buffer (IMeshBuffer) with the vertices. You add this to a mesh (IMesh) and you add the mesh to a scene node (IMeshSceneNode).

You draw a triangle the same way you draw a box, only with three vertices.

The front face of a triangle has the vertices running clockwise in Irrlicht, I think. The back face is the opposite. The GPU usually ignores back faces when rendering. This is the same for OpenGL and Direct3D.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Create a custom cube

Post by CuteAlien »

You don't have to create a mesh buffer, it just makes it often easier (especially once you want the mesh to stay on the graphic-card instead of sending it each frame - with meshbuffers that's just a flag). Your call above to drawVertexPrimitiveList is what Irrlicht uses to draw the triangles. That is more or less passed on to the graphic-card.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
samleo
Posts: 16
Joined: Mon Aug 17, 2015 12:26 am

Re: Create a custom cube

Post by samleo »

Thanks you mongoose7 and CuteAlien!
So, should I use an mesh like mongoose7 say? Is this better than like I did?
And about the my Block class, I will need to modify the vertex of the Block, but only sometimes, so does is better the IMesh to this kind of thing?
Oh, and I tried to put a texture on the block, I got the block textured but with a color above texture. Look this image:
Image
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Create a custom cube

Post by CuteAlien »

Hm, can't tell in general what is better. Take a look at the IMesh and IMeshBuffer interfaces if you like to use them or not :-)
Irrlicht Scenenodes use those interfaces often, but not always in their scenenodes. It's just making it easier sometimes to do the 'typical' stuff you need to do with meshes. For example you can then use all the functions of the IMeshManipulator class on them.

What would you expect see instead of the black thingy on blue background?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
samleo
Posts: 16
Joined: Mon Aug 17, 2015 12:26 am

Re: Create a custom cube

Post by samleo »

@CuteAlien
Ok, I think that I'll use IMesh and etc, just to learn more about 3d programming.
Oh, sorry, the block texture was this image:
Image
I don't know why the image stay blue.
samleo
Posts: 16
Joined: Mon Aug 17, 2015 12:26 am

Re: Create a custom cube

Post by samleo »

Ops, problem of blue block solved. It needs just set the vertex colors to 255,255,255,255
Thank you.
Post Reply