OpenGL基础之绘制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_34402358/article/details/79580216

OpenGL绘制函数!

绘制方式 说明
GL_POINTS点) 绘制点
GL_LINES(线段) 连个点一组进行点的绘制,如果只有一个点就会舍弃这个点
GL_LINES_STRIP(条带线) 按照顶点顺序连接顶点
GL_LINES_LOOP(循环线) 按照顶点顺序连接顶点,最后一个点连接第一点
GL_TRIANGLES(三角形) 三个点一组,如果不够三个点就会舍弃 多余的点
GL_TRIANGLE_STRIP(三角形带) 顶点按照顺序依次 组成三角形绘制,最后实际形成的是一个三角型带
GL_TRIANGLE_FAN(三角形扇面) 将第一个点作为中心点,其他点作为边缘点,绘制一系列的组成扇形的三角形
/**
 * 绘制点
 * @author qiaosen
 * @date 2018/3/16
 */

public class Points extends OpenGLUtils {
    private IntBuffer verIntBuffer, colorIntBuffer;
    private ByteBuffer indbuffer;

    public Points() {

        init();
    }

    // 初始化
    private void init() {
        //比例
        int UNIT_SIZE = 10000;
        //顶点数据(xyz)
        int ver[] = new int[]{
                -2 * UNIT_SIZE, 3 * UNIT_SIZE, 0,
                1 * UNIT_SIZE, 1 * UNIT_SIZE, 0,
                -1 * UNIT_SIZE, -2 * UNIT_SIZE, 0
        };
        //创建顶点缓冲
        verIntBuffer = getIntBuffer(ver);
        //支持65536色彩通道
        int one = 65536;
        //顶点个数=颜色个数
        //颜色数据(RGB A)
        int color[] = new int[]{
                one, 0, 0, 0,
                one, 0, 0, 0,
                one, 0, 0, 0,
                one, 0, 0, 0
        };
        //创建顶点颜色缓冲
        colorIntBuffer = getIntBuffer(color);
        // 索引
        byte indices[] = new byte[]{
                0,3,2
        };
        // 创建顶点索引缓冲
        indbuffer = getByteBuffer(indices);

    }

    public void drawSelf(GL10 gl){
        // 启用顶点数组坐标
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        // 启用顶点颜色数组坐标
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        // 设置 画笔
        //给画笔指定顶点数据
        gl.glVertexPointer(3,//坐标个数xyz
                GL10.GL_FIXED,//顶点的数据类型
                0,//间隔 默认0
                verIntBuffer);///顶点数据

        //给画笔指定顶点颜色数据
        gl.glColorPointer(4,//颜色组成个数
                GL10.GL_FIXED,//颜色的数据类型
                0,//间隔 默认0
                colorIntBuffer);///顶点颜色数据
        //设置顶点大小
        gl.glPointSize(10);
        //绘制(索引法)
        gl.glDrawElements(GL10.GL_POINTS,//绘制模型(点1 线段3 三角形3)
                3,//索引个数
                GL10.GL_UNSIGNED_BYTE,//数据类型
                indbuffer);//索引数据

    }
}

这里写图片描述

/**
 *
 * @author qiaosen
 * @date 2018/3/15
 */
// 绘制线
public class Lines extends OpenGLUtils {
    private IntBuffer verIntBuffer, colorIntBuffer;
    private ByteBuffer indbuffer;

    public Lines() {
        //初始化View
        init();
    }

    private void init() {
        //比例
        int UNIT_SIZE = 10000;
        //顶点数据(xyz)
        int ver[] = new int[]{
                -2 * UNIT_SIZE,
                3 * UNIT_SIZE,
                0,
                1 * UNIT_SIZE,
                1 * UNIT_SIZE,
                0,
                -1 * UNIT_SIZE,
                -2 * UNIT_SIZE,
                0,
                2 * UNIT_SIZE,
                -3 * UNIT_SIZE,
                0
        };
        //创建顶点缓冲
        verIntBuffer = getIntBuffer(ver);
        //支持65536色彩通道
        int one = 65536;
        //顶点个数=颜色个数
        //颜色数据(RGB A)
        int color[] = new int[]{
                one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0, one, 0, 0, 0
        };
        //创建顶点缓冲
        colorIntBuffer = getIntBuffer(color);
        //索引
        byte indices[] = new byte[]{
                0, 3, 2, 1
        };
        //创建顶点索引缓冲
        indbuffer = getByteBuffer(indices);
    }

    public void drawSelf(GL10 gl) {
        //启用顶点数组坐标
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //启用顶点颜色数组坐标
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        //设置“画笔”
        //给画笔指定顶点数据
        gl.glVertexPointer(3//坐标个数xyz
                , GL10.GL_FIXED//顶点的数据类型
                , 0//间隔 默认0
                , verIntBuffer);///顶点数据
        //给画笔指定顶点颜色数据
        gl.glColorPointer(4//颜色组成个数
                , GL10.GL_FIXED//颜色的数据类型
                , 0//间隔 默认0
                , colorIntBuffer);///顶点颜色数据
        gl.glLineWidth(10);
//        //绘制(索引法)
        gl.glDrawElements(GL10.GL_LINES //绘制模型(点1 线段3 三角形3)
                , 4//索引个数
                , GL10.GL_UNSIGNED_BYTE//数据类型
                , indbuffer);//索引数据

    }

}

这里写图片描述

/**
 *
 * @author qiaosen
 * @date 2018/3/15
 */
// 三角形
public class Triangle extends OpenGLUtils {
        private IntBuffer verIntBuffer, colorIntBuffer;
        private ByteBuffer indbuffer;

        public Triangle() {
            init();
        }

        //初始化view
        private void init() {
            int UNIT_SIZE = 10000;//比例
            //顶点数据(xyz)
            int ver[] = new int[]{
                    -2 * UNIT_SIZE,
                    5 * UNIT_SIZE,
                    0,
                    1 * UNIT_SIZE,
                    6 * UNIT_SIZE,
                    0,
                    -8 * UNIT_SIZE,
                    -8 * UNIT_SIZE,
                    0,
                    };
            //创建顶点缓冲
            verIntBuffer = getIntBuffer(ver);
            int one = 65536;//支持65536色彩通道
            //顶点个数=颜色个数
            //颜色数据(RGB A)
            int color[] = new int[]{
                    one, 0, 0, 0, one, 0, 1, 0, one, 0, 1, 0
            };
            //创建顶点缓冲
            colorIntBuffer = getIntBuffer(color);
        }

        public void drawSelf(GL10 gl) {
            //启用顶点数组坐标
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //启用顶点颜色数组坐标
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
            //设置“画笔”
            //给画笔指定顶点数据
            gl.glVertexPointer(3//坐标个数xyz
                    , GL10.GL_FIXED//顶点的数据类型
                    , 0//间隔 默认0
                    , verIntBuffer);///顶点数据
            //给画笔指定顶点颜色数据
            gl.glColorPointer(4//颜色组成个数
                    , GL10.GL_FIXED//颜色的数据类型
                    , 0//间隔 默认0
                    , colorIntBuffer);///顶点颜色数据
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        }
}

这里写图片描述

/**
 *
 * @author qiaosen
 * @date 2018/3/15
 */
//多边形
public class Polygon extends OpenGLUtils {
    private FloatBuffer verBuffer;
    private FloatBuffer colorBuffer;
    private int verNum;

    public Polygon() {
        init();
    }

    //初始化
    private void init() {
        int length = 45;//边长
        //顶点个数
        verNum = (360 / length + 2);
        float ver[] = new float[verNum * 3];
        float color[] = new float[verNum * 4];
        //计数器
        int count = 0;
        int count2 = 0;
        int one = 65536;//支持65536色彩通道
        for (int i = 0; i < 360 + length; i += length) {
            double di = Math.toRadians(i);

            ver[count++] = (float) (Math.cos(di) - Math.sin(di));
            ver[count++] = (float) (Math.cos(di) + Math.sin(di));
            ver[count++] = 0;

            color[count2++]=  0;
            color[count2++]=  one;
            color[count2++]=  one;
            color[count2++]=  one;
        }

        verBuffer = getFloatbuffer(ver);
        colorBuffer = getFloatbuffer(color);
    }

    public void drawSelf(GL10 gl) {
        //启用顶点数组坐标
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //启用顶点颜色数组坐标
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        //设置“画笔”
        //给画笔指定顶点数据
        gl.glVertexPointer(3//坐标个数xyz
                , GL10.GL_FLOAT//顶点的数据类型
                , 0//间隔 默认0
                , verBuffer);///顶点数据
        //给画笔指定顶点颜色数据
        gl.glColorPointer(4//颜色组成个数
                , GL10.GL_FLOAT//颜色的数据类型
                , 0//间隔 默认0
                , colorBuffer);///顶点颜色数据
        gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, verNum);
    }
}

这里写图片描述

/**
 * 继承GLSurfaceView 初始化GLSurfaceView
 * @author qiaosen
 * @date 2018/3/16
 */

public class DGLView extends GLSurfaceView {
    /**画点**/
    Points mPoints;
    /**画线**/
    Lines mLines;
    /**画三角形**/
    Triangle mTriangle;
    /**画多变形**/
    Polygon mPolygon;

    public DGLView(Context context) {
        super(context);
        init();
    }
    //初始化
    private void init() {
        //  渲染器
        setRenderer(new DGLRender());

        setRenderMode(RENDERMODE_CONTINUOUSLY);
        //  渲染模式  2中
        //  RENDERMODE_CONTINUOUSLY  主动渲染 一直刷新
        //  RENDERMODE_WHEN_DIRTY  被动渲染 需要requestRender();刷新

    }

    private class DGLRender implements Renderer {

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            //关闭抗抖动 :(对于可用颜色较少的系统 ,可以牺牲分辨率,通过颜色抖动来增加颜色数量)
            gl.glDisable(GL10.GL_DITHER);
            //设置背景为黑色 RGBA
            gl.glClearColor(0,0,0,0);
            //设置hint模式 (快速模式)
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
            //开启深度测试(3D里面)
            gl.glEnable(GL10.GL_DEPTH_TEST);

//            mPoints = new Points();
//            mLines = new Lines();
//            mTriangle = new Triangle();
            mPolygon = new Polygon();

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            //设置视口
            //x y 手机屏幕原点坐标
            //width height 视口大小
            gl.glViewport(0,0,width,height);
            //设置矩阵(投影矩阵)
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            //得到宽高比
            float r = (float)width/height;
            //设置视角
            gl.glFrustumf(-r,r,-1,1,1,20);

        }

        @Override
        public void onDrawFrame(GL10 gl) {
            //清除颜色和深度缓存
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
            //设置矩阵((模型矩阵)
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glLoadIdentity();
            gl.glTranslatef(0,0,-3.0f);
            //绘制
            /**画点**/
//            mPoints.drawSelf(gl);
            /**画线**/
//            mLines.drawSelf(gl);
            /**画三角形**/
//            mTriangle.drawSelf(gl);
            /**画多边形**/
            mPolygon.drawSelf(gl);

        }
    }
}
public class MainActivity extends AppCompatActivity {
    DGLView mDGLView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDGLView = new DGLView(this);
        setContentView(mDGLView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDGLView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDGLView.onPause();
    }
}

demo下载地址

猜你喜欢

转载自blog.csdn.net/github_34402358/article/details/79580216