Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Screen coordinates from a 3D Point? Opengl

Tags:

c++

opengl

I've seen alot of Screen-Coordinate to world coordinate posts, but have not seen the opposite. Is there an easy way of getting screen coordinates from a 3d point, from any angle I view it from? I'm using C++ and opengl

like image 641
Aero Avatar asked Sep 25 '10 04:09

Aero


1 Answers

erjot's answer is incomplete.

There are 2 additional things to take into account.

  1. with homogeneous = P . M . world, the homogeneous vector is a 4-d vector in homogeneous space. In particular, to bring it back to the [-1:1]^3 cube, you need to divide its first 3 coordinates by homogeneous.w. cube.x = homogeneous.x / homogeneous.w (what some people call the projection divide).
  2. You then need to transform the [-1:1]^3 cube to window coordinates by applying the Viewport transformation to it. window.x = viewport.x + viewport.width * (cube.x+1)/2

In case you care about the final Z, the transformation is not going through viewport, but DepthRange instead.

like image 186
Bahbar Avatar answered Sep 24 '22 15:09

Bahbar