原文地址:http://android.xsoftlab.net/training/graphics/opengl/projection.html##transform

在OpenGL ES环境中,投影相机View可以将所绘制的图形模拟成现实中所看到的物理性状。这种物理模拟是通过改变对象的数字坐标实现的:

  • 投影 - 这基于GLSurfaceView的高宽的坐标转换而实现。如果不采用这种计算,所绘制的对象就会由于窗口的不平等比例所曲解。投影转换只有在OpenGL视图的比例被确定或渲染器的onSurfaceChanged()方法发生改变后才会被计算。有关更多OpenGL的坐标映射关系,请参见Mapping Coordinates for Drawn Objects。
  • 相机视图 - 这基于虚拟相机的位置转变而实现。要着重注意OpenGL并没有定义真实的相机对象,而是提供了一种通过转换图形对象来模拟相机的方法。一个相机视图转换只有在GLSurfaceView被确定的时候才会开始计算,或者基于用户的行为或者应用的功能而动态的发生改变。

这节课描述了如何创建一个投影相机视图,并将其应用到图形的绘制中。

定义投影

投影转换的数据计算位于GLSurfaceView.Renderer的onSurfaceChanged()方法。下面的代码先取得了GLSurfaceView的高度与宽度,然后使用Matrix.frustumM()方法将数据填充到投影转换的Matrix中。

// mMVPMatrix is an abbreviation for "Model View Projection Matrix"
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {GLES20.glViewport(0, 0, width, height);float ratio = (float) width / height;// this projection matrix is applied to object coordinates// in the onDrawFrame() methodMatrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}

这些代码会填充投影矩阵:mProjectionMatrix,它可以被用来与相机视图转换整合。

Note: 仅仅将投影转换应用到要绘制的物体上,一般来说是没什么意义的。通常情况下,还必须将相机视图转换也一并应用。这样才能使物体呈现在屏幕上。

定义相机视图

完成相机视图的转换也是图像处理过程的一部分。在下面的代码中,相机视图转换通过Matrix.setLookAtM()方法进行计算,然后将结果整合进原先所计算好的矩阵中。最后被整合完毕的矩阵接着被用来绘制图形。

@Override
public void onDrawFrame(GL10 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 transformationMatrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);// Draw shapemTriangle.draw(mMVPMatrix);
}

应用投影转换及相机转换

为了可以使用上面所整合完毕的矩阵,首先需要将该矩阵变量添加到上节课程所定义的顶点渲染器中:

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 transformationprivate int mMVPMatrixHandle;...
}

接下来,修改绘制物体对象的draw()方法,以便可以接收整合后的矩阵,然后将其应用到图形中:

public void draw(float[] mvpMatrix) { // pass in the calculated transformation matrix...// get handle to shape's transformation matrixmMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");// Pass the projection and view transformation to the shaderGLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);// Draw the triangleGLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);// Disable vertex arrayGLES20.glDisableVertexAttribArray(mPositionHandle);
}

如果有正确的计算结果以及正确的使用了整合后的矩阵,那么图形会以正确的比例被绘制出来,最后看起来的效果应该是这样子的:

现在程序就可以正常显示比例正常的图形了,是时候为图形添加动作了。

Android官方开发文档Training系列课程中文版:OpenGL绘图之应用投影与相机视图相关推荐

  1. Android官方开发文档Training系列课程中文版:目录

    原文地址 : http://android.xsoftlab.net/training/index.html 引言 在翻译了一篇安卓的官方文档之后,我觉得应该做一件事情,就是把安卓的整篇训练课程全部翻 ...

  2. Android官方开发文档Training系列课程中文版:创建自定义View之View的创建

    原文地址:http://android.xsoftlab.net/training/custom-views/index.html 引言 Android框架含有大量的View类,这些类用来显示各式各样 ...

  3. Android官方开发文档Training系列课程中文版:OpenGL绘图之图形绘制

    原文地址:http://android.xsoftlab.net/training/graphics/opengl/draw.html 如果你还不清楚如何定义图形及坐标系统,请移步:Android官方 ...

  4. Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之Fragment创建

    原文地址:http://android.xsoftlab.net/training/basics/fragments/index.html 导言 为了在Android中创建动态的多面板用户界面,你需要 ...

  5. Android官方开发文档Training系列课程中文版:OpenGL绘图之添加动态效果

    原文地址:http://android.xsoftlab.net/training/graphics/opengl/motion.html 在屏幕上绘制物体只是OpenGL基础的基础,除了OpenGL ...

  6. Android官方开发文档Training系列课程中文版:调用相机之简单摄像

    原文地址:http://android.xsoftlab.net/training/camera/videobasics.html 这节课解释了如何通过已有的相机应用拍摄视频. 假设你的程序含有摄像功 ...

  7. Android官方开发文档Training系列课程中文版:打印内容之图像打印

    原文地址:http://android.xsoftlab.net/training/printing/index.html 引言 Android用户会很频繁的浏览设备上的内容,但是有部分情况例外,当屏 ...

  8. Android官方开发文档Training系列课程中文版:分享文件之配置文件共享

    原文地址:http://android.xsoftlab.net/training/secure-file-sharing/index.html 导言 APP经常需要给其它的APP提供一个或多个文件. ...

  9. Android官方开发文档Training系列课程中文版:添加ActionBar之设置ActionBar

    导言- 添加ActionBar 原文地址:http://android.xsoftlab.net/training/basics/actionbar/index.html ActionBar是很多重要 ...

最新文章

  1. 这可能是最详细的Python文件操作!
  2. linux shell 数组遍历方式(非原创)
  3. 深入理解Kinect for Windows开发
  4. RecyclerView滑动到指定位置,并置顶
  5. NEO versus Ethereum: Why NEO might be 2018’s strongest cryptocurrency
  6. 程序设计中的驼峰原则
  7. HarmonyOS之常用组件RoundProgressBar的功能和使用
  8. CSS实现【表格内容超过一行的部分,用省略号代替】
  9. 牛客题霸 [ 大数加法]C++题解/答案
  10. 关于显示和隐藏DIV标签
  11. 《Core Data应用开发实践指南》一2.15 小结
  12. 深度学习领域最新的技术(CV、NLP)
  13. 网页元素3D效果展示
  14. 一文搞懂this指向
  15. 以太坊 2.0 中的验证者经济模型,Part-1
  16. 现在做什么能挣钱?想要在互联网赚钱,一定要懂这些!
  17. [教程] KGFMapSystem - 快速创建游戏中的迷你地图
  18. 【仿真设计】仿真技术在智能制造中的作用;智能制造难点在模型,焦点在仿真;汽车行业CAE研究
  19. Android事件分发浅谈
  20. altium designer拼版例子

热门文章

  1. 句号一定要划在句子最美的地方
  2. liunx 下dhcp中继及服务器配置
  3. Animation 模拟纸盒的爆破
  4. 关于定于如何弄的漂亮点
  5. ios关于用xib创建的cell 自动返回cell的高度问题!
  6. Spring工厂常识
  7. BZOJ5324 洛谷4563 LOJ2545:[JXOI2018]守卫——题解
  8. infer构建项目失败
  9. 递归神经网络(Recurrent Neural Networks,RNN)
  10. Nginx PHP 使用 limit_req,limit_conn 限制并发,外加白名单