svaidya
svaidya

Reputation: 11

How to attach events to a cube drawn using OpenGL in gtk drawing area?

I am drawing a cube using OpenGL in a gtk drawing area widget. Now i want to attach events like "clicked" to the cube so that it can be selected for drag and drop etc. How should i proceed?

Upvotes: 1

Views: 637

Answers (1)

datenwolf
datenwolf

Reputation: 162327

OpenGL is not a scene graph (*gah* I'm, getting tired, writing this again, and again, and again). It draws things. You give it a bunch of triangles in 3D space, and it projects them to 2D, draws them to your desire, then forgets about it.

After you've drawn your cube, there's no longer anything around in OpenGL that would identify it as a coherent structure. That's entirely on your part.

What you have to do is:

  • maintain a representation of the scene you've drawn
  • install a mouse event handler on the GTK GL area
  • Use the mouse events to transform back pointer coordinates into scene coordinates. Basically you'll be "shooting" rays into the scene and test where they hit. Luckily testing a ray - cube intersection is trivial. You can speed things up a bit by first testing if the ray got even close, using a ray - bounding sphere test.

Keywords to follow are "OpenGL object picking"


so that it can be selected for drag and drop

BTW: You can't just drag around "objects" in OpenGL, because it doesn't know of "objects", in the sense of geometrical structures. OpenGL specifies objects, but those are actually abstracted sources of data, like textures or buffers of vertex and index data. Nothing that would make a manipulatable scene.

If you change something in a scene, with OpenGL you have to completely redraw it.

Upvotes: 6

Related Questions