Forum > OpenGL

Read OpenGLContent mouse position and convert back to coords.

(1/4) > >>

ranny:
Hello,

I have a couple of programs I use for graphing 3D polynomials where the data and curve shape is displayed in a OpenGLContent object, I got a lot of help with this from the forum in the past.  I am now working on a 3D beam analysis program for which I have all the maths and can draw the model in 3D in my OpenGLContent and it is beginning to look good.  I am having some scaling problems but I think I will get there.

My difficulty now is finding a way of clicking the mouse near a drawn node point on the OpenGLContent object and getting back world coordinates I can then use in other calculations, for example, being able to add another node offset to the one I have selected.  I am having no luck with this at all yet I am sure there is a neat method out there.  I am struggling to understand how, I think, that anything I draw on the OpenGLContent object always has it's centre 0,0,0 in the middle of the object but the mouse position is relative to the top and left of the object.  That's my first problem!

Does anybody have an idea where I can look for a solution?

Thanks in advance.

Dzandaa:
Hi,

I have the same problem :)

I didn't find any clue in the OpenGl manual.

Most of the solutions use ray casting:

https://antongerdelan.net/opengl/raycasting.html

Also look a the feedback mode glRenderMode(GL_FEEDBACK), not sure it is related.

B->

ranny:

--- Quote from: Dzandaa on May 16, 2024, 06:51:24 pm ---Hi,

I have the same problem :)

I didn't find any clue in the OpenGl manual.

Most of the solutions use ray casting:

https://antongerdelan.net/opengl/raycasting.html

Also look a the feedback mode glRenderMode(GL_FEEDBACK), not sure it is related.

B->

--- End quote ---


Hi Dzandaa,

So I am plotting lines to represent a beam structure which displays the beams and the numbers at the beam ends and middle of each beam.  The 3D coords from the file of data get scaled and drawn in the OpenGLContent object.  I was hoping I could "reverse" the scaling routine from a cursor position and get at least an X Y pair of coords in world terms that I can check against all of the nodes and find the nearest node to the cursor.  However, as it appears to me, how OpenGL plots lines and polygons etc. is a bit hard to work backwards from.  I also thought that I could store the OpenGLContent position in an array for interrogating later but not sure about how I will get it to work.

I would have thought there was some sort of routine that can do this, there must be OpenGL gaming situations where you place the cursor at a location and have code that responds to it.....



 

Khrys:
Are you using "modern" OpenGL (shaders, vertex buffer objects/arrays, framebuffers,  glDrawElements)? Or are you using the old fixed-function pipeline (glBegin, glEnd, glVertex3f, glCallList  etc.)? I'm asking because these are very different approaches, with the former being insanely more flexible compared to the latter.

In my (modern) OpenGL project for example I'm rendering the scene again with a shader that outputs (among other things) the position of each fragment (interpreted as RGB it may look something like the attached image). Then I do  glReadPixels at the mouse position to get the position.

The same thing should be possible by sampling the Z-buffer instead (which is pretty much always present), although that isn't ideal (nonlinear, loss of precision for objects far away from the camera, needs more camera information).

Unfortunately this technique isn't implementable in the fixed-function pipeline (glReadPixels  may still be useful however, for example by enumerating objects with specific color values to at least check what is under the mouse pointer). In that case I'm afraid you'll have to use CPU-based ray casting. In any case, the fixed-function pipeline makes anything slightly advanced immediately much, much harder if not outright impossible to accomplish (while also being deprecated since 2008 + shaders have been available for 20 years).

KodeZwerg:
Isnt that task not just a normalization? Like ClientToScreen/ScreenToClient methods.

Navigation

[0] Message Index

[#] Next page

Go to full version