OpenGL ES for Android 深度测试

在这里插入图片描述

什么是深度

深度就像是现实世界中物体与我们自己之间的距离,而在OpenGL中,深度是像素点(可以理解为现实世界中的物体)距离相机的距离,深度信息保存在深度缓存中,深度值越大则离相机越远。

深度测试有什么作用

在OpenGL ES中默认是不开启深度测试的,不使用深度测试的时候,先绘制较近的物体,然后绘制较远的物体,当远处的物体和近处的物体出现重叠时导致近处的物体被远处的物体遮挡,这不符合实际的现象,也不是我们想要的效果。
深度测试则是可以解决这种问题,开启深度测试后,深度缓存中存储着的每个像素点都包含深度信息,当绘制新的像素时,先和深度缓存中的深度值进行比较,当深度值比深度缓存中的深度值大时(也就是离相机远)则使用原来大颜色值绘制,反之则使用新的颜色值。
另外需要注意的是并不是Z的值越大深度值就越大,而是比较物体距相机的距离,启用深度测试不适用于绘制透明的物体。

启用深度测试

在OpenGL ES中默认是不开启深度测试的,开启深度测试代码如下:

GLES20.glEnable(GLES20.GL_DEPTH_TEST)

在每次绘制(onDrawFrame方法)时先清楚上次的深度缓存和颜色缓存:

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)

默认情况下新像素的深度值小于深度缓存中的值则使用新像素的值,也可以理解为通过深度测试,这是深度测试的默认方式,我们可以通过glDepthFunc方法修改其默认值,可以修改的方式有:

  • GL_NEVER:没有处理
  • GL_ALWAYS:总处理
  • GL_LESS:小于
  • GL_LEQUAL:小于等于
  • GL_EQUAL:等于
  • GL_GEQUAL:大于等于
  • GL_GREATER:大于
  • GL_NOTEQUAL:不等于
    默认值是GL_LESS。
    glDepthFunc方法使用方式如下:
 GLES20.glDepthFunc(GLES20.GL_LESS)

在[OpenGL ES for Android 绘制立方体](https://editor.csdn.net/md?articleId=104142276o p) 中是典型的深度测试用例,通过这篇文章可以查看开启深度测试和不开启的区别。

更多相关阅读:

In Pro OpenGL ES for Android, you'll find out how to harness the full power of OpenGL ES, and design your own 3D applications by building a fully-functional 3D solar system model using Open GL ES! OpenGL has set the standard for 3D computer graphics, and is an essential aspect of Android development. This book offers everything you need to know, from basic mathematical concepts to advanced coding techniques. You'll learn by building a fascinating 3D solar system simulator! After introducing Open GL ES, Pro OpenGL ES for Android explains the basics of 3D math and then orients you to the native Android 3D libraries you'll be using in your own 3D games and the solar system project you'll build using this book. Through the solar system example project, you'll learn how to incorporate a variety of graphic and animation techniques into your applications. You will also discover how the full spectrum of 3D development that awaits, with topics such as lighting, texture-mapping, modeling, shaders, blending modes, and several more advanced concepts. By the time you finish Pro OpenGL ES for Android, you'll have learned all the skills you'll need to build your own incredible 3D applications, based on one of the most powerful 3D libraries available. What you'll learn * The basics of 3D mathematics, and how they are applied in the OpenGL library * How to design and build your 3D worlds * To create 2D interfaces within the 3D world * To develop animation and 3D movement * How to implement 3D shading, coloring, and texturing * The differences between OpenGL and other 3D toolkits * To build a fully-functional 3D solar system simulator using OpenGL ES Who this book is for Experienced Android programmers who want to enter the 3D world of OpenGL ES programming. Table of Contents * Introduction to OpenGL ES and Our 3D Solar System Project * Generating a Basic OpenGL Program * Getting Past the 3D Math * Shading, Lighting and Colors * Materials and Textures * Animation * Creating a User Interface * Blending Modes, Buffer Objects, and Other Cool Stuff * Latest Features of OpenGL ES * Ray Tracing, Hidden Surfaces, and Other Advanced Topics Appendix A: APIs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老孟Flutter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值