当先锋百科网

首页 1 2 3 4 5 6 7

1.编写一个实现Renderer接口的类

  • onSurfaceChanged:设置视口大小,设置投影类型,矩阵模式,切换投影类型等
  • onSurfaceCreated:一些初始化工作,如设置顶点坐标的值.
  • onDrawFrame:对开关的操作,绘制3D图形,设置顶点坐标等

 class RectangleRender implements GLSurfaceView.Renderer{
        //定义顶点
        int one=0x10000;
        IntBuffer rectangleBuffer;
        int[] rectangleVertices=new int[] {
                -one,-one,0,
                -one,one,0,
                one,-one,0,
                one,one,0,
        };

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            //为ByteBuffer对象分配内存空间
            ByteBuffer byteBuffer=ByteBuffer.allocateDirect(rectangleVertices.length*4);
            //按本地字节顺序使用字节数据
            byteBuffer.order(ByteOrder.nativeOrder());
            //将ByteBuffer转换成intBuffer对象
            rectangleBuffer=byteBuffer.asIntBuffer();
            //将定义顶点坐标的int数组放到intBuffer对象中
            rectangleBuffer.put(rectangleVertices);
            //将IntBuffer对象的内部指针移到第1个字节的位置
            rectangleBuffer.position(0);
     }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {

            //计算视口宽度与高度比
            float ratio=width/height;
            //设置视口大小
            gl.glViewport(0,0,width,height);
            //改变矩阵模式为投影模式
            gl.glMatrixMode(GL10.GL_FOG_MODE);
            //将当前矩阵设为单位矩阵
            gl.glLoadIdentity();
            //设置当前的投影类型为透视投影
            gl.glFrustumf(-ratio,ratio,1,1,1,10);
            //改变矩阵模式为模型视图矩阵
            gl.glMatrixMode(GL10.GL_MODELVIEW);

        }

        @Override
        public void onDrawFrame(GL10 gl) {
            //必须清楚屏幕,否则不会清除上次的视图
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BITS);
            //允许使用顶点方式绘制图形
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //将当前矩阵设为单位矩阵
            gl.glLoadIdentity();
            //将当前位置向z轴负方向移6个单位
            gl.glTranslatef(0,0,-6);
            //旋转90度
            gl.glRotatef(90,0,1,0);
            //装载用于绘制图形的顶点坐标
//            gl.glVertexPointer(3,GL10.GL_FIXED,0,rete);
            //绘制图形
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);
            //禁止使用顶点方式绘制图形
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

         }
    }
eg:绘制三角形

1.Triangle类

package com.gst.user.application;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

/**
 * Created by user on 1/5/16.
 */
public class Triangle {
    int one = 0x10000;
    private IntBuffer triangleBuffer;
    private int[] triangleVertices = new int[]
            { 0, one, 0, -one, -one, 0, one, -one, 0 };

    public Triangle()
    {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(triangleVertices.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        triangleBuffer = byteBuffer.asIntBuffer();
        triangleBuffer.put(triangleVertices);
        triangleBuffer.position(0);
    }

    public void drawSelf(GL10 gl){
        gl.glVertexPointer(3,GL10.GL_FIXED,0,triangleBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
        gl.glRotatef(180, 0, 0, 1);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
    }
}
2.TriangleSurfaceView

package com.gst.user.application;

import android.opengl.GLSurfaceView;

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

/**
 * Created by user on 1/5/16.
 */
public class TriangleSurfaceView implements GLSurfaceView.Renderer{
    Triangle triangle;

    int angle1=0;
    int angle2=0;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        triangle=new Triangle();

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {

        float ratio=(float)width/height;
        gl.glViewport(0,0,width,height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio * 2, ratio * 2, -2, 2, 1, 10);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -2);
        gl.glRotatef(angle1++, 0, 0, 1);
        triangle.drawSelf(gl);

        gl.glLoadIdentity();
        angle2-=2;
        gl.glRotatef(angle2, 0, 0, 1);
        gl.glTranslatef(-3, 0, -4);
        gl.glRotatef(45, 0, 0, 1);
        triangle.drawSelf(gl);

        gl.glLoadIdentity();
        gl.glRotatef(angle2, 0, 0, 1);
        gl.glTranslatef(3, 0, -4);
        gl.glRotatef(45, 0, 0, 1);
        triangle.drawSelf(gl);

        gl.glLoadIdentity();
        gl.glRotatef(angle2, 0, 0, 1);
        gl.glTranslatef(0,3,-4);
        gl.glRotatef(45,0,0,1);
        triangle.drawSelf(gl);

        gl.glLoadIdentity();
        gl.glRotatef(angle2, 0, 0, 1);
        gl.glTranslatef(0,-3,-4);
        gl.glRotatef(45,0,0,1);
        triangle.drawSelf(gl);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}
3.在Activity的onCreate方法中显示
 GLSurfaceView glSurfaceView=new GLSurfaceView(this);
 glSurfaceView.setRenderer(new TriangleSurfaceView());
 setContentView(glSurfaceView);