我需要在OpenGL | ES 2.0中縮放對象。着色器:OpenGL ES 2.0縮放不起作用
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
//the matrix must be included as a modifier of gl_Position
" gl_Position = vPosition * uMVPMatrix;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
投影:
Matrix.orthoM(mProjMatrix,0,
-1.0f, // Left
1.0f, // Right
-1.0f/ratio, // Bottom
1.0f/ratio, // Top
0.01f, // Near
10000.0f);
繪圖設置:
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
實際渲染:
float[] scale = {5f,5f,1f};
Matrix.scaleM(scale_matrix, 0, scale[0], scale[1], scale[2]);
Matrix.multiplyMM(r_matrix, 0, scale_matrix, 0, mMVPMatrix, 0);
// Combine the rotation matrix with the projection and camera view
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, r_matrix, 0);
而且它不能擴展。我可以看到三角形,我可以旋轉它。但縮放不起作用。
乘法矩陣不可交換。解決這個問題的最簡單方法是反轉縮放和mvpMatrix乘法,但我認爲這不是標準方法。 'Matrix.multiplyMM(r_matrix,0,mMVPMatrix,0,scale_matrix,0);' – DanP 2013-03-27 03:00:28
謝謝你的信息...我試過換了他們,沒有運氣。 如何以正確的方式完成縮放?你能指點我一個資源嗎? – 2013-03-27 06:12:48
正確的順序是worldMatrix = scale * rotate * translate所以你的對象不會被毀容。然後計算wvpMatrix = worldMatrix * viewMatrix * projectionMatrix來說明相機和視角。那麼在頂點着色器上你必須做gl_Position = uMVPMatrix * vPosition; – DanP 2013-04-01 08:26:38