# How to draw a cross hairs in the center of the screen?

Hi,

1. I want to draw a Cross hairs (two lines) in the center of the screen with opengl.

2. my code is:

//void Callback_draw(…)

glPushMatrix();
int viewport[4];
float cx, cy, cross_len = 5.0f;
glGetIntegerv(GL_VIEWPORT, viewport);
cx = (viewport[2] - viewport[0])/2;
cy = (viewport[3] - viewport[1])/2;

glColor3ub(255, 255, 0);
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex2f(cx - cross_len, cy);
glVertex2f(cx + cross_len, cy);
glVertex2f(cx, cy - cross_len);
glVertex2f(cx, cy + cross_len);
glEnd();

glPopMatrix();

1. but the result is not right.

2. how can I do it?

Thanks.

Kou

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.