OpenGL ES(2)——定义图形
来源:互联网 发布:中科大linux内核分析 编辑:程序博客网 时间:2024/06/15 04:10
能够定义图形是创造好的图像作品的第一步。当你不知道OpenGL ES希望你怎样去定制图像对象的时候,使用OpenGL ES绘制可能会有点困难。
这节课解释了OpenGL ES的坐标系统和Android设备屏幕的关系,如何定义图形形状,图形表面,以及如何定义三角形和正方形。
OpenGL ES允许你使用三维坐标来绘制对象。因此,在你绘制三角形之前,你必须定义它的坐标。在OpenGL中,一个常用的方法是定义一个顶点的坐标数组。为了更好的效率,将这些坐标数组写入ByteBuffer中,它被传入OpenGL ES的图像管道中处理。
public class Triangle { private FloatBuffer vertexBuffer; // number of coordinates per vertex in this array static final int COORDS_PER_VERTEX = 3; static float triangleCoords[] = { // in counterclockwise order: 0.0f, 0.622008459f, 0.0f, // top -0.5f, -0.311004243f, 0.0f, // bottom left 0.5f, -0.311004243f, 0.0f // bottom right }; // Set color with red, green, blue and alpha (opacity) values float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; public Triangle() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (number of coordinate values * 4 bytes per float) triangleCoords.length * 4); // use the device hardware's native byte order bb.order(ByteOrder.nativeOrder()); // create a floating point buffer from the ByteBuffer vertexBuffer = bb.asFloatBuffer(); // add the coordinates to the FloatBuffer vertexBuffer.put(triangleCoords); // set the buffer to read the first coordinate vertexBuffer.position(0); }}
默认情况下,OpenGL ES假定[0,0,0][X,Y,Z]是GLSurfaceView的中心,[1,1,0]是右上角,[-1,-1,0]是左下角。详细的坐标系统描述,参考OpenGL ES开发者指南。
注意这个图形的坐标是按逆时针顺序定义的。绘制顺序决定了哪一边是图形的前面,也就是你希望绘制的那一面,哪一面是背面,这一面你可以通过OpenGL ES的选择表面功能来选择不去绘制。对于这个功能,同样参考OpenGL ES开发者指南。
定义一个三角形也许很简单,但是如果你想要复杂一点的?比如一个正方形?有许多方法实现,一个典型的方法是绘制两个三角形。
同样,你应该把逆时针方向的顶点数组传到ByteBuffer中。为了避免被同时使用的顶点定义两次,使用一个绘制列表来告诉OpenGL ES图像管道怎样去绘制这些顶点。
public class Square { private FloatBuffer vertexBuffer; private ShortBuffer drawListBuffer; // number of coordinates per vertex in this array static final int COORDS_PER_VERTEX = 3; static float squareCoords[] = { -0.5f, 0.5f, 0.0f, // top left -0.5f, -0.5f, 0.0f, // bottom left 0.5f, -0.5f, 0.0f, // bottom right 0.5f, 0.5f, 0.0f }; // top right private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices public Square() { // initialize vertex byte buffer for shape coordinates ByteBuffer bb = ByteBuffer.allocateDirect( // (# of coordinate values * 4 bytes per float) squareCoords.length * 4); bb.order(ByteOrder.nativeOrder()); vertexBuffer = bb.asFloatBuffer(); vertexBuffer.put(squareCoords); vertexBuffer.position(0); // initialize byte buffer for the draw list ByteBuffer dlb = ByteBuffer.allocateDirect( // (# of coordinate values * 2 bytes per short) drawOrder.length * 2); dlb.order(ByteOrder.nativeOrder()); drawListBuffer = dlb.asShortBuffer(); drawListBuffer.put(drawOrder); drawListBuffer.position(0); }}
这个例子让你快速知道了怎样去通过OpenGL去构建更加复杂的图形。通常,你会使用三角形的集合来绘制对象。下一节课中,你会学习到怎么把这些图形绘制到屏幕上。
- OpenGL ES(2)——定义图形
- OpenGL ES(3)——绘制图形
- OpenGL ES(2) - 定义形状
- OpenGL ES开发绘制2D图形
- OPENGL ES 图形管道
- opengl es 2.0游戏与图形编程——2.设置图形投影
- (2)OpenGL ES定义形状
- OpenGL ES定义形状
- 图形编程之 OpenGL ES 2.0 —— 从我们的世界到他们的世界 (2)
- Android 图形 II-OpenGL ES
- 使用OpenGL ES显示图形
- 动画和图形:OpenGL ES
- OpenGL ES(1)——构建OpenGL ES环境
- OpenGL ES——简述
- iOS OpenGL ES 研究二:OpenGL ES绘制图形精华
- Displaying Graphics with OpenGL ES(二)——定义形状Defining Shapes
- Android Training - 使用OpenGL ES(2) - 定义形状
- OpenGL ES绘制3D图形
- iOS Info.plist
- 深度学习方向的牛人个人主页,文章必须带代码呀,持续更新
- C语言关键字解析(一)
- c# 换行符
- 欢迎使用CSDN-markdown编辑器
- OpenGL ES(2)——定义图形
- 交叉引用的解决方法-类方法的应用
- leetcode-Longest Increasing Subsequence
- 程序员到底是怎么来的?
- iOS生成随机数
- 集合分割
- java反射
- IIS支持mp4等视频播放的设置
- J-Link clone问题