(五)移动图形(Adding Motion)

原文:https://developer.android.com/training/graphics/opengl/motion.html

Drawing objects on screen is a pretty basic feature of OpenGL, but you can do this with other Android graphics framwork classes, including Canvas and Drawable objects. OpenGL ES provides additional capabilities for moving and transforming drawn objects in three dimensions or in other unique ways to create compelling user experiences.
绘制图形只是OpenGL的基本功能,用其他的Android 绘图类(如Canvas和Drawable)也能实现同样的效果。OpenGL ES能提供更好的用户体验。OpenGL ES能够在三维空间或者其他独特的方式移动和变化图形。

In this lesson, you take another step forward into using OpenGL ES by learning how to add motion to a shape with rotation.
这一节,进一步学习如何用OpenGL ES移动图形。

Rotate a Shape

旋转图形

Rotating a drawing object with OpenGL ES 2.0 is relatively simple. In your renderer, create another transformation matrix (a rotation matrix) and then combine it with your projection and camera view transformation matrices:
用OpenGL ES旋转图形是小菜一碟。在render中,创建另一个转换矩阵用来旋转,然后和投影和相机矩阵进行结合。

private float[] mRotationMatrix = new float[16];
public void onDrawFrame(GL10 gl) {
    float[] scratch = new float[16];

    ...

    // Create a rotation transformation for the triangle
    long 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 triangle
    mTriangle.draw(scratch);
}

If your triangle does not rotate after making these changes, make sure you have commented out the GLSurfaceView.RENDERMODE_WHEN_DIRTY setting, as described in the next section.
如果做了以上的改变三角形还是没有旋转,检查一下是否注释了下一节会提到的GLSurfaceView.RENDERMODE_WHEN_DIRTY这一句。

Enable Continuous Rendering

开启连续绘制

If you have diligently followed along with the example code in this class to this point, make sure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL rotates the shape only one increment and then waits for a call to requestRender() from the GLSurfaceView container:
如果你按教程走到这里,确定你已经把绘制模式改为连续绘制模式,不然OpenGL只会把图形旋转一个角度之后就不再旋转了,只有等待调用GLSurfaceView的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);
}

Unless you have objects changing without any user interaction, it’s usually a good idea have this flag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable once again.
如果没有用户输入的情况下,图形仍会改变,那么就关闭这个模式,否则就开启。下一节中,我们会用到这行代码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容

  • 我发表了一篇新博文:CPU的七种寻址方式我的博客地址:不如怀念
    江流儿可爱哟阅读 681评论 0 0
  • 雨后新泥黏小径,缕缕暗香浓。 流连湖畔觅芳踪,袅娜藕花红。 粼粼水波托碧叶,翠色映芙蓉。 何事招惹无情风,怜花瓣,...
    西江月儿阅读 590评论 12 13
  • 固定资产的特殊问题总结 一、工程物资的细节区分 购进一批专用于安装设备的物资 计入“工程物资”; 进项税可以抵扣;...
    刘键阅读 847评论 0 0
  • 特大声明:本文复制于实验楼网站 Matplotlib 是一个优秀的 2D&3D 图形库, 主要功能是生成科学用图,...
    狼牙战士阅读 2,732评论 0 7
  • 做一个快乐的女子 珍惜拥有,懂得放弃 热爱生活也善待自己 用阳光的心态去面对世界 笑容会让你绽放最美丽的自己 《...
    墨默秋千阅读 595评论 3 0