PDA

View Full Version : Drawing a simple crosshair?



eratic-magician
03-06-2017, 09:59 AM
I'm having a lot of trouble drawing a crosshair to the centre of the screen with modern OpenGL. Most other tutorials or posts use outdated methods, or another library. How can I simply draw a crosshair shape (or a simple dot, small circle etc.) in the centre of the screen?

I've tried following tutorials like this: https://guidedhacking.com/showthread.php?6588-OpenGL-Draw-a-crosshair, but with no luck.

Right now, the only way I can see how to do this is by having a crosshair model and moving it in relation to the camera, but that seems a bit unnecessary. How would I do this with OpenGL lines for example?

Thanks.

GClements
03-06-2017, 02:02 PM
Don't use the same matrices for the crosshair (or any other HUD elements) that you'd use for objects which are part of the world.

Cornix
03-06-2017, 03:59 PM
Do you use your own shaders? If you do write a new shader for the crosshair which uses no modelview matrices and an orthogonal projection. After that it should be very easy to draw the actual crosshair. Either draw a VBO containing primitive shapes (lines, triangles or whatnot) or draw the texture of a crosshair as a quad.

If you dont use custom shaders just set the model and view matrices to identity and the projection matrix to orthogonal.

eratic-magician
03-06-2017, 04:11 PM
Do you use your own shaders? If you do write a new shader for the crosshair which uses no modelview matrices and an orthogonal projection. After that it should be very easy to draw the actual crosshair. Either draw a VBO containing primitive shapes (lines, triangles or whatnot) or draw the texture of a crosshair as a quad.

If you dont use custom shaders just set the model and view matrices to identity and the projection matrix to orthogonal.

I do have some simple texture shaders, but I'm not really confident with using them, so I don't know how I'd do it with a shader exactly. If I were to do the second thing you mentioned, how would I then draw the lines with orthogonal? Every tutorial or code snippet I look at doesn't seem to work with me since they're all outdated :/

john_connor
03-07-2017, 02:56 AM
I do have some simple texture shaders, but I'm not really confident with using them, so I don't know how I'd do it with a shader exactly. If I were to do the second thing you mentioned, how would I then draw the lines with orthogonal? Every tutorial or code snippet I look at doesn't seem to work with me since they're all outdated :/

try this example:
https://sites.google.com/site/john87connor/texture-object/2-1-texture-2d

read more about textures here:
https://alfonse.bitbucket.io/oldtut/Texturing/Tutorial%2014.html

basically you need to:
-- create a texture object
-- set some texture parameters
-- allocate memory / fill the memory with your data (e.g. an image or calculated values of a complicated function)
-- bind that texture object to a certain texture unit
-- tell the "uniform sampler2D tex;" in your shader from what texture unit to sample

then you can read the texture data via "vec4 texture(tex, ...some coords...);" in your shader, "vec2 somecoords" is the location from where to read the data, you have to provide that value somehow, e.g. via passing it from the vertexshader where it could be a "vertex attribute"