2012-07-02 110 views
1

這可能與我的轉換有關,但現在我無法弄清楚這一點,這讓我感到恍惚。我已經包裝了繪圖代碼,以便我可以輕鬆定義新的三角形。但是,當我把它放到一個函數中時,它只是顯示一個灰色屏幕。 TE功能代碼如下:OpenGL函數拒絕渲染三角形

public void Draw(float[] mViewMatrix, float[] mModelMatrix, float[] mProjectionMatrix, int mPositionHandle, int mColorHandle, int mMVPMatrixHandle) 
{ 

    long time = SystemClock.uptimeMillis() % 10000L; 
    float angleInDegrees = (360.0f/10000.0f) * ((int) time);  

    Matrix.setIdentityM(mModelMatrix, 0); 
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);  

    aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat) 
    .order(ByteOrder.nativeOrder()).asFloatBuffer(); 



    //aBuffer.position(mPositionOffset); 
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 
      mStrideBytes, aBuffer);   

    GLES20.glEnableVertexAttribArray(mPositionHandle);   

    // Pass in the color information 
    aBuffer.position(mColorOffset); 
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 
      mStrideBytes, aBuffer);   

    GLES20.glEnableVertexAttribArray(mColorHandle); 

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix 
    // (which currently contains model * view). 
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix 
    // (which now contains model * view * projection). 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3); 
} 

這是工作的代碼是:

 public void onDrawFrame(GL10 glUnused) 
{ 
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);      



    // Do a complete rotation every 10 seconds. 
    long time = SystemClock.uptimeMillis() % 10000L; 
    float angleInDegrees = (360.0f/10000.0f) * ((int) time); 

    // Draw the triangle facing straight on. 
    Matrix.setIdentityM(mModelMatrix, 0); 
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);   
    drawTriangle(mTriangle1Vertices); 

    // Draw one translated a bit down and rotated to be flat on the ground. 
    Matrix.setIdentityM(mModelMatrix, 0); 
    Matrix.translateM(mModelMatrix, 0, 0.0f, -1.0f, 0.0f); 
    Matrix.rotateM(mModelMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f); 
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f);   
    drawTriangle(mTriangle2Vertices); 

    // Draw one translated a bit to the right and rotated to be facing to the left. 
    Matrix.setIdentityM(mModelMatrix, 0); 
    Matrix.translateM(mModelMatrix, 0, 1.0f, 0.0f, 0.0f); 
    Matrix.rotateM(mModelMatrix, 0, 90.0f, 0.0f, 1.0f, 0.0f); 
    Matrix.rotateM(mModelMatrix, 0, angleInDegrees, 0.0f, 0.0f, 1.0f); 
    drawTriangle(mTriangle3Vertices); 
    */ 

    /* 
    for (int x = 0; x < staticHolder.objectList.size(); x++) 
    { 
     staticHolder.objectList.get(x).Draw(mViewMatrix, mModelMatrix, mProjectionMatrix, mPositionHandle, mColorHandle, mMVPMatrixHandle); 
    } 
    */ 
} 

/** 
* Draws a triangle from the given vertex data. 
* 
* @param aTriangleBuffer The buffer containing the vertex data. 
*/ 
private void drawTriangle(final FloatBuffer aTriangleBuffer) 
{  
    // Pass in the position information 
    aTriangleBuffer.position(mPositionOffset); 
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false, 
      mStrideBytes, aTriangleBuffer);   

    GLES20.glEnableVertexAttribArray(mPositionHandle);   

    // Pass in the color information 
    aTriangleBuffer.position(mColorOffset); 
    GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize, GLES20.GL_FLOAT, false, 
      mStrideBytes, aTriangleBuffer);   

    GLES20.glEnableVertexAttribArray(mColorHandle); 

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix 
    // (which currently contains model * view). 
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix 
    // (which now contains model * view * projection). 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);        
} 

我傳遞在同一個變量,這裏使用的最終變量初始化相同。封裝函數中還有其他一些工作。任何想法爲什麼它拒絕在函數中呈現?

下面的代碼加載在列表中的對象:

final float[] triangle1VerticesData = { 
      // X, Y, Z, 
      // R, G, B, A 
      -0.5f, -0.25f, 0.0f, 
      1.0f, 0.0f, 0.0f, 1.0f, 

      0.5f, -0.25f, 0.0f, 
      0.0f, 0.0f, 1.0f, 1.0f, 

      0.0f, 0.559016994f, 0.0f, 
      0.0f, 1.0f, 0.0f, 1.0f}; 

    final float[] triangle2VerticesData = { 
      // X, Y, Z, 
      // R, G, B, A 
      -0.5f, -0.25f, 0.0f, 
      1.0f, 1.0f, 0.0f, 1.0f, 

      0.5f, -0.25f, 0.0f, 
      0.0f, 1.0f, 1.0f, 1.0f, 

      0.0f, 0.559016994f, 0.0f, 
      1.0f, 0.0f, 1.0f, 1.0f}; 

    // This triangle is white, gray, and black. 
    final float[] triangle3VerticesData = { 
      // X, Y, Z, 
      // R, G, B, A 
      -0.5f, -0.25f, 0.0f, 
      1.0f, 1.0f, 1.0f, 1.0f, 

      0.5f, -0.25f, 0.0f, 
      0.5f, 0.5f, 0.5f, 1.0f, 

      0.0f, 0.559016994f, 0.0f, 
      0.0f, 0.0f, 0.0f, 1.0f}; 

    staticHolder.objectList.add(new Triangle(triangle1VerticesData)); 
    staticHolder.objectList.add(new Triangle(triangle2VerticesData)); 
    staticHolder.objectList.add(new Triangle(triangle3VerticesData)); 

接收類是:

public class Triangle extends shape 
    { 

    public Triangle(float[] data) 
    { 
      verts = data; 
    } 

    } 
+0

爲什麼你註釋掉了'aBuffer.position(mPositionOffset);'?這是一段必要的代碼。 – Eric

+0

我必須將它註釋掉,因爲我在試圖讓它工作 –

+0

如果它仍然不起作用,當你取消註釋時,那麼麻煩製造者的代碼位於別處(可能在表面初始化或GL對象)。 – Eric

回答

1

下面的代碼位後:

aBuffer = ByteBuffer.allocateDirect(verts.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer(); 

你必須把頂點進入緩衝區(否則,它是空白的!):

aBuffer.put(verts); 

這是因爲這三組頂點的緩衝區是預先分配的,並且頂點被放入它(初始化時)。他們每次只是傳遞給方法,所以他們不必再次成爲put()

在那個筆記上,你會想要避免在你的Draw方法中分配,因爲它被稱爲每幀多次,並可能導致渲染緩慢。一次分配aBuffer,並且每次都將新頂點放入其中。

+0

太棒了!非常感謝你!我對任何GL都很陌生(在C++中有一個在openGL上的課,絕對沒有任何可挽救的東西)。我一直在尋找一個解決方案几個小時!我欠你一個人情! –

+0

沒問題!我一直在學習GL ...... 2.0是如此的痛苦,不是嗎?祝你旅途愉快! :) – Eric

+0

呃,由於某種原因,大學/高中對教我什麼數學幾乎沒有興趣,所以幾何編程就像一片茂密的森林。你有什麼資源可以依靠快速學習嗎?我發現大多數的OpenGL教程都是相當寫的「哦,你應該已經知道這個,笨蛋!」善良的人,使他們的教程完全無用 –