(五)移動(dòng)圖形(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)也能實(shí)現(xiàn)同樣的效果。OpenGL ES能提供更好的用戶體驗(yàn)。OpenGL ES能夠在三維空間或者其他獨(dú)特的方式移動(dòng)和變化圖形。

In this lesson, you take another step forward into using OpenGL ES by learning how to add motion to a shape with rotation.
這一節(jié),進(jìn)一步學(xué)習(xí)如何用OpenGL ES移動(dòng)圖形。

Rotate a Shape

旋轉(zhuǎn)圖形

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旋轉(zhuǎn)圖形是小菜一碟。在render中,創(chuàng)建另一個(gè)轉(zhuǎn)換矩陣用來旋轉(zhuǎn),然后和投影和相機(jī)矩陣進(jìn)行結(jié)合。

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.
如果做了以上的改變?nèi)切芜€是沒有旋轉(zhuǎn),檢查一下是否注釋了下一節(jié)會(huì)提到的GLSurfaceView.RENDERMODE_WHEN_DIRTY這一句。

Enable Continuous Rendering

開啟連續(xù)繪制

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:
如果你按教程走到這里,確定你已經(jīng)把繪制模式改為連續(xù)繪制模式,不然OpenGL只會(huì)把圖形旋轉(zhuǎn)一個(gè)角度之后就不再旋轉(zhuǎn)了,只有等待調(diào)用GLSurfaceView的requestRender() 方法時(shí)才會(huì)繼續(xù)旋轉(zhuǎn)。

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.
如果沒有用戶輸入的情況下,圖形仍會(huì)改變,那么就關(guān)閉這個(gè)模式,否則就開啟。下一節(jié)中,我們會(huì)用到這行代碼。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 我發(fā)表了一篇新博文:CPU的七種尋址方式我的博客地址:不如懷念
    江流兒可愛喲閱讀 815評(píng)論 0 0
  • 雨后新泥黏小徑,縷縷暗香濃。 流連湖畔覓芳蹤,裊娜藕花紅。 粼粼水波托碧葉,翠色映芙蓉。 何事招惹無情風(fēng),憐花瓣,...
    西江月兒閱讀 729評(píng)論 12 13
  • 固定資產(chǎn)的特殊問題總結(jié) 一、工程物資的細(xì)節(jié)區(qū)分 購(gòu)進(jìn)一批專用于安裝設(shè)備的物資 計(jì)入“工程物資”; 進(jìn)項(xiàng)稅可以抵扣;...
    劉鍵閱讀 1,074評(píng)論 0 0
  • 特大聲明:本文復(fù)制于實(shí)驗(yàn)樓網(wǎng)站 Matplotlib 是一個(gè)優(yōu)秀的 2D&3D 圖形庫(kù), 主要功能是生成科學(xué)用圖,...
    狼牙戰(zhàn)士閱讀 2,947評(píng)論 0 7
  • 做一個(gè)快樂的女子 珍惜擁有,懂得放棄 熱愛生活也善待自己 用陽(yáng)光的心態(tài)去面對(duì)世界 笑容會(huì)讓你綻放最美麗的自己 《...
    墨默秋千閱讀 683評(píng)論 3 0

友情鏈接更多精彩內(nèi)容