Code Monkey home page Code Monkey logo

Comments (3)

turanszkij avatar turanszkij commented on May 24, 2024 1

You can use XMVector3Project: https://learn.microsoft.com/en-us/windows/win32/api/directxmath/nf-directxmath-xmvector3project

from wickedengine.

turanszkij avatar turanszkij commented on May 24, 2024 1

Can you offset your (0,0,0) farther from camera, like at (0,0,1) for example? Because might be that camera near clip plane in projection matrix is causing the unexpected values, because your point is outside the near plane.

Btw this XMVector3Project function is just a helper over multiplying your world space point with the camera's viewProjection matrix. So you can also try this:

XMVECTOR worldpos = XMVectorSet(0,0,1);
XMVECTOR screenpos = XMVector3TransformCoord(worldpos, camera.GetViewProjection()); // to normalized device coordinates in [-1,1] range
screenpos = screenpos * XMVectorSet(0.5f, -0.5f, 1, 1) + XMVectorSet(0.5f, 0.5f, 0, 0); // [-1,1] -> [0,1] range and flip on Y
screenpos = screenpos * XMVectorSet(3840, 2160, 1, 1); // [0,1] range to pixel coordinates

from wickedengine.

docstrang avatar docstrang commented on May 24, 2024

Thank you, it's probably my bad knowledge or I'm not configuring it correctly in the viewport, I don't know if it's the aspect ratio or the dpi but the result on the y axis is not correct I think, I'll show you the code and the result obtained and desired.

// Resolution -> width= 3840 --- height=2160 ---- dpi=144

Result obtained with input 3d pos (0,0,0) -> x=1920 | y=1454.122925 | z=0.009980
Result desired x=1920 | y=1080

Code used:
`
Viewport v;
v.width = GetInternalResolution().x;
v.height = GetInternalResolution().y;
v.min_depth = 0.0f;
v.max_depth = 1.0f;
v.top_left_x = 0.0f;
v.top_left_y = 0.0f;

EditorScene& e = GetCurrentEditorScene();

XMFLOAT3 in_v3 = XMFLOAT3(0.0f, 0.0f, 0.0f);
//XMVECTOR r_v = XMVector3Project(XMLoadFloat3(&in_v3), v.top_left_x, v.top_left_y, v.width, v.height, v.min_depth, v.max_depth, e.scene.camera.GetProjection(), e.scene.camera.GetView(), XMLoadFloat4x4(&e.camera_transform.world)));
XMVECTOR r_v = XMVector3Project(XMLoadFloat3(&in_v3), v.top_left_x, v.top_left_y, v.width, v.height, v.min_depth, v.max_depth, e.scene.camera.GetProjection(), e.scene.camera.GetView(), XMMatrixIdentity());

XMFLOAT3 r_pos;
XMStoreFloat3(&r_pos, r_v);`

I'm probably missing some dpi or aspect ratio calculation over z value, I've tried with several cameras and instead of using an identity matrix with the camera transformation world and I always get the same result.

from wickedengine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.