problem with glTexImage2D ?

Sorry because i have create a same topic in the advance forum but i realize that it is just a simple question which i should put it in the beginner forum.

i need to save my client area to a texture, the problem is glTexImage2D need the width and height to be power of 2 size. But the client area will not possible to be power of 2 always. Somebody told me that glTexSubImage2D can help but how?

here is my code

int port[4];
glGetIntegerv(GL_VIEWPORT,port);

if(colorSave != NULL)
    free(colorSave);

colorSave = (unsigned char*)malloc(port[2] * port[3] * 4 * sizeof(unsigned char));

glReadPixels(port[0],port[1], port[2],port[3], GL_RGB , GL_UNSIGNED_BYTE,
    colorSave);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1,&texName);
glBindTexture(GL_TEXTURE_2D,texName);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,port[2],port[3],0,GL_RGB,GL_UNSIGNED_BYTE,colorSave);

glBindTexture(GL_TEXTURE_2D,texName);

glTexSubImage2D(GL_TEXTURE_2D,0,port[0],port[1],port[2],port[3],GL_RGB,GL_UNSIGNED_BYTE,colorSave);

can somebody please correct my code so that it can save a the client area that is not a power of 2 in to a texture. Thanks in Advance

you malloc an array width x height x 4 of unsigned char but when reading, you tell opengl to get only RGB values.

you have to create an empty texture larger than the screen but with dimensions==power of 2.
then you call glTexSubImage to copy the screen into the texture. when rendering you have to adjust your texture coordinates:
if the screen have width=510, the texture will have width=512 and the s coordinate must be between 0.0 and screen_width/texture_width

Originally posted by yoyo:
[b]you malloc an array width x height x 4 of unsigned char but when reading, you tell opengl to get only RGB values.

you have to create an empty texture larger than the screen but with dimensions==power of 2.
then you call glTexSubImage to copy the screen into the texture. when rendering you have to adjust your texture coordinates:
if the screen have width=510, the texture will have width=512 and the s coordinate must be between 0.0 and screen_width/texture_width[/b]

thanks a lot for you reply. what is s coordinate and screen_width/texture_width means screen_width divided by texture_width is it?

when you map texture on a face, you give to opengl texture coordinates. they are often named s, t, r and q.
s is for the first dimension, t for the second, r the third and q for the homogeneous coordinate.

the first dimension corresponds has the width of the texture.
if s==0.0 the pixel used is on the first column and if s==1.0 the pixel used is on the last column.

it is the same for t and q.

the screen is smaller than the texture, so you must not use texture coordinate greater than screen_width/texture_width.

it is the same for the height.

Originally posted by yoyo:
[b]when you map texture on a face, you give to opengl texture coordinates. they are often named s, t, r and q.
s is for the first dimension, t for the second, r the third and q for the homogeneous coordinate.

the first dimension corresponds has the width of the texture.
if s==0.0 the pixel used is on the first column and if s==1.0 the pixel used is on the last column.

it is the same for t and q.

the screen is smaller than the texture, so you must not use texture coordinate greater than screen_width/texture_width.

it is the same for the height.

[/b]

thanks for your reply, i will try it