How to set correct order for vertices in draw2DVertexPrimitiveList

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
n00bc0de
Posts: 147
Joined: Tue Oct 04, 2022 1:21 am

How to set correct order for vertices in draw2DVertexPrimitiveList

Post by n00bc0de »

I have a function thats suppose to draw a solid color triangle but nothing gets drawn. I tried it with and without array.sort() and it still gives me inconsistent resutls. It will draw if I change the order of the indices but the order seems to be different for different vertices so I am not sure how to figure it out.

Here is my code for drawing the triangle (NOTE: I have tried it with and without sort() and still get inconsistent results)

Code: Select all

void rc_drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
	irr::core::array<irr::video::S3DVertex> v;
    v.push_back(video::S3DVertex(x1, y1, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));
    v.push_back(video::S3DVertex(x2, y2, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));
    v.push_back(video::S3DVertex(x3, y3, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));

    irr::core::array<irr::u16> i;
    i.push_back(0);
    i.push_back(1);
    i.push_back(2);

    v.sort();

    VideoDriver->draw2DVertexPrimitiveList(v.pointer(), 3, i.pointer(), 1);
}
n00bc0de
Posts: 147
Joined: Tue Oct 04, 2022 1:21 am

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by n00bc0de »

I think I solved it by sorting by the X position in descending order. I don't know whether my solution is a fluke or not but it seems to be working for everything I have tried so far.
CuteAlien
Admin
Posts: 10057
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by CuteAlien »

Don't use sort. What you have to think about is: When you look at a polygon are the 3 points sorted clockwise or counterclockwise. That's what defines what the front and the back-face of the polygon is. Irrlicht polygons should be clockwise (so that's the front) like in D3D (and unlike in GL, Irrlicht flips some matrix internally to get the same polygons working in GL).

Also other things that can go wrong:
- You need to have the correct world-matrices. Usually at the start you'll use setTransform(video::ETS_WORLD, core::IdentityMatrix) so your polygon and world coordinates are just identical.
- The camera has to look at your polygon (toward it's front). So you set the camera position at negative z and and the target at positive z (because Irrlicht uses a left-handed coordinate system so z goes into the screen).
- You have to set a material before calling draw2DVertexPrimitiveList
- When your material has lighting enabled you also need a light which is affecting the polygon.

Things that might help:
- Disable lighting in the material then you just see the vertex colors and don't have to figure out lighting
- To check if you messed up the front/backface thing (aka clockwise vs counterclockwise sorting) you can disable backface culling in the material. If the polygon then suddenly show up you know you got the order wrong.
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
n00bc0de
Posts: 147
Joined: Tue Oct 04, 2022 1:21 am

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by n00bc0de »

Thanks. The main thing I needed to know which you answered is that they should be clockwise. I modified my code to sort them into clockwise order.

Code: Select all

void rc_drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
	irr::core::array<irr::video::S3DVertex> v;
    v.push_back(video::S3DVertex(x1, y1, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));
    v.push_back(video::S3DVertex(x2, y2, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));
    v.push_back(video::S3DVertex(x3, y3, 0.f, 0.f, 1.f, 0.f, rc_active_color, 0.5f, 0.5f));

    irr::video::S3DVertex tmp_v;

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if( v[i].Pos.X < v[j].Pos.X )
            {
                tmp_v = v[i];
                v[i] = v[j];
                v[j] = tmp_v;
            }
        }
    }

    // swap the 1st and 3rd vertex if the middle vertex is greater
    // NOTE: These are 2D coordinates so Y increases going down
    if(v[1].Pos.Y > v[2].Pos.Y)
    {
        tmp_v = v[0];
        v[0] = v[2];
        v[2] = tmp_v;
    }

    irr::core::array<irr::u16> i;
    i.push_back(0);
    i.push_back(1);
    i.push_back(2);

    VideoDriver->draw2DVertexPrimitiveList(v.pointer(), 3, i.pointer(), 1);
}
CuteAlien
Admin
Posts: 10057
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by CuteAlien »

Hm, can't say if that code works or not. Though I appreciate anyone trying to do their own algorithm!

But if it's just 2d you can maybe use the cross-product between 2 sides. Like vectors v0v1 vs v1v2 and then check if z is < or > 0. If your polygon is flat on the plane then the cross product will give you a vector going up/down perpendicular to that plane and it changes direction depending on the winding order. And probably any single swap should be enough with 3 points.
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
n00bc0de
Posts: 147
Joined: Tue Oct 04, 2022 1:21 am

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by n00bc0de »

I am using it in 2D. I will look at my code to see if you method will work better. My current solution has worked on all the triangles I have tried it with but I probably need a more thorough test to ensure any solution I go with covers every case.

I am a little surprised that nobody else has had this issue already.
CuteAlien
Admin
Posts: 10057
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to set correct order for vertices in draw2DVertexPrimitiveList

Post by CuteAlien »

You probably mostly run into it when you do some kind of editor where users can place their own triangles. When creating my own small custom meshes I tend to use pen & paper. And as usual there's probably more ways to do this, I don't know what the most common method is. One other idea I had was creating a right angle for one edge and check if it's going closer or further way to the 3rd point (dot product can check this and creating right angle in 2d is something like -x,y or x,-y). All those methods probably have trouble with degenerate triangles btw (all 3 point on a line), but I guess nothing one can do about that.
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