OpenGL ES应用开发实践指南(android 卷)笔记 第一章

本文介绍了如何在Android上使用OpenGL ES进行应用开发,通过FirstOpenGLActivity和FirstOpenGLRenderer类展示了如何设置GLSurfaceView,检查OpenGL ES 2.0支持,以及实现渲染器的onSurfaceCreated、onSurfaceChanged和onDrawFrame方法,以进行基本的屏幕颜色设置和清除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class FirstOpenGLActivity extends Activity {
    private GLSurfaceView glSurfaceView;

    //用于记住GLSurfaceView是否处于有效状态
    private boolean rendererSet = false;

    /**
     * GLSurfaceView会在一个单独的线程中调用渲染器的方法。
     * 默认情况下,GLSurfaceView会以显示设备的刷新频率不断地渲染,
     * 当然,它也可以配置为按请求渲染,只需要用GlSurfaceView.RENDERMODE_WHEN_DIRTY作为参数调用GLSurfaceView.setRenderMode()即可
     *
     * 既然Android的GLSurfaceView在后台线程中执行渲染,就必须要小心,
     * 只能在这个渲染线程中调用OpenGL,在Android主线程中使用UI(用户界面)相关的调用;
     * 两个线程直接的通信可以用如下方法:
     * 在主线程中的GLSurfaceView实例可以调用queueEvent()方法传递一个Runnable给后台渲染线程,
     * 渲染线程可以调用Activity的runOnUIThread()来传递事件(event)给主线程
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glSurfaceView = new GLSurfaceView(this);

        final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
        final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000
                || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
                    && (Build.FINGERPRINT.startsWith("generic")
                    || Build.FINGERPRINT.startsWith("unknown")
                    || Build.MODEL.contains("google_sdk")
                    || Build.MODEL.contains("Emulator")
                    || Build.MODEL.contains("Android SDK built for x86")));

        if(supportsEs2){
            glSurfaceView.setEGLContextClientVersion(2);
            glSurfaceView.setRenderer(new FirstOpenGLRenderer());
            rendererSet = true;
        } else {
            Toast.makeText(this, "This device does not support OpenGL ES 2.0",Toast.LENGTH_SHORT).show();
            return;
        }
        setContentView(glSurfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(rendererSet){
            glSurfaceView.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(rendererSet){
            glSurfaceView.onResume();
        }
    }
}
public class FirstOpenGLRenderer implements GLSurfaceView.Renderer{

    /**
     * 当Surface被创建的时候,GLSurfaceView会调用这个方法;
     * 这发生在应用程序第一次运行的时候,并且,当设备被唤醒或者用户从其他activity切换回来时,这个方法也可能会被调用。
     * 在实践中,这意味着,当应用程序运行时,本方法可能会被调用多次。
     * @param gl10
     * @param eglConfig
     */
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        //红绿蓝透明度,渲染后结果为红色
        gl10.glClearColor(1.0f,0.0f,0.0f,0.0f);
    }

    /**
     * 在Surface被创建以后,每次Surface尺寸变化时,这个方法都会被GLSurfaceView调用到。在横屏、竖屏来回切换的时候,Surface尺寸会发生变化
     * @param gl10
     * @param width
     * @param height
     */
    @Override
    public void onSurfaceChanged(GL10 gl10, int width, int height) {
        gl10.glViewport(0, 0, width, height);
    }

    /**
     * 当绘制一帧时,这个方法会被GLSurfaceView调用。
     * 在这个方法中,我们一定要绘制一些东西,即使只是清空屏幕;
     * 因为,在这个方法返回后,渲染缓冲区会被交换并显示在屏幕上,如果什么都没画,可能会看到糟糕的闪烁效果
     * @param gl10
     */
    @Override
    public void onDrawFrame(GL10 gl10) {
        gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值