2013-05-13 94 views
2

我試過了以下關於繪製形狀和應用投影的教訓,發現here,但投影似乎不起作用。據我知道我拷貝粘貼所有的代碼片段在我的項目,這裏是重要的部分代碼:OpenGL-es投影似乎不起作用

在我Renderer類:

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

    // 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); 

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

@Override 
public void onSurfaceChanged(GL10 unused, int width, int height) { 
    GLES20.glViewport(0, 0, width, height); 

    float ratio = (float) width/height; 
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); 
} 

Triangle類的draw方法:

public void draw(float[] mvpMatrix) { 
    GLES20.glUseProgram(mProgram); 

    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); // get handle to vertex shader's vPosition member 
    GLES20.glEnableVertexAttribArray(mPositionHandle); // Enable a handle to the triangle vertices 

    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); // Prepare the triangle coordinate data 

    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); // get handle to fragment shader's vColor member 
    GLES20.glUniform4fv(mColorHandle, 1, color, 0); // Set color for drawing the triangle 


    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // get handle to shape's transformation matrix 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Apply the projection and view transformation 

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

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

這個三角形還在被拉伸。我究竟做錯了什麼?

回答

1

您是否嘗試在onSurfaceChanged中進行調試?這些線路會發生什麼?尤其是:

    • 什麼是傳遞的值widthheight
    • 哪個值是float ratio = (float) width/height;計算?
    • 您是否看到mProjMatrix的值有任何變化,請致電Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
  1. 您的private final String fragmentShaderCodeclass Triangle是怎麼樣的?由於行" gl_Position = uMVPMatrix * vPosition;" +中乘法的順序很重要。

您提供的代碼在這裏看起來很安靜,很困難。