上篇文章讲了opengl es的基本知识,GLSurfaceView的创建,画点等内容,这篇文章将opengl es另外的一些基本操作,比如画线,画三角形,正方形等,以及这些例子中涉及到了相关的知识点。
1、画线,画线的操作和点差不多,具体看如下代码
public void DrawLine(GL10 gl) {
ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArray.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(0);
//设置顶点
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
index++;
index%=10;
switch(index){
case 0:
case 1:
case 2:
//设置顶点颜色
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
//通过顶点画线,使用了GL10.GL_LINES参数代表画线,下面的GL10.GL_LINE_STRIP,GL10.GL_LINE_LOOP都是画线的参数,具体区别上篇文章讲到了。
gl.glDrawArrays(GL10.GL_LINES, 0, 4);
break;
case 3:
case 4:
case 5:
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, 4);
break;
case 6:
case 7:
case 8:
case 9:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);
break;
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
2,画三角形,和画点,线基本一致,这里加入的顶点颜色设置,设置颜色时,glEnableClientState(GL10.GL_COLOR_ARRAY)调用该方法启用颜色设置,gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer),调用该方法把颜色设置好
public void DrawSJ(GL10 gl) {
//设置每个顶点颜色
ByteBuffer cbb
= ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArraysj.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArraysj);
vertex.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
//设置每个顶点颜色
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
index++;
index%=10;
index = 0;
switch(index){
case 0:
case 1:
case 2:
//为每个顶点设置颜色之后,这里就没有效果了
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
break;
case 3:
case 4:
case 5:
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
break;
case 6:
case 7:
case 8:
case 9:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
break;
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
3、画矩形
//画矩形
private void drawZFX(GL10 gl){
//正方形顶点,顺序要注意好
float[] cubeVertices = {
//左面
-0.5f,0.5f,0.5f,
-0.5f,-0.5f,0.5f,
-0.5f,0.5f,-0.5f,
-0.5f,-0.5f,-0.5f,
//右面
0.5f, 0.5f,0.5f,
0.5f,-0.5f,0.5f,
0.5f,-0.5f,-0.5f,
0.5f,0.5f,-0.5f ,
//前面
-0.5f,0.5f,0.5f,
-0.5f,-0.5f,0.5f,
0.5f,-0.5f,0.5f,
0.5f, 0.5f,0.5f,
//后面
0.5f,-0.5f,-0.5f,
0.5f,0.5f,-0.5f,
-0.5f,0.5f,-0.5f,
-0.5f,-0.5f,-0.5f,
//上面
-0.5f,0.5f,0.5f,
0.5f, 0.5f,0.5f,
0.5f,0.5f,-0.5f,
-0.5f,0.5f,-0.5f,
//下面
-0.5f,-0.5f,0.5f,
0.5f,-0.5f,0.5f,
0.5f,-0.5f,-0.5f,
-0.5f,-0.5f,-0.5f
};
// 颜色数组
float [] cubeColors = {
1f,0f,0f,1f ,
1f,0f,0f,1f ,
1f,0f,0f,1f ,
1f,0f,0f,1f ,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
1f,0f,0f,1f ,
1f,0f,0f,1f ,
1f,0f,0f,1f ,
1f,0f,0f,1f ,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,1f,0f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
0f,0f,1f,1f,
};
//索引数组,索引数组就是顶点画成三角形的顺序,这里比如说0,1,2构成一个三角形,0,2,3构成一个三角形,这两个三角形就构成一个面了
short[] indices={
0,1,2,
0,2,3,
4,5,6,
4,6,7,
8,9,10,
8,10,11,
12,13,14,
12,14,15,
16,17,18,
16,18,19,
20,21,22,
20,22,23,
};
angle++;
angle = angle%360;
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
ibb.order(ByteOrder.nativeOrder());
ShortBuffer mIndixBuffer = ibb.asShortBuffer();
mIndixBuffer.put(indices);
mIndixBuffer.position(0);
ByteBuffer cbb
= ByteBuffer.allocateDirect(cubeColors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(cubeColors);
colorBuffer.position(0);
ByteBuffer vbb
= ByteBuffer.allocateDirect(cubeVertices.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(cubeVertices);
vertex.position(0);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 启用顶点座标数据
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 启用顶点颜色数据
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 重置当前的模型视图矩阵
gl.glLoadIdentity();
// 沿着Y轴旋转
gl.glRotatef(angle, 0f, 1f, 0f);
// 设置顶点的位置数据
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vbb);
// 设置顶点的颜色数据
gl.glColorPointer(4, GL10.GL_FLOAT, 0, cbb);
//这里有两个画三角形的方法,具体区别可以自己试试看看效果
//绘制三角形,按照顶点数组画三角形
// gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP , 0, 24);
//绘制三角形,按照mIndixBuffer中索引画三角形
//glDrawElements各个参数如下:
mode:GL_POINTS,GL_LINE_STRIP,GL_LINE_LOOP,GL_LINES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN 和GL_TRIANGLES
count:指明被渲染的元素个数。
type:索引指的类型,GL_UNSIGNED_BYTE 或GL_UNSIGNED_SHORT。
indices:存储索引的位置数组
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, mIndixBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
4.位置变换
/**
* 变换,平移,旋转,缩放
* @param gl
*/
public void Translate(GL10 gl) {
//设置每个顶点颜色
ByteBuffer cbb
= ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArrays3.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArrays3);
vertex.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
//设置每个顶点颜色
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
//需调用此方法设为单位矩阵才能进行变换
gl.glLoadIdentity();
//平移,x方向平移1
gl.glTranslatef(1f,0.0f,0.0f);
//旋转,以x轴旋转60
gl.glRotatef(60,1f,0f,0f);
//缩放,缩放为0.5
gl.glScalef(0.5f,0.5f,0.5f);
//再次调用,恢复原来的图形,上面的变换失效
// gl.glLoadIdentity();
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
5、设置观察者角度
/**
* 设置观察者角度
* @param gl
*/
public void gluLookAt(GL10 gl) {
angle++;
angle = angle%360;
Log.i("aaa",""+angle);
//设置每个顶点颜色
ByteBuffer cbb
= ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArrays3.length*4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArrays3);
vertex.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
//设置每个顶点颜色
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
gl.glLoadIdentity();
//旋转
gl.glRotatef(angle,0f,1f,0f);
GLU.gluLookAt(gl, 0f, 0f, 0f, 0f, 0f, -0.5f, 0.0f, -1f, 0f);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
// eyex,eyey,eyez:表示指定观测点的空间坐标。
// centerx,centery,centerz:指定被观测物体的参考点的坐标。
// upx,upy,upz:指定观测点方向为“上”的向量。
// gluLookAt(GL10 gl, float eyeX, float eyeY, float eyeZ,
// float centerX, float centerY, float centerZ, float upX, float upY,
// float upZ)
上面的代码可以下载下来,在onDrawFrame中替换不同画图方法来试试上面的效果
@Override
public void onDrawFrame(GL10 gl) {
System.out.println("MyRenderer onDrawFrame");
// 清除屏幕
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// drawPoint(gl);
// DrawLine(gl);
// DrawSJ(gl);
// Translate(gl);
// gluLookAt(gl);
drawZFX(gl);
}
demo下载地址:http://pan.baidu.com/s/1c2oXQes 密码:7jz2