OpenGL picking is the process of determining which 3D object on the screen a user is selecting with the mouse cursor. It renders the scene to calculate the correct positions of objects and returns all objects under the mouse coordinates. Another method involves locating an object by color, which can be faster.
OpenGL® picking in computer programming is the process of determining which object within a three-dimensional (3D) scene is at which point on the screen once the scene has been rendered. It can also refer to locating multiple objects in one spot or within a box. Most often, OpenGL® selection is used to determine which 3D object on the screen a user is trying to select with the mouse cursor. While this might seem simple, there are several subtleties in how OpenGL® renders a scene that can make it quite complex. Additionally, there are inherent problems in some graphics cards and drivers that can cause the OpenGL® select function to fail and return false results.
When a user views a 3D scene on a computer monitor, the resulting image is known as a rendering of the scene. The scene is actually stored in memory as a collection of primitive shapes or polygons, which are themselves just collections of 3D points within the scene space. The computer uses world coordinates, sometimes called world coordinates, to perform the most basic functions that manipulate objects in the scene. In most applications, the user is able to maneuver the view of the scene from different angles so that objects can be seen in different perspectives. The user’s virtual position within the scene is called the camera angle or camera position.
The complexity of OpenGL® picking comes from determining the mouse position on the two-dimensional (2D) screen from a possibly arbitrary position and angle within the scene, the camera position. Also, since the rendering from the human observer’s point of view is actually 2D, there is no way for the user to provide the depth of the mouse click within the scene. The OpenGL® pick function solves this complex problem in two ways.
The first is that instead of doing a series of separate computations to abstractly translate where the viewer is and then find an object in the render window, the function actually renders the scene as it does when it normally works, with the exception that the rendering used for selection is not displayed, it is only used to calculate the correct positions of the objects. The difference is that instead of rendering the entire area that would be visible to the user, it renders only the area where the mouse is. This means that all rendered objects are technically where the mouse pointer is.
The second problem, not having a way to indicate the depth of a selected area, is solved by returning all the objects in the scene that are under the coordinates of the mouse. The OpenGL® select function returns all objects in an array along with their distance from the position of the viewer. This allows a program to quickly find the closest object if desired.
One way to visualize the OpenGL® selection is to imagine a line, sometimes referred to as a ray in 3D programming, moving from the mouse pointer’s location in the scene and away from the viewer’s location. Each object this ray touches is added to an array of objects, along with the distance from the observer. This is a very simple explanation of how an OpenGL® withdrawal form works.
Another method of selecting objects in OpenGL® involves locating an object by color and can be significantly faster. This method renders the scene but instead of applying lighting and texture to objects, they are instead rendered with a single simple color. Each object or group of objects has its own distinct color. The scene is only rendered in memory and not displayed, so this doesn’t affect what the user sees. Instead of looking for 3D collisions between objects, the color at the mouse cursor location is returned and that color will be related to a specific object.
Protect your devices with Threat Protection by NordVPN