直接放例子代码,代码中以任意四边形为例,如果需要做任意多边形,注意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);
}

  

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

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

    在gis系统中 经常会用到一些裁剪的方法,首先推荐一个非常好用的空间分析JavaScript库--Turf.js,不仅功能强大.使用简单,同时处理速度也很快. Turf.js中提供了一中多边形的裁剪方 ...

  2. css3创建多边形clip属性,可用来绘制不规则图形了

    .path1 { clip-path: polygon(5px 10px, 16px 3px, 16px 17px); } .path2 { clip-path: polygon(3px 5px, 1 ...

  3. css3的clip-path方法剪裁实现

    本例讲解如何通过clip-path把一个div(元素,可以是图片等)裁切成不同的形状,这里以一个div为例宽高均为300px 注意:不支持IE和Firefox,支持webkit浏览器,在现代浏览器中需 ...

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

    libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟. Polygon ...

  5. 百度地图笔记_覆盖物(标注marker,折线polyline,多边形polygon)的点击弹窗和右键菜单

    利用绘制工具绘制点线面,并在执行绘制完成回调事件给相应覆盖物添加事件操作,提供标注的点击弹窗和标注.折线.多边形的右键删除 效果图如下: 完整代码如下:html+js <!DOCTYPE htm ...

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

    [OpenGL][SharpGL]用Polygon Offset解决z-fighting和stitching问题 本文参考了(http://www.zeuscmd.com/tutorials/open ...

  7. [LeetCode] Convex Polygon 凸多边形

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  8. [算法]A General Polygon Clipping Library

    A General Polygon Clipping Library Version 2.32    http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...

  9. 【LeetCode】469. Convex Polygon 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算向量夹角 日期 题目地址:https://leet ...

随机推荐

  1. 曲线救国:IIS7集成模式下如何获取网站的URL

    如果我们在Global中的Application_Start事件中访问HttpContext.Current.Request对象,如: protected void Application_Start ...

  2. 通过ios实现RSA加密和解密

    在加密和解密中,我们需要了解的知识有什么事openssl:RSA加密算法的基本原理:如何通过openssl生成最后我们需要的der和p12文件. 废话不多说,直接写步骤: 第一步:openssl来生成 ...

  3. Oracle行转列、列转行的Sql语句总结(转)

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...

  4. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  5. maven-replacer-plugin

    今天多认识了下这个maven插件. 基本用法: <plugin> <groupId>com.google.code.maven-replacer-plugin</grou ...

  6. Intellij IDEA中的Mybatis Plugin破解

    具体的破解过程请看:https://github.com/luyanliang/profile/blob/master/idea/plugin/MybatisPlugin/Mybatis-Plugin ...

  7. Android开发常用工具类

    来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...

  8. sprintf溢出的bug

    向printf.sprintf这种函数在编译时很难检查错误,所以程序员必须小心.比如我就遇到了这样的bug: void test() { ]; sprintf(t,"); } //执行spr ...

  9. BDYY【面试题】

    1.引用与多态的关系:引用是除指针外另一个可以产生多态效果的手段.这意味着,一个基类的引用可以指向它的派生类实例. 2.C++可以多继承 3.引用与指针区别:

  10. JQuery上传插件Uploadify使用详解

    本文转载http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不错 ...