android opengles

本文详细介绍了如何使用OpenGL ES 2.0在Android平台上绘制3D图形,从GLSurfaceView的使用到Shader的编写,再到具体形状如三角形的绘制过程。文章深入浅出地讲解了OpenGL ES 2.0的编程流程和技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1One of the more straight-forward ways to do this is to implement both a GLSurfaceView and a GLSurfaceView.Renderer.

GLSurfaceView is a view container for graphics drawn with OpenGL

andGLSurfaceView.Renderer controls what is drawn within that view

 

 

2onCreate -> MyGLSurfaceView(setRenderer(mRenderer);)

 

3define a triangle

the typical way to do this is to define a vertex array of floating point numbers for the coordinates. For maximum efficiency, you write these coordinates into a ByteBuffer, that is passed into the OpenGL ES graphics pipeline for processing

 

4draw a shape

Drawing a defined shape using OpenGL ES 2.0 requires a significant amount of code, because you must provide a lot of details to the graphics rendering pipeline. Specifically, you must define the following:

  • Vertex Shader - OpenGL ES graphics code for rendering the vertices of a shape.
  • Fragment Shader - OpenGL ES code for rendering the face of a shape with colors or textures.
  • Program - An OpenGL ES object that contains the shaders you want to use for drawing one or more shapes

5In order to draw your shape, you must compile the shader code, add them to a OpenGL ES program object and then link the program. Do this in your drawn object’s constructor, so it is only done once

 

6draw()

specify several parameters to tell the rendering pipeline what you want to draw and how to draw it. Since drawing options can vary by shape, it's a good idea to have your shape classes contain their own drawing logic,eg:postion and color 

 

apply projection and camera transformations

 

7in the GLSurfaceView.Renderer class

 onSurfaceChanged()  define a projection

 

The projection matrix recalculates the coordinates of your graphics so that they map correctly to Android device screens

 

 

onDrawFrame(GL10 unused) define a camera view

The camera view matrix creates a transformation that renders objects from a specific eye position

 

 在MyGLSurfaceView构造里setRenderer(mRenderer);

 public MyGLSurfaceView(Context context) {

        super(context);

 

        // Create an OpenGL ES 2.0 context.

        setEGLContextClientVersion(2);

 

        // Set the Renderer for drawing on the GLSurfaceView

        mRenderer = new MyGLRenderer();

        setRenderer(mRenderer);

 

        // Render the view only when there is a change in the drawing data

        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }

 

接着在MyGLRenderer里的onSurfaceCreated方法初始化形状

 

            //创建三角形对对象 

 

            tle=new Triangle(MyTDView.this); 

 

初始化进入Triangle()构造方法进行

       //初始化顶点坐标与着色数据放置于ByteBuffer

    initVertexData();

    //初始化shader,编译compile和链接Link  shader

    initShader(mv);

 

接着在MyGLRenderer里的onDrawFrame绘制形状

 

          //绘制三角形对

 

           tle.drawSelf(); 

 

MyGLRenderer里的onSurfaceChanged()

 

//调用此方法计算产生透视投影矩阵

 Matrix.frustumM(Triangle.mProjMatrix, 0, -ratio, ratio, -1, 1, 1, 10);

以供与视图矩阵相结合成最终矩阵

 Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);  

 

 //调用此方法产生摄像机9参数位置矩阵,即设置相机的位置,camera postion

Matrix.setLookAtM(Triangle.mVMatrix, 0, 0,0,3,0f,0f,0f,0f,1.0f,0.0f);

 

每个形状绘制自己是用opengles编程

如下:

 

//制定使用某套shader程序

    GLES20.glUseProgram(mProgram);        

    //初始化变换矩阵

         Matrix.setRotateM(mMMatrix,0,0,0,1,0);

         //设置沿Z轴正向位移1

//         Matrix.translateM(mMMatrix,0,0,0,1);

         Matrix.translateM(mMMatrix,0,0,0,1);

 

 

         Matrix.translateM(mMMatrix,0,xAngle,0,1);

 

//         //设置绕x轴旋转

//         Matrix.rotateM(mMMatrix,0,xAngle,1,0,0);

         //

         GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, Triangle.getFianlMatrix(mMMatrix), 0); 

         //为画笔指定顶点位置数据

         GLES20.glVertexAttribPointer(

         maPositionHandle,   

         3, 

         GLES20.GL_FLOAT, 

         false,

                3*4,   

                mVertexBuffer

         );

         GLES20.glVertexAttribPointer  

         (

        maColorHandle,

         4,

         GLES20.GL_FLOAT,

         false,

                4*4,

                mColorBuffer

         );

         //允许顶点位置数据数组

         GLES20.glEnableVertexAttribArray(maPositionHandle);  

         GLES20.glEnableVertexAttribArray(maColorHandle);  

         //绘制三角形

         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); 

 

mMMatrix 具体物体的移动旋转矩阵,旋转、平移

 

//结合移动矩阵mMMatrix 和 视图摄像机矩阵mVMatrix    到mMVPMatrix

 

Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);

 

//结合投影矩阵mProjMatrix 和 上述矩阵mMVPMatrix  到mMVPMatrix

Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0); 

 

 

 

 

 

http://www.arcsynthesis.org/gltut/

 

http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html

 

http://www.learnopengles.com/android-lesson-one-getting-started/

 

http://blog.youkuaiyun.com/kesalin/article/details/8223649

 

http://www.learnopengles.com/android-lesson-one-getting-started/

 

http://www.learnopengles.com/understanding-opengls-matrices/

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值