[英]working with multiple Textures in OpenGL ES 2.0
我遇到了麻煩的問題。
我有一個DrawableTexturedPlane類。 此類指定帶有字節數組的簡單Plane作為Texture。 可以通過簡單的函數調用來更改數組數據。 好了,該類將特定的着色器程序作為參數。 在我的OpenGL視圖中,我需要其中兩個平面。 好吧,第一個正確渲染,但是第二個根本不顯示。 我很確定我錯過了一些東西,但是我不確定代碼的哪一部分不正確。 我將不勝感激任何幫助 ;)
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,它就可以正常工作。 (盡管在10到20秒后會出現嚴重的閃爍,但我不知道為什么)。
如果我按照上面的方法進行初始化,則只能正確顯示具有TEXTURE_0的一個,而另一個則為黑色。
我知道我應該寫一個TextureManager類,但是對於2個Textures,這是一個簡單的矯kill過正。
提前致謝
您沒有在正確的時間綁定紋理。 另外,您不能將2種不同的紋理綁定到GL_TEXTURE_2D。
它看起來應該像這樣:
GLES20.glActiveTexture( m_nGLTextureChannel ); //Activate the texture channel
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, nGLTextureID); //Bind here!
//Bind before you draw
我知道我應該寫一個TextureManager類,但是對於2個Textures,這是一個簡單的矯kill過正。
至於這不是現在,而是整個項目的需求。 以后需要經理嗎? 前期設計會有所收獲,但不會太過分。 (金達自相矛盾!祝你好運!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.