32 pixel gaps at the top of the opengl es texture
I am trying to render a texture using openGL ES (for iPhone) and then display the texture on the screen. Everything works, except there is a 32-line gap at the top of the texture, and the bottom 32 lines are cropped. It's like my whole drawing is offset 32 โโpixels down, which results in the bottom 32 lines not being drawn as they are outside 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];
}
glBindTexture( GL_TEXTURE_2D, texture );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer );
}
And here's the result:
By simply setting the color with glColor4f () instead of calling RenderToTexture (), you get a red screen as expected.
source to share
Those 32 pixels are missing in 512: 512 - 480 = 32. The reason is that you can only use texture sizes that have two strengths with GL_TEXTURE_2D. So you have to round off your width and height to 512. You can only display the part of the texture you want using texture coordinates or by setting the texture.
source to share
Under some conditions, iPhone 3GS supports textures without powering two textures. Everything must be true:
- GL_TEXTURE_WRAP_S must be set to GL_CLAMP_TO_EDGE
- GL_TEXTURE_WRAP_T must be set to GL_CLAMP_TO_EDGE
- Mipmapping must be disabled; minify with GL_LINEAR, not GL_LINEAR_MIPMAP_LINEAR
source to share