Android OpenGL ES 简明开发教程六:真正的3D图形

本文介绍如何使用OpenGL ES框架构建基本3D图形,包括四面体、椎体等,通过Mesh(网格)概念,讲解了组合模式设计原则,并演示了如何创建平面、立方体及管理多个几何体。

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

  • 摘要:该系列文章均转载自/content/540025.html由于原文好像无法打开,正好自己有记录,所以正好分享出来。Mesh(网格,三角面)是构成空间形体的基本元素,前面的正方形也是由两个Mesh构成的。本篇将介绍使用Mesh构成四面体,椎体等基本空间形体。Design设计在使用OpenGL框架时一个好的设计原则是使用“CompositePattern”(组合模式),本篇采用如下设计:Mesh首先定义一个基类Mesh,所有空间形体最基本的构成元素为Mesh(三角形网格),其基本
  • 该系列文章均转载自 

    /content/540025.html 

    由于原文好像无法打开,正好自己有记录,所以正好分享出来。 

    Mesh(网格,三角面)是构成空间形体的基本元素,前面的正方形也是由两个Mesh构成的。本篇将介绍使用Mesh构成四面体,椎体等基本空间形体。 

    Design设计在使用OpenGL 框架时一个好的设计原则是使用“Composite Pattern”(组合模式),本篇采用如下设计: 

    Android OpenGL ES 简明开发教程_真正的3D图形 
    Mesh首先定义一个基类 Mesh,所有空间形体最基本的构成元素为Mesh(三角形网格) ,其基本定义如下: 

    [code]public class Mesh { // Our vertex buffer. private FloatBuffer verticesBuffer = null; // Our index buffer. private ShortBuffer indicesBuffer = null; // The number of indices. private int numOfIndices = -1; // Flat Color private float[] rgba = new float[] { 1.0f, 1.0f, 1.0f, 1.0f }; // Smooth Colors private FloatBuffer colorBuffer = null; // Translate params. public float x = 0; public float y = 0; public float z = 0; // Rotate params. public float rx = 0; public float ry = 0; public float rz = 0; public void draw(GL10 gl) { // Counter-clockwise winding. gl.glFrontFace(GL10.GL_CCW); // Enable face culling. gl.glEnable(GL10.GL_CULL_FACE); // What faces to remove with the face culling. gl.glCullFace(GL10.GL_BACK); // Enabled the vertices buffer for writing and //to be used during // rendering. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // Specifies the location and data format //of an array of vertex // coordinates to use when rendering. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer); // Set flat color gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]); // Smooth color if (colorBuffer != null) { // Enable the color array buffer to be //used during rendering. gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); } gl.glTranslatef(x, y, z); gl.glRotatef(rx, 1, 0, 0); gl.glRotatef(ry, 0, 1, 0); gl.glRotatef(rz, 0, 0, 1); // Point out the where the color buffer is. gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, GL10.GL_UNSIGNED_SHORT, indicesBuffer); // Disable the vertices buffer. gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // Disable face culling. gl.glDisable(GL10.GL_CULL_FACE); } protected void setVertices(float[] vertices) { // a float is 4 bytes, therefore //we multiply the number if // vertices with 4. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); verticesBuffer = vbb.asFloatBuffer(); verticesBuffer.put(vertices); verticesBuffer.position(0); } protected void setIndices(short[] indices) { // short is 2 bytes, therefore we multiply //the number if // vertices with 2. ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); ibb.order(ByteOrder.nativeOrder()); indicesBuffer = ibb.asShortBuffer(); indicesBuffer.put(indices); indicesBuffer.position(0); numOfIndices = indices.length; } protected void setColor(float red, float green, float blue, float alpha) { // Setting the flat color. rgba[0] = red; rgba[1] = green; rgba[2] = blue; rgba[3] = alpha; } protected void setColors(float[] colors) { // float has 4 bytes. ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.position(0); }} 

    setVertices 允许子类重新定义顶点坐标。 

    setIndices 允许子类重新定义顶点的顺序。 

    setColor /setColors允许子类重新定义颜色。 

    x,y,z 定义了平移变换的参数。 

    rx,ry,rz 定义旋转变换的参数。 

    Plane有了Mesh定义之后,再来构造Plane,plane可以有宽度,高度和深度,宽度定义为沿X轴方向的长度,深度定义为沿Z轴方向长度,高度为Y轴方向。 

    Android OpenGL ES 简明开发教程_真正的3D图形 

    Segments为形体宽度,高度,深度可以分成的份数。 Segments在构造一个非均匀分布的Surface特别有用,比如在一个游戏场景中,构造地貌,使的Z轴的值随机分布在-0.1到0.1之间,然后给它渲染好看的材质就可以造成地图凹凸不平的效果。 

    Android OpenGL ES 简明开发教程_真正的3D图形 
    上面图形中Segments为一正方形,但在OpenGL中我们需要使用三角形,所有需要将Segments分成两个三角形。为Plane 定义两个构造函数: 

    // Let you decide the size of the plane but still only one segment. 

    public Plane(float width, float height) 

    // For alla your settings. 

    public Plane(float width, float height, int widthSegments, int heightSegments) 

    比如构造一个1 unit 宽和 1 unit高,并分成4个Segments,使用图形表示如下: 

    Android OpenGL ES 简明开发教程_真正的3D图形 

    左边的图显示了segments ,右边的图为需要创建的Face(三角形)。 

    Plane类的定义如下: 

    [code]public class Plane extends Mesh { public Plane() {this(1, 1, 1, 1); } public Plane(float width, float height) {this(width, height, 1, 1); } public Plane(float width, float height, int widthSegments, int heightSegments) {float[] vertices = new float[(widthSegments + 1) * (heightSegments + 1) * 3];short[] indices = new short[(widthSegments + 1) * (heightSegments + 1) * 6];float xOffset = width / -2;float yOffset = height / -2;float xWidth = width / (widthSegments);float yHeight = height / (heightSegments);int currentVertex = 0;int currentIndex = 0;short w = (short) (widthSegments + 1);for (int y = 0; y < heightSegments + 1; y++) {for (int x = 0; x < widthSegments + 1; x++) { vertices[currentVertex] = xOffset + x * xWidth; vertices[currentVertex + 1] = yOffset + y * yHeight; vertices[currentVertex + 2] = 0; currentVertex += 3; int n = y * (widthSegments + 1) + x; if (y < heightSegments &;&; x < widthSegments) {// Face oneindices[currentIndex] = (short) n;indices[currentIndex + 1] = (short) (n + 1);indices[currentIndex + 2] = (short) (n + w);// Face twoindices[currentIndex + 3] = (short) (n + 1);indices[currentIndex + 4] = (short) (n + 1 + w);indices[currentIndex + 5] = (short) (n + 1 + w - 1);currentIndex += 6; }}}setIndices(indices);setVertices(vertices); }} 

    Cube下面来定义一个正方体(Cube),为简单起见,这个四面体只可以设置宽度,高度,和深度,没有和Plane一样提供Segments支持。 

    [code]public class Cube extends Mesh { public Cube(float width, float height, float depth) {width /= 2;height /= 2;depth /= 2;float vertices[] = { -width, -height, -depth, // 0 width, -height, -depth, // 1 width, height, -depth, // 2 -width, height, -depth, // 3 -width, -height, depth, // 4 width, -height, depth, // 5 width, height, depth, // 6 -width, height, depth, // 7};short indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2, };setIndices(indices);setVertices(vertices); }} 

    GroupGroup可以用来管理多个空间几何形体,如果把Mesh比作Android的View ,Group可以看作Android的ViewGroup,Android的View的设计也是采用的“Composite Pattern”。 

    Group的主要功能是把针对Group的操作(如draw)分发到Group中的每个成员对应的操作(如draw)。 

    Group定义如下: 

    [code]public class Group extends Mesh { private Vector<Mesh> children = new Vector<Mesh>(); @Override public void draw(GL10 gl) {int size = children.size();for( int i = 0; i < size; i++)children.get(i).draw(gl); } /** * @param location * @param object * @see java.util.Vector#add(int, java.lang.Object) */ public void add(int location, Mesh object) {children.add(location, object); } /** * @param object * @return * @see java.util.Vector#add(java.lang.Object) */ public boolean add(Mesh object) {return children.add(object); } /** * * @see java.util.Vector#clear() */ public void clear() {children.clear(); } /** * @param location * @return * @see java.util.Vector#get(int) */ public Mesh get(int location) {return children.get(location); } /** * @param location * @return * @see java.util.Vector#remove(int) */ public Mesh remove(int location) {return children.remove(location); } /** * @param object * @return * @see java.util.Vector#remove(java.lang.Object) */ public boolean remove(Object object) {return children.remove(object); } /** * @return * @see java.util.Vector#size() */ public int size() {return children.size(); }} 

    Genderer代码: 

    [code]public class OpenGLRenderer implements GLSurfaceView.Renderer { private Mesh root; public OpenGLRenderer() {// Initialize our cube.Group group = new Group();Cube cube = new Cube(1, 1, 1);cube.rx = 45;cube.ry = 45;group.add(cube);root = group; } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition * .khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig) */ @Override public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig eglConfig) {// Set the background color to black ( rgba ).gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);// Enable Smooth Shading, default not really needed.gl.glShadeModel(GL10.GL_SMOOTH);// Depth buffer setup.gl.glClearDepthf(1.0f);// Enables depth testing.gl.glEnable(GL10.GL_DEPTH_TEST);// The type of depth testing to do.gl.glDepthFunc(GL10.GL_LEQUAL);// Really nice perspective calculations.gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition. * khronos.opengles.GL10) */ @Override public void onDrawFrame(GL10 gl) {// Clears the screen and depth buffer.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// Replace the current matrix with the identity matrixgl.glLoadIdentity();// Translates 4 units into the screen.gl.glTranslatef(0, 0, -4);// Draw our scene.root.draw(gl); } /* * (non-Javadoc) * * @see * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition * .khronos.opengles.GL10, int, int) */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) {// Sets the current view port to the new size.gl.glViewport(0, 0, width, height);// Select the projection matrixgl.glMatrixMode(GL10.GL_PROJECTION);// Reset the projection matrixgl.glLoadIdentity();// Calculate the aspect ratio of the windowGLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);// Select the modelview matrixgl.glMatrixMode(GL10.GL_MODELVIEW);// Reset the modelview matrixgl.glLoadIdentity(); }} 

    其它建议上面我们定义里Mesh, Plane, Cube等基本空间几何形体,对于构造复杂图形(如人物),可以预先创建一些通用的几何形体,如果在组合成较复杂的形体。除了上面的基本形体外,可以创建如Cone,Pryamid, Cylinder等基本形体以备后用。 

    Android OpenGL ES 简明开发教程_真正的3D图形 
    本例示例代码显示结果如下: 

    代码下载: 

    https://github.com/upperLucky/3DModelDemo 

    Android OpenGL ES 简明开发教程_真正的3D图形
  • 以上是Android OpenGL ES 简明开发教程_真正的3D图形的内容,更多 简明 图形 真正 Android 教程 OpenGL 开发 ES 的内容,请您使用右上方搜索功能获取相关信息。
本书共分两篇,第一篇介绍了Android 3D游戏开发的基础知识,主要对OpenGL ES的相关内容进行了介绍。   章 名主 要 内 容   第1章 英雄还看今朝—Android简介本章介绍了市场上主流的手机平台,同时也分析了未来手机平台的发展趋势及Android平台的前景   第2章 数风流人物—当前流行游戏类型简介本章以分类的方式简要地介绍了当前流行的游戏的玩法,游戏的视觉效果,游戏的设计及《仙剑》等著名游戏的历史   第3章 不积跬步,无以至千里—游戏开发基础知识本章初步介绍了游戏开发的基础知识   第4章 千里之行,始于足下—3D开发基础知识本章介绍了3D开发中的基础知识,包括OpenGL ES的介绍及OpenGL ES中绘制模型的原理,并通过点、线和三角形的绘制介绍了OpenGL ES中模型的几种绘制方式。最后介绍了3D场景中常用的两种投影方式,并通过例子比较了这两种投影的区别   第5章 愿君多采撷,此物最相思—光照效果的开发本章介绍了光照的基础知识,包括环境光、散射光及镜面光   第6章 为伊消得人憔悴——纹理映射本章主要介绍了纹理的基础知识,以及纹理的不同拉伸方式和纹理过滤高级技术,从绘制三角形开始到绘制地月系,可能会经历很长时间,但是这对以后的学习是有帮助的   第7章 海阔凭鱼跃,天高任鸟飞—3D基本形状的构建在本章中介绍了圆柱体、圆锥体、圆环、抛物面、双曲面和螺旋面在OpenGL ES中的渲染方法。这些基本形状在3D世界中应用广泛,在构造一些复杂物体时,经常会运用这些基本形状来进行拼装组合   第8章 执子之手,与子偕老—坐标变换本章介绍了坐标变换的应用。绘制3D场景的过程,主要是旋转和平移操作的组合,通过合理的堆栈操作,就比较容易绘制出所需的3D场景   第9章 孤帆远影碧空尽—摄像机与雾特效在本章中,首先对摄像机及其配置做了介绍。摄像机在3D编程中至关重要,没有正确的配置,摄像机可能不能获得想要的场景效果。然后对雾特效做了具体介绍,应用雾特效可以使场景更加逼真,并且可以减少场景渲染量来提高性能   第10章 假作真时真亦假—混合本章主要为读者介绍了混合,从混合的背景知识到如何配置源因子和目标因子。在介绍源因子和目标因子的时候,向读者介绍了一些预定义常量和一些常用的组合方式,以及如何启用混合   第11章 蓦然回首,那人却在灯火阑珊处—3D高级技术本章主要为读者介绍了3D的一部分高级技术。每一项技术通过讲解其原理和案例,使读者对3D高级技术有一定的了解   第12章 心有灵犀一点通—传感器在本章中,向读者介绍了Android中传感器的相关知识。包括传感器的种类、配置,并且着重介绍了姿态传感器的应用   第13章 千锤万凿出深山—游戏中的数学与物理在本章中对3D游戏中可能会用到的数学及物理知识进行了简单的介绍,这在3D游戏开发中是相当重要的。游戏中的核心算法,基本上都要用到数学和物理知识。一款游戏的性能很大程度上取决于游戏设计的算法   第14章 山舞银蛇,原驰蜡象—AI基本理念本章主要介绍了AI、AI引擎的基本组成与设计,以及游戏AI中图的搜索和模糊逻辑,其中游戏AI中图的搜索为本章的重点。在本章中详细介绍了5种算法的原理与实现   第15章 独上高楼,望尽天涯路—开发小秘籍本章介绍了地图设计器、多键技术、虚拟键盘、查找表技术、状态机、AABB边界框、穿透效应、拾取技术,以及天空盒和天空穹在OpenGL ES中的应用 第二篇以7个比较大的案例来说明Android平台下3D游戏的开发流程,通过这7个案例的讲解,读者对3D游戏的开发将会有更深层次的理解。   章 名主 要 内 容   第16章 体育类游戏——《疯狂投篮》本章介绍了Android 3D游戏《疯狂投篮》的开发。通过该案例向读者介绍了在Android平台下进行3D游戏开发的相关知识和基本流程,并对游戏开发中的编程技巧进行了介绍,并主要介绍了篮球与地面、墙面及篮框的碰撞检测及运动动画的实现方法   第17章 益智类游戏——《旋转积木》本章介绍了Android 3D游戏《旋转积木》的开发。主要介绍了积木旋转的不同状态的实现方法和地图设计器的应用   第18章 休闲类游戏——《摩天大楼》本章介绍了Android 3D游戏《摩天大楼》的开发。主要介绍了楼层与楼层之间的衔接与碰撞及掉落后翻转动画的实现   第19章 动作类游戏——《3D空战》本章介绍了Android 3D游戏《3D空战》的开发。主要介绍了飞机的构造方法和我方战机与敌方战机的操控及动画实现   第20章 桌面类游戏——《激情台球》本章介绍了Android 3D游戏《激情台球》的开发。主要介绍了台球与台球的碰撞检测实现、台球与球桌的碰撞检测实现和进球的判定实现   第21章 射击类游戏——《抢滩登陆》本章介绍了Android 3D游戏《抢滩登陆》的开发。主要运用了灰度图生成技术并且主要介绍了坦克运动的实现方法及炮弹碰撞检测的实现   第22章 竞技类游戏——《乡村飙车》本章介绍了Android 3D游戏《乡村飙车》的开发。主要介绍了运用分层绘制和拼接绘制的策略进行场景的优化绘制,并且对场景部件进行了分类控制   本书面向的读者   本书的内容详细,且几乎涵盖了Android 3D游戏开发所有相关的技术,并向读者介绍了真实项目的开发流程,主要面向以下读者。   Android的初学者   本书详细介绍了OpenGL ES的基础知识,并对Android 3D游戏程序的开发进行了介绍。作为一名Android的初学者,通过本书的学习可以快速全面地掌握Android 3D游戏开发的相关知识,稳健地步入Android 3D游戏开发人员的行列。   有一定Android基础且希望学习Android 3D游戏开发的读者   有一定Android基础的读者通过阅读本书的前半部分便可快速掌握OpenGL ES的基础知识,然后通过7个真实案例的学习迅速掌握Android平台下应用程序的开发。   在职的开发人员
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值