Figure 6: The PerspectiveInit member function

void CScenView::PerspectiveInit()
{
   int i,j,kv;
  
   double vr,sn,cs;
  
   m_WorldToScreenR = m_ScvViewPtZ / 
                      (m_ScvViewPtZ - m_ScvZoffset);
  
   // Offsets, Object position + point position
   // View X,Y,Z offsets
  
   m_ScOffpPosnX = m_ScvXoffset;
   m_ScOffpPosnY = m_ScvYdrop;
   m_ScOffpPosnZ = m_ScvZoffset;
  
   m_ScvAngle = 3.14153 * m_ScvDegAngle / 180.0;
  
   sn = sin(m_ScvAngle);
   cs = cos(m_ScvAngle);
  
   // Set up perspective transformation matrix
  
   for(i=0; i<4; i++){
      for(j=0; j<4; j++){
      
         m_ScPerspective[i][j] = 0.0;
      
         m_ScnObjCoords[j] = 0.0;    // Object coords
      
      }
   }
  
   vr = -1.0 / m_ScvViewPtZ; // View point distance
  
   m_ScPerspective[0][0] = 1.0;  
   m_ScPerspective[1][1] = 1.0;
   m_ScPerspective[2][3] = vr;
   m_ScPerspective[3][3] = 1.0;
  
   m_ScPerspective[0][0] = cs;
   m_ScPerspective[2][0] = sn;
   m_ScPerspective[0][3] = -vr * sn;
   m_ScPerspective[2][3] = vr * cs;
  
   m_ScnObjCoords[3] = 1.0;
  
   m_ObjPosX = 0.0;  // Object posn
   m_ObjPosY = 0.0;
   m_ObjPosZ = 0.0;    
  
   m_ObjPtX = 0.0;  // Set to origin
   m_ObjPtY = 0.0;
   m_ObjPtZ = 0.0;    
  
   // Get screen origin position via world point (0, 0, 0)
   // at normalised screen point (0.2, 0.2)
  
   // m_NormWoX = 0.2;
   // m_NormWoY = 0.2;
  
   m_WorgIy = m_nDeviceYtop +
              int(double(m_nDeviceYsize) * (1.0 - m_NormWoY));
  
   m_WorgIx = m_nDeviceXleft +
              int(double(m_nDeviceXsize) * m_NormWoX);
  
   // Transform the point; only S and T are valid
   PerspectiveTr();
  
   m_WorgSx = m_ScreenS;
   m_WorgTy = m_ScreenT;
  
   // Now DeviceIx and DeviceIy are valid
   // Get the Screen viewport coords
   kv = m_nDeviceXleft;
   m_ScreenXleft = m_WorgSx +
                   double(kv - m_WorgIx) /  m_ScreenGainX;
  
   kv = m_nDeviceXleft + m_nDeviceXsize;   // x right
   m_ScreenXright = m_WorgSx +
                    double(kv - m_WorgIx) /  m_ScreenGainX;
  
   m_ScreenXrange = fabs(m_ScreenXright - m_ScreenXleft);
  
   kv = m_nDeviceYtop;
   m_ScreenYtop = m_WorgTy -
                  double(kv - m_WorgIy) / m_ScreenGainY; 
  
   kv = m_nDeviceYtop + m_nDeviceYsize;   // y bottom
   m_ScreenYbot = m_WorgTy -
                  double(kv - m_WorgIy) / m_ScreenGainY; 
  
   // Get horizon
   m_ObjPtZ = -1000000.0;  // Horizon point coords    
  
   PerspectiveTr();        // transform the point    
   j = m_DeviceIx;
   m_DeviceHorizon = m_DeviceIy;
}