Using GLM UnProject

I am not sure how to use the Unproject method provided by GLM.

In particular, in what format does the viewer window go? And why does a function not require a presentation matrix, as well as a projection and world matrix?

+3
source share
2 answers

It takes a bit of history here. GLOM unproject is actually a more or less direct replacement for the gluUnProject function, which uses legacy rendering with the fixed OpenGL function. In this mode, the Model and View matrix were actually combined in the ModelView matrix. Apparently, the author of GLM dropped the part of the “view” in the title, which confuses the thing even more, but it comes down to transferring something like view*model .

Now for actual use:

  • win is a vector that contains three components that make sense in the coordinates of the window. These are the "x, y" coordinates in your viewport, the "z" coordinate that you usually extract by reading the depth buffer in (x,y)
  • a model, a matrix of representations and projections should speak for itself, even if you are thinking about using this function. But a useful (refengl-specific) refresher .
  • The viewport is defined as glViewport , which means (x, y, w, h). X and Y indicate the lower left corner of your viewport (usually 0.0). Width and height (w, h). Note that in many other systems, x, y sets the upper left corner, you must then convert the y-coordinate, which is shown in the NeHe code, which I refer to below.

When applied, you simply end up converting the provided window coordinates back to object coordinates, more or less inverse to what your rendering code usually does.

A semi-ordered explanation of the source gluUnProject can be found in the NeHe article. But of course, this is OpenGL-specific, while glm can be used in other contexts.

+14
source

The viewport is transmitted as four floats: the x and y coordinates of the viewport, followed by its width and height. The same order that is used, for example, on glGetFloatv(GL_VIEWPORT, ...) . Therefore, in most cases, the first two values ​​should be 0.

As KillianDS has already pointed out, the model argument is actually the model view matrix, see the example of using unProject() in gtx_simd_mat4.cpp , function test_compute_gtx() :

  glm::mat4 E = glm::translate(D, glm::vec3(1.4f, 1.2f, 1.1f)); glm::mat4 F = glm::perspective(i, 1.5f, 0.1f, 1000.f); glm::mat4 G = glm::inverse(F * E); glm::vec3 H = glm::unProject(glm::vec3(i), G, F, E[3]); 

As you can see, the matrix passed as the second argument is basically a translation and a perspective transformation.

+2
source

Source: https://habr.com/ru/post/1487059/


All Articles