0

I have around 600 spheres in my PyOpenGL window. I am using PyQt to manage my window, so I use QGLWidget for that. I would like to pick from these spheres, but the problem is they are all same color metallic gray. So I am not using any image textures.

At this point, I have made some research and I found this tutorial which uses the C routine, and since I am not familiar with that it is quite challenging for me.

https://www.lighthouse3d.com/tutorials/opengl-selection-tutorial/

I would like to also mention that I have been using the core-profile instead of immediate mode like in the below link.

Using glfw window inside Pyqt Window

Is that possible to do the picking still using the core-profile, since it is pretty basic? Or do I need to switch my PyOpenGL code to immediate mode?

Oz T.
  • 1
  • 1

1 Answers1

0

I haven't used the QGLWidget before, but from looking at a few things you provided and the docs, you'd probably want to:

  1. Make sure mouse tracking is enabled on the gl widget object.
  2. Make sure you have a function connect to the gl widgets paintEvent function
  3. Get your mouse position
  4. Create a QVector with all the objects in the scene.
  5. Create a QVectorIterator to iterate through all the objects in the scene.
  6. Cast/convert each of the 3d objects position transforms into screen space, then calculate the delta add to a set (maybe even to a dict with {delta: qglobject}). Keep track of the lowest delta
  7. Select the object with my_objects[lowest_delta], and do your operations.

I've never used this before, so there might be some classes and methods which will do most of this for you, look into it. If you're going to have tons of objects on screen, you might want to have some sort of caching/memoization in place to boost performance. One thing that might help would be creating a dict from sorted deltas for each object from the gl_widget origin/center, then using binary search to find the closest objects to your mouse position.

Check some of these out:

https://doc.qt.io/qt-5/qglwidget.html

https://doc.qt.io/qt-5/qglwidget.html#paintEvent

Jacob Kaniuk
  • 175
  • 6
  • Thanks for the explanations. But the problem here is I need to rotate the spheres occasionally or move them with my mouse movement. Can the QVector be used even in this situation? – Oz T. Aug 04 '20 at 08:53
  • It sounds like you'll need to use raycasting in order to do this. Unfortunately, it doesn't look like QGLWidget doesn't have anything like this built in, so you'd either have to implement it yourself, or use a library. You'll need to grab every objects' vertices and dump these into a list of QVector3D objects in order to find the intersecting triangle using vector math. Unfortunately again, I don't know how fast python would be able to do this so you will probably have to do it in C++ to get reasonable results. That's also why 3D graphics libraries are almost always written in C/C++. – Jacob Kaniuk Aug 04 '20 at 15:03
  • Yes, that's where I am planning to do color picking instead. But at that point, I don't know how I can manage to do that with two different colors in an out buffers. – Oz T. Aug 05 '20 at 10:57