opengl - How to use glm::project to get the coordinates of a point in world space? -


i have point (1, 2) in 2d space expressed vector:

glm::vec3 pt = glm::vec3(1, 2, 0) 

(here set third component 0 - not sure if that's correct?)

i have model-view matrix apply translation point:

glm::mat4 modelview = glm::mat4(1.0f); modelview = glm::translate(modelview, glm::vec3(3.0f, 3.0f, 0.0f)); 

now want find actual coordinate of point in world space. did research , glm::project() seems can use this. takes in 4 parameters:

detail::tvec3<t> glm::gtc::matrix_transform::project(detail::tvec3<t> const & obj,                                                      detail::tmat4x4<t> const & model,                                                      detail::tmat4x4<t> const & proj,                                                      detail::tvec4<u> const & viewport  ) 

the first 2 parameters point , model view matrix have. should use 3rd , 4th parameters (the projection matrix , viewport vector)? how can create/get them?

i think may more interested in glm::unproject, inverse of glm::project. long story short, glm::frustum, glm::perspective , glm::perspectivefov candidates building proj matrix, while along lines of vec4(0, 0, screenwidth, screenheight) should valid viewport vector. depends on how set opengl camera.

a full example should help.

to screen space , back

import required libraries:

#include <iostream> #include <glm/vec3.hpp> #include <glm/mat4x4.hpp> #include <glm/gtx/transform.hpp> #include <glm/gtc/matrix_transform.hpp>  using namespace std; using namespace glm; 

our main:

int main(int argc, char *argv[]) { 

this original point, in so-called object space. if loading mesh file, find these xyz coordinates in file.

    vec3 original(1.0f, -2.0f, 3.0f); 

the model matrix specifies object positioned in scene. view matrix specifies relative position of positioned object respect camera. in opengl these matrices combined in single matrix called modelview. opted term model here, because glm documentation uses, term modelview more appropriate case, believe:

    mat4 model = translate(mat4(1.0f), vec3(0.0f, 0.0f, -10.0f)); 

the projection matrix represents lenses , aperture of camera, , deforms scene in way simulates perspective, making objects far away smaller. can use glm function such frustum behaves gl counterpart glfrustum:

    mat4 projection = frustum(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 100.0f); 

the viewport specifies size , position of drawing area. 640x360 window typically use this:

    vec4 viewport(0.0f, 0.0f, 640.0f, 360.0f); 

the project function magic of projecting original point screen:

    vec3 projected = glm::project(original, model, projection, viewport); 

the unproject function opposite:

    vec3 unprojected = glm::unproject(projected, model, projection, viewport); 

you can see these 2 functions 1 inverse of other:

    cout << original.x << " " << original.y << " " << original.z << endl;     cout << projected.x << " " << projected.y << " " << projected.z << endl;     cout << unprojected.x << " " << unprojected.y << " " << unprojected.z << endl;      return 0; } 

mathematically, happening behind scenes: point original in object space projected screen space multiplying 4 matrices model, view, projection , viewport matrix:

projected = viewport * projection * view * model * original 

while opposite transformation, looking for, essentially:

unprojected = (viewport * projection * view * model)^-1 * projected 

full code

#include <iostream> #include <glm/vec3.hpp> #include <glm/mat4x4.hpp> #include <glm/gtx/transform.hpp> #include <glm/gtc/matrix_transform.hpp>  using namespace std; using namespace glm;  int main(int argc, char *argv[]) {     vec3 original(1.0f, -2.0f, 3.0f);      mat4 model = translate(mat4(1.0f), vec3(0.0f, 0.0f, -10.0f));     mat4 projection = frustum(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 100.0f);     vec4 viewport(0.0f, 0.0f, 640.0f, 360.0f);      vec3 projected = glm::project(original, model, projection, viewport);     vec3 unprojected = glm::unproject(projected, model, projection, viewport);      cout << original.x << " " << original.y << " " << original.z << endl;     cout << projected.x << " " << projected.y << " " << projected.z << endl;     cout << unprojected.x << " " << unprojected.y << " " << unprojected.z << endl;      return 0; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -