开发者

OpenGL ES Textures do not display properly on HTC Magic (1.6) but display fine on Emulator and Mytouch 4g

开发者 https://www.devze.com 2023-03-01 10:37 出处:网络
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

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();
    mTextureBu开发者_开发问答ffer.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);
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消