LeonNeol

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?
LeonNeol
  • 記事: 10

Nate

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.
アバター
Nate

Nate
  • 記事: 11351

LeonNeol

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?
LeonNeol
  • 記事: 10

Nate

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
アバター
Nate

Nate
  • 記事: 11351

LeonNeol

That’s so awesome! Thanks for the help and responses!
LeonNeol
  • 記事: 10


Return to Runtimes