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

久未见 提交于 2020-01-24 19:35:09

问题


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:

How it looks with my code:


回答1:


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


来源:https://stackoverflow.com/questions/4837928/opengl-es-texture-problem-4-duplicate-columns-and-horizontal-lines-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!