Libgdx clipping polygons (clip polygon, masking polygon)

Source: Internet
Author: User
Tags libgdx

Directly put the example code, the code in the arbitrary four-sided shape for example, if you need to do freeform, note LIBGDX can not be directly filled with shaperender polygon, need to first cut into triangles.

public static void Drawclip (batch batch, Polygon Polygon, Textureregion region, float x, float y) {float[] Vertice        s = polygon.getvertices ();        if (shapes = = null) {shapes = new shaperenderer (); }//2.        Clear our depth buffer with 1.0 GDX.GL.GLCLEARDEPTHF (1f);        Gdx.gl.glClear (Gl20.gl_depth_buffer_bit); 3.        Set the function to less Gdx.gl.glDepthFunc (gl20.gl_less); 4.        Enable depth writing Gdx.gl.glEnable (gl20.gl_depth_test); 5.        Enable depth Writing, disable RGBA color writing Gdx.gl.glDepthMask (true);        Gdx.gl.glColorMask (False, False, false, false); Draw mask shape (s)//6.        Render your primitive Shapes Shapes.begin (ShapeRenderer.ShapeType.Filled);        Shapes.setcolor (1f, 0f, 0f, 0.5f);        Shapes.identity ();        Shapes.triangle (Vertices[0], vertices[1], vertices[4], vertices[5], vertices[2], vertices[3]); Shapes.triangle (Vertices[0], veRTICES[1], vertices[4], vertices[5], vertices[6], vertices[7]);//Shapes.polyline (Polygon.getvertices ());        Shapes.end ();        Draw Sprite (s) to be masked if (!batch.isdrawing ()) {batch.begin (); }//8. Enable RGBA Color Writing//(Spritebatch.begin () would disable depth mask) Gdx.gl.glColorMask (true, True,        True, true); 9.        Make sure testing is enabled.        Gdx.gl.glEnable (gl20.gl_depth_test); 10.        Now depth discards pixels outside our masked shapes Gdx.gl.glDepthFunc (gl20.gl_equal);        Push to the batch Batch.draw (region, X, y);        End/flush your batch batch.end ();    Gdx.gl.glDisable (gl20.gl_depth_test); }

  

Libgdx clipping polygons (clip polygon, masking polygon)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.