原文地址:http://vormplus.be/blog/article/opengl-es-for-iphone-drawing-a-circle-part-ii
在上一篇中,我们学习了怎么在iPhone上绘制一个圆,在这一篇中,我们将学习怎么样绘制一个更好的圆。
添加随机颜色
在实际使用中经常使用随机颜色,在iPhone上可以很容易的实现这个功能。我们可以把圆周分成360等份,在绘制圆时我们要把每一等分都分配一种随机颜色。每个随机颜色的值由4部分组成,分别是red、green、blue和alpha,所以我们需要一个数组来存储圆的每一等份的4个值。下面的代码创建了一个GLfloat类型的数组,用来存放0.0~1.0之间的随机RGB值和固定为1.0不变的Alpha值。
// create empty array to store color values
GLfloat colors[1440];
// fill array with color values
for (int i = 0; i < 1440; i += 4) {
// red value
colors[i] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
// blue value
colors[i+1] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
// green value
colors[i+2] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
// alpha value
colors[i+3] = 1.0;
}
如果要随机颜色来绘制圆的话,需要将原来的glColor4f()方法用glColorPointer()方法来替换。代码如下:
glColorPointer(4, GL_FLOAT, 0, colors);
glEnableClientState(GL_COLOR_ARRAY);
运行程序后,你将会看到如下所示的起点在右边的很奇怪的一副图:
如何绘制的起点在中间,使所有三角都一样大小,相信会好看得多。
画一个更好的圆
画一个更漂亮的圆其实是很容易的。你指需要将x=0.0和y=0.0的地方设置为起点,那么所有的有绘制区域就都是从圆心到圆周了。我们发现,改变绘制方式以后数组的长度只有722了。
上一节教程的程序的这里有一个小小的bug,在这里你需要将等份的计算方法改为DEGREES_TO_RADIANS(i/2)来获得0~360之间的相位值。改变后的代码如下:
// create an array with a size of 722
GLfloat vertices[722];
// add 2 vertices at 0.0 (x and y for the first point)
vertices[0] = 0.0;
vertices[1] = 0.0;
// fill the rest of the array with x and y values
// on the perimeter of the circle.
for (int i = 0; i < 720; i += 2) {
vertices[i+2] = (cos(DEGREES_TO_RADIANS(i/2)) * 1);
vertices[i+3] = (sin(DEGREES_TO_RADIANS(i/2)) * 1);
}
// 2 vertices to close the circle so it looks perfect
vertices[719] = 0.0;
vertices[720] = 1.0;
You’ll also need to add one extra color to the colors array. Change the colors code to this:
GLfloat colors[1444];
for (int i = 0; i < 1444; i += 4) {
colors[i] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
colors[i+1] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
colors[i+2] = RANDOM_FLOAT_BETWEEN(0.0, 1.0);
colors[i+3] = 1.0;
}
最后的一点小更改是为了告诉OpenGL要绘制361等份,不是360等份。
glDrawArrays(GL_TRIANGLE_FAN, 0, 361);
运行程序,一个漂亮的圆将会展现在你的眼前。
Downloads
你可以在这里下载教程的工程文档:openglcircle2.zip
partIII,我将会告诉你们怎样优化代码让程序运行的更加的流畅。
翻译:yuezang (ityran.com)
校对:u0u0 (ityran.com)
licensed under Creative Commons license
|
相关推荐
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....
OpenGL ES 2 for Android A Quick-Start Guide
Interactive Computer Graphics:A.Top-Down Approach with Shader-Based OpenGL(6th.2011)
Learning OpenGL ES for iOS a Hands-on Guide to Modern 3D Graphics Programming, 基于iOS 讲解OpenGL ES开发,支持kindle
Mali_OpenGL_ES模拟器,Mali_OpenGL_ES_Emulator-v3.0.2.g694a9-Windows-32bit.zip
赠送jar包:opengl-api-gl1.1-android-2.1_r1.jar; 赠送原API文档:opengl-api-gl1.1-android-2.1_r1-javadoc.jar; 赠送源代码:opengl-api-gl1.1-android-2.1_r1-sources.jar; 赠送Maven依赖信息文件:opengl-...
OpenGL ES Tutorial for Android – Part II – Building a polygon OpenGL ES Tutorial for Android – Part III – Transformations OpenGL ES Tutorial for Android – Part IV – Adding colors OpenGL ES ...
赠送jar包:opengl-api-gl1.1-android-2.1_r1.jar; 赠送原API文档:opengl-api-gl1.1-android-2.1_r1-javadoc.jar; 赠送源代码:opengl-api-gl1.1-android-2.1_r1-sources.jar; 赠送Maven依赖信息文件:opengl-...
OpenGL ES for iphone 学者福音
Buck_-_OpenGL_ES_for_iPhone_Game,对OpenGL Es游戏及相关引擎做了一个详细的介绍,很值得一看。
OpenGL ES 教程 for iphone
实验一 OpenGL+GLUT开发平台搭建 5 小实验1: 开发环境设置 5 小实验2: 控制窗口位置和大小 6 小实验3:默认的可视化范围 6 小实验4:自定义可视化范围 7 小实验5: 几何对象变形的原因 8 小实验6: 视口坐标系及...
opengles31-quick-reference-card.pdf
OpenGL is a powerful software interface used to produce high-quality, computer-generated images and interactive applications using 2D and 3D objects, bitmaps, and color images. The OpenGL®...
Chapter V-2: Implementing a GPU-Only Particle-Collision System with ASTC 3D Textures and OpenGL ES 3.0 Chapter V-3: Animated Characters with Shell Fur for Mobile Devices Chapter V-4: High Dynamic ...
1 Introduction to 3D Graphics and OpenGL 2 Using OpenGL 3 Drawing in Space: Geometric Primitives and Buffers 4 Geometric Transformations: The Pipeline ...22 OpenGL ES – OpenGL on the Small
study of OpenGL, 西蒙iphone-OpenGL ES 教程-01
OpenGL快速查找手册-OpenGL-ES-2_0-Reference-card
1 Introduction to 3D Graphics and OpenGL 2 Using OpenGL 3 Drawing in Space: Geometric Primitives and Buffers 4 Geometric Transformations: The Pipeline ...22 OpenGL ES – OpenGL on the Small
iPhone-OpenGL-ES-Tutorial-Series-master iphone opengl es 源码