Manually creating meshes (from vertexes)

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.
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Hi,

As part of a voxel engine I am trying to make (following this excellent tutorial: https://sites.google.com/site/letsmakeavoxelengine/ which sadly is focused on openGL rather than irrlicht),

I need to specify vertices manually (in a mesh) and have the engine draw them. The world will be composed of cubes.

I know I need to use the IMesh or IMeshbuffer, but looking through the documentation, I have no idea how to do it. Even tutorial 23 leaves me clueless.

How can I do this?
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Additionally: (This is a really simple question but I haven't been able to find the answer ANYWHERE)

Whats the difference between a vertex and an indice? The SD3Vertex Has 4 attributes; Colour, Normal, Position, and Texture. What is the normal (same as in math?)
Tempest
Posts: 9
Joined: Fri Jan 11, 2008 11:26 pm

Re: Manually creating meshes (from vertexes)

Post by Tempest »

I did something similar recently and found tutorial #3 helpful.

The vertexes specify a point cloud.
The indices tell which vertexes are connected to form a triangle.
The normals are for defining the orientation.

Hope that helped a little.
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Does that mean that if I have

u16 indices[] = { 0,2,1 };

It will create a triangle that operates between Vertice[0], Vertice[2], Vertice[1]??

Also, how do you calculate the normal?
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Also, should I use driver->drawVertexPrimitiveList() or a mesh buffer of some sort?
Tempest
Posts: 9
Joined: Fri Jan 11, 2008 11:26 pm

Re: Manually creating meshes (from vertexes)

Post by Tempest »

You'd use an SMeshBuffer for this.
Actually, reading your original post once more... since you only need cubes, maybe you don't need to mess with mesh manipulation at all but just use the primitive?

Anyway...
twitchyliquid64 wrote:Does that mean that if I have

u16 indices[] = { 0,2,1 };

It will create a triangle that operates between Vertice[0], Vertice[2], Vertice[1]??
Yup.
twitchyliquid64 wrote:Also, how do you calculate the normal?
Would be pretty tough to "calculate" them, in most cases you should just know or import them.
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Tempest wrote:
Would be pretty tough to "calculate" them, in most cases you should just know or import them.
What I mean is this:

I have a triangle with vertices (0,0,0) (0,1,0) (0,1,1)

What value for the normal do I need to have if I want it to face the sky? likewise to the left?

At this stage, I have no idea how to use the 'normal', all I know is that it determines the face.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Manually creating meshes (from vertexes)

Post by CuteAlien »

Normals are mostly used for light-calculations. And they should be perpendicular to the plane on which the triangle lies. Where is the sky in your case? Often y is does mean up (as that's the default in Irrlicht examples), but your triangle has all x values set to 0 ... so having a normal point upwards wouldn't be a good idea probably. In your case a normal for the face would be (1,0,0) or (-1,0,0). For the vertex it's harder to tell - usually you average the normals of the faces bordering at it or use the average of the connected edges.

Irrlicht has some a function IMeshManipulator::recalculateNormals which you can use by the way. You only have to be careful that the winding of the vertices for each triangle is correct for this to work I suppose.
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
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

CuteAlien wrote:
Irrlicht has some a function IMeshManipulator::recalculateNormals which you can use by the way. You only have to be careful that the winding of the vertices for each triangle is correct for this to work I suppose.
Winding?


So as it stands in my head, this is how I will write my voxel thing?

1. Generate a set of 6 vertices for each side of the cube.
2. Generate indice mappings for each cube.
3. Pass all that into an instance of SMeshBuffer
4. Calculate normals; (work out co-ords of perpendicular point from surface)
5. Render.

Does anyone have an example lying around of when they have used SMeshBuffers to build their own geometry from scratch? That would make things supremely easier.

Thankyou all so much for your help ^^
Tempest
Posts: 9
Joined: Fri Jan 11, 2008 11:26 pm

Re: Manually creating meshes (from vertexes)

Post by Tempest »

twitchyliquid64 wrote:Winding?
Means you have to give the vertices for the face in clockwise order.
twitchyliquid64 wrote:1. Generate a set of 6 vertices for each side of the cube.
4 per side will do, or 8 for the entire cube. You can use each vertex more than once, For example 0-1-2 and 1-3-2.
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

And lastly, indexs (as in IIndexBuffer) are the same as indices, right?

Also,

Code: Select all

 
                    scenemgr->drawAll();
                    
                    u16 indices[] = {   0,2,3, 2,1,3, 1,0,3, 2,0,1  };
                    video::IVideoDriver* driver = scenemgr->getVideoDriver();
 
                    driver->setMaterial(Material);
                    core::matrix4 AbsoluteTransformation;
 
            AbsoluteTransformation.setRotationDegrees(core::vector3df(0,0,0));
            AbsoluteTransformation.setTranslation(core::vector3df(0,0,0));
 
                    driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
                    driver->drawIndexedTriangleList(&Vertices[0], 4, &indices[0], 4);
                    
                    driver->endScene();
 
Is very similar to tutorial three, but I cant get it working (nothing appears). The problem is the

Code: Select all

 
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
 
I want to set the position of the object to 0,0,0 and I really dont know how to manipulate this core::matrix4 structure that setTransform uses.
Tempest
Posts: 9
Joined: Fri Jan 11, 2008 11:26 pm

Re: Manually creating meshes (from vertexes)

Post by Tempest »

You never create a mesh there.

Try something like this:

Code: Select all

irr::scene::SMesh* mymesh;
 
    mymesh = new irr::scene::SMesh();
    
    irr::scene::SMeshBuffer *buf = 0;
    buf = new irr::scene::SMeshBuffer();
    mymesh->addMeshBuffer(buf);
    
    buf->drop();
After that, you feed your data into buf->vertices and buf->indices.

Code: Select all

buf->Vertices[i] = irr::video::S3DVertex( ... )
buf->Indices[i] = ...
finally

Code: Select all

buf->recalculateBoundingBox();
irr::scene::IMeshSceneNode* node = smgr->addMeshSceneNode( mymesh );
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Manually creating meshes (from vertexes)

Post by hybrid »

The SMeshBuffer creation is shown in example 23. The winding order defines the front side of your triangles. If you look onto the triangle, and the indices are given in clock-wise order, everything is correct. Otherwise use the indices in the backwards order.
For the voxel scenarios, you should not use the cube primitives. You will get too many scene nodes by this. So you must use Meshbuffers and your own meshes. Or just use a ready-made voxel engine, which you can also connect to Irrlicht.
twitchyliquid64
Posts: 11
Joined: Sun Jul 08, 2012 11:49 am

Re: Manually creating meshes (from vertexes)

Post by twitchyliquid64 »

Thankyou all so much! Thats everything I need to make my chunk class and start building a cubic voxel engine.


One absolutely final thing: adding different textures for each cube face, like the minecraft 'grass' block.

How do I texture each face of the cube?

Im guessing textures are called 'Materials' In irrlicht.
So how can I 'set' the material of an SMesh/SMeshBuffer/MeshSceneNode? and then how to I set the texture co-ordinates of each vertex such to draw the correct tile?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Manually creating meshes (from vertexes)

Post by CuteAlien »

You have 2 choices - you can have different materials per meshbuffer (that's more or less what a meshbuffer is there for - 1 meshbuffer = 1 material). The other (probably better) solution is to use uv-coordinates. You have those in each SVertex and they are numbers between 0 and 1 for the position of that vertex on your texture (so 0 is for left and top and 1 is for right and bottom while 0.5 is exactly the center etc.).
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
Post Reply