I am developing an application that dynamically changes the texture of
an object. The textures I download from the internet and are of fairly
random aspect ratios. I apply these textures to a 2^nth texture using
GLUtils.texSubImage2D supplying the bitmap containing the texture, and
only use the part of the constructed texture that holds the newly
applied data.

This works fine when using textures with a greater width than height.
However, when using tall images, I sometimes get images that are
skewed. That is, it seems as if the texture is copied to the new
texture, but with the width being one off, so each line just one pixel
too short.

This happens consistently when using the same images, however it does
not happen for all tall images.

Code for applying the texture:

public void setTexture(GL10 gl, Bitmap texture) {
                int height = texture.getHeight();
                int width  = texture.getWidth();
                float heightRatio = ((float)height) / 128;
                float widthRatio = ((float)width) / 128;
                maspect = width / (float)height;
                int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);
                mTextureID = textures[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);

        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_BLEND);

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mImg, 0);
        GLUtils.texSubImage2D(GL10.GL_TEXTURE_2D, 0, 0, 0, texture);

        Buffer texBuf = ByteBuffer.allocateDirect(texture.getRowBytes
() * height);
        texture.copyPixelsToBuffer(texBuf);

        ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
        tbb.order(ByteOrder.nativeOrder());
        mTexBuffer = tbb.asFloatBuffer();

        float tex[] = {
                0.0f,  0.0f,
                0.0f,  heightRatio,
                widthRatio,  0.0f,
                widthRatio,  heightRatio,
        };
        mTexBuffer.put(tex);
        mTexBuffer.position(0);
}



Code for drawing it to screen:

public void draw(GL10 gl, long offset){
                if(maspect < 1.25f){
                        szX = maspect;
                        szY = 1;
                }else{
                        szX = 1.25f;
                        szY = 1 / maspect * 1.25f;
                }

                gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_REPLACE);
                gl.glActiveTexture(GL10.GL_TEXTURE0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
                gl.glFrontFace(GL10.GL_CCW);
                gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
                gl.glEnable(GL10.GL_TEXTURE_2D);
                gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);

                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.glPushMatrix();

                gl.glTranslatef(x, y, z);
                gl.glScalef(szX, szY, 1);
                gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, 
GL10.GL_UNSIGNED_BYTE,
mIndexBuffer);

        gl.glPopMatrix();
}

This is really bugging me, so any help would be great!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to