Texture coordinates to screen pixel coordinates

Hi,

suppose i have a texture of WxH (not a power of two texture).
I use it to texture a quad with the same aspect ratio. This quad can be rotated and scaled before it is rendered on screen.

Now, i have a screen pixel (x,y) and i want to know to which texture pixel it correponds (in my application, i am sure that (x,y) is a pixel that belongs to the textured quad).

I use gluUnproject using the x,y coordinates of the pixel in this way (the screen has resolution ScreenW X ScreenH):

// This is the rendering of the quad
// w and h is the width and height of the texture
// refImageScale = w/h
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex3f (-1refImageScale, -1, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f (1
refImageScale, -1, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f (1refImageScale, 1, 0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f (-1
refImageScale, 1, 0);
glEnd();

// This code will get model view, projection and viewport
// matrices
GLdouble mvMat[16], prjMat[16];
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX, mvMat);
glGetDoublev(GL_PROJECTION_MATRIX, prjMat);
glGetIntegerv(GL_VIEWPORT, viewport);

// Read back the depth buffer
glReadPixels(0, 0, ScreenW, ScreenH, GL_DEPTH_COMPONENT, GL_FLOAT, depthBuffer);

// Suppose we have a pixel on the screen with coordinates (x,y)
// Unproject screen pixel to retrieve object coordinates
gluUnProject(x, viewport[3] - y,(double)depthBuffer[(viewport[3]-y) * ScreenW + x],
mvMat,
prjMat,
viewport,
&x1, &y1, &z1);

After that, i want to compute the coordinates of the texture pixel that corresponds (at least approximately) to the screen pixel:

// w and h is the width and height of the texture
// refImageScale = w/h
x1 = (x1 + refImageScale)/(2.0*refImageScale);
y1 = (-y1 + 1.0)/2.0;
x1 = ROUND(x1 * (double)(w-1));
y1 = ROUND(y1 * (double)(h-1));

It does not seems too precise. Am i doing things in the right way??

Thank you,
Luca