package com.yue.wh;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class GLRender implements Renderer {
float rotateTri,rotateQuad;
int one=0x10000;
int width=400;
int height=800;
public static IntBuffer mBuffer ;
//如果不使用此种方式而直接使用下面的IntBuffer.wrap()的方式则
//很容易产生illegalArgumentException异常(android must use a native order direct buffer)
public static IntBuffer iBuffer(int [] a){
//先初始化buffer,数组的长度*4,因为一个int占4个字节
ByteBuffer mbb=ByteBuffer.allocateDirect(a.length*4);
//数组排列用nativeOrder
mbb.order(ByteOrder.nativeOrder());
mBuffer=mbb.asIntBuffer();
mBuffer.put(a);
mBuffer.position(0);
return mBuffer;
}
//三角形的一个顶点
// private IntBuffer triggerBuffer=IntBuffer.wrap(new int[]{
// 0,one,0, //上顶点
// -one,-one,0, //左顶点
// one,-one,0 //右下点
// });
//
// //正方形的四个顶点
// private IntBuffer quateBuffer=IntBuffer.wrap(new int[]{
// one,one,0,
// -one,-one,0,
// one,-one,0,
// -one,-one,0
// });
// private IntBuffer colorBuffer=IntBuffer.allocate(12).wrap(new int[]{
// one,0,0,one,
// 0,one,0,one,
// 0,0,one,one
// });
private IntBuffer triggerBuffer=iBuffer(new int[]{
0,one,0, //上顶点
-one,-one,0, //左顶点
one,-one,0 //右下点
});
//正方形的四个顶点
private IntBuffer quateBuffer=iBuffer(new int[]{
one,one,0,
-one,-one,0,
one,-one,0,
-one,-one,0
});
private IntBuffer colorBuffer=iBuffer(new int[]{
one,0,0,one,
0,one,0,one,
0,0,one,one
});
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
// 清除屏幕和深度缓存
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.glRotatef(rotateTri, 0.0f, 1.0f, 0.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);
//绘制三角形结束
gl.glFinish();
/***********************/
/* 渲染正方形 */
// 重置当前的模型观察矩阵
gl.glLoadIdentity();
// 左移 1.5 单位,并移入屏幕 6.0
gl.glTranslatef(1.5f, 0.0f, -6.0f);
// 设置当前色为蓝色
gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
//设置旋转
gl.glRotatef(rotateQuad, 1.0f, 0.0f, 0.0f);
//设置和绘制正方形
gl.glVertexPointer(3, GL10.GL_FIXED, 0, quateBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
//绘制正方形结束
gl.glFinish();
//取消顶点数组
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//改变旋转的角度
rotateTri += 0.5f;
rotateQuad -= 0.5f;
}
@Override
public void onSurfaceChanged(GL10 gl, int arg1, int arg2) {
// TODO Auto-generated method stub
float ratio = (float) width / height;
//设置OpenGL场景的大小
gl.glViewport(0, 0, width, height);
//设置投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION);
//重置投影矩阵
gl.glLoadIdentity();
// 设置视口的大小
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
// 选择模型观察矩阵
gl.glMatrixMode(GL10.GL_MODELVIEW);
// 重置模型观察矩阵
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
// TODO Auto-generated method stub
// 启用阴影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
// 黑色背景
gl.glClearColor(0, 0, 0, 0);
// 设置深度缓存
gl.glClearDepthf(1.0f);
// 启用深度测试
gl.glEnable(GL10.GL_DEPTH_TEST);
// 所作深度测试的类型
gl.glDepthFunc(GL10.GL_LEQUAL);
// 告诉系统对透视进行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
}
}
android openGL ES使用小例子
需积分: 9 58 浏览量
2012-01-05
22:43:22
上传
评论 1
收藏 50KB RAR 举报
yxm659217822
- 粉丝: 5
- 资源: 27
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈