在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 添加顏色
3D 繪圖基本概念
概述
真正的 3D 圖形
添加顏色
3D 坐標(biāo)變換
構(gòu)造
材質(zhì)渲染

添加顏色

前面的例子顯示的正方形都是白色,看其來(lái)不是很吸引人,本篇介紹如何給 Mesh(網(wǎng)格)添加顏色。OpenGL ES 使用顏色是我們熟知的 RGBA 模式(紅,綠,藍(lán),透明度)。

顏色的定義通常使用 Hex 格式 0xFF00FF 或十進(jìn)制格式(255,0,255), 在 OpenGL 中卻是使用 0…1 之間的浮點(diǎn)數(shù)表示。0 為 0,1 相當(dāng)于 255(0xFF)。

最簡(jiǎn)單的上色方法叫做頂點(diǎn)著色 (Vertxt coloring),可以使用單色,也可以定義顏色漸變或者使用材質(zhì)(類同于二維圖形中各種 Brush 類型)。

Flat coloring(單色)

是通知 OpenGL 使用單一的顏色來(lái)渲染,OpenGL 將一直使用指定的顏色來(lái)渲染直到你指定其它的顏色。

指定顏色的方法為

public abstract void glColor4f(float red, float green, float blue, float alpha)。   

缺省的 red,green,blue 為 1,代表白色。這也是為什么前面顯示的正方形都是白色的緣故。

我們創(chuàng)建一個(gè)新的類為 FlatColoredSquare,作為 Sequare 的子類,將它的 draw 重定義如下:

public void draw(GL10 gl) {
 gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
 super.draw(gl);
}  

將 OpenGLRenderer 的 square 的類型改為 FlatColoredSquare。

private FlatColoredSquare square=new FlatColoredSquare();  

編譯運(yùn)行,正方形顏色變成了藍(lán)色:

http://wiki.jikexueyuan.com/project/opengl-es-basics/images/26.png" alt="" />

Smooth coloring (平滑顏色過(guò)渡)

當(dāng)給每個(gè)頂點(diǎn)定義一個(gè)顏色時(shí),OpenGL 自動(dòng)為不同頂點(diǎn)顏色之間生成中間過(guò)渡顏色(漸變色)。

在項(xiàng)目中添加一個(gè) SmoothColoredSquare 類,作為 Square 子類,為每個(gè)頂點(diǎn)定義一個(gè)顏色值。

// 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
};  

顏色定義的順序和頂點(diǎn)的順序是一致的。為了提高性能,和頂點(diǎn)坐標(biāo)一樣,我們也把顏色數(shù)組放到Buffer 中:

// 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);  

最后修改 draw 方法,如下:

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);
// Point out the where the color buffer is.
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
super.draw(gl);
// Disable the color buffer.
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}   

將 OpenGLRenderer 中的 Square 類型改成 SmoothColoredSquare,編譯運(yùn)行結(jié)果如下:

http://wiki.jikexueyuan.com/project/opengl-es-basics/images/27.png" alt="" />

上一篇:3D 坐標(biāo)變換下一篇:概述