我遇到了一个问题。
我有一个类 DrawableTexturedPlane。此类指定一个带有字节数组的简单平面作为纹理。数组数据可以通过一个简单的函数调用来改变。好吧,该类将特定的着色器程序作为参数。在我的 OpenGL 视图中,我需要 2 个这样的平面。好吧,第一个渲染正确,但第二个根本没有出现。我很确定我错过了一些东西,但我不确定我的代码的哪一部分不正确。我将不胜感激任何帮助 ;)
TexturedPlaneClass(相关部分):
public DrawableTexturedPlane( float[] fVertices, int nProgrammHandle, int nGLTextureChannel, int nGLTextureID )
{
super( nProgrammHandle );
m_bHasBorder = bHasBorder;
m_nGLTextureChannel = nGLTextureChannel;
m_oVertexBuffer = _GetVertexBuffer( fVertices );
m_oTextureBuffer = _GetTextureCoordinates();
GLES20.glActiveTexture( m_nGLTextureChannel );
// Set filtering
GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST );
GLES20.glTexParameterf( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST );
GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
m_cTexture = new byte[ 640 * 480 ];
Log.i( "Java/DrawableTexturedPlane", "Drawable Textured Plane created!" );
}
绘制方法:
@Override
public void Draw( float[] mvpMatrix )
{
GLES20.glActiveTexture( m_nGLTextureChannel );
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glUseProgram( m_nProgramHandle );
m_HTextureUniform = GLES20.glGetUniformLocation( m_nProgramHandle, "uTexture" );
m_HTextureCoordinate = GLES20.glGetAttribLocation( m_nProgramHandle, "TexCoordinate" );
// get handle to the vertex shader's vPosition member
m_nPositionHandle = GLES20.glGetAttribLocation( m_nProgramHandle, "vPosition" );
ByteBuffer oDataBuf = ByteBuffer.wrap( m_cTexture );
// Prepare the triangle data
GLES20.glTexImage2D( GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, 640, 480,
0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, oDataBuf );
// Prepare the triangle data
GLES20.glVertexAttribPointer( m_nPositionHandle, 3, GLES20.GL_FLOAT, false, 12, m_oVertexBuffer );
GLES20.glEnableVertexAttribArray( m_nPositionHandle );
GLES20.glVertexAttribPointer( m_HTextureCoordinate, 2, GLES20.GL_FLOAT, false, 12, m_oTextureBuffer);
GLES20.glEnableVertexAttribArray( m_HTextureCoordinate );
m_nMVPMatrixHandle = GLES20.glGetUniformLocation( m_nProgramHandle, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv( m_nMVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}
以下是纹理和 2 个平面的创建方式:
GLES20.glGenTextures( m_nTextureStorage.length, m_nTextureStorage, 0);
for( int i : m_nTextureStorage )
{
GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, i );
}
m_oInfoPlane = new DrawableTexturedPlane( oInfoRect.GetAsFloatArray(), true, m_nShaderPrograms[0], GLES20.GL_TEXTURE0, m_nTextureStorage[0] );
m_oMainPlane = new DrawableTexturedPlane( oMainRect.GetAsFloatArray(), true, m_nShaderPrograms[0], GLES20.GL_TEXTURE1, m_nTextureStorage[1] );
关键是,如果我用 GL_TEXTURE0 初始化 BOTH,它工作正常。(虽然 10 - 20 秒后出现严重闪烁,但我不知道为什么)。
如果我按照上面的方式初始化它们,则只有带有 TEXTURE_0 的那个显示正确,另一个显示为黑色。
我知道我应该编写一个 TextureManager 类,但是对于 2 个纹理,这是一个简单的矫枉过正。
提前致谢