Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

3D world coords to 2D screen coords



  • Hello,

    I'm currently developing a raytracer with OpenGL and the QGLViewer class.
    I want to transform any given vertex defined in the world coordinate system into the screen coordinate system.
    To do so, I saw in the documentation that there is a method called projectedCoordinatesOf but that method gives me wrong some outputs.

    Here is the code I'm using:

            Vec3 vertex = points_localCoords[index];
            std::cout << "vertex local coords = (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
            vertex = Vec3(node->transfo * Vec4(vertex, 1.0f)); // world coordinates
            std::cout << "vertex world coords = (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
            qglviewer::Vec vertex_screen = rt.sv->camera()->projectedCoordinatesOf(qglviewer::Vec(vertex));
            Vec3 vert(vertex_screen.x, vertex_screen.y, vertex_screen.z); // screen coordinates
            std::cout << "vertex screen coords = (" << vert.x << ", " << vert.y << ", " << vert.z << ")" << std::endl;
    

    and one of the numerous outputs:

    vertex local coords = (0.999849, 0.999849, 1)
    vertex world coords = (2.99955, 2.99955, 3)
    vertex screen coords = (-194.954, 819.324, 1.00793)

    The negative value for the screen coordinates doesn't make any sense, what am I getting wrong ?


Log in to reply