"cropping" 2d texture

Hi all,
I’m a beginner in OpenGL and I’m using it to display 2d image from a camera.
I created a tecture 2d with the size power of 2 so it’s always bigger than my buffer that is 384x288.
When drawing I’d like to see only the image from the camera but I get all the texture.
How can I “crop” it to see only the portion of interest?

Thanks in advance

On creation:

glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_width, texture_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

On Resize event:

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();

On Paint event:

glTranslatef(0, 0,-1);

// flip
glScalef (1.0, -1.0, 1.0);

glTexSubImage2D( GL_TEXTURE_2D, // target
0, // level
0, // x offset
0, // y offset
width, // width
height, // height
GL_RGB, // format
data ); // *pixels

You have to reduce the texture coordinates. The range [0.0, 1.0] always gives the whole texture.

I assume the texture size is 512, the image you want to display is 384 pixels wide and it starts at pixel 0. Then you have to use texture coordinates from 0 to 384/512.

The same goes for the y coordinate…

but I didn’t understand where to put 384/512, on resizing (in glViewPort) or when viewing(glScalef?)?
thanks again

While drawing. too bad it is the only chunk of code don’t show :wink:

In fact you can do it everywher you want, just I think while drawing is the best design.

384.0/512 should be put in the glTexCoord call, for each vertice forming the quad, like (untested) :

float s = 384.0f / 512.0f ;
float t = 288.0f / 512.0f ;






Of course, you could also put it into a glScale on the texture matrix.

Why not use NPOT textures, or surely best here, texture rectangles (even if I don’t know how widely texture rectangles are supported) ?

NPOT textures are not widely supported yet.

Texture rectangles don’t work with mipmapping, but if you don’t need it, it’s propably the best method. They should be supported on nearly every card out there. I think they are supported on ATI since 7000 and on nVidia on every GeForce card.