OpenGL学习1

1.创建一个Activity

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

public class OpenGLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        GLSurfaceView glView = new GLSurfaceView(this);
        glView.setRenderer(new OpenGLRender());
        setContentView(glView);
    }
}

 

2.实现Renderer接口

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer

public class OpenGLRender implements Renderer{

    /**
     * 调用此方法绘制当前窗口
     */
    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);  //清楚屏幕和深度缓存
    }

    /**
     * 窗口被创建或者窗口大小改变时被调用
     */
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);        //设置投影矩阵
        gl.glLoadIdentity();      //重置投影矩阵
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);    //设置视口大小
    }

    /**
     * 窗口被创建或者被重新创建时调用,做初始化工作
     */
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0, 0, 1.0f, 0.5f);    //设置背景色,蓝色
    }

}

 3.效果如下所示


OpenGL学习1
 

你可能感兴趣的:(工作,android,OS)