Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Wm4::Matrix4f calcProjectionMatrix (Wm4::Vector3f eyePos, int screenIndex, float znear, float zfar)
- {
- // Get coordinates of screen corners
- Wm4::Vector3f topLeft = screenCoordinates[screenIndex-1][0];
- Wm4::Vector3f topRight = screenCoordinates[screenIndex-1][1];
- Wm4::Vector3f bottomLeft = screenCoordinates[screenIndex-1][2];
- Wm4::Vector3f bottomRight = screenCoordinates[screenIndex-1][3];
- // Support vectors of Projection-plane
- Wm4::Vector3f support1 = topRight - topLeft;
- Wm4::Vector3f support2 = bottomLeft - topLeft;
- // Normal on Projection-plane
- Wm4::Vector3f normal = support2.Cross (support1);
- normal.Normalize();
- // Distance Screen/(0,0,0)
- float distance = topLeft.Dot(normal);
- // Distance Screen/Eye Position
- float eyeDistance = eyePos.Dot(normal) - distance;
- // Look-At-Point
- Wm4::Vector3f atPoint = eyePos - normal * eyeDistance;
- // Vector from topRight to topLeft
- Wm4::Vector3f tr_tl = topLeft-topRight;
- float tr_tl_length = tr_tl.Length();
- // Distance LookAt Point <-> right edge of Frustum on the Projection plane
- // calculated by projection of Vector topRight->atPoint onto the Vector
- // topRight->topLeft
- float rightP = tr_tl.Dot(atPoint-topRight) / tr_tl_length;
- // Distance LookAt Point <-> left edge of frustum
- float leftP = rightP - tr_tl_length;
- // Vector from topLeft to bottomLeft
- Wm4::Vector3f tl_bl = bottomLeft-topLeft;
- float tl_bl_length = tl_bl.Length();
- // Distance LookAt Point <-> upper edge of Frustum on the Projection plane
- // calculated by projection of Vector topLeft->atPoint onto the Vector
- // topLeft->bottomLeft
- float topP = tl_bl.Dot(atPoint-topLeft) / tl_bl_length;
- // Distance LookAt Point <-> bottom edge of frustum
- float bottomP = topP - tl_bl_length;
- // Distance LookAt Point<->right edge of frustum on near clipping plane
- float right = rightP * znear / eyeDistance;
- // Distance LookAt Point<->left edge of frustum on near clipping plane
- float left = leftP * znear / eyeDistance;;
- // Distance LookAt Point<->top edge of frustum on near clipping plane
- float top = topP * znear / eyeDistance;;
- // Distance LookAt Point<->bottom edge of frustum on near clipping plane
- float bottom = bottomP * znear / eyeDistance;;
- // Construction of Projection matrix
- // Basically a glFrustum call with left, right, bottom, top, znear
- // and zfar as Parameters
- //Right handed
- Wm4::Matrix4f m(
- 2*znear/(right-left), 0.0f, (right+left)/(right-left), 0.0f,
- 0.0f, 2*znear/(top-bottom), (top+bottom)/(top-bottom), 0.0f,
- 0.0f, 0.0f, -(zfar+znear)/(zfar-znear), -2*zfar*znear/(zfar-znear),
- 0.0f, 0.0f, -1.0f, 0.0f
- );
- m = m.Transpose();
- return m;
- }
- Wm4::Matrix4f getModelview(const Wm4::Vector3f& eyePos, int screenIndex)
- {
- Wm4::Vector3f topLeft = screenCoordinates[screenIndex-1][0];
- Wm4::Vector3f topRight = screenCoordinates[screenIndex-1][1];
- Wm4::Vector3f bottomLeft = screenCoordinates[screenIndex-1][2];
- Wm4::Vector3f bottomRight = screenCoordinates[screenIndex-1][3];
- Wm4::Vector3f support1 = topRight - topLeft;
- Wm4::Vector3f support2 = bottomLeft - topLeft;
- Wm4::Vector3f normal = support2.Cross (support1);
- normal.Normalize();
- float distance = topLeft.Dot(normal);
- float eyeDistance = eyePos.Dot(normal) - distance;
- // LookAt point
- Wm4::Vector3f atPoint = eyePos - normal * eyeDistance;
- // Up-Vector
- Wm4::Vector3f upVector = topLeft - bottomLeft;
- upVector.Normalize();
- // Construction of ModelviewMatrix
- // Basically gluLookAt()-Function wth eyePos, atPoint and upVector
- // View Vector
- Wm4::Vector3f f = atPoint - eyePos;
- f.Normalize();
- // Supportvector orthogonal to up- and viewvector
- Wm4::Vector3f s = f.Cross(upVector);
- // Upvector
- Wm4::Vector3f u = s.Cross(f);
- Wm4::Matrix4f m(
- s[0], s[1], s[2], -(s[0]*eyePos[0] + s[1]*eyePos[1] + s[2]*eyePos[2]),
- u[0], u[1], u[2], -(u[0]*eyePos[0] + u[1]*eyePos[1] + u[2]*eyePos[2]),
- -f[0], -f[1], -f[2], (f[0]*eyePos[0] + f[1]*eyePos[1] + f[2]*eyePos[2]),
- 0.0f, 0.0f, 0.0f, 1.0f
- );
- m = m.Transpose();
- return m;
- }
Advertisement
Add Comment
Please, Sign In to add comment