GLSurfaceView 绘制三角形

GLSurfaceView 绘制三角形


GLSurfaceView 的渲染类

public class MyRenderer implements Renderer {

    private TriangleHelp triangleHelp;

    @Override
    public void onDrawFrame(GL10 gl) {
        // 清除屏幕和深度缓存
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        triangleHelp.draw(gl);
    }

    @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();
        // 设置画面比例
        //GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,100.0f);
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
        // 选择模型观察矩阵
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // 黑色背景
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        // 启用阴影平滑(不是必须的)
        gl.glShadeModel(GL10.GL_SMOOTH);
        // 设置深度缓存
        gl.glClearDepthf(1.0f);
        // 启用深度测试
        gl.glEnable(GL10.GL_DEPTH_TEST);
        // 所作深度测试的类型
        gl.glDepthFunc(GL10.GL_LEQUAL);
        // 对透视进行修正
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        triangleHelp = new TriangleHelp();
    }
}

用于绘制三角形的帮助类

public class TriangleHelp {

    private FloatBuffer triangleBuffer;
    private FloatBuffer colorBuffer;

    // GLSurfaceView的中心为坐标圆心
    // 这里也确定了三角形的底边和高 的长度为 2f
    private float[] triangleArray = {
            0f, 1f, 0f,
            -1f, -1f, 0f,
            1f, -1f, 0f
    };

    private float[] colorArray={
            1f,0f,0f,1f,     //红
            0f,1f,0f,1f,     //绿
            0f,0f,1f,1f      //蓝
    };

    public TriangleHelp(){
        triangleBuffer = BufferUtil.floatToBuffer(triangleArray);
        colorBuffer = BufferUtil.floatToBuffer(colorArray);
    }

    /*
     * 绘制三角形
     */
    public void draw(GL10 gl){
        //使用数组作为颜色
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);

        //绘制小三角形
        gl.glLoadIdentity();
        gl.glTranslatef(-2f, 0.0f, -8.0f); // 控制三角形的位置,-2f为左移一个三角形的距离
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer);// 数组指向三角形顶点buffer
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        gl.glFinish();

    }
}

工具类

/*
 * 传入不同类型的数组,返回相应类型的缓冲数组
 */
public class BufferUtil {

    public static FloatBuffer floatToBuffer(float[] array){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());

        FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put(array);
        floatBuffer.position(0);

        return floatBuffer;
    }

    public static IntBuffer intToBuffer(int[] array){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());

        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(array);
        intBuffer.position(0);

        return intBuffer;
    }
}

你可能感兴趣的:(Android游戏开发)