PDA

View Full Version : how to calculate coordinates of rect by image dimension



zydgyy
08-18-2012, 05:36 AM
if i have an image 123x256 pixels
if i draw a rect that have the same size as this image,how can i calculate the coordinates of vertices of this rect by modelview,projectio,viewport and image dimensions!

thokra
08-18-2012, 05:45 AM
Do you want the image to cover the whole viewport?

V-man
08-18-2012, 11:36 AM
Assuming your window size is width and height, setup the viewport
glViewport(0, 0, width, height);

setup your ortho projection to match the viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

You can leave the modelview as is
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and render your quad
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex2f(0.0, 0.0);
glVertex2f(123.0, 0.0);
glVertex2f(123.0, 256.0);
glVertex2f(0.0, 256.0);
glEnd();

zydgyy
08-18-2012, 06:56 PM
Do you want the image to cover the whole viewport?
No,i want the image cover my rect,and it's not distorted

zydgyy
08-18-2012, 06:57 PM
Assuming your window size is width and height, setup the viewport
glViewport(0, 0, width, height);

setup your ortho projection to match the viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, -1.0, 1.0);

You can leave the modelview as is
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and render your quad
glColor4f(1.0, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex2f(0.0, 0.0);
glVertex2f(123.0, 0.0);
glVertex2f(123.0, 256.0);
glVertex2f(0.0, 256.0);
glEnd();

Can this method achieve?

zydgyy
08-20-2012, 01:15 AM
Is there anybody can help me out?

tonyo_au
08-20-2012, 06:56 PM
Do you know how to render a texture (image) to a quad?

zydgyy
08-20-2012, 07:09 PM
Do you know how to render a texture (image) to a quad?
Yes,i know this,but how to make the image not magnify (http://dict.youdao.com/w/magnify/) and shrink (http://dict.youdao.com/w/shrink/)

thokra
08-20-2012, 11:28 PM
If you transform the quad the image is mapped to using a perspective projection or an ortho projection with certain parameters there is no way you can keep it the same size on the screen. The image will theoretically still cover the whole quad but the quad itself will cover less pixel after projection.

What exactly is your goal here? Do you want the image to always cover the an area equal to the image dimensions? Do you want somethng like a HUD?