使用open GL ES 绘制三角形

首先自定义一个GLSurfaceView

    class MyGLSurceView extends GLSurfaceView {

        public MyGLSurceView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyGLSurceView(Context context) {
super(context);
} }

再定义一个给GLSurfaceView进行渲染的渲染器Renderer,他是GLSurfaceView的内部接口类。

class MyRenderer implements GLSurfaceView.Renderer {

        @Override
public void onDrawFrame(GL10 gl) {
//清除缓存颜色区域
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//设置模型视图
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();//加载单位矩阵
//眼睛放置的位置
//GL10 gl, float eyeX, float eyeY, float eyeZ, 眼睛放置的位置
//float centerX, float centerY, float centerZ, 相机放置的位置
//float upX, float upY, float upZ 相机的朝向
GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
//定义三角形的点x,y,z
float[] coords = {
0f,0.5f,0f,
-0.5f,-0.5f,0f,
0.5f,-0.5f,0f
};
//存到内存缓存区
ByteBuffer buffer = ByteBuffer.allocateDirect(coords.length *4);
buffer.order(ByteOrder.nativeOrder());
//放置顶点坐标
FloatBuffer floatBuf = buffer.asFloatBuffer();
floatBuf.put(coords);//将点存放到floatBuffer里面
buffer.position(0);//指针指向0
//制定绘制点的颜色
gl.glColor4f(1f, 0f, 0f, 1f);
//开始绘制点
//3 三维点, type数据类型,0跨度,顶点缓冲区
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, buffer);
//设置绘制三角形
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); } @Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
//设置视口区域
gl.glViewport(0, 0, width, height);
//设置绘制模式
gl.glMatrixMode(GL10.GL_PROJECTION);
//设置为单例矩阵
gl.glLoadIdentity();
float ration = (float)width/(float)height;
//设置平截头体 左,右,下,上,近平面,远平面
gl.glFrustumf(-1f, 1f, -ration, ration, 3, 7); } @Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
//设置清屏颜色
gl.glClearColor(1, 1, 1, 1);
//启动绘制顶点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
} }
public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyGLSurceView glSurceView = new MyGLSurceView(this);
glSurceView.setRenderer(new MyRenderer());
setContentView(glSurceView);
}
}

运行效果图如下:

Android_GLSurfaceView的更多相关文章

随机推荐

  1. easyui 改变单元格背景颜色

    另外一种方法:https://www.cnblogs.com/raitorei/p/9989649.html easyui的datagrid改变整行颜色:https://www.cnblogs.com ...

  2. node.js中express使用cookie-parser 和 cookie-session处理会话

    cookie-parser 中间件用来解析客户端传过来的cookie,cookie-session 中间件用来建立基于cookie的会话session. 一.安装 cookie-parser 和 co ...

  3. spring mvc controller中的参数验证机制(二)

    这里我们介绍以下自定义的校验器的简单的使用示例 一.包结构和主要文件 二.代码 1.自定义注解文件MyConstraint package com.knyel.validator; import ja ...

  4. 在JSP页面获取集合的长度

    在jsp页面上经常遇到得到集合长度.字符长度.字符切取等应用需,在2.0以前这种需是许多程序员对JSTL及为不满意的地方之一.为此在2.0 中添加了functions标签,其提供对以上需求的支持. 使 ...

  5. Python基础(六)

  6. 08-jQuery的位置信息

    Query的位置信息跟JS的client系列.offset系列.scroll系列封装好的一些简便api. 一.宽度和高度 获取宽度 .width() 描述:为匹配的元素集合中获取第一个元素的当前计算宽 ...

  7. 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)

    一.Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...

  8. 冲刺博客NO.9

    今天做了什么: 看书,看视频学UI设计,尝试设计并美化,然并没有美感,感觉自己设计的界面太丑. 主体进度差不多完成了,美化.

  9. volatile和synchronized

    volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;              int geti1() {return i1;} vo ...

  10. three.js 微信小游戏

    最近在 https://classroom.udacity.com/courses/cs291 学习了一些 3D 引擎和 three.js 的知识 把 three.js 弄到微信小游戏里,先随便跑一跑 ...