opengl – convert world to screen coordinates with camera position and camera angle

Heyho 🙂

I’m trying to convert world coordinates to screen coordinates. I have available: fov, screen width, screen height, camera position, camera angle and obviously the position of the object in world space.

This is what I tried:

glm::vec3 world_to_screen(glm::vec3 pos,
                          glm::vec3 cam_angle,
                          glm::vec3 cam_pos) {
    glm::mat4 projection = glm::perspective(
        glm::radians(FOV), (float)SCREEN_W / (float)SCREEN_H, NEAR, FAR);

    glm::mat4 model(1.0);

    model = glm::translate(model, cam_pos);

    model = glm::rotate(model, cam_angle.x, glm::vec3(1.0f, 0.0f, 0.0f));
    model = glm::rotate(model, cam_angle.y, glm::vec3(0.0f, 1.0f, 0.0f));
    model = glm::rotate(model, cam_angle.z, glm::vec3(0.0f, 0.0f, 1.0f));

    glm::mat4 view = glm::inverse(model);

    glm::mat4 modelview = view * model;

    return glm::project(pos, modelview, projection,
                        glm::vec4(0, 0, SCREEN_W, SCREEN_H));

However it is not working, the output screen coords are over 30000 (I don’t have a 30k monitor…) and I’m not sure what I did wrong.
There is a correlation though, sometimes the screen coords happen to be in my screen (I draw an indicator at the position to see if I did it right) and if the object moves the indicator also moves with (kinda) the same speed etc.

Help is very much appreciated 🙂 Thanks a lot in advance!