Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 176439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:52:40+00:00 2026-05-11T13:52:40+00:00

I want to implement my own cursor in an OpenGL / GLUT window. The

  • 0

I want to implement my own cursor in an OpenGL / GLUT window. The usual way to do this is to freeze the cursor (so it can’t hit the edges of the screen) and keep track of its position yourself. I can make the onscreen cursor invisible using

glutSetCursor(GLUT_CURSOR_NONE); 

and then inside of my glutPassiveMotionFunc callback move the pointer to the middle of the window using

int centerX = (float)kWindowWidth / 2.0; int centerY = (float)kWindowHeight / 2.0;  int deltaX = (x - centerX); int deltaY = (y - centerY);  mouseX += deltaX / (float)kWindowWidth; mouseY -= deltaY / (float)kWindowHeight;  glutWarpPointer( centerX, centerY ); 

This works in that it keeps the pointer stuck to the middle of the window. The problem is that when I am drawing the ‘OpenGL’ mouse (inside of the glutDisplayFunc() callback) it is extremely jerky.

I have looked online and found that there can be an issue where glutWarpPointer() causes the glutPassiveMotionFunc callback to be called again, resulting in a loop, but this doesn’t seem to happen here.

I’m on Mac OS X and I found a post saying that CGDisplayMoveCursorToPoint was a better fit for this. Calling CGDisplayMoveCursorToPoint works but the movement is still very jerky (and I seem to get a lot of events where x and y are both 0). In any case, I’d like this to work on Linux as well so a Mac only solution is not ideal (but I’m okay having to do different things on the different systems).

I’ve reduced this to a testcase.

#include <stdio.h> #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h>  int curX = 0; int curY = 0;  void display() {     glClearColor( 0.0, 0.0, 0.0, 1.0 );     glClear( GL_COLOR_BUFFER_BIT );      float vx = (float)curX / 300.0 + 0.5;     float vy = (float)curY / 300.0 + 0.5;      glColor3f( 1.0, 0.0, 0.0 );     glBegin( GL_POINTS );         glVertex3f( vx, vy, 0.0 );     glEnd();      glutSwapBuffers();  }  void passivemotion( int x, int y ) {     int centerX = 150;     int centerY = 150;      int deltaX = x - centerX;     int deltaY = y - centerY;     curX += deltaX;     curY -= deltaY;      glutWarpPointer( centerX, centerY ); }  void timer( int val ) {     glutTimerFunc( 16, &timer,  0);     glutPostRedisplay(); }  int main (int argc, char * argv[]) {     glutInit(&argc, argv);     glutInitDisplayMode(GLUT_RGB);     glutInitWindowSize(300,300);     glutCreateWindow('FPS Mouse Sample');     glutDisplayFunc(&display);     glutPassiveMotionFunc(&passivemotion);     glutSetCursor( GLUT_CURSOR_NONE );     glutTimerFunc( 16, &timer, 0 );     glutMainLoop();     return 0; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:52:41+00:00Added an answer on May 11, 2026 at 1:52 pm

    Thanks aib for the tips. You got me looking into the disassembly of glutWarpPointer and it became obvious what was going on. Calling glutWarpPointer CGPostMouseEvent which results in a bunch of nonsense events (and there isn’t any way to skip them since you only get Mouse Events once per frame, the new ‘real’ events will be late). The solution I’ve found is to only warp when the pointer is at the edge of the screen ( the point, after all, is to pretend like the point can never reach the edge of the screen ). In any case, here is the code.

    int lastX = 150; int lastY = 150; void passivemotion( int x, int y ) {         int deltaX = x - lastX;     int deltaY = y - lastY;      lastX = x;     lastY = y;      if( deltaX == 0 && deltaY == 0 ) return;      int windowX     = glutGet( GLUT_WINDOW_X );     int windowY     = glutGet( GLUT_WINDOW_Y );     int screenWidth     = glutGet( GLUT_SCREEN_WIDTH );     int screenHeight    = glutGet( GLUT_SCREEN_HEIGHT );      int screenLeft = -windowX;     int screenTop = -windowY;     int screenRight = screenWidth - windowX;     int screenBottom = screenHeight - windowY;      if( x <= screenLeft+10 || (y) <= screenTop+10 || x >= screenRight-10 || y >= screenBottom - 10) {         lastX = 150;         lastY = 150;         glutWarpPointer( lastX, lastY );         //  If on Mac OS X, the following will also work (and CGwarpMouseCursorPosition seems faster than glutWarpPointer).         //  CGPoint centerPos = CGPointMake( windowX + lastX, windowY + lastY );         //  CGWarpMouseCursorPosition( centerPos );         // Have to re-hide if the user touched any UI element with the invisible pointer, like the Dock.         //  CGDisplayHideCursor(kCGDirectMainDisplay);     }      curX += deltaX;     curY -= deltaY; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the TimeSpan.FromSeconds method. May 11, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure what exactly are you asking. "Vim's features,… May 11, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer In the last week I've been in contact with the… May 11, 2026 at 4:50 pm

Related Questions

I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I have a UserControl that consists of three TextBoxes. On a form I can
I'm using a remote 3rd party storage solution that does not have support for
I'm working on building my own base user interface classes. On them, I want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.