W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
編寫:jdneo - 原文:http://developer.android.com/training/graphics/opengl/projection.html
在OpenGL ES環(huán)境中,利用投影和相機視角可以讓顯示的繪圖對象更加酷似于我們用肉眼看到的真實物體。該物理視角的仿真是對繪制對象坐標的進行數(shù)學變換實現(xiàn)的:
這節(jié)課將解釋如何創(chuàng)建一個投影和一個相機視角,并應用它們到GLSurfaceView中的繪制圖像上。
投影變換的數(shù)據(jù)會在GLSurfaceView.Renderer類的onSurfaceChanged()方法中被計算。下面的代碼首先接收GLSurfaceView的高和寬,然后利用它并使用Matrix.frustumM()方法來填充一個投影變換矩陣(Projection Transformation 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() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
該代碼填充了一個投影矩陣:mProjectionMatrix,在下一節(jié)中,我們可以在onDrawFrame()方法中將它和一個相機視角變換結合起來。
Note:在繪圖對象上只應用一個投影變換會導致顯示效果看上去很空曠。一般而言,我們還要實現(xiàn)一個相機視角,使得所有對象出現(xiàn)在屏幕上。
在渲染器中添加一個相機視角變換作為繪圖過程的一部分,以此完成我們的繪圖對象所需變換的所有步驟。在下面的代碼中,使用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);
}
為了使用在之前章節(jié)中結合了的相機視角變換和投影變換,我們首先為之前在Triangle類中定義的頂點著色器添加一個Matrix變量:
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;
...
}
之后,修改圖形對象的draw()
方法,使得它接收組合后的變換矩陣,并將它應用到圖形上:
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);
}
一旦我們正確地計算并應用了投影變換和相機視角變換,我們的圖形就會以正確的比例繪制出來,它看上去會像是這樣:
現(xiàn)在,應用已經(jīng)可以通過正確的比例顯示圖形了,下面就為圖形添加一些動畫效果吧!
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: