在opengl中, 可以开启模板测试功能,来限定某一部分是可画的,某一部分是不可画的。这可通过设置模板模式来控制哪部分是可画的, 有点类似在墙上喷字。

虽说剪刀测试也可以限定蓝屏的某一部分可画,但不适用于不规则的区域,不如模板测试灵活。下面提供一个Android模板测试的实例代码:

public class MyRenderer implements Renderer {private Square square1 = null;private FloatBuffer vertices = null;// Step size in x and y directions// (number of pixels to move each time)private float x = 0;private float y = 0;private float xstep = 1.0f;private float ystep = 1.0f;// Keep track of windows changing width and heightprivate float windowWidth;private float windowHeight;public MyRenderer(Context ctx) {square1 = new Square(ctx, false);}private void makeStencilPattern(GL10 gl) {float dRadius = 0.1f;ByteBuffer bb = ByteBuffer.allocateDirect(4000 * 4 * 2);bb.order(ByteOrder.nativeOrder());vertices = bb.asFloatBuffer();vertices.position(0);for(float dAngle = 0; dAngle < 400.0; dAngle += 0.1){vertices.put((float) (dRadius * Math.cos(dAngle)));vertices.put((float) (dRadius * Math.sin(dAngle)));dRadius *= 1.002;}vertices.position(0);}@Overridepublic void onDrawFrame(GL10 gl) {gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_STENCIL_BUFFER_BIT);// Replace the current matrix with the identity matrixgl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();gl.glStencilFunc(GL10.GL_NEVER, 0x0, 0x0);gl.glStencilOp(GL10.GL_INCR, GL10.GL_INCR, GL10.GL_INCR);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);gl.glColor4f(0, 1, 1, 1);gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 4000 * 2);gl.glStencilFunc(GL10.GL_NOTEQUAL, 0x1, 0x1);gl.glStencilOp(GL10.GL_KEEP, GL10.GL_KEEP, GL10.GL_KEEP);gl.glColor4f(1, 0, 0, 1);// Reverse direction when you reach left or right edgeif(x > windowWidth-20 || x < -windowWidth)xstep = -xstep;// Reverse direction when you reach top or bottom edgeif(y > windowHeight || y < -windowHeight + 20)ystep = -ystep;// Check bounds. This is in case the window is made// smaller while the rectangle is bouncing and the // rectangle suddenly finds itself outside the new// clipping volumeif(x > windowWidth-20)x = windowWidth-20-1;if(y > windowHeight)y = windowHeight-1; // Actually move the squarex += xstep;y += ystep;gl.glTranslatef(x, y, 0);square1.draw(gl);}@Overridepublic void onSurfaceChanged(GL10 gl, int w, int h) {float aspectRatio;// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensionsgl.glViewport(0, 0, w, h);// Reset coordinate systemgl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();// Establish clipping volume (left, right, bottom, top, near, far)aspectRatio = (float)w / (float)h;if (w <= h) {windowWidth = 100;windowHeight = 100 / aspectRatio;gl.glOrthof(-100.0f, 100.0f, -windowHeight, windowHeight, 1.0f, -1.0f);}else {windowWidth = 100 * aspectRatio;windowHeight = 100;gl.glOrthof(-windowWidth, windowWidth, -100.0f, 100.0f, 1.0f, -1.0f);}gl.glMatrixMode(GL10.GL_MODELVIEW);gl.glLoadIdentity();}@Overridepublic void onSurfaceCreated(GL10 gl, EGLConfig config) {// TODO Auto-generated method stubgl.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);  // Enable Smooth Shading, default not really needed.gl.glShadeModel(GL10.GL_SMOOTH);gl.glEnable(GL10.GL_STENCIL_TEST);gl.glClearStencil(0);makeStencilPattern(gl);}}

转载于:https://my.oschina.net/fuyajun1983cn/blog/263963

opengl模板测试实例相关推荐

  1. OpenGL模板测试通俗理解

    看书籍或资料往往还是云里雾里的,这里写一下自己的理解.希望对需要的人有帮助. 简而言之: 模板缓冲区和帧缓冲区一样大 模板缓冲区初始状态是什么? 在glClearBuffer的时候清空,所以初始状态是 ...

  2. opengl 模板测试 glStencilOp glStencilFunc

    下面来设置蒙板缓存和蒙板测试. 首先我们启用蒙板测试,这样就可以修改蒙板缓存中的值. 下面我们来解释蒙板测试函数的含义: 当你使用glEnable(GL_STENCIL_TEST)启用蒙板测试之后,蒙 ...

  3. OPenGL模板缓冲区示例程序

    模板测试是把像素存储在模板缓冲区的值与一个参考值进行比较.根据测试的结果,对模板缓冲区中得这个值进行相应的修改. Note:模板测试只有在存在模板缓冲区的情况下才会执行,如果不存在模板缓冲区,模板测试 ...

  4. OpenGL stencil test模板测试的实例

    OpenGL stencil test模板测试 先上图,再解答. 完整主要的源代码 源代码剖析 先上图,再解答. 完整主要的源代码 #include <glad/glad.h> #incl ...

  5. (zt)OpenGL中的Alpha测试,深度测试,模板测试,裁减测试

    转自http://www.cppblog.com/flashboy/archive/2009/09/01/94974.html 大家好.现在因为参加工作的关系,又是长时间没有更新.趁着国庆的空闲,总算 ...

  6. OpenGL基础30:模板测试

    前置:OpenGL基础29:深度测试 一.模板测试 前面一章提到过:深度缓冲测试在片段着色器运行.以及模板测试(Stencil Testing)之后,那么这下知道模板测试是在什么时候了吧,模板测试和深 ...

  7. OpenGL学习笔记(七)-深度测试-模板测试-混合

    参考网址:LearnOpenGL 中文版 第四章 高级OpenGL 4.1 深度测试 4.1.1 深度缓冲 1.深度缓冲用来防止被阻挡的面渲染到其它面的前面,由窗口系统自动创建,在每个片段中储存了它的 ...

  8. OpenGL学习二十九:模板缓冲区与模板测试

    帧缓冲区有许多缓冲区构成,这些缓冲区大致分为: 颜色缓冲区:用于绘图的缓冲区,它包含了颜色索引或者RGBA颜色数据. 深度缓冲区:存储每个像素的深度值,当启动深度测试时,片段像素深度值和深度缓冲区深度 ...

  9. OpenGL程序:实例练习

    说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640*480分辨率.16色 ...

最新文章

  1. vlan指的是什么?—Vecloud微云
  2. ./configure会报错:pr command not found
  3. 6常见的HTML和CSS面试问答
  4. [运维]PowerShell简体中文编码转换
  5. IE6的3像素神奇bug:缘起与解决方案
  6. Windows cmd常用命令
  7. 新手怎么在GitHub上传代码?----最新教程
  8. mysql-bin文件删除与产生的原因
  9. 小米笔记本Ruby默认开启fn键如何解决方法步骤
  10. 如何将mp3转wav格式?
  11. 程序员增加收入实用指南,基于android的app开发平台综述
  12. 合同智能审核软件-提高审查效率和准确性
  13. Android利用TranslateAnimation 动画实现上下平移横线(模拟人脸识别扫描,二维码识别扫描)
  14. C++ QT调用python脚本并将软件打包发布
  15. 大数据技术原理与应用 实验6 Spark数据处理系统的搭建
  16. linux 内核 mtd读取,MTD坏块管理(二)-内核获取Nandflash的参数过程
  17. php empty 注意
  18. vscode使用chatGPT
  19. 磊科nw705p虚拟服务器设置,教你如何设置磊科nw705p无线路由器的详细步骤【图文】...
  20. 支持qm/mm的gromacs编译(gromacs+cp2k)

热门文章

  1. C++输出流的格式控制
  2. Mysql学习总结(11)——MySql存储过程与函数
  3. SAP R3 Create Client: T-code:SCC4
  4. 一个简单RPC框架是怎样炼成的(II)——制定RPC消息
  5. httpd中工作模型的比较
  6. 【Android游戏开发二十三】自定义ListView【通用】适配器并实现监听控件!
  7. Windows Server AppFabric Caching
  8. jstack分析cpu占用100%
  9. HTML5 特性检测:Canvas(画布)
  10. linux yum install libsdl-dev 报错:No package libsdl-dev available 解决方法