颜色和旋转
1.平滑着色
glColorPointer (int size, int type, int stride, Buffer pointer)
给每个点定义颜色值,各点之间平滑着色
需要gl.glEnableClientState(GL10.GL_COLOR_ARRAY);启动该功能,使用完需要gl.glDisableClientState(GL10.GL_COLOR_ARRAY);关闭平滑着色功能
2.单调着色
glColor4f (float red, float green, float blue, float alpha) 参数值在0.0-1.0之间
glColor4x (int red, int green, int blue, int alpha) int的高16位代表整数,低16为代表小数,因此,(0x10000,0,0,0x10000)表示红色不透明
3.旋转
glRotatex (int angle, int x, int y, int z)
第一个参数表示旋转的角度,(x,y,z)和(0,0,0)组成的直线代表旋转的轴
4.demo
package com.lanhuidong.opengl; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; import com.lanhuidong.opengl.util.BufferToNativeOrder; /** * @author lan * @since 2011-6-27 * @version */ public class OpenGLRender implements Renderer { private static int x = 0x10000 / 4; private static float f = 0.5f; private static float[] array = new float[] { -f, 0, 0, 0, (float) (f * Math.sqrt(3) / 2), 0, f, 0, 0 }; private static int[] array2 = new int[] { x, x, 0, x, -x, 0, -x, x, 0, -x, -x, 0 }; private static int[] array3 = new int[] { x, x, 0, -x, x, 0, -2 * x, 0, 0, -x, -x, 0, x, -x, 0, 2 * x, 0, 0 }; private static float[] colorarray = new float[] { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 }; private static float rotateTri = 0f; private static int rotateQuad = 0x10000; /** * 调用此方法绘制当前窗口 */ @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(-0.5f, 0.5f, -1.0f); // 画三角行 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); FloatBuffer triangleBuffer = BufferToNativeOrder.getNativeOrderFloatBuffer(array); gl.glRotatef(rotateTri, 0f, 1f, 0f); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer); gl.glColorPointer(4, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderFloatBuffer(colorarray)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); rotateTri += 0.5; gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glLoadIdentity(); gl.glTranslatef(0.5f, 0.5f, -1.0f); // 画矩形 IntBuffer quadrangleBuffer = BufferToNativeOrder.getNativeOrderIntBuffer(array2); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quadrangleBuffer); gl.glColor4f(1f, 0f, 0f, 1f); gl.glRotatex(rotateQuad, 1, 0, 0); rotateQuad -= 0x10000; gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glLoadIdentity(); gl.glTranslatef(-0.5f, -0.5f, -1.0f); // 画矩形 gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderIntBuffer(array3)); gl.glColor4x(0x10000, 0, 0x10000, 0x10000); gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } /** * 窗口被创建或者窗口大小改变时被调用 */ @Override public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); } /** * 窗口被创建或者被重新创建时调用,做初始化工作 */ @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0, 0, 1.0f, 0.5f); } }
5.效果