PDA

View Full Version : translation problem need help



deshan
11-08-2011, 07:59 AM
Hello

I have a problem when drawing a quad.

But to draw the quad at top left corner of the window I have to do a translation like glTranslatef(-0.55, 0, -1) I do not know why this value 0.55. I calculated this by running the application multiple times and adjusting.


My code

gluPerspective(45.0f, 640/480, 0.1f, 5000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(-0.55, 0, -1); // problem here what is this 0.55?

glPushMatrix();
glBegin(GL_QUADS);
glVertex3f (0.0, 0.0, 0.0);
glVertex3f (0.5, 0.0, 0.0);
glVertex3f (0.5, 0.5, 0.0);
glVertex3f (0.0, 0.5, 0.0);
glEnd();
...

please help

glfreak
11-08-2011, 09:19 AM
Actually what you need is the world coordinate of the upper left corner of the window.

p = UnprojectPoint(0, windowHeight - 1, 0);

p is the upper left coordinate in world space.

glBegin(GL_QUADS);
glVertex3f(p.x, p.y, p.z);
glVertex3f(p.x, p.y + 1, p.z);
glVertex3f(p.x + 1, p.y + 1, p.z);
glVertex3f(p.x + 1, p.y, p.z);
glEnd();

Assuming a CCW backface culling mode.

Another way to do it is using the upper left coordinate of the near clipping plane...

There are different ways as well depending what you exactly need to achieve.

Ed Daenar
11-08-2011, 10:38 AM
The reason why your translation seems so odd is because you are using a perspective projection. By definition this means that the further away from the origin of the space your quad is, the smaller it will look.

If you simply want draw a quad taking the full space of your screen, you'll be much better off using an ortographic projection or simply no projection at all which by definition will make your clip space go from -1 to 1 in each direction.

Look for information on ortographic projections, perspective and clip space.

deshan
11-10-2011, 12:27 AM
Both of you thank you very much for answering.

By using UnprojectPoint some how I managed to draw the quad on the screen.
My code for UnprojectPoint

Vector3f getOGLPosition(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return Vector3f(posX, posY, posZ);
}

And when drawing

Vector3f leftBottom = getOGLPosition(m_Left, m_Bottom);
Vector3f rightBottom = getOGLPosition(m_Right, m_Bottom);
Vector3f rightTop = getOGLPosition(m_Right, m_Top);
Vector3f leftTop = getOGLPosition(m_Left, m_Top);

glPushMatrix();
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2);

glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (leftBottom.x, leftBottom.y, leftBottom.z +1);

glTexCoord2f (1.0, 0.0);
glVertex3f (rightBottom.x, rightBottom.y, rightBottom.z +1);

glTexCoord2f (1.0, 1.0);
glVertex3f (rightTop.x, rightTop.y, rightTop.z +1);

glTexCoord2f (0.0, 1.0);
glVertex3f (leftTop.x, leftTop.y, leftTop.z +1);
glEnd();

glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glPopMatrix();

The problem I face now is texturing.
I have printed the values of those 4 corners of quad and they are,
(-543, 650, -5917)
(202, 658, -5622)
(-13 56, -68)
(108, -88, -5434)

So texturing is extremely bad.

Kindly advice me if you know any other way of doing this?

Thank you

BionicBytes
11-10-2011, 04:46 AM
texturing is extremely bad.

What does this mean?

Saying something is 'bad' or 'good' is not helpful as it does not describe what the problem is, only that you don't like it.

Are you saying the texturing is too pixelated? I.e the texture is streatched too much?