TRIANGLE_FAN circle not drawn after draw squares with textures
UPDATE: Summary: I can draw a circle using TRIANGLE_FAN and, separately, I also can draw two squares with bitmaps as textures. But the problem is when I draw the textures a开发者_StackOverflownd then the circles. Circles aren't drawn.
I'm drawing two texturized squares (4 vertex each). Then I draw a circle using GL_TRIANGLE_FAN but it isn't being drawn correctly (see images).
When I draw the circles without the squares, it is drawn correctly.
Any ideas where could be the problem?
Please, ask for more information. Thanks
Adding some code that I think is important:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_BLEND);
gl.glCullFace(GL10.GL_BACK);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glOrthof(0, w, -0, h, -1, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
circle.draw(gl);
needle.draw(gl);
synchronized (tokens) {
for (Token d : tokens) {
d.draw(gl);
}
}
}
Update: Some screenshots. Without drawing circle and needle objects:
Drawing circle and needle:
(Look at those red lines where should be a circle)The only change in the code between those images is commenting the lines
circle.draw(gl);
needle.draw(gl);
Token:
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glScalef(radius, radius, 0f);
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, nVertices+2);
gl.glPopMatrix();
}
Circle and Needle:
public void draw(GL10 gl) {
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
if (shouldLoadTexture) {
loadGLTexture(gl);
shouldLoadTexture = false;
}
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glRotatef((float) angle, 0f, 0f, 1f);
angle += rotAngle;
if(angle+rotAngle > 360.0)
angle -= 360.0;
gl.glScalef(width, height, 0f);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glPopMatrix();
}
private void loadGLTexture(GL10 gl) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
textureId = textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
}
Update:
Following what Arne says in the first answer, I changed:
public void draw(GL10 gl) {
//New line
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
if (shouldLoadTexture) {
loadGLTexture(gl);
shouldLoadTexture = false;
}
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glPushMatrix();
gl.glTranslatef(x, y, 0f);
gl.glRotatef((float) angle, 0f, 0f, 1f);
angle += rotAngle;
if(angle+rotAngle > 360.0)
angle -= 360.0;
gl.glScalef(width, height, 0f);
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glPopMatrix();
//New line
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
Now I get no circle at all. Neither red line as in the second image.
Update:
When I don't load the texture of circle and needle (just commenting the call to loadTexture() ), I get this:
So the problem should be with the textures.
You enable texturing (by calling glEnable(GL_TEXTURE_2D)
) at the beginning, but you don't provide your circles (tokens) any texture coordinates. As I suppose these shouldn't be textured, you should only enable texturing for the objects that are really textured and disable texturing again after drawing them, the same way you enable and disable the texCoord array in your updated code.
Try to disable GL_TEXTURE_COORD_ARRAY before drawing the circles with
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
精彩评论