Ertan D.
Ertan D.

Reputation: 832

OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)

I have a buffer of RGB (or RGBA) texture image, and I want to display it on my Android device with the following code. I use OpenGL from NDK.

glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             256,
             256,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             this->pBuffer);

I have also set the PixelFormat from the Java side with:

this.getHolder().setFormat(PixelFormat.RGBA_8888);
this.setEGLConfigChooser(8, 8, 8, 8, 0, 0);
setRenderer(new MyRenderer());

The image is displayed but there are four columns (identical and contains recognizable parts of the original image) and there are horizontal lines all over the image.

What can be the problem?

Original Image:

Original Image

How it looks with my code:

How it looks with my code

Upvotes: 2

Views: 1263

Answers (1)

richq
richq

Reputation: 56478

This looks like the image size is not 256 by 256, but maybe about 150 pixels wide. Your texture has to be a power of 2 big, but if you need to upload a smaller texture you can use glTexSubImage2D:

glTexSubImage2D(GL_TEXTURE_2D,  /* target */
            0,      /* level */
            0,      /* xoffset */
            0,      /* yoffset */
            150,        /* width */
            256,        /* height */
            GL_RGBA,    /* format */
            GL_UNSIGNED_BYTE,   /* type */
            this->pBuffer); /* data */

In the initial glTexImage2D call, just pass NULL instead of the pixel buffer. Try something like this, see if it makes a difference.

If you're using glDrawTexiOES to draw the texture, then to crop the smaller texture use GL_TEXTURE_CROP_RECT_OES:

int rect[4] = {0, imageHeight, imageWidth, -imageHeight};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, rect);
glDrawTexiOES(0, 0, 0, windowWidth, windowHeight);

Upvotes: 2

Related Questions