Capture a 3D click in openGL

Asked

Viewed 106 times

1

I am doing a job in openGL. I wanted to click on a window and a drawing appear where it was clicked. As the scenario is in 3D wanted to know how I do to get the value of the coordinate in Z.

Could someone tell a step-by-step how to do? =)

  • welcome to Stackovarflow, would be interesting if you put together your question, an excerpt from the code you have already done. This helps to give a final answer.

2 answers

1

You have to work on an Opengl texture from viewport that can be used by Opengl functions.

So I know 2 solutions:

  1. Use a function that reads a pixel (glReadPixel if I’m not mistaken). But usually this communication between GPU and CPU is slow.

  2. Create a code glsl that passes as uniform the coordinates of the mouse and the texture (created with the glGenTextures). The work would be done directly at the GPU, so it would be a more efficient way.

1


There are different techniques for working with object selection in OpenGL. I know these two:

  • Each object in the scene must have an id in memory, when you capture the click, you must go through all the objects being rendered and check if there is collision between the click coordinate and each object. You must apply your projection matrix before checking the collision.

  • Draw into a buffer separate, using different colors for each object, upon receiving the click event, read the pixel color at that position and refer to the color in a dictionary of cores->objetos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.