1One of the more straight-forward ways to do this is to implement both a GLSurfaceView
and a GLSurfaceView.Renderer
.
A 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/