(四)使用投影和相機視角(Applying Projection and Camera Views)

原文:https://developer.android.com/training/graphics/opengl/projection.html##transform

In the OpenGL ES environment, projection and camera views allow you to display drawn objects in a way that more closely resembles how you see physical objects with your eyes. This simulation of physical viewing is done with mathematical transformations of drawn object coordinates:
在 OpenGL ES環(huán)境中,投影和相機視角可以讓畫出來的圖形模擬一種人眼看真實物體的方式顯示。這種視角是通過將繪制圖形的坐標進行數(shù)學變換實現(xiàn)的。

  • Projection - This transformation adjusts the coordinates of drawn objects based on the width and height of the GLSurfaceView where they are displayed. Without this calculation, objects drawn by OpenGL ES are skewed by the unequal proportions of the view window. A projection transformation typically only has to be calculated when the proportions of the OpenGL view are established or changed in the onSurfaceChanged() method of your renderer. For more information about OpenGL ES projections and coordinate mapping, see Mapping Coordinates for Drawn Objects.
    投影- 這個變換根據(jù)GLSurfaceView的寬高來調節(jié)繪制物體的坐標氮兵。沒有這些轉換計算箱季,OpenGL ES 繪制出來的形狀會因為GLSurfaceView所在的window比例不同而變形贯莺。只需要在OpenGL view建立時乎婿,或者在render的onSurfaceChanged()方法中進行投影變換饥臂。關于更多的OpenGL ES 投影和坐標映射师溅,可以看映射繪制物體的坐標茅信。

  • Camera View - This transformation adjusts the coordinates of drawn objects based on a virtual camera position. It’s important to note that OpenGL ES does not define an actual camera object, but instead provides utility methods that simulate a camera by transforming the display of drawn objects. A camera view transformation might be calculated only once when you establish your GLSurfaceView, or might change dynamically based on user actions or your application’s function.
    This lesson describes how to create a projection and camera view and apply it to shapes drawn in your GLSurfaceView.
    相機視角- 這個轉換是根據(jù)一個虛擬的相機位置來調節(jié)繪制物體的坐標。需要注意的是墓臭,OpenGL ES并沒有定義一個真實的相機對象蘸鲸,而且提供一個轉變繪制形狀顯示的方法來模擬一個相機。相機視角轉換可能只計算一次窿锉,也可能因為應用功能的不同而動態(tài)變換氓扛。
    這里主要講解如何建立投影和相機視角棕硫,并把它應用到GLSurfaceView繪制的圖形上。

Define a Projection

定義投射

The data for a projection transformation is calculated in the onSurfaceChanged() method of your GLSurfaceView.Renderer class. The following example code takes the height and width of the GLSurfaceView and uses it to populate a projection transformation Matrix using the Matrix.frustumM() method:
投影變換的數(shù)據(jù)是在GLSurfaceView.Renderer的onSurfaceChanged() 方法中進行計算的。接下來的示例代碼中待错,利用GLSurfaceView的寬高和Matrix.frustumM() 方法來生成一個投影變換矩陣(Maxtrix)。

// 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() method
    Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);//wtf is -1,1,3,7?
}

This code populates a projection matrix, mProjectionMatrix which you can then combine with a camera view transformation in the onDrawFrame() method, which is shown in the next section.
這段代碼生成了一個投影矩陣(mProjectionMatrix)琳水,在下一節(jié)中的onDrawFrame() 方法中借嗽,你可以把它和相機視角矩陣結合起來

  • Note: Just applying a projection transformation to your drawing objects typically results in a very empty display. In general, you must also apply a camera view transformation in order for anything to show up on screen.
    注意:對繪制圖形只使用投影變換會導致沒有任何內容顯示。一般來說判沟,為了讓屏幕上顯示正確的內容耿芹,你必須應用相機視角變換。

Define a Camera View

定義相機視角

Complete the process of transforming your drawn objects by adding a camera view transformation as part of the drawing process in your renderer. In the following example code, the camera view transformation is calculated using the Matrix.setLookAtM() method and then combined with the previously calculated projection matrix. The combined transformation matrices are then passed to the drawn shape.
做為renderer繪制的一部分挪哄,需要把相機視角變換加進來完善轉換過程吧秕。在下面的代碼中,用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 transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    // Draw shape
    mTriangle.draw(mMVPMatrix);
}

Apply Projection and Camera Transformations

應用投影和相機視頻變換

In order to use the combined projection and camera view transformation matrix shown in the previews sections, first add a matrix variable to the vertex shader previously defined in the Triangle class:
為了使用前的小節(jié)中投影和相機視角變換結合后的矩陣斯入,首先添加一個矩陣變量到之前定義的Triangle類的頂點shader中砂碉。

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;

    ...
}

Next, modify the draw() method of your graphic objects to accept the combined transformation matrix and apply it to the shape:
接下來,修改形狀的draw()方法刻两,增加一個結合的變換矩陣做為參數(shù)增蹭,把這個參數(shù)應用到形狀上來。

public void draw(float[] mvpMatrix) { // pass in the calculated transformation matrix
    ...

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

    // Pass the projection and view transformation to the shader
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

Once you have correctly calculated and applied the projection and camera view transformations, your graphic objects are drawn in correct proportions and should look like this:
如果你的正確地計算和應用了投影和相機視角變換磅摹,你的圖形正確地繪制如下圖所示:

Figure 1. Triangle drawn with a projection and camera view applied.

Now that you have an application that displays your shapes in correct proportions, it's time to add motion to your shapes.
現(xiàn)在你的應用能正確地顯示形狀了滋迈,該讓形狀動起來了。下一課

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末户誓,一起剝皮案震驚了整個濱河市饼灿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帝美,老刑警劉巖碍彭,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡硕旗,警方通過查閱死者的電腦和手機窗骑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漆枚,“玉大人创译,你說我怎么就攤上這事∏交” “怎么了软族?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長残制。 經常有香客問我立砸,道長,這世上最難降的妖魔是什么初茶? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任颗祝,我火速辦了婚禮,結果婚禮上恼布,老公的妹妹穿的比我還像新娘螺戳。我一直安慰自己,他們只是感情好折汞,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布倔幼。 她就那樣靜靜地躺著,像睡著了一般爽待。 火紅的嫁衣襯著肌膚如雪损同。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天鸟款,我揣著相機與錄音膏燃,去河邊找鬼。 笑死何什,一個胖子當著我的面吹牛蹄梢,可吹牛的內容都是我干的。 我是一名探鬼主播富俄,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼而咆!你這毒婦竟也來了霍比?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤暴备,失蹤者是張志新(化名)和其女友劉穎悠瞬,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡浅妆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年望迎,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凌外。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡辩尊,死狀恐怖,靈堂內的尸體忽然破棺而出康辑,到底是詐尸還是另有隱情摄欲,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布疮薇,位于F島的核電站胸墙,受9級特大地震影響,放射性物質發(fā)生泄漏按咒。R本人自食惡果不足惜迟隅,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望励七。 院中可真熱鬧智袭,春花似錦、人聲如沸呀伙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剿另。三九已至箫锤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間雨女,已是汗流浹背谚攒。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留氛堕,地道東北人馏臭。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像讼稚,于是被迫代替她去往敵國和親括儒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容