一、搭建OpenGL ES环境
1、在AndroidManifest.xml中申明OpenGL ES版本
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mao.openglesdemo">
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application>
...
</application>
</manifest>
2、创建一个GLSurfaceView和GLSurfaceView.Renderer
GLSurfaceView继承自SurfaceView,在它的基础上实现了一个GLThread,用来管理OpenGL ES的绘制,将绘制的结果显示在SurfaceView所提供的Surface上。
public class Cus1GLSurfaceView extends GLSurfaceView {
public Cus1GLSurfaceView(Context context) {
super(context);
init();
}
public Cus1GLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
//设置版本
setEGLContextClientVersion(2);
//设置渲染器
setRenderer(new Cus1Renderer());
//设置渲染模式
setRenderMode(RENDERMODE_WHEN_DIRTY);
}
}
渲染模式有两种:
- RENDERMODE_WHEN_DIRTY:这种渲染模式只会在surface被创建或者我们手动调用了requestRender的时候才会去渲染,按需渲染,性能更高。
- RENDERMODE_CONTINUOUSLY:默认的渲染方式,每隔一定时间会去刷新当前界面
Renderer是一个渲染器,用来真正的处理绘制逻辑
public class Cus1Renderer implements GLSurfaceView.Renderer {
//当surface创建时调用,可以做一些OpenGLES上下文的初始化
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(1,0,0,0);
}
//surface发生变化的时候调用
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0,0,width,height);
}
//绘制的时候调用
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
}
GLES20.glClearColor( float red, float green, float blue, float alpha):设置清屏时候的颜色
GLES20.glViewport( int x, int y, int width, int height):设置绘制显示的窗口大小
GLES20.glClear:清屏
最后显示在页面上:显示效果为红色
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Cus1GLSurfaceView(this));
}
}
github
最后
以上就是多情毛衣最近收集整理的关于搭建OpenGL ES环境(一)的全部内容,更多相关搭建OpenGL内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复