PDA

View Full Version : help understanding glTexCoord2f parameters



xargon
01-04-2006, 05:47 AM
Hi everyone,

I am using glTexCoord2f function for rendering a texture to an OpenGL device context. However, it is behaving somewhat weird. I have not been unable to figure out the problem yet.

I am trying to render a subset of a texture into the screen. The idea is that a portion of the texture should be in the center.

This is what I do. The fraction value in bmpX and bmpY is the region of interest. I am also showing areas in the bmp/2.0 to the left and right direction and bmpY/2.0 in the top and bottom direction. So, the region around bmp should always be shown on the screen, but that is not the case. I am pretty sure I am screwing up something majorly here...

float leftXFactor = bmpX / 2.0;
float bottomYFactor = bmpY / 2.0;
float rightXFactor = bmpX + leftXFactor;
if (rightXFactor > 1.0)
rightXFactor = 1.0;
float topYFactor = bmpY + bottomYFactor;
if (topYFactor > 1.0)
topYFactor = 1.0;

glBegin(GL_QUADS);
glTexCoord2f(leftXFactor, bottomYFactor); glVertex2i(0, height); glTexCoord2f(rightXFactor, bottomYFactor);
glVertex2i(width, height);
glTexCoord2f(rightXFactor, topYFactor);
glVertex2i(width, 0);
glTexCoord2f(leftXFactor, topYFactor);
glVertex2i(0, 0);
glEnd();

I would really appreciate any help.

Cheers,
xarg

jide
01-04-2006, 07:17 AM
Texture coordinates are set between 0 and 1 for seeing the whole texture image. It also depends on the paramters you gave for your texture.

joshua_falken
01-05-2006, 06:38 AM
Hi! Your code looks about right as far as the use of texture coordinates is concerned. As the previous poster noted, you really only have to keep in mind that texture coordinates are always in the range [0;1]. Moreover, depending on how you defined your texture (glTexImage or gluBuild2DMipMaps), there might be an area in the texture bitmap which does not belong to the actual image - but you should be aware of that yourself.

Apart from that, your actual program logic looks a little odd. Maybe you would like to give us a little more detail about what your program is supposed to do and what it actually does. What is the supposed meaning of bmpX/bmpY? It seems strange that you should divide them by 2 and then add the result to the original values. Are these positions within the image? Then this would mean that your displayed image portion shrinks or grows whenever you change them.

Joshua