2014-03-12 94 views
1

首先我在創造什麼:基於2d瓦片的RPG遊戲。 我現在在做什麼,我會在這裏發佈,並評論一些我不確定我是否正確使用它們的地方。啓用和禁用GLES20屬性的正確方法是什麼?

在GlSurfaceViewRenderer:

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    //I enable some attributes 
    //Don't know if its needed in GLES20, there isnt GL20.GL_PERSPECTIVE_CORRECTION_HINT attribute at all 
    GLES20.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
    //next I don't really know if I need them all: 
    GLES20.glClearColor(0, 0, 0, 1); 
    GLES20.glClearDepthf(1.0f); 
    GLES20.glDisable(GLES20.GL_CULL_FACE);// No culling of back faces 
    GLES20.glDisable(GLES20.GL_DEPTH_TEST); 
    GLES20.glEnable(GLES20.GL_TEXTURE_2D); 
    GLES20.glDisable(GLES20.GL_DITHER); 
    GLES20.glDisable(GL10.GL_LIGHTING); 
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 
    // for transperent pixels 
    GLES20.glEnable(GLES20.GL_BLEND); 
    GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); 

    //Load shaders: (1 vertex and fragment shader I hope is enouph) 
    iProgId = Utils.LoadProgram(vertexShaderCode, fragmentShaderCode); 
    GLES20.glUseProgram(iProgId); 
    // get handle to vertex shader's vPosition member 
    mPositionHandle = GLES20.glGetAttribLocation(iProgId, "vPosition"); 
    // get handle to textures shader's a_TexCoordinate member 
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(iProgId, "a_TexCoordinate"); 
    // get handle to transformation matrix 
    mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix"); 

    //Now in here not sure: some people enable arrays in every draw frame, I do it once: 
    // Enable a handle to the triangle vertices 
    GLES20.glEnableVertexAttribArray(mPositionHandle); 
    // Enable a handle to the texture vertices 
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); 
} 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
    GLES20.glViewport(0, 0, width, height); 
    Matrix.frustumM(mProjMatrix, 0, ratio, -ratio, 1, -1, 1, 10000); 
    //.... and other 
} 

@Override 
public void onDrawFrame(GL10 gl) { 
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 
    //set camera position 
    Matrix.setLookAtM(...); 
    Matrix.multiplyMM(...); 

    //begin drawing: 
    for(Sprite spr : Sprite_list){ 

     spr.draw(){ 
     //whats happening in draw: 
     //First setting the location of the sprite: 
     Matrix.setIdentityM(...x & y...); 
     Matrix.translateM(...); 
     Matrix.setIdentityM(...); 
     Matrix.multiplyMM(...); 

     //Some developers enables vertex attrib arrays here and then disables at the          end of this drawing method. But I enable it in on surface created and don't disable it, maybe it's faster this way, not sure. 
     // Prepare the triangle coordinate data 
     GLES20.glVertexAttribPointer(GLRenderer.mPositionHandle, DIMENSION,   GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); 
     // Prepare the triangle coordinate data 
     GLES20.glVertexAttribPointer(GLRenderer.mTextureCoordinateHandle, DIMENSION, GLES20.GL_FLOAT, false, vertexStride, textureBuffer); 

     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textID); 
     GLES20.glUniformMatrix4fv(GLRenderer.mMVPMatrixHandle, 1, false, GLRenderer.mMVPMatrix, 0); 

     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 

     } 

    } 

} 

,一切工作,現在,但我真的不知道,如果我在這裏做任何的錯誤,有人能詳細點嗎?

回答

0

代碼本身並不一定會做任何不正確的事情,我可以看到。 除了GLES20.glDisable(GL10.GL_LIGHTING);因爲它不是ES2中可用的功能。 雖然代碼是非常有限的,它將能夠做什麼。原因是你在SurfaceCreated中上傳了大部分渲染狀態,而不是當你真的需要它們/改變渲染狀態時。

例如:

//現在在這裏不知道:有些人能在每一個畫幀陣列,我做一次: //啓用的句柄三角形頂點
GLES20.glEnableVertexAttribArray (mPositionHandle); //啓用紋理頂點的句柄 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

這是完全正常的,只是使屬性陣列在一個地方,但是如果你想利用不同的着色器程序在你的場景中的特定幾何體,這將變得相當困難。考慮到這一點,如果你想改變程序,你需要潛在地啓用/禁用該程序的特定屬性。您還需要綁定程序(glUseProgram)

因此,當您在代碼註釋中引用自己時,其他人通常在渲染期間啓用/禁用這些屬性是出於該特定原因。 但是,它不僅適用於屬性流,還適用於所有類型的渲染器,如更改程序,啓用Cullmode,混合等等。

現在人們不應該發瘋,只需在每次平局之前上傳並更改所有這些內容,因爲更改狀態非常昂貴。 一個人會嘗試批量使用相同類型的渲染器和資源(如紋理/程序等)的所有繪製調用,以最小化渲染器更改的數量。 因此,在renderloop中,您將渲染可渲染對象的排序場景,然後上傳可能需要更改或已失效的任何渲染器。

相關問題