VCAlfox 148 Report post Posted September 9, 2014 Hello, this is my problem. I have a camera with an origin point (Vector3 with x, y, z) and its vector3 direction , and a screen width and height. I want to transform each pixel of the screen view in a vector3 in 3d space. I found this code on internet, and adapted to my project: glm::vec3 cd = CamGetDirection(); glm::vec3 cp = CamGetPosition(); glm::vec4 viewport = glm::vec4(0.0f, 0.0f, WINDOW_WIDTH, WINDOW_HEIGHT); glm::mat4 tmpView = getViewMatrix(); glm::mat4 tmpProj = getProjectionMatrix(); glm::vec3 screenPosS = glm::vec3(0.0,0.0,0.0); glm::vec3 screenPosE = glm::vec3((float)WINDOW_WIDTH, (float)WINDOW_HEIGHT,0.0); glm::vec3 wpS = glm::unProject(screenPosS, tmpView, tmpProj, viewport); glm::vec3 wpE = glm::unProject(screenPosE, tmpView, tmpProj, viewport); CamGetDirection() give me the vec3 direction of the camera CamGetPosition() of course give me the vec3 position of the camera getViewMatrix() give me the view matrix and this is the code: ViewMatrix = glm::lookAt( position, // Camera is here position+direction, // and looks here : at the same position, plus "direction" up // Head is up (set to 0,-1,0 to look upside-down) ); getProjectionMatrix() is this: ProjectionMatrix = glm::perspective(FoV /* FoV is 45.0f degree */, 4.0f / 3.0f, 0.1f, 100.0f); glm::vec3 screenPosS and glm::vec3 screenPosE are two vec3 of the first pixel and last pixel of the screen view WINDOW_WIDTH and WINDOW_HEIGHT are my screen project setting in this case 1024 and 768 It seems working but I'm not sure on the numbers, I printed this: printf_s("CDir: %f %f %f\n",cd.x,cd.y,cd.z); printf_s("CPos: %f %f %f\n",cp.x,cp.y,cp.z); printf_s("WPStart: %f %f %f\n",wpS.x,wpS.y,wpS.z); printf_s("WPEnd: %f %f %f\n",wpE.x,wpE.y,wpE.z); and the output is: [attachment=23532:out.jpg] Can you confirm if this code works? Thanks 0 Share this post Link to post Share on other sites
KulSeran 3267 Report post Posted September 9, 2014 Honestly, the fastest way to see if this type of thing is working, is to just draw it. Store the unprojected vectors and draw them as lines, then fly around and look at the lines you drew to see if it looks right. But if you want to hand-check this. Pick matricies that are easier to hand-check. make CDir <0, 0, 1> make CPos <0, 0, 0> make ViewMatrix the identity matrix. make ProjectionMatrix the identity matrix. Then confirm that your lines are what you expected. 1 Share this post Link to post Share on other sites
VCAlfox 148 Report post Posted September 9, 2014 (edited) Hello KulSeran, thanks for reply I changed my code as you suggest: glm::vec3 cd = glm::vec3(0.0,0.0,1.0);//CamGetDirection(); glm::vec3 cp = glm::vec3(0.0,0.0,0.0);//CamGetPosition(); glm::vec4 viewport = glm::vec4(0.0f, 0.0f, WINDOW_WIDTH, WINDOW_HEIGHT); glm::mat4 tmpView = glm::mat4(1.0f);//getViewMatrix(); glm::mat4 tmpProj = glm::mat4(1.0f);//getProjectionMatrix(); glm::vec3 screenPosS = glm::vec3(0.0,0.0,0.0); glm::vec3 screenPosE = glm::vec3((float)WINDOW_WIDTH, (float)WINDOW_HEIGHT,0.0); glm::vec3 screenPosM = glm::vec3((float)WINDOW_WIDTH/2, (float)WINDOW_HEIGHT/2,0.0); glm::vec3 wpS = glm::unProject(screenPosS, tmpView, tmpProj, viewport); glm::vec3 wpE = glm::unProject(screenPosE, tmpView, tmpProj, viewport); glm::vec3 wpM = glm::unProject(screenPosM, tmpView, tmpProj, viewport); printf_s("CDir: %f %f %f\n",cd.x,cd.y,cd.z); printf_s("CPos: %f %f %f\n",cp.x,cp.y,cp.z); printf_s("WPStart: %f %f %f\n",wpS.x,wpS.y,wpS.z); printf_s("WPEnd: %f %f %f\n",wpE.x,wpE.y,wpE.z); printf_s("WMid: %f %f %f\n",wpM.x,wpM.y,wpM.z); I also added the middle screen position glm::vec3 screenPosM = glm::vec3((float)WINDOW_WIDTH/2, (float)WINDOW_HEIGHT/2,0.0); and the output is: [attachment=23534:out.jpg] so I think it works. Just a doubt on the Z axis...is normal it's -1? Edited September 10, 2014 by Alfox 0 Share this post Link to post Share on other sites
dpadam450 2357 Report post Posted September 10, 2014 Yes. postive z (0,0,1) is pointing from your montiro towards your head. (0,0,-1) is from head to monitor. So you start looking down -1. 1 Share this post Link to post Share on other sites
VCAlfox 148 Report post Posted September 10, 2014 thanks a lot! 0 Share this post Link to post Share on other sites