Open Scene Graph - Usage of DrawElementsUInt: Drawing a cloth without duplicating vertices

439 views Asked by At

I am currently working on simulating a cloth like material and then displaying the results via Open Scene Graph. I've gotten the setup to display something cloth like, by just dumping all the vertices into 1 Vec3Array and then displaying them with a standard Point based DrawArrays. However I am looking into adding the faces between the vertices so that a further part of my application can visually see the cloth.

This is currently what I am attempting as for the PrimitiveSet

    // create and add a DrawArray Primitive (see include/osg/Primitive).  The first
    // parameter passed to the DrawArrays constructor is the Primitive::Mode which
    // in this case is POINTS (which has the same value GL_POINTS), the second
    // parameter is the index position into the vertex array of the first point
    // to draw, and the third parameter is the number of points to draw.
    unsigned int k = CLOTH_SIZE_X;
    unsigned int n = CLOTH_SIZE_Y;
    osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt(GL_QUADS, (k) * (n));
    for (uint y_i = 0; y_i < n - 1; y_i++) {
        for (uint x_i = 0; x_i < k - 1; x_i++) {
            (*indices)[y_i * k + x_i] = y_i * k + x_i;
            (*indices)[y_i * (k + 1) + x_i] = y_i * (k + 1) + x_i;
            (*indices)[y_i * (k + 1) + x_i + 1] = y_i * (k + 1) + x_i + 1;
            (*indices)[y_i * k + x_i] = y_i * k + x_i + 1;
        }
    }

    geom->addPrimitiveSet(indices.get());

This does however cause memory corruption when running, and I am not fluent enough in Assembly code to decipher what it is trying to do wrong when CLion gives me the disassembled code.

My thought was that I would iterate over each of the faces of my cloth and then select the 4 indices of the vertices that belong to it. The vertices are inputted from top left to bottom right in order. So:

0  1    2    3    ... k-1
k  k+1  k+2  k+3  ... 2k-1
2k 2k+1 2k+2 2k+3 ... 3k-1 
...

Has anyone come across this specific use-case before and does he/she perhaps have a solution for my problem? Any help would be greatly appreciated.

1

There are 1 answers

0
XenonofArcticus On

You might want to look into using DrawArrays with QUAD_STRIP (or TRIANGLE_STRIP because quads are frowned upon these days). There's an example here: http://openscenegraph.sourceforge.net/documentation/OpenSceneGraph/examples/osggeometry/osggeometry.cpp

It's slightly less efficient than Elements/indices, but it's also less complicated to manage the relationship between the two related containers (the vertices and the indices).

If you really want to do the Elements/indices route, we'd probably need to see more repro code to see what's going on.