2011-03-22 86 views
0

編輯:解決它!我犯了一個愚蠢的錯誤,我有一個紋理,我忘記了它應該使用的textureID。在OpenGL ES中爲2D遊戲渲染圖像android

好吧,我充分意識到,這是一個反覆出現的問題,並且有大量的教程和開放源代碼。但是我一直在儘可能好地嘗試一段時間,而我的屏幕仍然是空白的(使用glClearColor()設置的任何顏色)。

所以,我會爲一些指針,我做錯了什麼,甚至更好,一些工作的代碼,將呈現一個資源圖片感激。

我會告訴我已經這麼遠(通過做一些狡猾的複製粘貼)在我實現了渲染器的類的onDrawFrame。我已經刪除了一些方法之間的跳轉,並將它們按照執行順序粘貼。

隨意無視我當前的代碼,我更樂意重新開始,如果有人可以給我的代碼工作件。

設置:

bitmap = BitmapFactory.decodeResource(panel.getResources(), 
      R.drawable.test); 
    addGameComponent(new MeleeAttackComponent()); 

    // Mapping coordinates for the vertices 
    float textureCoordinates[] = { 0.0f, 2.0f, // 
      2.0f, 2.0f, // 
      0.0f, 0.0f, // 
      2.0f, 0.0f, // 
    }; 

    short[] indices = new short[] { 0, 1, 2, 1, 3, 2 }; 

    float[] vertices = new float[] { -0.5f, -0.5f, 0.0f, 
             0.5f, -0.5f, 0.0f, 
            -0.5f, 0.5f, 0.0f, 
             0.5f, 0.5f, 0.0f }; 

    setIndices(indices); 
    setVertices(vertices); 
    setTextureCoordinates(textureCoordinates); 

protected void setVertices(float[] vertices) { 
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4. 
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); 
    vbb.order(ByteOrder.nativeOrder()); 
    mVerticesBuffer = vbb.asFloatBuffer(); 
    mVerticesBuffer.put(vertices); 
    mVerticesBuffer.position(0); 
} 


protected void setIndices(short[] indices) { 
    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2. 
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); 
    ibb.order(ByteOrder.nativeOrder()); 
    mIndicesBuffer = ibb.asShortBuffer(); 
    mIndicesBuffer.put(indices); 
    mIndicesBuffer.position(0); 
    mNumOfIndices = indices.length; 
} 


protected void setTextureCoordinates(float[] textureCoords) { 

    // float is 4 bytes, therefore we multiply the number of 
    // vertices with 4. 
    ByteBuffer byteBuf = ByteBuffer 
      .allocateDirect(textureCoords.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    mTextureBuffer = byteBuf.asFloatBuffer(); 
    mTextureBuffer.put(textureCoords); 
    mTextureBuffer.position(0); 
} 

//該onDrawFrame(GL10 GL)

gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    gl.glLoadIdentity(); 
    gl.glTranslatef(0, 0, -4); 

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    // Specifies the location and data format of an array of vertex 
    // coordinates to use when rendering. 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer); 

    if(shoudlLoadTexture){ 
     loadGLTextures(gl); 
     shoudlLoadTexture = false; 
    } 

    if (mTextureId != -1 && mTextureBuffer != null) { 
     gl.glEnable(GL10.GL_TEXTURE_2D); 
     // Enable the texture state 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

     // Point to our buffers 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer); 
     gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId); 
    } 

    gl.glTranslatef(posX, posY, 0); 
    // Point out the where the color buffer is. 
    gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices, 
      GL10.GL_UNSIGNED_SHORT, mIndicesBuffer); 
    // Disable the vertices buffer. 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 

    if (mTextureId != -1 && mTextureBuffer != null) { 
     gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
    } 


private void loadGLTextures(GL10 gl) { 

    int[] textures = new int[1]; 
    gl.glGenTextures(1, textures, 0); 
    mTextureID = textures[0]; 

    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID); 

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); 

    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); 
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); 
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE); 

    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 

} 

它不會崩潰,沒有例外,只是一個空白屏幕顏色。我在那裏印了東西,所以我很確定它全部被執行。

我知道這是不是最佳的,只是貼上代碼,但在那一刻,我只是想能夠做什麼,我能夠用帆布:)做

非常感謝

+0

即時問題:你是否將模型視圖和項目堆棧初始化到某處不顯示? – Tommy 2011-03-22 18:00:49

+0

可能是這樣,因爲我不確定你會怎麼做,我想我會展示大部分代碼。它確實運行並給了我背景顏色,所以我正在做正確的事情。你能詳細說明你的意思嗎? (對不起,我缺乏理解,這是一個可怕的未知領域:S) – 2011-03-22 18:17:52

+0

我需要發佈一個答案才能獲得足夠的空間,即使它不一定是答案。 – Tommy 2011-03-23 00:29:29

回答

1

如果您'獲取背景顏色,這意味着您的窗口已正確設置。 OpenGL連接到屏幕的那個區域。但是,OpenGL會剪切到近端和遠端剪輯平面,以確保對象不會與攝像機交叉或相交(在數學和邏輯上都不合理),而且距離太遠的對象不會出現。因此,如果您沒有正確設置模型視圖和投影,很可能所有的幾何圖形都將被剪裁。

模型變換用於映射從世界到眼睛空間。投影從眼睛空間映射到屏幕空間。因此,典型的應用程序使用前者在場景內定位對象,並將場景相對於相機進行定位,然後後者處理相機是否有視角看,有多少個世界單元製作多少個屏幕單元等。

如果你看一下例子like this one,特別onSurfaceChanged,你會看到固定在原點的相機的透視投影的一個例子。由於攝像機位於(0,0,0)處,因此在z = 0時將幾何圖形留在z = 0上會使其被剪裁。在該示例代碼,他們已經成立近剪裁平面是在z = 0.1,所以在現有的代碼,你可以改變:

gl.glTranslatef(posX, posY, 0); 

要:

gl.glTranslatef(posX, posY, -1.0); 

把你的幾何圖形返回足夠遠遠地出現在屏幕上。

+0

謝謝,但它沒有改變任何東西:S你可以看到,在頂部那裏,我設置glTranslatef(0,0,-4),並據我瞭解的教程,應該已經做到了這一招 – 2011-03-23 10:35:20

+0

您的代碼仍然不會顯示您設置模型視圖和投影的位置。我真的認爲這就是你絆倒的地方,如果沒有看到那些東西,就很難提供更具體的建議。 – Tommy 2011-03-23 12:09:11