PDA

View Full Version : How to "not streching a texture"?



TurboMan
01-11-2011, 05:51 AM
Hi,

I am experiencing OpenGL.
But I am stuck, I wonder if you guys could give me a hand.
I have an image of 700x512 size and I scaled it down to 512x512 using gluScaleImage.
And would like to map to a rectangle(square) with no streching at all.
I have the following code segment. But it streches
Any help is appreciated.

Regards
TM

Code segment
============
...
...
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, imageBuffer);
....
....


glBindTexture(GL_TEXTURE_2D, texture_id); //Texture for the frame
glBegin(GL_QUADS);
glTexCoord2f(1, 0); glVertex3f( 4,-1,-12);
glTexCoord2f(1, 1); glVertex3f( 4, 1,-12);
glTexCoord2f(0, 1); glVertex3f(-4, 1,-12);
glTexCoord2f(0, 0); glVertex3f(-4,-1,-12);
glEnd();

ZbuffeR
01-11-2011, 06:12 AM
you are drawing a square.
if you want a rectangle with same aspect ratio as original texture, try this :
double aspectx = 700.0/512.0;
glTexCoord2f(1, 0); glVertex3f( 4.0*aspectx,-1,-12);
glTexCoord2f(1, 1); glVertex3f( 4.0*aspectx, 1,-12);
glTexCoord2f(0, 1); glVertex3f(-4.0*aspectx, 1,-12);
glTexCoord2f(0, 0); glVertex3f(-4.0*aspectx,-1,-12);

or this :
double aspecty = 512/700.0;
glTexCoord2f(1, 0); glVertex3f( 4.0*aspecty,-1,-12);
glTexCoord2f(1, 1); glVertex3f( 4.0*aspecty, 1,-12);
glTexCoord2f(0, 1); glVertex3f(-4.0*aspecty, 1,-12);
glTexCoord2f(0, 0); glVertex3f(-4.0*aspecty,-1,-12);

TurboMan
01-11-2011, 06:54 AM
Thanks ZbuffeR,
It still streches.
But, when I try
double aspectx = 700.0/512.0;
glTexCoord2f(1, 0); glVertex3f( 1.0*aspectx,-1,-12);
glTexCoord2f(1, 1); glVertex3f( 1.0*aspectx, 1,-12);
glTexCoord2f(0, 1); glVertex3f(-1.0*aspectx, 1,-12);
glTexCoord2f(0, 0); glVertex3f(-1.0*aspectx,-1,-12);
It looks better,