Crosshair cursor isn't smooth?

I’m currently trying to use this to set up a simple crosshair cursor:

GLFWcursor* crosshairCursor = glfwCreateStandardCursor(GLFW_CROSSHAIR_CURSOR);
glfwSetCursor(window, crosshairCursor);

It works, and has saved me time doing it with OpenGL lines, but I find that the crosshair cursor is very janky, despite the program performing well otherwise, being in release mode and the camera behaving normally as well.

Does anyone have any ways to resolve this problem? Or is this a recognised issue?
Thanks

EDIT: I forgot that I had this code:

glfwSetCursorPos(window, 1024 / 2, 768 / 2);

Without it, the cursor is smooth but is much harder to control since it’s not being recentered each frame. Therefore, the ‘janky’ cursor movement is a result of the cursor being instantly snapped back to the centre of the screen. Is there a better way to handle this?

You shouldn’t use a hardware cursor for a screen center cross hair, as the cursor can update at a different frequency than your rendered frame amongst other potential problems.

Instead you should draw a cross hair in the middle and use glfwSetInputMode to disable the cursor.

To help with rendering a cross hair you could use NanoVG which can do some very fancy 2D rendering, or dear ImGui which though it’s main purpose is for debug UI can also be used to draw anti-aliased 2D shapes.

1 Like

Fair enough, I was thinking I might have to just draw it. Thanks for the library suggestions but I’m trying to stick to raw OpenGL, so do you have any code/tutorials for drawing a crosshair at the screen centre in modern OpenGL? All of the examples that I find tend to be outdated.

Thanks though :slight_smile:

If you’re cross hair isn’t very large, a quick hack approach would be to draw a square with two triangles and then use a fragment shader to use gl_FragCoord to detect when to draw the pixel.

Something like:

if( ( gl_FragCoord.x == uboValueScreenMiddle.x ) || ( gl_FragCoord.y == uboValueScreenMiddle.y ) )
{
    // set frag colour
}
else
{
    discard;
}

Thanks, I’ll take a look at that, quick hacks are always welcome in my opinion :slight_smile: