# how to draw at right position?

Hi there,

With opengl i know how to draw a picture occupied all client area, but i dont know how to draw
in a specified rect, ie, draw in rect(100,100,200,200). with gdi this is simple, but with
opengl i dont know where is rect(100,100,200,200), so i often draw in different area.
Any ideas? Thanks very much!

thanks a lot!

Hi ZbuffeR,

Thanks for your reply, but i think you dont understand my problem.

ie, i need draw a rect on my client area, which position at (200, 200, 600, 400).
with opengl i do the following:

glVertex3f(fxRight, fyTop, 0.0f);
glVertex3f(fxLeft, fyTop, 0.0f);
glVertex3f(fxLeft, fyBottom, 0.0f);
glVertex3f(fxRight, fyBottom, 0.0f);

but i dont know what value should i set for the fxRight to represent the 600 pixel(also what values for fxLeft,
fyTop and fyBottom to represent 200, 200 and 400.

I have to try many times in order to find the right position. ie, i first set fxLeft to 1.0f, then i run my
program to see where it position. if it position at left of 200 pixel, i increase fxLeft a little(ie, add 0.1f)
and try again. I do the same with fxRight, fyTop and fyBottom. But this is too complex and i need a simple
method.

Does anybody have good ideas? Thanks a lot!

“what values for fxLeft, fyTop and fyBottom to represent 200, 200 and 400.”
If you draw in pixel space, just use 200, 200 and 400.

In fact yes, I do not understand your problem.

Hi ZbuffeR,
Thanks a lot! I draw in opengl coordinate space and
cannt use 200, 200 and 400.

If you set up an orthographic projection matrix that has the same dimensions as your window like was suggested above then there is no guess work to be done. You would use screen coordinates to do your drawing.

If, for some reason, you don’t want to do this then you can try using gluUnproject (http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml)

to map where you would want to draw in screen space to object space.
You would then use the values you get from gluUnproject to position your rect.

Hi Aeluned,

Thanks very much, now i get it.

To ZbuffeR,

Thanks for your help too and sorry for my carelessness. What you said is right and you understand my problem clearly!

Thanks again!