Reputation: 2817
I ran into a problem latley.
I have a class DrawableTexturedPlane. This class specifies a simple Plane with a byte array as Texture. The array data can be changed by a simple function call. Well, the class takes a specific shader program as parameter. In my OpenGL View, i need 2 of those planes. Well, the first renders correctly, but the second does not show up at all. Im quite sure im missing something, but i am not sure which part of my code is incorrect. I would appreciate any help ;)
TexturedPlaneClass (relevant parts):
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!" );
}
the Draw Method:
@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);
}
Here is how the textures and the 2 planes are created:
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] );
Well the point is, if i initialize BOTH wirh GL_TEXTURE0, it works fine. (Although heavy flickering occurs after 10 - 20 seconds, i dont know why ).
If i initialize them the way i did it above, only the one with TEXTURE_0 is shown correctly, the other one apperas to be black.
I know i should write a TextureManager class, but for 2 Textures, this is a simple overkill.
Thanks in advance
Upvotes: 1
Views: 4753
Reputation: 5492
Your not binding the texture at the right time. Plus you can't bind 2 different textures to GL_TEXTURE_2D.
It should look like this:
GLES20.glActiveTexture( m_nGLTextureChannel ); //Activate the texture channel
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, nGLTextureID); //Bind here!
//Bind before you draw
I know i should write a TextureManager class, but for 2 Textures, this is a simple overkill.
As for this its not about right now, but about the needs of the overall project. Will you need a manager later on? Upfront design pays off but don't go overboard. (Kinda contradicting! Good luck!)
Upvotes: 1