@@ -6449,10 +6449,6 @@ bool afCamera::createFromAttribs(afCameraAttributes *a_attribs)
64496449 setName (a_attribs->m_identificationAttribs .m_name );
64506450 setNamespace (a_attribs->m_identificationAttribs .m_namespace );
64516451
6452- m_camPos << a_attribs->m_kinematicAttribs .m_location .getPosition ();
6453- m_camLookAt << a_attribs->m_lookAt ;
6454- m_camUp << a_attribs->m_up ;
6455-
64566452 setOrthographic (a_attribs->m_orthographic );
64576453
64586454 if (a_attribs->m_stereo ){
@@ -6481,11 +6477,53 @@ bool afCamera::createFromAttribs(afCameraAttributes *a_attribs)
64816477 }
64826478
64836479 // ////////////////////////////////////////////////////////////////////////////////////
6480+ m_camPos << a_attribs->m_kinematicAttribs .m_location .getPosition ();
6481+ m_camLookAt << a_attribs->m_lookAt ;
6482+ m_camUp << a_attribs->m_up ;
64846483 // position and orient the camera
64856484 setView (m_camPos, m_camLookAt, m_camUp);
6485+ m_camera->setClippingPlanes (a_attribs->m_nearPlane , a_attribs->m_farPlane );
6486+
6487+ if (a_attribs->m_intrinsics .m_defined ){
6488+ const int rows = 4 , cols = 4 ;
6489+ for (int r = 0 ; r < rows ; r++){
6490+ for (int c = 0 ; c < cols ; c++){
6491+ getInternalCamera ()->m_projectionMatrix (r, c) = 0.0 ;
6492+ }
6493+ }
6494+ computeProjectionFromIntrinsics (&a_attribs->m_intrinsics ,
6495+ a_attribs->m_windowResolution .m_width ,
6496+ a_attribs->m_windowResolution .m_height ,
6497+ a_attribs->m_nearPlane ,
6498+ a_attribs->m_farPlane );
6499+
6500+ cerr << " INFO! USING CAMERA INTRINSICS FOR " << getQualifiedName () << endl;
6501+ cerr << " \t Focal Length(x, y): (" << a_attribs->m_intrinsics .m_fx << " , " << a_attribs->m_intrinsics .m_fy << " )" << endl;
6502+ cerr << " \t Principal Offset (x, y): (" << a_attribs->m_intrinsics .m_cx << " , " << a_attribs->m_intrinsics .m_cy << " )" << endl;
6503+ cerr << " \t Shear: " << a_attribs->m_intrinsics .m_s << endl;
6504+ }
6505+ else if (a_attribs->m_useCustomProjectionMatrix ){
6506+ getInternalCamera ()->m_useCustomProjectionMatrix = true ;
6507+ const int rows = 4 , cols = 4 ;
6508+ for (int r = 0 ; r < rows ; r++){
6509+ for (int c = 0 ; c < cols ; c++){
6510+ getInternalCamera ()->m_projectionMatrix (r, c) = a_attribs->m_projectionMatrix [r][c];
6511+ }
6512+ }
6513+
6514+ cerr << " INFO! USING CUSTOM PROJECT MATRIX FOR CAMERA: " << getQualifiedName () << endl;
6515+ for (int r = 0 ; r < rows ; r++){
6516+ cerr << " \t [" ;
6517+ for (int c = 0 ; c < cols ; c++){
6518+ cerr << a_attribs->m_projectionMatrix [r][c] << " " ;
6519+ }
6520+ cerr << " \t ]" << endl;
6521+ }
6522+
6523+ }
6524+
64866525 m_initialTransform = getLocalTransform ();
64876526 // set the near and far clipping planes of the camera
6488- m_camera->setClippingPlanes (a_attribs->m_nearPlane , a_attribs->m_farPlane );
64896527
64906528 // set stereo mode
64916529 m_camera->setStereoMode (m_stereoMode);
@@ -6593,15 +6631,18 @@ bool afCamera::createWindow()
65936631
65946632 m_monitor = m_monitors[m_monitorNumber];
65956633
6596- // compute desired size of window
65976634 const GLFWvidmode* mode = glfwGetVideoMode (m_monitor);
6598- int w = 0.8 * mode->width ;
6599- int h = 0.5 * mode->height ;
6600- int x = 0.5 * (mode->width - w);
6601- int y = 0.5 * (mode->height - h);
6635+ // compute desired size of window
6636+ afCameraAttributes* camAttribs = (afCameraAttributes*)getAttributes ();
6637+ if (camAttribs->m_windowResolution .m_defined ){
6638+ m_width = camAttribs->m_windowResolution .m_width ;
6639+ m_height = camAttribs->m_windowResolution .m_height ;
66026640
6603- m_width = w;
6604- m_height = h;
6641+ }
6642+ else {
6643+ m_width = 0.8 * mode->width ;
6644+ m_height = 0.5 * mode->height ;
6645+ }
66056646
66066647 if (getVisibleFlag () == false ){
66076648 cerr << " INFO! CAMERA \" " << m_name << " \" SET TO INVISIBLE. THEREFORE IT IS ONLY VIEWABLE"
@@ -6612,7 +6653,16 @@ bool afCamera::createWindow()
66126653 glfwWindowHint (GLFW_VISIBLE, GLFW_TRUE);
66136654 }
66146655
6615- m_window = glfwCreateWindow (w, h, window_name.c_str (), nullptr , s_mainWindow);
6656+ int screenOriginX, screenOriginY;
6657+ glfwGetMonitorPos (m_monitor, &screenOriginX, &screenOriginY);
6658+
6659+ int windowOriginX = 0.5 * (mode->width - m_width);
6660+ int windowOriginY = 0.5 * (mode->height - m_height);
6661+
6662+ m_window = glfwCreateWindow (m_width, m_height, window_name.c_str (), nullptr , s_mainWindow);
6663+ // set position of window
6664+ glfwSetWindowPos (m_window, screenOriginX + windowOriginX, screenOriginY + windowOriginY);
6665+
66166666 if (s_windowIdx == 0 ){
66176667 s_mainWindow = m_window;
66186668 }
@@ -6635,14 +6685,6 @@ bool afCamera::createWindow()
66356685 // get width and height of window
66366686 glfwGetWindowSize (m_window, &m_width, &m_height);
66376687
6638-
6639- int xpos, ypos;
6640- glfwGetMonitorPos (m_monitor, &xpos, &ypos);
6641- x += xpos; y += ypos;
6642-
6643- // set position of window
6644- glfwSetWindowPos (m_window, x, y);
6645-
66466688// glfwSetWindowMonitor(m_window, m_monitor, m_win_x, m_win_y, m_width, m_height, mode->refreshRate);
66476689
66486690 // initialize GLEW library
@@ -6705,6 +6747,32 @@ bool afCamera::assignWindowCallbacks(afCameraWindowCallBacks *a_callbacks)
67056747 return true ;
67066748}
67076749
6750+ bool afCamera::computeProjectionFromIntrinsics (const afCameraIntrinsics *a_attribs, double a_width, double a_height, double a_nearPlane, double a_farPlane){
6751+ double fx = a_attribs->m_fx ;
6752+ double fy = a_attribs->m_fy ;
6753+ double cx = a_attribs->m_cx ;
6754+ double cy = a_attribs->m_cy ;
6755+ double s = a_attribs->m_s ;
6756+ double W = a_width;
6757+ double H = a_height;
6758+ double image_center_x = W/2 .;
6759+ double image_center_y = H/2 .;
6760+ double n = a_nearPlane;
6761+ double f = a_farPlane;
6762+
6763+ getInternalCamera ()->m_useCustomProjectionMatrix = true ;
6764+ getInternalCamera ()->m_projectionMatrix (0 ,0 ) = (2 * fx) / W;
6765+ getInternalCamera ()->m_projectionMatrix (0 ,1 ) = (2 * s) / W;
6766+ getInternalCamera ()->m_projectionMatrix (0 ,2 ) = (W - 2 * cx) / W;
6767+ getInternalCamera ()->m_projectionMatrix (1 ,1 ) = (2 * fy) / H;
6768+ getInternalCamera ()->m_projectionMatrix (1 ,2 ) = (-H + 2 * cy) / H;
6769+ getInternalCamera ()->m_projectionMatrix (2 ,2 ) = (-f - n) / (f - n);
6770+ getInternalCamera ()->m_projectionMatrix (2 ,3 ) = (-2 * f * n) / (f - n);
6771+ getInternalCamera ()->m_projectionMatrix (3 ,2 ) = -1.0 ;
6772+
6773+ return true ;
6774+ }
6775+
67086776// /
67096777// / \brief afCamera::publishDepthCPUBased
67106778// /
@@ -6893,6 +6961,11 @@ void afCamera::render(afRenderOptions &options)
68936961 // get width and height of window
68946962 glfwGetFramebufferSize (m_window, &m_width, &m_height);
68956963
6964+ afCameraIntrinsics intrinsics = (((afCameraAttributes*)getAttributes ())->m_intrinsics );
6965+ if (intrinsics.m_defined ){
6966+ computeProjectionFromIntrinsics (&intrinsics, m_width, m_height, getInternalCamera ()->getNearClippingPlane (), getInternalCamera ()->getFarClippingPlane ());
6967+ }
6968+
68966969 // Update the Labels in a separate sub-routine
68976970 if (options.m_updateLabels && !m_publishDepth && !m_publishImage){
68986971 updateLabels (options);
@@ -6960,6 +7033,11 @@ void afCamera::renderSkyBox(){
69607033void afCamera::renderFrameBuffer (){
69617034 if (m_publishImage || m_publishDepth){
69627035
7036+ afCameraIntrinsics intrinsics = (((afCameraAttributes*)getAttributes ())->m_intrinsics );
7037+ if (intrinsics.m_defined ){
7038+ computeProjectionFromIntrinsics (&intrinsics, m_frameBuffer->getWidth (), m_frameBuffer->getHeight (), getInternalCamera ()->getNearClippingPlane (), getInternalCamera ()->getFarClippingPlane ());
7039+ }
7040+
69637041 activatePreProcessingShaders ();
69647042
69657043 m_frameBuffer->renderView ();
0 commit comments