Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I made a method called the
loadTexture()
in my cubeTexture class that has all the code to add texture to the cube. I put this method in the
onSurfaceCreated()
of the GLSurfaceView Renderer, like this:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0, 0, 0, 1f);
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    cubeTexture.loadTextures(gl);

}


Then I drew the shape using the onDraw() like this:

@Override
   public void onDrawFrame(GL10 gl) {
       gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
       gl.glDisable(GL10.GL_DITHER);
       gl.glMatrixMode(GL10.GL_MODELVIEW);
       gl.glLoadIdentity();
       GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 2, 0);

       square.draw(gl);
       triangle.draw(gl);


       cubeTexture.draw(gl);

   }


My problem is when I run my app the texture goes on all the shapes I drew instead of just going on the cubeTexture shape (which is the only shape I want it to go on). I was wondering if anyone had a fix for my problem, is there a certain method I should call? Or should I place the loadTexture() somewhere other then the onSurfaceCreated()? Any advice would be very helpful, thank you!
Posted
Comments
Fredrik Bornander 10-Jun-15 2:23am    
What does your draw method and loadTextures method look like?

Unless you specifically tell openGL to render the shapes using just color, it'll render them with what ever settings were applied and in your case it looks like you're binding the texture and then leaving it bound for all shapes.
Member 11492419 11-Jun-15 11:43am    
Is there a certain method I need to call to unbind it? When I call gl.glDisable() it removes the texture for all the shapes.
Member 11492419 11-Jun-15 11:46am    
Here is what they look like:
<pre> public void loadTextures(GL10 gl) {
//Generate Texture
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
int textureIds[] = new int[1];
gl.glGenTextures(1, textureIds, 0);
int textureId = textureIds[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDisable(GL10.GL_TEXTURE_2D);

bitmap.recycle();
} </pre>

<pre> public void draw(GL10 gl) {

//Set rotation speed/Rotate around the Y axis
/* long time = SystemClock.uptimeMillis() % 4000L;
float angle = .090f * ((int) time);
gl.glRotatef(angle, 0, 3, 0); */

//Draw Cube
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, floatBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, shortBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
} </pre>

1 solution

Textures and colors are set on the pipeline before draw calls, and are then used/re-used where applicable on subsequent draw calls.

That means if you want your Square to have a different texture that your Triangle, you'll need to bind the texture before drawing the Square and then disable it after having drawn the Square.

Try changing your loadTextures<code> method, so that it doesn't call <code>gl.glEnable(GL10.GL_TEXTURE_2D);, then make sure you can get to the texture id of the loaded texture.

Then you can pass the texture id into the constructor of your Square class and bind it before the draw, something like this;

Java
class Square {

  private int textureId;

  public Cube(int textureId) {
    this.textureId = textureId;
  }

  public void draw(GL10 gl) {
    //Set rotation speed/Rotate around the Y axis
    /* long time = SystemClock.uptimeMillis() % 4000L;
    float angle = .090f * ((int) time);
    gl.glRotatef(angle, 0, 3, 0); */

    //Draw Cube
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture, GL10.GL_TEXTURE_20, textureId);
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, floatBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, shortBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glDisable(GL10.GL_TEXTURE_2D);
  }
}


Where textureId is the id of the texture loaded.
Or you can pass the texture id to the draw method.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 11492419 12-Jun-15 14:40pm    
OH my goodness it worked, thank you so much, I'v been trying to solve this issue for 2 days now, thank you!
Fredrik Bornander 12-Jun-15 17:01pm    
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900