I want to draw a Cross hairs (two lines) in the center of the screen with opengl.
my code is:
float cx, cy, cross_len = 5.0f;
cx = (viewport - viewport)/2;
cy = (viewport - viewport)/2;
glColor3ub(255, 255, 0);
glVertex2f(cx - cross_len, cy);
glVertex2f(cx + cross_len, cy);
glVertex2f(cx, cy - cross_len);
glVertex2f(cx, cy + cross_len);
but the result is not right.
how can I do it?
see the faq (www.opengl.org righht side) or the red book
u need an orthogonal viewport
I dont think you need an orthogonal view.
Maybe you could draw the crosshair before you setup the modelview matrix.
I havent actually tried it…but it might work.
Your viewport does not necessarily map one to one to the dimensions of your viewing frustrum. So calculating based on your viewport is wrong. If you use gluPerspective you’ll have to use the FOV, Aspect Ratio, and Near value plus some trigonometry to figure out what the true centre is. If you use glOrtho or gluOrtho2d then Centrex = Left + (Right - Left)/2 and CentreY = Bottom + (Top - Bottom)/2.
Also call glLoadIdentity after glPushMatrix. Otherwise any transforms before the cross hair code will affect it.
[EDIT] A lazy way to get you’re centre with a perspective projection is to gluUnproject the centre of your viewport (the one you had calculated). It should be much slower than doing the maths but just as reliable.
[This message has been edited by Furrage (edited 03-19-2002).]
You can draw the crosshair quad before you set up the gluLookAt transformation of the viewport matrix.