/// <summary> /// Build a look at view matrix. /// </summary> /// <param name="eye">The eye.</param> /// <param name="center">The center.</param> /// <param name="up">Up.</param> /// <returns></returns> public static mat4 lookAt(vec3 eye, vec3 center, vec3 up) { vec3 forward = (center - eye).normalize(); vec3 right = forward.cross(up).normalize(); vec3 standardUp = right.cross(forward); mat4 Result = new mat4(1); // Left (X) Axis Result[0, 0] = right.x; Result[0, 1] = standardUp.x; Result[0, 2] = -forward.x; // Up (Y) Axis Result[1, 0] = right.y; Result[1, 1] = standardUp.y; Result[1, 2] = -forward.y; // Forward (Z) Axis Result[2, 0] = right.z; Result[2, 1] = standardUp.z; Result[2, 2] = -forward.z; // Translation Result[3, 0] = -right.dot(eye); // dot(s, eye); Result[3, 1] = -standardUp.dot(eye); // dot(u, eye); Result[3, 2] = forward.dot(eye); // dot(f, eye); return(Result); }
/// <summary> /// Build a look at view matrix. /// </summary> /// <param name="eye">The eye.</param> /// <param name="center">The center.</param> /// <param name="up">Up.</param> /// <returns></returns> public static mat4 lookAt(vec3 eye, vec3 center, vec3 up) { vec3 f = (center - eye); f.Normalize(); vec3 s = f.cross(up); s.Normalize(); vec3 u = s.cross(f); mat4 Result = new mat4(1); Result[0, 0] = s.x; Result[1, 0] = s.y; Result[2, 0] = s.z; Result[0, 1] = u.x; Result[1, 1] = u.y; Result[2, 1] = u.z; Result[0, 2] = -f.x; Result[1, 2] = -f.y; Result[2, 2] = -f.z; Result[3, 0] = -s.dot(eye); // dot(s, eye); Result[3, 1] = -u.dot(eye); // dot(u, eye); Result[3, 2] = f.dot(eye); // dot(f, eye); return(Result); }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public void MouseMove(int x, int y) { if (this.MouseDownFlag) { IViewCamera camera = this.Camera; if (camera == null) { return; } vec3 back = this.back; vec3 right = this.right; vec3 up = this.up; Size bound = this.bound; Point downPosition = this.downPosition; { float deltaX = -horizontalRotationFactor * (x - downPosition.X) / bound.Width; float cos = (float)Math.Cos(deltaX); float sin = (float)Math.Sin(deltaX); vec3 newBack = new vec3( back.x * cos + right.x * sin, back.y * cos + right.y * sin, back.z * cos + right.z * sin); back = newBack; right = up.cross(back); back.Normalize(); right.Normalize(); } { float deltaY = verticalRotationFactor * (y - downPosition.Y) / bound.Height; float cos = (float)Math.Cos(deltaY); float sin = (float)Math.Sin(deltaY); vec3 newBack = new vec3( back.x * cos + up.x * sin, back.y * cos + up.y * sin, back.z * cos + up.z * sin); back = newBack; up = back.cross(right); back.Normalize(); up.Normalize(); } camera.Position = camera.Target + back * (float)((camera.Position - camera.Target).Magnitude()); camera.UpVector = up; this.back = back; this.right = right; this.up = up; this.downPosition.X = x; this.downPosition.Y = y; } }