Confused about vertices in Irrlicht

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
Ceultem
Posts: 7
Joined: Sat Feb 25, 2017 10:44 am

Confused about vertices in Irrlicht

Post by Ceultem »

Hi!

I'm new to 3D programming, and I have now started to play around and program with Irrlicht. I recently started checking the Irrlicht tutorial number 3, which covers creating your own custom scene node. I am new to 3D programming, so I was a bit confused about vertices and indices. I therefore decided to look for more information about them on the internet.

So, if I have understood correctly, vertices are just points in 3D space, and indices tell which vertices should be connected to make a triangle, and these triangles then form the 3D object. So for example a cube would have 8 vertices (corners). The cube could the be formed by using indices to tell which of those vertices need to be connected to form the triangles. Each face of the cube would be formed from two triangles, and so the whole cube would be formed by 12 triangles. :)

Have I understood these things correctly? This all seems very logical and even beautiful. :)

Currently however I'm a bit confused about the vertices in Irrlicht. The reason for that is that the constructor for irr::video::S3DVertex takes in so many values:

Code: Select all

S3DVertex (const core::vector3df &pos, const core::vector3df &normal, SColor color, const core::vector2d< f32 > &tcoords)
If a vertex is just a point in 3d space, why are this many inputs required? I understand that the first vector tells the position of the vertex and the color value is used for coloring the 3d objects. But what are the other two vectors, normal and tcoords, used for?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: Confused about vertices in Irrlicht

Post by Seven »

i am no expert........

vertices can be just 3d points, but most shaders etc, would like much more information than just position.
for example, lighting calculations need normals in order to calculate the bounce of a light.
irrlicht vertices have the position of the vertice, in addition to other information.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Confused about vertices in Irrlicht

Post by CuteAlien »

As Seven mentioned - normals are mostly used in light calculations. The "tcoords" are more commonly called uv-coordinates and describe which point on a texture is needed for that vertex. (0,0) is left-top and (1,1) right-bottom of a texture. Those texture coordinates are also the main reason why you might need more than just 8 vertices for a cube. Because you generally want different uv-coordinates for the top and bottom plane of a cube than for the sides (you might even want different uv's for each side). In that case they need their own vertices which have the same position but different uv coordinates.

S3DVertex2TCoords has a second set of uv-coordinates which allows you to combine textures with different uv values on the same polygon (and yes, having even more than 2 different uv's for even more textures would be useful... I guess that vertex format is going to change one day to allow that).

S3DVertexTangents has some values needed needed by normal-maps (the kind of textures which looks like it has depth information).
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
Ceultem
Posts: 7
Joined: Sat Feb 25, 2017 10:44 am

Re: Confused about vertices in Irrlicht

Post by Ceultem »

Okay, thanks for the answers for both of you! :) Your answers cleared up the confusion. =)

I have now done some testing with these vertex things in the style of the tutorial number 3. I'm pretty much using the same code as in the tutorial, but I first tried to create a custom scene node with only 1 triangle. That worked fine, the triangle was spinning in the screen nicely. :) Although while rotating, it sometimes disappeared, soon coming back to visible again. I suppose the disappearing is because of the triangle getting perpendicular with the camera.

I then tried creating a custom scene node with two triangles that would create a 2-dimensional square. I defined the vertices like this:

Code: Select all

 
vertices[0] = video::S3DVertex(0,0,0, 1,1,0, video::SColor(255,0,255,0), 0,1);
vertices[1] = video::S3DVertex(0,10,0, 1,0,0, video::SColor(255,255,0,0), 1,1);
vertices[2] = video::S3DVertex(0,0,10, 0,1,1, video::SColor(255,0,0,255), 1,1);
vertices[3] = video::S3DVertex(0,10,10, 0,0,1, video::SColor(255,0,0,255), 0,0);
 
And then on the render function I did this:

Code: Select all

 
u16 indices[] = {0,1,2, 1,2,3};
video::IVideoDriver* driver = SceneManager->getVideoDriver();
driver->setMaterial(material);
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
driver->drawVertexPrimitiveList(&vertices[0], 4, &indices, 2, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
 
With this code I do get two triangles, but they are not forming a square. In fact, I can't even see them on the screen at the same time. When the object is rotating, only one of the triangles is visible at all times. Why does this happen? Shouldn't both of the triangles be flat on the YZ-plane? And if they are, shouldn't they be visible at the same time and form a square? Have I defined the vertices or indices somehow wrongly? If I have understood correctly, my vertices should be on the YZ-plane like this:

vertex 0----> 0,0--------------0,10 <--------vertex 2
| |
| |
vertex 1---->10,0-----------------10,10 <------ vertex 3

And thanks for your previous answers once more! :)

EDIT: The forum software seems to have removed the whitespace from my 'square', but you get the idea. :D
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Confused about vertices in Irrlicht

Post by CuteAlien »

2 things.

First one is that index order matters. Your first triangle is clockwise - the second one is counter-clockwise. They have to be the same (clockwise). This is used in Irrlicht (and direct3d and opengl) to define which side is to the front and which one to the back.
(Minor side-note: Other software like Blender uses the normals to decide on front/back-side. When exporting to triangles the normal direction is used to decide on the triangle-order. In general it's helpful to have the normal point to the front - meaning in Irrlicht when you look at a clock the normal would be coming out towards you).
edit: Also tiny trick if you suspect something is wrong with front/back face order. Disable the backface-culling in SMaterial - if it works without backface culling then it's nearly certainly a problem with that.

Second thing (not the problem here, but will be a problem when you add light) is that your normals are wrong. They are usually perpendicular to the plane. As our normals are per vertex and not per plane and a vertex can be shared by several triangles we usually will use the average of the plane-normals for each polygon touching the vertex. So for your case with polygon on the yz plane the normal would point along the x-axis (1,0,0). Also even if you don't want perpendicular normals for some reason (very special light requirements for example) the normals still should be normalized (length = 1). So a normal like (1,1,0) which is len=sqrt(2) instead of 1 is bad (core::vector3df has a normalize function for this).
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
Ceultem
Posts: 7
Joined: Sat Feb 25, 2017 10:44 am

Re: Confused about vertices in Irrlicht

Post by Ceultem »

Thank you very much CuteAlien! :)
I now understand the vertices and indices in Irrlicht, along with the normals and tcoords. =) I was able to make the square work, and then I also successfully made a beautiful cube. Thanks for helping me get started in the world of 3D programming! :)
mnunesvsc
Posts: 22
Joined: Sun May 28, 2006 9:04 pm
Contact:

Re: Confused about vertices in Irrlicht

Post by mnunesvsc »

Can you post your experience, your code of the square, thanks in advance
Post Reply