Oh and where does the Lens value 74.51911163330078 come from? It is used to calculate lens radius for depth of field. Is it a constant?
Code: Select all
static float proj[16];
glMatrixMode(GL_PROJECTION);
auto _proj = perspective(radians(fov), cam_w / cam_h, 0.05f, 500.0f);
glPushMatrix();
glLoadIdentity();
glMultMatrixf(&_proj[0][0]);
glGetFloatv(GL_PROJECTION_MATRIX, proj);
glPopMatrix();
const double _nearScale = 1.0;
const double _near = proj[11] / (proj[10] - 1.0);
const double nLeft = _near * _nearScale *(proj[2] - 1.0) / proj[0];
const double nRight = _near * _nearScale *(1.0 + proj[2]) / proj[0];
const double nTop = _near * _nearScale *(1.0 + proj[6]) / proj[5];
const double nBottom = _near * _nearScale *(proj[6] - 1.0) / proj[5];
glMatrixMode(GL_MODELVIEW);
glLineStipple(1, 0xAAAA);
glLineWidth(2.0);
glPushMatrix();
glMultMatrixf(&inverse(viewMat)[0][0]);
glBegin(GL_LINES);
glColor3f(1.f, 1.f, 1.f);
glVertex3f(nLeft, nBottom, -_near);
glVertex3f(nRight, nBottom, -_near);
glVertex3f(nRight, nTop, -_near);
glVertex3f(nLeft, nTop, -_near);
glVertex3f(nLeft, nTop, -_near);
glVertex3f(nLeft, nBottom, -_near);
glVertex3f(nRight, nTop, -_near);
glVertex3f(nRight, nBottom, -_near);
glEnd();
glPopMatrix();