0

I am loading a 512x512 texture (atlas: containing many images), (tried from both drawable and mipmap) using this function:

public static int loadTexture(final Context context, String textureName, final int resourceId, int textilesForWidth)
    {
        final int[] textureHandle = new int[1];

        glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0)
        {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;   // do not scale the image

            // Read in the resource
            final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

            // Bind to the texture in OpenGL
            glBindTexture(GL_TEXTURE_2D, textureHandle[0]);



            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

            // Set filtering
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

            // Load the bitmap into the bound texture.
            GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);

            // Recycle the bitmap, since its data has been loaded into OpenGL.
            bitmap.recycle();

        }

        if (textureHandle[0] == 0) {
            throw new RuntimeException("Error loading texture.");
        }

        textures.put(textureName, textureHandle[0]);
        texture_length.put(textureName, textilesForWidth);
        return textureHandle[0];
    }

However the textures stretch as shown: enter image description here

I have searched all over the place, but seems I am the only one getting this problem (The other answered questions had NPOT images or no GL_REPEAT).

How can I repeat one part of the atlas on a face again and again? is it possible from within the shaders?

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • The problem isn't likely to be with your shaders, it's mostly likely down to the texture coordinates you are passing in. Are your UV coordinates looking sensible? – solidpixel Aug 03 '16 at 23:29
  • The problem is definitely in the UV coordinates that you are passing to the shaders. I don't see any problem with you code or shaders. – codetiger Aug 04 '16 at 05:01
  • a single cube that it 1x1x1 looks perfect, however if i scale that cube to 2x2x2 or 2x1x2 or whatever, the texture scales with it. I am sure the UV mappings are correct – Rami Dabain Aug 04 '16 at 08:03
  • Yes - if you scale a triangle, the texture scales with it, that's how texturing works. Your UV coordinates are associated with the vertices, so if the vertices are further apart then the texture is simple scaled to stretch between them. If you want to avoid stretching (e.g. so a 2x1 cube gets two repeats of the texture), then you need to change your texture coordinates to encode the repeat, and set the texture wrap mode accordingly. E.g. set the max texture UV to 2.0 in that axis rather than 1.0, and set wrap mode to GL_REPEAT. – solidpixel Aug 04 '16 at 08:35
  • Sorry guys, I didn't mention it was a texture atlas (my bad), however I found that it is not possible to do it the normal way, is there any other way for that? – Rami Dabain Aug 04 '16 at 19:55

0 Answers0