Question

I have been searching for this for the pass two days .I am developing an android game using Open GL in which there is a situation where i have to display two images at the same time, one should be the background image and second one is displayed depending on a condition. I have displayed the background imgae using Open GL Texture property.But when i tried to display the second image by using texture first one is not displaying.Since i am dealing with multiple texture i have used a GLTexture class to manipulate the texture (http://tkcodesharing.blogspot.in/2008/05/working-with-textures-in-androids.html).Even then i am not able to display two images at the same time.

Here is some code :

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    glTextures= new GLTextures(gl,this.context);//class to manipulate textures

   //adding image resources

    this.glTextures.add(R.drawable.back);

    this.glTextures.add(R.drawable.hand);

   //loading texture

   this.glTextures.loadTextures();

    gl.glEnable(GL10.GL_TEXTURE_2D);
   ...
}

In onDrawFrame

   gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);

   gl.glLoadIdentity();

   gl.glTranslatef(0.0f, 0.0f, -3.5f);

   glTextures.setTexture(R.drawable.back);//it will bind this particular texture for drawing

  draw(gl);

  if(condition == 1){

            gl.glLoadIdentity();
            gl.glTranslatef(0.0f, 1.0f, -7.0f);
            glTextures.setTexture(R.drawable.hand);

            drawSecond(gl);
        }
////////////////////////////////////////////////////////

In draw code

   public void draw(GL10 gl){

              gl.glActiveTexture(GL10.GL_TEXTURE0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glFrontFace(GL10.GL_CW);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3);


        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

  }


  public void drawSecond(GL10 gl){

        gl.glActiveTexture(GL10.GL_TEXTURE1);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        //GLES20.glUniform1i(isShaked, 1);
        gl.glFrontFace(GL10.GL_CW);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3);


        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

   }
Was it helpful?

Solution

remove "gl.glActiveTexture(GL10.GL_TEXTURE0);" and gl.glActiveTexture(GL10.GL_TEXTURE1);
lines from the rendering code.

you use multiple texture units only if you need multi-texturing,
which is using multiple textures in one draw call.

just bind the first texture to the active texture unit (default to GL_TEXTURE0) and draw the first triangle strip, than bind the second texture (without changing the active texture unit GL_TEXTURE0) and draw the second triangle strip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top