2

How can i convert a camera space vector to world space, given the camera direction vector, and the camera space vector?

All vecs are normalized 3D vectors, and the camera space projection is correct as it displays properly, but ignores camera orientation as its not converted to world space.

std::vector<double> calculate_ray_heading(std::vector<double> input_vector, double fov, std::vector<double> uv) {
    // Calculate the heading of the ray in world space given
    //  -camera vector (world space)
    //  -uv (converted to camera normalized device coordinates)
    //  -fov (ignored for now)

    //convert uv2 to NDC (normalized device coordinates)
    // changes range from [0,-1] to [-1,1]
    std::vector<double> ndc = {
        (2*uv[1]) - 1,
        (2*(1-uv[0])) - 1
    };

    //calculate ray heading in camera space
    std::vector<double> cam_space = normalize_vector(
        {
            1, ndc[0], ndc[1]
        }
    );

    //convert cam space to world space
    //...

    //return world space
    return world_space;
};

Also note i'm aware of my poor datatypes i am in the process of changing everything to proper vec types

6
  • i should note that "input_vector" is the camera direction vector Commented Mar 5, 2025 at 6:36
  • 1
    NOTE : std::vector in C++ is NOT a mathematical vector. Do NOT use it for matrix/vector calculations. Use a math or better 3D graphics library for that. The math you need is explained in many online source. But this might get you started wrt to th e concepts you need : Maths for game development Commented Mar 5, 2025 at 8:07
  • 1
    std::vector is a C++ container, think of it as a resizable array. What you need is a struct or class that models a vector with at least an x,y and z component (and probably a homogeneous component as well). Commented Mar 5, 2025 at 8:28
  • 1
    library for linear algebra in C++: stackoverflow.com/questions/1380371/… and en.wikipedia.org/wiki/Comparison_of_linear_algebra_libraries (tldr: eigen) Commented Mar 5, 2025 at 11:02
  • I believe you may need not just the position of the camera in the world coordinate system, but also the orientation of the camera in world space, e.g. the coordinates of the horizontal vector aligned with the screen and the vertical vector aligned with the screen of the camera (in world space), or something like that. In some cases you may also need the focal distance from the camera's center to the screen... Commented Mar 6, 2025 at 19:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.