PDA

View Full Version : "cropping" 2d texture



Jenk
05-12-2006, 11:59 AM
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:
glClear(GL_COLOR_BUFFER_BIT | L_DEPTH_BUFFER_BIT);
glLoadIdentity();

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
GL_UNSIGNED_BYTE, // type
data ); // *pixels

Overmind
05-12-2006, 01:52 PM
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...

Jenk
05-12-2006, 02:20 PM
thanks,
but I didn't understand where to put 384/512, on resizing (in glViewPort) or when viewing(glScalef?)?
thanks again

ZbuffeR
05-13-2006, 02:37 AM
While drawing. too bad it is the only chunk of code don't show ;-)

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 ;

glBegin(GL_QUADS);

glTexCoord2f(0,0);
glVertex2f(0,0);

glTexCoord2f(s,0);
glVertex2f(1,0);

glTexCoord2f(s,t);
glVertex2f(1,1);

glTexCoord2f(0,t);
glVertex2f(0,1);
glEnd();

Overmind
05-13-2006, 03:56 AM
Of course, you could also put it into a glScale on the texture matrix.

jide
05-13-2006, 04:27 AM
Why not use NPOT textures, or surely best here, texture rectangles (even if I don't know how widely texture rectangles are supported) ?

Overmind
05-13-2006, 05:46 AM
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.