Sign in to follow this  

screen view to 3d space

This topic is 1190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by Alfox

Share this post


Link to post
Share on other sites

This topic is 1190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this