Setup: OpenGL ES 1.0, Android
Term background image here means a full screen texture which is drawn over two triangles which make the full screen rectangle.
If I draw full scene, including background image, I get black background color instead of clear color background, and I do not see my texture for background displayed
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
drawParticles(scene.getNumDots());
Render image attached:
However, if I draw only the background image, I see it.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
// drawParticles(scene.getNumDots()); commented out
Render image attached:
Implementations:
// how it was setup
public void setupGl(#NonNull final GL10 gl) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
// This was called
public void setDimensions(#NonNull final GL10 gl, final int width, final int height) {
gl.glViewport(0, 0, width, height);
gl.glOrthof(0, width, 0, height, 1, -1);
}
// textures were loaded like this
private void loadTexture(
#NonNull final GL10 gl,
#NonNull final Bitmap texture,
final int handleOffset) {
gl.glGenTextures(1, textureHandle, handleOffset);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[handleOffset]);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
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_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
}
private void drawBackground() {
backgroundTextureCoordinates.position(0);
backgroundCoordinates.position(0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_BACKGROUND]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, backgroundTextureCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, backgroundCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
private void drawParticles(final int count) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
particlesTexturesCoordinates.position(0);
particlesTrianglesCoordinates.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_PARTICLE]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, particlesTexturesCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, particlesTrianglesCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, count * VERTICES_PER_PARTICLE);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
public void setClearColor(
#NonNull final GL10 gl,
#ColorInt int color) {
gl.glClearColor(
Color.red(color) / 255f,
Color.green(color) / 255f,
Color.blue(color) / 255f, 0f);
}
The problem was in the way I used to draw a background
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
There, I specified the number of triangles: 2.
Whereas I had to specify the number of vertices for the two triangles: 6.
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
Related
Following is my Sprite code. The image drawn using this code has a black background (which covers the transparent background) and has a color overlay making the image look distorted.
How can I improve transparent image drawing on open gl es.
class Ship {
public int life = 5;
public FloatBuffer ShipVertexBuffer;
public FloatBuffer ShipTextureBuffer;
public PieceCluster cluster;
//! TEXTURES
private int[] textures = new int[1];
//! TEXTURES
public float ShipVerticles[] = {
0, 0, // лево низ
0, 30, // лево вверх
30, 0, // право низ
30, 30 // право вверх
};
//! TEXTURES
public float ShipTextures[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
//! TEXTURES
public Ship(PieceCluster c) {
//! Буффер вертексов
cluster = c;
ByteBuffer bb = ByteBuffer.allocateDirect(36);
bb.order(ByteOrder.nativeOrder());
ShipVertexBuffer = bb.asFloatBuffer();
ShipVertexBuffer.put(ShipVerticles);
ShipVertexBuffer.position(0);
//! TEXTURES
bb = ByteBuffer.allocateDirect(ShipTextures.length * 4);
bb.order(ByteOrder.nativeOrder());
ShipTextureBuffer = bb.asFloatBuffer();
ShipTextureBuffer.put(ShipTextures);
ShipTextureBuffer.position(0);
//! TEXTURES
}
public void loadGLTexture(GL10 gl) {
// loading texture
// generate one texture pointer
Bitmap bitmap = cluster.Picture;
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
// 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_LINEAR);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
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.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_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
public void draw(GL10 gl, float x, float y) {
//! TEXTURE
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//! TEXTURE
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glTranslatef(x, y, 0.0f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, ShipVertexBuffer);
//! TEXTURE
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, ShipTextureBuffer);
//! TEXTURE
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
//! TEXTURE
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//! TEXTURE
}
It doesn't look like you've enabled blending.
Try adding glEnable(GL_BLEND) before your draw call. Use glBlendFunc() and glBlendEquation() to change the blending style to suit what effect you want.
i have a png image which draw it into my rectangular mesh in opengl, the problem is the image background is transparent but, when i bind it to my mesh, its background gets white; i want the image background to still be transparent.
i tried glEnableClientState(GLES10.GL_COLOR_ARRAY); and glColorPointer but didn't work as i want.
UPDATE:
the render openGL code:
void render(GL10 gl){
// First allocate texture if there is not one yet.
if (DRAW_TEXTURE && mTextureIds == null) {
// Generate texture.
mTextureIds = new int[2];
gl.glGenTextures(2, mTextureIds, 0);
for (int textureId : mTextureIds) {
// Set texture attributes.
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
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.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_CLAMP_TO_EDGE);
}
}
if (DRAW_TEXTURE && mPage.getTexturesChanged()) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
Bitmap texture = mPage.getTexture(mTextureRectFront,
CurlPage.SIDE_FRONT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
texture.recycle();
mTextureBack = mPage.hasBackTexture();
if (mTextureBack) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
texture = mPage.getTexture(mTextureRectBack,
CurlPage.SIDE_BACK);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
texture.recycle();
} else {
mTextureRectBack.set(mTextureRectFront);
}
mPage.recycle();
reset();
}
// Some 'global' settings.
gl.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
if (DRAW_TEXTURE) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mBufTexCoords);
}
gl.glVertexPointer(3, GLES10.GL_FLOAT, 0, mBufVertices);
//inja khasiate transparent ro faAl mikonim ke betoonim too CurlPage be kaaghaz alpha bedim
gl.glEnable(GLES10.GL_BLEND);
gl.glBlendFunc(GLES10.GL_SRC_ALPHA, GLES10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDisable(GLES10.GL_LIGHTING);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDrawArrays(GLES10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
// Draw front facing texture.
if (DRAW_TEXTURE) {
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_TEXTURE_2D);
if (!mFlipTexture || !mTextureBack) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
} else {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
}
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
gl.glDisable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
int backStartIdx = Math.max(0, mVerticesCountFront - 2);
//Log.d("mVerticesCountFront", ""+mVerticesCountFront+"");
int backCount = mVerticesCountFront + mVerticesCountBack - backStartIdx;
// Draw back facing blank vertices.
//gl.glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
gl.glDrawArrays(GLES10.GL_TRIANGLE_STRIP, backStartIdx, backCount);
// Disable textures and color array.
gl.glDisableClientState(GLES10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
}
OnSurfaceCreated Method:
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// mCurlMesh.setup(gl);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);
//gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_LINE_SMOOTH);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_DITHER);
}
I'm trying to understand OpenGL ES 2.0 in android. I got a code from a tutorial, but I do not understand how the texture works. I can't even understand where the rendering took place. Here is the code I got.
#Override
public void surfaceCreated(GL10 gl) {
// set up the surface
gl.glDisable(GL10.GL_DITHER);
gl.glHint(
GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(0.4f, 0.2f, 0.2f, 0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
// fetch the checker-board
initImage(gl);
}
#Override
public void drawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// apply the checker-board to the shape
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glTexEnvx(
GL10.GL_TEXTURE_ENV,
GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_S,
GL10.GL_REPEAT);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// animation
int t = (int) (SystemClock.uptimeMillis() % (10 * 1000L));
gl.glTranslatef(6.0f - (0.0013f * t), 0, 0);
// draw
gl.glFrontFace(GL10.GL_CCW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuf);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuf);
gl.glDrawElements(
GL10.GL_TRIANGLE_STRIP,
5,
GL10.GL_UNSIGNED_SHORT, indexBuf);
}
private void initImage(GL10 gl) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[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_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_CLAMP_TO_EDGE);
gl.glTexEnvf(
GL10.GL_TEXTURE_ENV,
GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_REPLACE);
InputStream in = context.getResources().openRawResource(R.drawable.cb);
Bitmap image;
try { image = BitmapFactory.decodeStream(in); }
finally {
try { in.close(); } catch(IOException e) { }
}
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
image.recycle();
}
Can anyone explain to me where the image is loaded from a drawable file, where the conversion to texture, and where the rendering took place? I just can't seem to understand this. I'm familiar with how to do it in OpenGL but it seems OpenGL ES 2.0 uses a different technique?
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0); is a utility function which converts an android Bitmap into an OpenGL texture.
When this is called, it stores the bitmap in image into the currently bound texture, which in your case is textures[0].
glDrawElements is the command that actually fires off the rendering call. If you want to know what any of the other functions do, I suggest taking a look at their online help pages.
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 and 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);
A texture used for a cube is being used for other objects in the view even though they aren't assigned it. This only happens on my HTC Magic running 1.6. I load the texture image from resources using the standard Bitmap libraries. This shouldn't be the problem however since it correctly applies texture to intended models.
I tried deleted the texture after using it however the texture is completely wiped and the cubes never seem to display it.
I recycled a lot of code taken from tutorials around the net.
Code:
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(sceneroty, 0, 1, 0);
gl.glTranslatef(xtrans, 0, ztrans);
root.draw(gl);
Root.Draw Function:
gl.glPushMatrix();
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);
// Smooth color
if (colorBuffer != null) {
// Enable the color array buffer to be used during rendering.
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
}
// New part...
if (mShouldLoadTexture) {
loadGLTexture(gl);mShouldLoadTexture = false;
}
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
}
// ... end new part.
gl.glTranslatef(x, y, z);
gl.glRotatef(rx, 1, 0, 0);
gl.glRotatef(ry, 0, 1, 0);
gl.glRotatef(rz, 0, 0, 1);
// Point out the where the color buffer is.
gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices,
GL10.GL_UNSIGNED_SHORT, indicesBuffer);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// New part...
if (mTextureId != -1 && mTextureBuffer != null) {
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
// ... end new part.
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);
gl.glPopMatrix();
Other used Functions:
protected void setTextureCoordinates(float[] textureCoords) {
// float is 4 bytes, therefore we multiply the number if
// vertices with 4.
ByteBuffer byteBuf = ByteBuffer.allocateDirect(
textureCoords.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mTextureBuffer = byteBuf.asFloatBuffer();
mTextureBuffer.put(textureCoords);
mTextureBuffer.position(0);
}
public void loadBitmap(Bitmap bitmap) {
this.mBitmap = bitmap;
mShouldLoadTexture = true;
}
private void loadGLTexture(GL10 gl) {
// Generate one texture pointer...
gl.glGenTextures(1, textures, 0);
mTextureId = textures[0];
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
// Create Nearest Filtered Texture
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);
// Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
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);
// Use the Android GLUtils to specify a two-dimensional texture image
// from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
}
It looks like you aren't disabling texturing nor unbinding the TEXTURE_2D target. If your other cubes are untextured they will then be drawn using the same texture left over from the previous draw operation. You should disable texturing if the object doesn't have a texture assigned to it:
if (mTextureId == -1) {
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
}