libgdx 裁剪多边形(clip polygon、masking polygon)

直接放例子代码,代码中以任意四边形为例,如果需要做任意多边形,注意libgdx不能直接用ShapeRender填充多边形,需要先切割成三角形。

public static void drawClip(Batch batch, Polygon polygon, TextureRegion region, float x, float y) {
        float[] vertices = 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() will 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);
    }

  

时间: 2024-08-26 14:29:43

libgdx 裁剪多边形(clip polygon、masking polygon)的相关文章

WebGIS裁剪算法-线裁剪多边形

在gis系统中 经常会用到一些裁剪的方法,首先推荐一个非常好用的空间分析JavaScript库--Turf.js,不仅功能强大.使用简单,同时处理速度也很快. Turf.js中提供了一中多边形的裁剪方法是使用多边形去裁剪多边形,但是如果实际工作中需要使用到线去裁剪多边形却无法满足. http://turfjs.org/docs#bboxClip 这边文章使用turf.js的基本方法,在此基础上构建了线裁剪多边形的方法. 点击可查看在线demo demo预览 算法原理 (一)单个polygon的裁

libgdx学习记录26——Polygon多边形碰撞检测

libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon内部自带是否包含点contains这个函数,通过这个函数我们可以判断两个多变行是否碰撞,即检测两个多边形的每个点是否在另一个多边形中. 检测代码: 1 public static boolean isOverlap(Polygon polygon1, Polygon polygon2){ 2 for

[OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题

[OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题 本文参考了(http://www.zeuscmd.com/tutorials/opengl/15-PolygonOffset.php),用SharpGL重写了示例代码,您可以点击文末的链接下载. 什么是stitching和z-fighting 在OpenGL中,如果想绘制一个多边形同时绘制其边界,可是先使用多边形模式GL_FILL绘制物体,然后使用多边形模式GL_LINE和不同的颜色

[算法]A General Polygon Clipping Library

A General Polygon Clipping Library Version 2.32    http://www.cs.man.ac.uk/~toby/alan/software/gpc.html Alan Murta Advanced Interfaces Group Department of Computer Science University of Manchester Manchester M13 9PL, UK Abstract: This document descri

矩形窗口裁剪(以裁剪直线和复杂多边形为例)

今天yogurt想要和大家分享一个大家在玩电脑时经常会用到的一个功能"窗口裁剪"的C语言编程实现方法~~相信用过QQ截屏或者其他截屏软件的盆友都知道截屏就是对一个图形或者图案用一个矩形框或者圆形框框起来,只保留框内的内容,而框外的内容自动舍去.那么它是怎么实现的呢?今天就让美丽可爱善良机智的yogurt来告诉你这个神奇的东东吧! ===================================yogurt小课堂开课啦================================

[LeetCode] Convex Polygon 凸多边形

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

任意多边形切割/裁剪(附C#代码实现)

本实现主要参考了发表于2003年<软件学报>的<一个有效的多边形裁剪算法>(刘勇奎,高云,黄有群)这篇论文,所使用的理论与算法大都基于本文,对论文中部分阐述进行了详细解释,并提取了论文中一些重要的理论加以汇总.另外对于论文描述无法处理的一些情况也进行了试探性的分析. 多边形裁剪用于裁剪掉被裁剪多边形(又称为实体多边形,后文用S表示)位于窗口(又称为裁剪多边形,后文用C表示)之外的部分.裁剪的结果多边形是由实体多边形位于裁剪多边形内的边界和裁剪多边形位于实体多边形内的边界组成的.见下

Leetcode: Convex Polygon

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). Note: There are at least 3 and at most 10,000 points. Coordinates are in the range -10,000 to 10,000. You may assume the

flex polygon 序列化为txt 文本

当我们要把一个地块导出为txt的时候,应该怎么写,这是比较有用的这样可以帮助我们存档之类的,这里是基于某个地方的独立坐标系,是基于自己发布地图,如果是用百度地图或者其他网上的地图可能不适用. 1 package com.szpl.extension.util 2 { 3 import com.esri.ags.geometry.MapPoint; 4 import com.esri.ags.geometry.Polygon; 5 import com.esri.ags.layers.Graphi