32 pixel gap at top of opengl es texture
I am trying to render to a texture using openGL ES (for iPhone) and then display the texture on the screen. Everything works except that there is a 32 row gap at the top of the texture and the bottom 32 rows are cut off. It's like all of my drawing is being offset 32 pixels down, which results in the bottom 32 rows not being drawn as they are outside of the texture.
Here's a very simple example:
void RenderToTexture( int texture )
{
unsigned char buffer[4 * 320 * 480];
unsigned char colour[4];
colour[0] = 255;
colour[1] = 0;
colour[2] = 0;
colour[3] = 128;
for ( int i = 0; i < 4 * 320 * 480; i += 4 )
{
buffer[i] = colour[0];
buffer[i+1] = colour[1];
buffer[i+2] = colour[2];
buffer[i+3] = colour[3];
}
glBindTextu开发者_如何学编程re( GL_TEXTURE_2D, texture );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
}
And here's the result:
Just setting the colour using glColor4f() instead of calling RenderToTexture() results in a red screen as expected.
That 32 pixels are the missing ones to 512: 512 - 480 = 32. The reason is you can use only texture sizes that are powers of two with GL_TEXTURE_2D. So you have to round up your width and height to 512. You can still display only the part of the texture you want by using texture coordinates or by setting a texture matrix.
The iPhone 3GS supports non power-of-two textures under certain conditions. All the following must be true:
- GL_TEXTURE_WRAP_S should be set to GL_CLAMP_TO_EDGE
- GL_TEXTURE_WRAP_T should be set to GL_CLAMP_TO_EDGE
- Mipmapping must be turned off; minify with GL_LINEAR rather than GL_LINEAR_MIPMAP_LINEAR
精彩评论