MyException - 我的异常网
当前位置:我的异常网» 移动开发 » OpenGL ES课程IV之着色(原文对照)

OpenGL ES课程IV之着色(原文对照)

www.MyException.Cn  网友分享于:2013-07-16  浏览:12次
OpenGL ES教程IV之着色(原文对照)

OpenGL ES Tutorial for Android – Part IV – Adding colors

January 14th, 2010 by Per-Erik BergmanAndroid, 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(红,绿,蓝,透明度)。前面三个就不需要解释了,第4Alpha指的是透明度。(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映射为#001映射为#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

 

文章评论

10个调试和排错的小建议
10个调试和排错的小建议
漫画:程序员的工作
漫画:程序员的工作
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
程序员和编码员之间的区别
程序员和编码员之间的区别
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
程序员都该阅读的书
程序员都该阅读的书
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
如何成为一名黑客
如何成为一名黑客
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
那些争议最大的编程观点
那些争议最大的编程观点
一个程序员的时间管理
一个程序员的时间管理
60个开发者不容错过的免费资源库
60个开发者不容错过的免费资源库
程序员必看的十大电影
程序员必看的十大电影
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
Java程序员必看电影
Java程序员必看电影
程序员应该关注的一些事儿
程序员应该关注的一些事儿
编程语言是女人
编程语言是女人
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
程序员的鄙视链
程序员的鄙视链
亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
老程序员的下场
老程序员的下场
总结2014中国互联网十大段子
总结2014中国互联网十大段子
鲜为人知的编程真相
鲜为人知的编程真相
为什么程序员都是夜猫子
为什么程序员都是夜猫子
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
我的丈夫是个程序员
我的丈夫是个程序员
旅行,写作,编程
旅行,写作,编程
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
我是如何打败拖延症的
我是如何打败拖延症的
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
 程序员的样子
程序员的样子
代码女神横空出世
代码女神横空出世
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有