Reputation: 167
I've been following the tutorials over at http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html and I've encountered a problem.
I've managed to get both of the following examples to work:
2.7 Example 5: 3D Shapes - Rotating Color Cube and Pyramid (Nehe Lesson 5: 3D Shapes) Example 2: Cube2.java
2.8 Example 6: Texture (Nehe Lesson 6: Texture)
But when I try to draw both a coloured cube and a textured cube I get the following:
https://i.sstatic.net/dhMZH.png (First part of the image)
The coloured cube is invisible but clips the texture cube and the texture cube's textures are coloured by the last colour (yellow) of the coloured cube.
I basically just draw both of the cubes with:
// ----- Render the Color Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(0.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
cube.draw(gl); // Draw the cube (NEW)
// Update the rotational angle after each refresh (NEW)
angleCube += speedCube; // (NEW)
// ----- Render the Texture Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(-1.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
texturecube.draw(gl); // Draw the cube (NEW)
I tried adding a third so called "Photo cube" from the part "2.9 Example 6a: Photo-Cube".
(Can only post 2 links check the imgur link above)
I added:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
to the texture cube and noticed that the blue colour "spills" over to the photo cube.
When I add:
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
the following textures are no longer coloured but this feels like a crude way to remove past colours..
When I remove:
texturecube.loadTexture(gl, context); // Load image into Texture (NEW)
photocube.loadTexture(gl); // Load image into Texture (NEW)
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable texture (NEW)
this happens:
(Can only post 2 links check the imgur link above)
The colour cube is back and all other surfaces are coloured.
My questions:
What am I doing wrong when the coloured cube disappears? (I guess that it should be possible to both use textures and colours at the same time)
Is there a better way to "clear" the colour from previous cubes/objects other than "gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);"?
Upvotes: 3
Views: 2350
Reputation: 35923
Your question is very detailed which is good, though it would help if you posted the full code sample of your drawing section. It's hard for me to guess what you might be doing that would cause the problem.
Anyway I'll try your two questions:
First: My guess is that you don't disable texturing before drawing your colored cube. But you don't have the full code so I can't say with certainty. Call glDisable(GL_TEXTURE_2D)
before rendering your second cube, and reenable it before drawing the next textured cube.
Second: No, that's the correct way to do it (Though you probably want 1,1,1,1
, not 1,1,1,0
in case you ever want to do anything with transparency). Alternatively there's a way to do it via pushing the color state onto an OpenGL stack and popping it later, though that's deprecated and shouldn't be used.
Upvotes: 3