Guest User

Projection/Modelview

a guest
May 13th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. Wm4::Matrix4f calcProjectionMatrix (Wm4::Vector3f eyePos, int screenIndex, float znear, float zfar)
  2. {
  3.   // Get coordinates of screen corners
  4.   Wm4::Vector3f topLeft = screenCoordinates[screenIndex-1][0];
  5.   Wm4::Vector3f topRight = screenCoordinates[screenIndex-1][1];
  6.   Wm4::Vector3f bottomLeft = screenCoordinates[screenIndex-1][2];
  7.   Wm4::Vector3f bottomRight = screenCoordinates[screenIndex-1][3];
  8.  
  9.   // Support vectors of Projection-plane
  10.   Wm4::Vector3f support1 = topRight - topLeft;
  11.   Wm4::Vector3f support2 = bottomLeft - topLeft;
  12.  
  13.   // Normal on Projection-plane
  14.   Wm4::Vector3f normal = support2.Cross (support1);
  15.   normal.Normalize();
  16.  
  17.   // Distance Screen/(0,0,0)
  18.   float distance = topLeft.Dot(normal);
  19.  
  20.   // Distance Screen/Eye Position
  21.   float eyeDistance = eyePos.Dot(normal) - distance;
  22.  
  23.   // Look-At-Point
  24.   Wm4::Vector3f atPoint = eyePos - normal * eyeDistance;
  25.  
  26.   // Vector from topRight to topLeft
  27.   Wm4::Vector3f tr_tl = topLeft-topRight;
  28.   float tr_tl_length = tr_tl.Length();
  29.  
  30.   // Distance LookAt Point <-> right edge of Frustum on the Projection plane
  31.   // calculated by projection of Vector topRight->atPoint onto the Vector
  32.   // topRight->topLeft
  33.   float rightP = tr_tl.Dot(atPoint-topRight) / tr_tl_length;
  34.  
  35.   // Distance LookAt Point <-> left edge of frustum
  36.   float leftP = rightP - tr_tl_length;
  37.  
  38.   // Vector from topLeft to bottomLeft
  39.   Wm4::Vector3f tl_bl = bottomLeft-topLeft;
  40.   float tl_bl_length = tl_bl.Length();
  41.  
  42.   // Distance LookAt Point <-> upper edge of Frustum on the Projection plane
  43.   // calculated by projection of Vector topLeft->atPoint onto the Vector
  44.   // topLeft->bottomLeft
  45.   float topP = tl_bl.Dot(atPoint-topLeft) / tl_bl_length;
  46.  
  47.   // Distance LookAt Point <-> bottom edge of frustum
  48.   float bottomP = topP - tl_bl_length;
  49.  
  50.   // Distance LookAt Point<->right edge of frustum on near clipping plane
  51.   float right = rightP * znear / eyeDistance;
  52.   // Distance LookAt Point<->left edge of frustum on near clipping plane
  53.   float left = leftP * znear / eyeDistance;;
  54.   // Distance LookAt Point<->top edge of frustum on near clipping plane
  55.   float top = topP * znear / eyeDistance;;
  56.   // Distance LookAt Point<->bottom edge of frustum on near clipping plane
  57.   float bottom = bottomP * znear / eyeDistance;;
  58.  
  59.   // Construction of Projection matrix
  60.   // Basically a glFrustum call with left, right, bottom, top, znear
  61.   // and zfar as Parameters
  62.  
  63.   //Right handed
  64.   Wm4::Matrix4f m(
  65.     2*znear/(right-left), 0.0f, (right+left)/(right-left), 0.0f,
  66.     0.0f, 2*znear/(top-bottom), (top+bottom)/(top-bottom), 0.0f,
  67.     0.0f, 0.0f, -(zfar+znear)/(zfar-znear), -2*zfar*znear/(zfar-znear),
  68.     0.0f, 0.0f, -1.0f, 0.0f
  69.   );
  70.  
  71.   m = m.Transpose();
  72.  
  73.   return m;
  74.  
  75. }
  76.  
  77.  
  78. Wm4::Matrix4f getModelview(const Wm4::Vector3f& eyePos, int screenIndex)
  79. {
  80.   Wm4::Vector3f topLeft = screenCoordinates[screenIndex-1][0];
  81.   Wm4::Vector3f topRight = screenCoordinates[screenIndex-1][1];
  82.   Wm4::Vector3f bottomLeft = screenCoordinates[screenIndex-1][2];
  83.   Wm4::Vector3f bottomRight = screenCoordinates[screenIndex-1][3];
  84.  
  85.   Wm4::Vector3f support1 = topRight - topLeft;
  86.   Wm4::Vector3f support2 = bottomLeft - topLeft;
  87.  
  88.   Wm4::Vector3f normal = support2.Cross (support1);
  89.   normal.Normalize();
  90.  
  91.   float distance = topLeft.Dot(normal);
  92.  
  93.   float eyeDistance = eyePos.Dot(normal) - distance;
  94.  
  95.   // LookAt point
  96.   Wm4::Vector3f atPoint = eyePos - normal * eyeDistance;
  97.  
  98.   // Up-Vector
  99.   Wm4::Vector3f upVector = topLeft - bottomLeft;
  100.   upVector.Normalize();
  101.  
  102.   // Construction of ModelviewMatrix
  103.   // Basically gluLookAt()-Function wth eyePos, atPoint and upVector
  104.  
  105.   // View Vector
  106.   Wm4::Vector3f f = atPoint - eyePos;
  107.   f.Normalize();
  108.  
  109.   // Supportvector orthogonal to up- and viewvector
  110.   Wm4::Vector3f s = f.Cross(upVector);
  111.  
  112.   // Upvector
  113.   Wm4::Vector3f u = s.Cross(f);
  114.  
  115.   Wm4::Matrix4f m(
  116.      s[0],  s[1],  s[2], -(s[0]*eyePos[0] + s[1]*eyePos[1] + s[2]*eyePos[2]),
  117.      u[0],  u[1],  u[2], -(u[0]*eyePos[0] + u[1]*eyePos[1] + u[2]*eyePos[2]),
  118.     -f[0], -f[1], -f[2],  (f[0]*eyePos[0] + f[1]*eyePos[1] + f[2]*eyePos[2]),
  119.     0.0f, 0.0f, 0.0f, 1.0f
  120.   );
  121.  
  122.   m = m.Transpose();
  123.  
  124.   return m;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment