[Android开发学习11]Android OpenGL ES 颜色
一、基础知识:
1.平滑着色(Smooth coloring):
将多个顶点的不同颜色混合在一起,创建出漂亮的色彩混合。
2.单调着色:
给图形涂上一种固定单一的颜色。
3.三角形定义的颜色数组(平滑着色):
[java]
int one = 0x10000;
//三角形的顶点颜色值(r,g,b,a)
private IntBuffer colorBuffer = IntBuffer.wrap(new int[]{
one,0,0,one,
0,one,0,one,
0,0,one,one,
});
int one = 0x10000;
//三角形的顶点颜色值(r,g,b,a)
private IntBuffer colorBuffer = IntBuffer.wrap(new int[]{
one,0,0,one,
0,one,0,one,
0,0,one,one,
});
[java]
// 开启颜色渲染功能.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 设置三角形顶点的颜色
gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);
//关闭颜色渲染功能.
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// 开启颜色渲染功能.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 设置三角形顶点的颜色
gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);
//关闭颜色渲染功能.
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
4.正方形定义的颜色数组(单调着色):
[java]
/* 渲染正方形 */
// 设置当前色为蓝色
gl.glColor4f(1.0f, 0.5f, 1.0f, 1.0f);
/* 渲染正方形 */
// 设置当前色为蓝色
gl.glColor4f(1.0f, 0.5f, 1.0f, 1.0f);
二、代码编辑:
【Activity01.java】
[java]
package com.yarin.android.Examples_12_02;
import android.app.Activity;
import android.opengl.GLSu易做图ceView;
import android.opengl.GLSu易做图ceView.Renderer;
import android.os.Bundle;
public class Activity01 extends Activity
{
Renderer render = new GLRender();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GLSu易做图ceView glView = new GLSu易做图ceView(this);
glView.setRenderer(render);
setContentView(glView);
}
}
package com.yarin.android.Examples_12_02;
import android.app.Activity;
import android.opengl.GLSu易做图ceView;
import android.opengl.GLSu易做图ceView.Renderer;
import android.os.Bundle;
public class Activity01 extends Activity
{
Renderer render = new GLRender();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GLSu易做图ceView glView = new GLSu易做图ceView(this);
glView.setRenderer(render);
setContentView(glView);
}
}
【GLRender.java】
[java]
package com.yarin.android.Examples_12_02;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSu易做图ceView.Renderer;
public class GLRender implements Renderer
{
int one = 0x10000;
//三角形三个顶点
private IntBuffer triggerBuffer = IntBuffer.wrap(new int[]{
0,one,0, //上顶点
-one,-one,0, //坐下点
one,-one,0,}); //右下点
//正方形的4个顶点
private IntBuffer quaterBuffer = IntBuffer.wrap(new int[]{
one,one,0,
-one,one,0,
one,-one,0,
-one,-one,0});
//三角形的顶点颜色值(r,g,b,a)
private IntBuffer colorBuffer = IntBuffer.wrap(new int[]{
one,0,0,one,
0,one,0,one,
0,0,one,one,
});
@Override
public void onDrawFrame(GL10 gl)
{
// 清除屏幕和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 重置当前的模型观察矩阵
gl.glLoadIdentity();
// 左移 1.5 单位,并移入屏幕 6.0
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
//设置定点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//设置颜色数组 -- 开启颜色渲染功能.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 设置三角形顶点的颜色
gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);
// 设置三角形顶点
gl.glVertexPointer(3, GL10.GL_FIXED, 0, triggerBuffer);
//绘制三角形
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
//关闭颜色数组 -- 关闭颜色渲染功能.
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
 
补充:移动开发 , Android ,