Jump to content
  • Advertisement
Sign in to follow this  
Foxito Foxeh

Drawing Crosshair in perspective?

This topic is 770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!
I'm trying to draw a crosshair in the middle of the screen in a perspective view...
I've seen a lot of code's in google but they all just draw in Ortho. How could I draw a crosshair in perspective view and while moving/rotating camera?

Share this post


Link to post
Share on other sites
Advertisement

Is there some reason you need to use the same camera to draw the crosshair? Generally it's much simpler to use a separate camera to draw HUD elements.

Share this post


Link to post
Share on other sites
You could create a point along the look at vector of your camera and expand this in the geometry Shader to a Quad and Draw the cross hair texture with transparency on this. You have to make sure that the Quad is perpendicular to your look at vector.

Share this post


Link to post
Share on other sites

Just call

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0,0,-crosshairDistanceFromCamera);

//Draw Quad

//Profit.

 

No special tricks needed. The quad will always be drawn relative to the camera in this example. Issues: what happens when your character or camera are close to a wall? The cursor will probably go through the wall.

Share this post


Link to post
Share on other sites

Is there some reason you need to use the same camera to draw the crosshair? Generally it's much simpler to use a separate camera to draw HUD elements.

 

So, how to do that?

 

You could create a point along the look at vector of your camera and expand this in the geometry Shader to a Quad and Draw the cross hair texture with transparency on this. You have to make sure that the Quad is perpendicular to your look at vector.

 

Just call

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0,0,-crosshairDistanceFromCamera);

//Draw Quad

//Profit.

 

No special tricks needed. The quad will always be drawn relative to the camera in this example. Issues: what happens when your character or camera are close to a wall? The cursor will probably go through the wall.

 

Thanks! I will try it later and see how this works and I guess the wall bug is what could happen. Thanks! :3

Share this post


Link to post
Share on other sites

Hi!
I'm trying to draw a crosshair in the middle of the screen in a perspective view...
I've seen a lot of code's in google but they all just draw in Ortho. How could I draw a crosshair in perspective view and while moving/rotating camera?

 

You switch to an ortho view, then draw your crosshair.

 

With this kind of setup, each frame looks like this (I note that you tagged GL1.1 so I'll use the typical calls that you'd use with that version):

 

glMatrixMode (GL_PERSPECTIVE);

glLoadIdentity ();

gluPerspective (blah, blah, blah, blah);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity ();

// draw 3D stuff in perspective projection here...

 

glMatrixMode (GL_PERSPECTIVE);

glLoadIdentity ();

glOrtho (blah, blah, blah, blah);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity ();

// draw 2D stuff in ortho projection here...

 

SwapBuffers (blah);

 

If you have followed typical online tutorials you might be under the impression that you can't do this, perhaps because they (assuming GLUT) only reset the projection matrix in the reshape func.  But of course you can do this; you can change the projection matrix mid-frame as many times as you wish (just don't do it between a glBegin and it's corresponding glEnd).  With this kind of setup your 3D stuff is still drawn in perspective, but your crosshair is drawn in 2D and you can use screen co-ords for positioning it (depending on the parameters to your glOrtho call, of course).

 

Performance: it's not a performance optimization to only change the projection matrix when the screen size changes (e.g in a reshape func if using GLUT).  Calculating a new projection matrix is just a dozen or so floating point ops, compared to the thousands, tens of thousands or hundereds of thousands that you may be doing elsewhere.  It's not even going to register as noise on any kind of performance graph.

Share this post


Link to post
Share on other sites

 

Hi!
I'm trying to draw a crosshair in the middle of the screen in a perspective view...
I've seen a lot of code's in google but they all just draw in Ortho. How could I draw a crosshair in perspective view and while moving/rotating camera?

 

You switch to an ortho view, then draw your crosshair.

 

With this kind of setup, each frame looks like this (I note that you tagged GL1.1 so I'll use the typical calls that you'd use with that version):

 

glMatrixMode (GL_PERSPECTIVE);

glLoadIdentity ();

gluPerspective (blah, blah, blah, blah);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity ();

// draw 3D stuff in perspective projection here...

 

glMatrixMode (GL_PERSPECTIVE);

glLoadIdentity ();

glOrtho (blah, blah, blah, blah);

glMatrixMode (GL_MODELVIEW);

glLoadIdentity ();

// draw 2D stuff in ortho projection here...

 

SwapBuffers (blah);

 

If you have followed typical online tutorials you might be under the impression that you can't do this, perhaps because they (assuming GLUT) only reset the projection matrix in the reshape func.  But of course you can do this; you can change the projection matrix mid-frame as many times as you wish (just don't do it between a glBegin and it's corresponding glEnd).  With this kind of setup your 3D stuff is still drawn in perspective, but your crosshair is drawn in 2D and you can use screen co-ords for positioning it (depending on the parameters to your glOrtho call, of course).

 

Performance: it's not a performance optimization to only change the projection matrix when the screen size changes (e.g in a reshape func if using GLUT).  Calculating a new projection matrix is just a dozen or so floating point ops, compared to the thousands, tens of thousands or hundereds of thousands that you may be doing elsewhere.  It's not even going to register as noise on any kind of performance graph.

 

 

Wow thanks dude! This is really working and has no performance issues :))
Thanks a lot, this helped me to create the crosshair ^_^

 

By the way, there's no GL_PERSPECTIVE in OpenGL 1.1 (Don't know if in superior ones there's). It's GL_PROJECTION :)

Edited by Zoditu

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!