OpenGL ES for Android研究总结

本文总结了OpenGL ES在Android平台上的应用,包括视角设置GLU.gluLookAt,纹理坐标处理glTexCoordPointer,以及解决绘图遮挡问题的深度缓冲调整。通过glGenTextures和GLUtils.texImage2D实现纹理加载,利用glTexParameterf控制纹理缩放效果,同时揭示了UV映射坐标与顶点坐标的关系。通过调整视区的深度范围,如gluPerspective,可以避免近处物体的遮挡问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GLU.gluLookAt(gl, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)

 

0>  如果没有主动调用这个函数,系统默认相当于调用了GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 1, 0);

       调用gl.glTranslatef(0,0,-1); 这个API后看到的效果等同于调用了GLU.gluLookAt(gl, 0, 0, 1, 0, 0, 0, 0, 1, 0);但调用后者不会影响当前全景坐标系。

1>  eyeX, eyeY, eyeZ 是观察者所在点对应于在当前全景坐标系下的坐标。

2>  centerX, centerY, centerZ是观察者的视线中心点在当前全景坐标系下的坐标。考虑你的手机屏幕这个平面,这个平面垂直于观察者的视线,所有屏幕上显示的物体形状,大小,其实是在全景坐标系下的物体沿视线投影到手机屏幕这个面的形状和大小。

3> upX, upY, upZ代表从(0,0,0)到 (x,y,z)的直线,它表示了观察者认为的“上”方向。改变这几个值相当于观察者的视线不变,自己的脸沿垂直于视线的平面旋转。

 

gl.glTranslatef(0,0,-1);

 

改变当前的全景坐标系, 使坐标系整体向屏幕内平移一个单位,这样所有的物体就看起来向屏幕内平移了一个单位。

 

 

glTexCoordPointer

坐标决定纹理贴图的大小,但是真正的纹理内容的左边总是在(0,1)之间,所以这个贴图越大,内容所占的比例就越小.

然后glTexCoordPointer决定的纹理贴图贴到glVertexPointer决定的”画布”上.这个过程是一比一的,”画布”是多大,纹理贴图就做相应的变形适合”画布”形状.

最后把画布显示在glViewport决定的屏幕显示区域,但是上面提到了,屏幕显示区域是一个坐标在(-1,+1)之间的一个正方形,所以”画布”的面积越大,即坐标范围越大,在显示这一步时是会被裁的,只剩(-1,+1)部分的图像.

 

贴图的方法

首先图片的操作依然是Bitmap作为图片的元素,如何读取Bitmap就不说了,在Opengl中准备可以贴的图片需要一个过程,首先,需要申明你需要贴图的图名字,这个是一个unsigned int ,用未用过的int来标志每一个你需要的texture,这个过程是通过:

 

gl.glGenTextures(1,  textures , 0);

//1 代表制需要创建一个名字,textures 是个int 数组,用于把创建的名字存储回这个数组中,0是说从第0个位置开始存储。

 

再创建好Texture名字后,需要把对应的名字跟具体的Texture关联,所谓的关联其实就是为对应的Texture设置好属性,此时名字的属性已经设置好了,还有不少属性可以设置,最关键的是要指明该Texture的类型,并且该Texture具体的图片:

gl.glBindTexture(GL10. GL_TEXTURE_2D textures [0]);

//此处是用于申明当前所有对 GL_TEXTURE_2D 的操作都是对 textures[0] 的操作,这两个是一体。

 

GLUtils. texImage2D (GL10. GL_TEXTURE_2D , 0, bitmap, 0);

//为 GL_TEXTURE_2D 当前的Texture指定图片,第一个0是指等级, 但是我换成1就显示不出来了。第二个0代表border边缘的宽度,此处只能为0(Opengl Es中对不少选择做了限制)

 

其中还有一些关于边缘的设置,例如当图片小于目标时如何处理,图片大于时如何处理之类的选项:

gl.glTexParameterf(GL10. GL_TEXTURE_2D , GL10. GL_TEXTURE_MIN_FILTER , GL10. GL_NEAREST );

 

此时,你已经为每一个名称的Texture指定好了应该指定的参数,这些过程可能会比较慢,不能老做,应该放在一个地方统一进行。当所有Texture指定好了,就可以开始使用每一个Texture了,使用时是先通过

gl.glBindTexture(GL10. GL_TEXTURE_2D textures [0]);

指定你当前准备使用的Texture,这个是非常快的,可以频繁使用。

指定好后,通过为每一个目标Vertext 指定对应 贴图中的位置来确定,该贴图最终被贴上去的样子,所以也需要准备一个Texture的FloatBuffer。其中因为贴图是2d所以指定的是2纬坐标,1为对应图片的宽或者高,从0 到1之间取数。

激活

gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRAY );

gl.glTexCoordPointer(2, GL10. GL_FLOAT , 0,  textureBuffer );

跟着画出点的时候,就把贴图给画出来了。

 

贴图时的坐标问题

根据 Per-Erik Bergman的说明, UV maping的坐标对应如下

所以顶点坐标数组应该为 

        float textureCoordinates[] = { 0.0f, 1.0f, //
                1.0f, 1.0f, //
                0.0f, 0.0f, //
                1.0f, 0.0f, //
        };

 

绘图平面有交叠时的遮挡问题

绘制A物体平面,在物体A所在平面的某一区域再绘制B平面。 当从平面的角度看时,B完全遮挡了A。 但是从远处立体远观时,B和A的遮挡情况就不一定了,有时后B反而被A遮挡。

这个原因是 视区范围的深度范围太大,以至于在32位内无法判定深度值大小差别。
典型的修改方法是最近的z深度值放大。例如:
gluPerspective(...,...,10,100000000);
改为 gluPerspective(...,...,100,100000000);

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
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. The Android robot is reproduced from work created and shared by Google and is used according to terms described in the Creative Commons 3.0 Attribution License (http://creativecommons.org/licenses/by/3.0/us/legalcode). The unit circle image in Figure 43, from http://en.wikipedia.org/wiki/File:Unit_circle.svg, is used according to the terms described in the Creative Commons Attribution-ShareAlike license, located at http://creativecommons.org/licenses/by-sa/3.0/legalcode. Day skybox and night skybox courtesy of Jockum Skoglund, also known as hipshot, [email protected],http://www.zfight.com. The image of the trace capture button is created and shared by the Android Open Source Project and is used according to terms described in the Creative Commons 2.5 Attribution License.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值