Android_GLSurfaceView
使用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的更多相关文章
随机推荐
- ES6 Iterator
不同数据集合怎么用统一的方式读取 可以用for...of循环了
- LWIP学习
转自:https://blog.csdn.net/kzq_qmi/article/details/46900589 数据包pbuf: LwIP采用数据结构 pbuf 来描述数据包,其结构如下: ...
- How Xtuner E3 works for BMW 520d Diagnosis and initialization of CBS service
Using Xtuner E3 to perform BMW 520d Diagnosis and initialization of CBS service in step by step proc ...
- nagios 报警参数
host_notification_options: d = notify on DOWN host states, u = notify on UNREACHABLE host states r = ...
- 使用Maven搭建SpringMVC
1.创建Maven Project 注意选择webapp 2.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- Python day 4
阅读目录 内容回顾: 流程控制: if分支结构: while循环控制: for循环(迭代器): ##内容回顾: #1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 ...
- 9. Bookshops in London 伦敦书店
9. Bookshops in London 伦敦书店 (1) Londoner are greater readers.They buy vast numbers of newspapers and ...
- A股、B股区别
A股也称为人民币普通股票.流通股.社会公众股.普通股.是指那些在中国大陆注册.在中国大陆上市的普通股票.以人民币认购和交易. A股不是实物股票,以无纸化电子记帐,实行“T+1”交割制度,有涨跌幅(10 ...
- qhfl-1 跨域
CORS跨域请求 CORS即Cross Origin Resource Sharing 跨域资源共享,那么跨域请求还分为两种,一种叫简单请求,一种是复杂请求 简单请求 HTTP方法是下列方法之一 HE ...
- UEditor的jQuery插件化 -转
UEditor本身并不依赖jQuery,但如果在项目中同时使用两者的话,可能会希望使用jQuery语法创建和获取编辑器实例.为此,需要为jQuery编写插件,代码如下: (function ($) { ...