在OpenGL ES环境中,投影和相机视图可以使被绘制的对象在某种方式上看起来更像实际的物体,这个模拟实际对象的场景是通过被绘制对象坐标的数学转换实现的: 投影 - 这种调整绘制对象坐标的变换是根据GLSurfaceView显示位置的宽度和高度。没有这个计算,OpenGL ES绘制的对象就会在不同比例的view window中扭曲变形。典型的投影变换只有在OpenGL view比例被确立或者改变的时候进行计算,在renderer的onSurfaceChanged()方法中。 相机视图 - 这种调整绘制对象坐标的变换是依据一个虚拟的相机位置。实际上OpenGL ES并没有定义一个实际的camera对象,而是提供了一个方法模拟camera来对绘制对象的显示进行变换。相机视图变换可能会在GLSurfaceView建立的时候计算一次,或者根据用户的行为动态的改变。 这个教程介绍了如何创建一个投影和相机视图,并把它们应用到GLSurfaceView绘制的图形中去。 1. 定义投影 投影变换的数据是在GLSurfaceView.Renderer类中的 onSurfaceChanged()方法里计算的。下面的代码获取了GLSurfaceView的高度和宽度,并使用它们来填充一个投影变换矩阵,使用Matrix.frustumM()方法:// mMVPMatrix is an abbreviation for "Model View Projection Matrix"// mMVPMatrix是"Model View Projection Matrix"的缩写private final float[] mMVPMatrix = new float[];private final float[] mProjectionMatrix = new float[];private final float[] mViewMatrix = new float[];@Overridepublic void onSurfaceChanged(GL unused, int width, int height) { GLES.glViewport(0, 0, width, height); float ratio = (float) width / height; // this projection matrix is applied to object coordinates // in the onDrawFrame() method // 这个投影矩阵在onDrawFrame()方法中被应用到对象坐标 Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);} 上面的代码填充了一个投影矩阵,mProjectionMatrix 可以在 onDrawFrame() 方法中和相机视图的变换结合在一起,后面会有提及。 注意:只对绘制的对象应用投影矩阵,一般会得到一个空的显示,通常,你应该应用一个相机视图变换为了使对象显示在屏幕上。 2. 定义相机视图 通过在renderer的绘制进程中添加相机视图的变换来完成绘制对象的变换过程,在下面的示例代码中,相机视图的变换是使用Matrix.setLookAtM()方法来计算的,会和前面计算的投影矩阵结合,结合后的变换矩阵会被传递到被绘制的图形。@Overridepublic void onDrawFrame(GL unused) { ... // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); // Draw shape mTriangle.draw(mMVPMatrix);} 3. 应用投影和相机视图变换 为了结合使用投影和相机视图变换矩阵,首先添加矩阵参数到顶点着色器:public class Triangle { private final String vertexShaderCode = // This matrix member variable provides a hook to manipulate // the coordinates of the objects that use this vertex shader "uniform mat4 uMVPMatrix;" "attribute vec4 vPosition;" "void main() {" // the matrix must be included as a modifier of gl_Position // Note that the uMVPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. " gl_Position = uMVPMatrix * vPosition;" "}"; // Use to access and set the view transformation private int mMVPMatrixHandle; ...} 接下来,修改对象的draw()方法,将结合后的变换矩阵应用到图形上:public void draw(float[] mvpMatrix) { // pass in the calculated transformation matrix ... // get handle to shape's transformation matrix mMVPMatrixHandle = GLES.glGetUniformLocation(mProgram, "uMVPMatrix"); // Pass the projection and view transformation to the shader GLES.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Draw the triangle GLES.glDrawArrays(GLES.GL_TRIANGLES, 0, vertexCount); // Disable vertex array GLES.glDisableVertexAttribArray(mPositionHandle);}
推荐整理分享Android OpenGL ES绘图教程之四 :投影和相机视图变换(android opengles2.0教程),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:android opengl es教程,android opengl es教程,opengl es api,android opengl es教程,android view opengl,android opengl es教程,android opengles2.0教程,android opengles2.0教程,内容如对您有帮助,希望把文章链接给更多的朋友!
![Android OpenGL ES绘图教程之四 :投影和相机视图变换(android opengles2.0教程)](https://www.jiuchutong.com/image/20240129/1706502709.jpg)
一旦你已经正确的计算和应用了投影和相机视图变换,你的图形对象会以正确的比例绘制,就像下面这样:
Android OpenGL ES绘图教程之五 :加入运动 在屏幕上面绘制对象是OpenGL的一个非常好的基本功能,但是你也可以在其他的Android图形框架上面实现这些功能,比如Canvas和Drawable对象。OpenGLES提供了额
Android OpenGL ES绘图教程之六 :响应触摸事件 使对象根据预设的程序进行运动,比如旋转三角形,可以吸引人的注意力。但是如果你想让用户同你的OpenGLES图形进行交互会怎么样呢?使你的OpenGLES应
VS下基于Glut OpenGL显示一些立体图形示例程序 转自: