2014-03-12 102 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中,您将渲染可渲染对象的排序场景,然后上传可能需要更改或已失效的任何渲染器。

相关问题