2015-05-04 44 views
0

我在Android上繪製,座標在-1.0和1.0之間,但渲染器將這些座標映射到錯誤的位置。在景觀中,寬度不會延伸到角落,而在縱向上,寬度略微超出屏幕邊框。我試圖讓這個填充屏幕分辨率。這裏是我的代碼:Android OpenGL2.0渲染器縮放到正確的分辨率

package com.mycompany.brickbreaker; 

import android.opengl.GLES20; 
import android.opengl.GLSurfaceView; 
import android.opengl.Matrix; 
import android.util.Log; 

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

public class MyGLRenderer implements GLSurfaceView.Renderer { 
    private BrickBreaker game; 

    public MyGLRenderer(BrickBreaker activity) { 
     super(); 
     game = activity; 
    } 

    //mMVPMatrix stands 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]; 

    public void onSurfaceCreated(GL10 unused, EGLConfig config) { 
     //Set the background frame color 
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    } 

    @Override 
    public void onDrawFrame(GL10 unused){ 
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 

     //Set the camera position (View Matrix) 
     Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0f); 

     //Calculate the projection and view transformation 
     Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); 

     //Draw the rectangle! 
     game.draw(mMVPMatrix); 
    } 

    @Override 
    public void onSurfaceChanged(GL10 unused, int width, int height){ 

     GLES20.glViewport(0, 0, width, height); 

     Log.d("Touch", "Surface changed at height/width " + height + "/" + width); 
     float ratio = (float) width/(float) height; 

     //This projection matrix is applied to object coordinates in 
     //the onDrawFrame() method 
     Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7); 
    } 

    public static int loadShader(int type, String shaderCode){ 
     //create the shader type 
     int shader = GLES20.glCreateShader(type); 

     //add the code to the shader, and compile it 
     GLES20.glShaderSource(shader, shaderCode); 
     GLES20.glCompileShader(shader); 

     return shader; 
    } 
} 
+0

如果要使用-1.0和1.0之間的座標進行繪製,爲什麼要使用視圖和投影矩陣?這就是你默認的範圍,沒有任何**視圖或投影矩陣。 –

回答

0

我不是openGL的專家,但我確實有一個工作的引擎。

在你的SurfaceChanged中,你傳遞了舊的投影矩陣。

您應該添加以下代碼:

//Clear the matricies 
    for(int i=0;i<16;i++) 
    { 
     mProjectionMatrix[i] = 0.0f; 
     mViewMatrix[i] = 0.0f; 
     mMVPMatrix[i] = 0.0f; 
    } 

    // Setup our screen width and height for normal sprite translation. 
    Matrix.orthoM(mProjectionMatrix, 0, 0f, width, 0.0f, height, 0, 50); 

    // Set the camera position (View matrix) 
    Matrix.setLookAtM(mViewMatrix, 0, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 

    // Calculate the projection and view transformation 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); 

希望能解決你的問題。

+0

雖然清除矩陣是一個好主意,但我沒有想到,這仍然不會改變任何東西:(。什麼是orthoM更改?我應該將其添加到除onSurfaceChanged以外的任何地方嗎? –

0

所以我解決了這個問題。發生的情況是當用Matrix.frustumM(0,0,-ratio,ratio ....)創建投影矩陣時,openGl網格實際上在X和Y兩者中都從-1變爲1,在y中將其轉換爲-1到1的網格,並在x座標中使用-ratio到ratio範圍。乘以傳遞給frustumM的比率所使用的任何x座標,使所有事情都再次開心。

+0

這真的更復雜比如我在上面的評論中所說的,如果你的座標已經在默認的OpenGL座標系中,那麼根本不應用一個轉換。 –

相關問題