Drawing a Point that follows the mouse :)

Hay All,

I am implementing an application where a user is able to select colors and color some objects, The application is a 2D only. I am trying to implement something similar to a cursor that follows the mouse basically a circle (group of pixels) that is colored with the currently selected coloring color.
I am using a glutPassiveMotionFunc that basically knows the new x and y and draws the circle respectively, but I should clear the old point, intutively is tried to set its color to the background color but this makes it act as an eraser …
I have come to an idea that i can have a 3d ortho an draw this “mouse follower” on a differnt Z value and then be able to set the previous point to a transparent color, That did not work, the color is yet white and so erases what is beneath it … Now I think there can be a way if i can remove the the point by translating this plane (plane at the Z where the follower is drawn) …

Any suggestion how I can get this to work … could there be a more direct way ???

Thanks a lot for reading through :slight_smile:

Ok so I spent 5 minutes to understand your situation.

If I understood well :

  • you have a scene already drawn
  • you want to draw a 2d cursor on top, that follows the mouse.

2 ways to do it, the simple one first :
1: draw your scene, then the cursor. swapbuffers. clear. goto 1

Aha ! not very hard, isn’t it ? :slight_smile:

The complex way allow you to draw the scene only once :
0: draw scene
1: copy it to a texture
2: draw your texture with a fullscreen quad, then the cursor. swapbuffers. clear. goto 2

Have fun.

Hay Thanks for taking the 5 minutes to read my post :smiley:

Well i like your first solution… but I am a newbie can u elaborate (I don’t know what swap buffer is ) …
Well there is something worth saying that when I tried to use glutPostRedisplay I got flickering because basically the mouse is really too fast + I have the problem that if the user had colored an object on refreshing the info is lost will this be the case???

Again Thanks a lot for your time and help !

flickering ???
Possible causes :

  • do you render to the front buffer ? you should not. And you should use a double-buffered render context, with glut you request it with something like : glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  • you call glClear() at the wrong place
  • the scene is slow to render (well not much to do about this, unless use solution 2 above)

With glut, swapbuffers is implicitly called just after GLUT called your glutDisplayFunc or glutIdleFunc, so don’t worry about it.
Swaping buffers is needed with double buffering, as it exchanges the front buffer (visible on your screen) with the back buffer (where opengl commands draw stuff). That way, only complete scenes are displayed.
On the contrary, if you use single buffering, you man see the scene with missing bits, moving stuff, flickering, random transparency, etc.

  • I have the problem that if the user had colored an object on refreshing the info is lost will this be the case???

Does not compute. You must be doing something very wrong.
Simplify your code and debug it.