• Runtimes
  • How to get Clipping attachment to draw

I read in the User Guide that one can boost performance with Clipping Attachment with Stencil Mask. I am using Raylib runtime (https://github.com/WEREMSOFT/spine-raylib-runtimes (based on http://esotericsoftware.com/spine-c)) with direct access to OpenGL so I think such option is feasible. I want to draw the Clipping attachment to a fbo with the visible area appears as white on the fbo's black background. Then apply that mask following this: https://stackoverflow.com/questions/5097145/opengl-mask-with-multiple-textures

However Clipping Attachment was programmed in a way that support other runtimes, which wasn't meant to be drawn, so it doesn't seem to have other properties like the other codes for rendering meshes (texture, uv, colors, triangles, trianglesCount) and to be honest I don't really understand them. Can someone provide some guide for rendering Clipping Attachment as if it was a mesh?

Related Discussions
...
  • Изменено

This is not really a simple task. First you'd use ClippingAttachment computeWorldVertices to get the world coordinates that make up the polygon you want to render. Then you need to render the polygon using your game toolkit or OpenGL. There are many ways to do that, but nothing in the Spine Runtimes renders in that way. Meshes are textured quads, so not the same. You can Google it but you probably want something like this:
https://community.khronos.org/t/concave-polygon-via-stencil-buffer/63826/4
I suggest getting simple polygon rendering to work before trying to render your clipping polygon.

Note the same can be done for a bounding box. If you don't actually want clipping, you just want to render the polygon, then you should use a bounding box instead.

It is indeed a complicated task. However by using the Triangulator provided by the Spine Runtimes I managed to do it, worked for both concave and convex clipping polygon.

Here is my working code:

if (attachment->type == ...) {...} //Other types
else if (attachment->type == SP_ATTACHMENT_CLIPPING) {

spClippingAttachment *mesh = (spClippingAttachment *) attachment;
spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, mesh->super.worldVerticesLength,
                                        worldVerticesPositions, 0, 2);

spTriangulator* triangulator = spTriangulator_create();
spFloatArray* verticesArray = spFloatArray_create(mesh->super.worldVerticesLength);
spFloatArray_addAllValues(verticesArray, worldVerticesPositions, 0, mesh->super.worldVerticesLength);
spShortArray* triangles = spTriangulator_triangulate(triangulator, verticesArray);

glBegin(GL_TRIANGLES); //Draw Triangles
    glColor4ub(255, 255, 255, 255); //White
    for (int i = 0; i < triangles->size; i++)
    {
        int index = triangles->items[i] << 1;
        glVertex2f(position.x + worldVerticesPositions[index], position.y + worldVerticesPositions[index + 1]);
    }
glEnd();
//Free arrays...
}

The Triangulator is amazing btw, could it be a separate library?

Nice! That is a decent way to do it. You can also draw a filled polygon using OpenGL without needing the triangulation by using the stencil buffer, something like this:
https://gist.github.com/983/79c20c447457b1259ae1380ba591a42f

Thanks, I agree the triangulator in the Spine Runtimes is pretty neat! We don't provide it as FOSS, but we do provide other triangulators as part of libgdx under the Apache 2 license (Mario and I are the libgdx copyright holders):
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/math/EarClippingTriangulator.java
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/math/DelaunayTriangulator.java

That’s so awesome! Thanks for the help and responses!

8 месяцев спустя

I have a question about proposed solution by Nate. I'm not sure how you can support concave clipping shape without tringulation. Link to draw trinagles fan with OpenGL will not work with concave mesh, if I undestand everything correct.

What I suggested was only about drawing a polygon. Using a triangle fan and stencil buffer, you can draw a concave polygon without triangulation. It's a neat trick! Above I linked an executable demo and here's a link to more about it:
http://www.glprogramming.com/red/chapter14.html#name13

You won't be able to directly perform clipping this way, only to draw the clipping polygon. However, I suppose you could draw the clipping polygon to a buffer, then use THAT as a mask to perform clipping, for example in a shader.

I understood that this is to draw polygon. This is a nit trick to draw concave polygon, I dind't know that, it was the thing I missed, thank you!