Android 启用和禁用GLES20心房肌的正确方法是什么?
首先,我在创建什么:基于2d瓷砖的rpg游戏。 我现在所做的我将在这里发布,并评论一些我不确定是否正确使用它们的地方 在glsurfaceviewrender中:Android 启用和禁用GLES20心房肌的正确方法是什么?,android,attributes,drawing,opengl-es-2.0,texture2d,Android,Attributes,Drawing,Opengl Es 2.0,Texture2d,首先,我在创建什么:基于2d瓷砖的rpg游戏。 我现在所做的我将在这里发布,并评论一些我不确定是否正确使用它们的地方 在glsurfaceviewrender中: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { //I enable some attributes //Don't know if its needed in GLES20, there isnt GL20.GL_PERSPECTIV
@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);
}
}
}
现在一切正常,但我真的不确定我是否犯了什么错误,有人能详细说明一下吗 从我所看到的代码本身不一定做了任何不正确的事情。 除了GLES20.glDisable(GL10.GLU照明);因为它不是ES2中可用的功能。 虽然代码非常有限,它将能够做什么。原因是您正在上载surfaceCreated中的大多数渲染状态,而不是在实际需要它们/更改渲染状态时 例如: //现在在这里不确定:有些人在每个画框中启用数组,我只启用一次: //启用三角形顶点的控制柄
GLES20.GlenableVertexAttributeArray(mPositionHandle); //启用纹理顶点的控制柄 GLES20.GlenableVertexAttributeArray(mTextureCoordinateHandle) 只在一个位置启用属性数组是完全可以的,但是,如果您想为场景中的特定几何体使用不同的着色器程序,这将成为一个很大的问题 考虑到这一点,如果您想更改程序,您可能需要启用/禁用该程序的特定属性。您还需要绑定程序(glUseProgram) 因此,当您在代码注释中引用自己时,其他人通常在呈现期间启用/禁用这些是出于特定的原因。 不过,它不仅适用于属性流,还适用于所有类型的渲染状态,如更改程序、启用Cullmode、混合等 现在,人们不应该发疯,在每次抽签之前上传并更改所有这些,因为更改州是昂贵的。 您将尝试批处理所有将同时使用相同类型的渲染状态和资源(如纹理/程序等)的绘制调用,以最小化渲染状态更改的数量。 因此,在renderloop中,您将渲染可渲染对象的排序场景,然后上载可能需要更改或无效的任何renderstate