OpenGL ES Tutorial for Android – Part IV – Adding colors
January 14th, 2010 by Per-Erik Bergman — Android, Embedded
Last tutorial was about transformations. This tutorial will be a short one. I'm going to talk about adding color to your mesh. I will continue with the source code from tutorial II.
本教程讲述的是着色
Adding color
3D models with no colors are pretty boring so let's add some color to it. In general colors need no explanation. OpenGL ES uses a color model called RGBA ( Red, Green, Blue and Alpha ). The first three are self explained. The fourth is transparency, how solid the color should be. If you like to read more about colors go to: RGB color model - Wikipedia, the free encyclopedia
3D模型如果没着色的话,那么看上去一点意思也没有。所以,我们给它们添加颜色。一般来说不需要对颜色展开解释。OpenGL ES使用的颜色模型为RGBA(红,绿,蓝,透明度)。前面三个就不需要解释了,第4个Alpha指的是透明度。(0,完全透明,1,完全不透明)
You might be familiar with defining colors with hex (#FF00FF) or with decimals (255, 0, 255) we will use 0..1 where 0 map to 0 (#00) and 1 map against 255 (#FF).
可能你对16进制或10进制的颜色表示法很熟悉。但在OpenGL中,使用的是0…1来表示,0映射为#00,1映射为#FF(255)
The easiest way of coloring meshes is called vertex coloring and I am going to show you two different ways of doing that. Flat coloring that gives one solid color and smooth coloring that will blend colors specified for each vertex. Texturing is also a way of giving your mesh colors but it is not vertex coloring so I will show you how to do that in a later tutorial.
老方式,从易而难,最简单的是对顶点着色,下面我将介绍两种方式。填充方式给每个顶点设置一个相同的颜色;平滑渐变方式为每个顶点设置一个渐变色。纹理映射也是一种着色方式,但不属于顶点着色范围。有关纹理映射,请见后续教程。
Flat coloring
Flat coloring is really easy just tell OpenGL ES what color to use when it is going to render. One thing to remember is that when you set the color OpenGL ES uses this color until you change the color. This means that if you have two different squares and you tell OpenGL ES to change the color right before the second square the first frame the two squares will have different color but the next rendered frame both squares will have the same color.
填充着色实在是太简单了,只需要告诉OpenGL ES渲染要使用的颜色就可以了。但要记住的是,设置的颜色一直生效,直到你更改了颜色。如,你有两个方块,你在画第二个方块之前更改了颜色,那么第一帧,两个方块颜色不一样,但是后面的帧,颜色就一样了。
To tell OpenGL ES what color to work with you use this command:
使用如下函数设置颜色
public abstract void glColor4f(float red, float green, float blue, float alpha)
The default values are: red = 1, green = 1, blue = 1 and alpha = 1. Those values are white, and that's why all the squares we previous made has a white color.
函数参数的默认值都为1,这也是为什么前面看到的方块都是白色的缘故。
Create a new class called FlatColoredSquare it should be identical to the Square class. Then in the FlatColoredSquare function draw, add this line:
创建一新新类名为FlatColoredSquare以区别于Square类。在绘画函数中添加下面这行
gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); // 0x8080FFFF
I usually add a comment like the one above ( // 0x8080FFFF ) because I am used to read that. It makes it easier for me when reviewing the code.
个人建议在注释中写上十六进制的颜色值,不仅易于读,而且在代码评审中也更加直观
It should now look like this:
public void draw(GL10 gl) {
gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
...
Then change in the renderer so it uses the FlatColoredSquare instead of the Square.
public class OpenGLRenderer implements Renderer {
private FlatColoredSquare flatSquare; // CHANGED
public OpenGLRenderer() {
// Initialize our square.
flatSquare = new FlatColoredSquare(); // CHANGED
}
public void onDrawFrame(GL10 gl) {
...
flatSquare.draw(gl); // Don't forget to change this one.
...
}
Remember that anything rendered after you set a color uses the same color and that this spans over frames and will not be reset in-between.
注:在设置颜色之后,不需要再次设置颜色,即便是以后的各帧。
If you compile and run the application you will see one big flat colored blue square.
Just to give place to the smooth colored square coming up we move the flat square up.
public void onDrawFrame(GL10 gl) {
gl.glLoadIdentity();
// Translates 7 units into the screen and 1.5 units up.
gl.glTranslatef(0, 1.5f, -7);
// Draw our flat square.
flatSquare.draw(gl);
}
Notice that with flat coloring you don't need to tell OpenGL ES to turn it on or off. OpenGL ES uses flat coloring as a default way of coloring the meshes.
注:填充着色不需要告诉OpenGL ES开启或关闭此功能,OpenGL ES默认的就是填充方式
Smooth coloring
Smooth coloring is gained when you give each vertex its own color. OpenGL ES will interpolate the colors between the vertices and you will gain a smooth coloring effect. Just as with the flat coloring you tell OpenGL ES what to work with and it will be used as long as you don't say anything else.
平滑渐变着色对每个顶点都使用自己的颜色。OpenGL ES会在顶点之间插入颜色以得到平滑(渐变)颜色效果。与填充着色一样,设置的颜色可以被长期使用。
Create a new class called SmoothColoredSquare it should be identical to the Square class just as you did with the FlatColoredSquare. Modify the new class with this:
创建一个名为SmoothColoredSquare的新类
Define the colors you like your vertices to have.
定义顶点颜色
public class SmoothColoredSquare {
...
// The colors mapped to the vertices.
float[] colors = {
1f, 0f, 0f, 1f, // vertex 0 red
0f, 1f, 0f, 1f, // vertex 1 green
0f, 0f, 1f, 1f, // vertex 2 blue
1f, 0f, 1f, 1f, // vertex 3 magenta
};
...
The order of defining the colors are important since they map against the vertices so in this example above the first color (1f, 0f, 0f, 1f ) map against the top left vertex ( -1.0f, 1.0f, 0.0f ) the green against the bottom left vertex and the rest you can figure out. Hint: Look at the image above.
颜色的定义顺序很重要,它是和顶点一一对应的。如例中,红色(1f, 0f, 0f, 1f )对应于顶点0( -1.0f, 1.0f, 0.0f )。
And put them in a buffer just as we did with the vertices and indices.
如同顶点及顶点顺序数组一样,将它们放入字节缓冲中。
public SmoothColoredSquare() {
...
// float has 4 bytes, colors (RGBA) * 4 bytes
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
}
Don't forget to add colorBuffer as a variable to the class as well.
别忘了添加colorBuffer类变量
// Our color buffer.
private FloatBuffer colorBuffer;
We also need to enable the color buffer and tell openGL where it is.
我们需要告诉OpenGL开启颜色字节缓冲
public void draw(GL10 gl) {
...
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// Enable the color array buffer to be used during rendering.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // NEW LINE ADDED.
// Point out the where the color buffer is.
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); // NEW LINE ADDED.
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
...
// Disable the color buffer.
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
...
}
Don't forget to disable the use of the color array. If you don't disable the color array both squares will be smooth colored. Try it.
注:别忘了最后禁止颜色缓冲,如果你忘了的话,前面的填充着色方块也会变成渐变着色的。
Let's use this new smooth square as well. Start by adding it to your renderer.
public class OpenGLRenderer implements Renderer {
private FlatColoredSquare flatSquare;
private SmoothColoredSquare smoothSquare; // NEW LINE ADDED.
public OpenGLRenderer() {
// Initialize our squares.
flatSquare = new FlatColoredSquare();
smoothSquare = new SmoothColoredSquare(); // NEW LINE ADDED.
}
We need to move the square down a bit so they don't collide.
public void onDrawFrame(GL10 gl) {
...
// Translate to end up under the flat square.
gl.glTranslatef(0, -3f, 0);
// Draw our smooth square.
smoothSquare.draw(gl);
}
Now if you compile and run the application you will see two squares, one solid blue and one smooth with different colors.
References
The info used in this tutorial is collected from:
Android Developers
OpenGL ES 1.1 Reference Pages
You can download the source for this tutorial here: Tutorial_Part_IV
You can also checkout the code from: code.google.com
Previous tutorial: OpenGL ES Tutorial for Android – Part III – Transformations
Next tutorial: OpenGL ES Tutorial for Android – Part V – More on Meshes
Per-Erik Bergman
Consultant at Jayway
分享到:
相关推荐
Android平台上的OpenGL ES教程主要帮助开发者了解如何在Android应用程序中集成和使用这个库来创建交互式的图形界面。 在Android上设置OpenGL ES视图是开发过程中的第一步,而`GLSurfaceView`是实现这一目标的关键...
教程名称:OpenGL ES经典教程大全课程目录:【】Android应用OpenGLES制作3D图像【技术文档】【】OpenGL ES 2.0 官方手册【】OpenGL ES 2.0 编程指南【】OpenGL ES 2.0【】OpenGL ES【】OpenGL ES教程以及COCOS 2D...
它通常与OpenGL ES不直接交互,但在构建例如3D地理信息系统或可视化应用时,可能会利用Elasticsearch来检索和组织数据,然后通过OpenGL ES进行呈现。 总的来说,OpenGL ES 3.0编程指南涵盖了移动和嵌入式设备图形...
### Android OpenGL ES 开发教程详解 #### 概述与历史沿革 OpenGL ES(OpenGL for Embedded Systems)作为OpenGL API的子集,专为移动设备、PDA和游戏主机等嵌入式系统设计,旨在简化3D图形应用的开发流程。自2003...
OpenGL es 2.0教程 OpenGL es 2.0教程 附带源码和库 part2: http://download.csdn.net/source/3152396
opengles 教程
- 实例代码:`opengles_sample`可能包含了一些实际的示例代码,通过分析和运行这些代码,可以加深对OpenGL ES 2.0的理解。 - 在线教程:网上有许多教程和指南,如LearnOpenGLES.com、OpenGL-Tutorial.org等,它们...
OpenGL ES 1.0 教程是一份专为学习移动设备和嵌入式系统图形编程设计的资源。OpenGL ES(Embedded Systems)是OpenGL标准的精简版本,特别针对低功耗设备,如智能手机、平板电脑和掌上电脑,如Windows CE平台。这个...
最后,了解如何与硬件加速的OpenGLES API交互,以及熟悉GLSL语言(OpenGL Shading Language),是成为OpenGL ES 2.0专家的必经之路。通过编写高效、可读性强的着色器代码,可以创造出各种复杂的视觉效果,实现令人...
Cocos2D是一个跨平台的2D游戏引擎,它在OpenGL ES之上提供了一层高级的抽象。Cocos2D的关键特性包括: 1. **场景(Scenes)**:游戏的顶级容器,包含多个层(Layers)和节点(Nodes)。 2. **节点(Nodes)**:基本...
本教程将带你从入门到精通,深入理解OpenGL ES在图形编程中的应用。 一、OpenGL ES基础 OpenGL ES的基础概念包括上下文(Context)、顶点(Vertices)、着色器(Shaders)和帧缓冲区(Framebuffers)。上下文是...
资源名称:OpenGL着色器教程资料合集资源目录:【】GlSl介绍【】glsl编程实例【】HLSL特效【】JOGLv4中文版【】OpenGL【】OpenGL【】opengl_es第1章【】OpenGL_Shading语言介绍【】OpenGL低级着色语言与高级着色语言...
Rost、Bill Licea-Kane等人共同编著,涵盖了OpenGL及其着色语言的详细教程。书中不仅介绍了OpenGL的基础知识,还深入讲解了GLSL的高级用法。其中提到的.contributions by Dan Ginsburg, John M. Kessenich, ...
西蒙iphone-OpenGL ES 中文教程涉及到的知识点主要包括了iOS平台下的OpenGL ES技术,这是一套用于嵌入式系统中开发二维和三维图形应用程序的跨语言、跨平台的API。OpenGL ES是由OpenGL三维图形API简化而来的,专为...
OpenGL ES 3.0编程指南是面向初学者的优秀教程,旨在帮助读者深入理解并掌握OpenGL ES 3.0这一移动设备和嵌入式系统上的图形处理标准。OpenGL ES(Embedded Systems)是OpenGL的轻量级版本,专为资源有限但需要高...
这个“Android OpenGL ES 简明开发教程相关源码材质渲染”旨在教授如何在Android应用中利用OpenGL ES进行材质渲染,从而创建出丰富的视觉效果。 材质渲染是OpenGL ES中的关键概念,它涉及到物体表面的外观属性,如...
[Apress] OpenGL ES 入门教程 (英文版) [Apress] Learn OpenGL ES (E-Book) ☆ 出版信息:☆ [作者信息] Prateek Mehta [出版机构] Apress [出版日期] 2013年08月30日 [图书页数] 220页 [图书语言] 英语 ...
本教程旨在帮助开发者从零基础开始掌握OpenGLES 2.0,并通过实例Demo加深理解。 入门篇: 1. **环境配置**:首先,你需要安装Android Studio并创建一个新的Android项目。然后,在项目的build.gradle文件中添加对...