失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android官方开发文档Training系列课程中文版:OpenGL绘图之添加动态效果

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

时间:2021-06-11 10:05:07

相关推荐

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

原文地址:/training/graphics/opengl/motion.html

在屏幕上绘制物体只是OpenGL基础的基础,除了OpenGL,你还可以使用Canvas及Drawable对象做到同样的功能。OpenGL还提供了额外的功能,我们可以使用这些功能在三维空间中移动或者旋转物体,或者以其独有的方式创造绚丽的用户效果。

这节课将会学习OpengGL ES使用的另一种方式:使图形旋转。

旋转图形

使用OpenGL使图形旋转起来还相对简单。在图形渲染器中,创建另一个转换矩阵(一个旋转矩阵),然后将其整合进原来创建的投影与相机视图转换矩阵:

private float[] mRotationMatrix = new float[16];public void onDrawFrame(GL10 gl) {float[] scratch = new float[16];...// Create a rotation transformation for the trianglelong time = SystemClock.uptimeMillis() % 4000L;float angle = 0.090f * ((int) time);Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);// Combine the rotation matrix with the projection and camera view// Note that the mMVPMatrix factor *must be first* in order// for the matrix multiplication product to be correct.Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);// Draw trianglemTriangle.draw(scratch);}

如果采用了上面的代码而三角形没有旋转,那么应该检查是否注释了这行代码:GLSurfaceView.RENDERMODE_WHEN_DIRTY,这部分将会在下一小节中讨论。

开启连续渲染

如果代码写到这里,需要确保你注释了一行代码:这行代码会设置渲染模式为只有在请求渲染的时候才会绘制的模式。另外OpenGL只会执行一次旋转,并会等待requestRender()方法调用:

public MyGLSurfaceView(Context context) {...// Render the view only when there is a change in the drawing data.// To allow the triangle to rotate automatically, this line is commented out://setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);}

如果不需要依靠用户的触发,那么这会是一个很好的解决办法。准备好取消这行代码,因为下节课会适时的再调用一次。

如果觉得《Android官方开发文档Training系列课程中文版:OpenGL绘图之添加动态效果》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。