openGL基礎

1.新建一個類MyGLSurfaceView繼承GLSurfaceView(),Activity contentView的內容設置爲這個類的對象。java

2.一個GLSurfaceView對象須要一個渲染對象GLRender。因此:新建一個MyGLRender繼承GLSurfaceView.Renderer。在GLRender的三個方法中進行渲染相關的工做。android

3.在設置setContentView以前。把一個GLRender對象傳給建立的GLSurfaceView對象。app

package com.screenrecord.anychat.opengl

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class OpenGL2Activity : AppCompatActivity() {


    var mGlSurface: GLSurfaceView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mGlSurface = MyGLSurfaceView(this)
        mGlSurface?.setRenderer(MyGLRender())
        setContentView(mGlSurface)
    }


    class MyGLSurfaceView(context: Context): GLSurfaceView(context){
        private var mGLRender:MyGLRender ?= null

        init {
            setEGLContextClientVersion(2)
            mGLRender = MyGLRender()
        }
    }

    class MyGLRender: GLSurfaceView.Renderer{


        override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
            GLES20.glClearColor(0f,0f,0f,1f)
        }

        override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
            GLES20.glViewport(0, 0, width, height)
        }


        override fun onDrawFrame(gl: GL10?) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        }
    }
}
相關文章
相關標籤/搜索