问题
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