You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
  1. #include"Camera.h"
  2. #include"Ray.h"
  3. #include<math.h>
  4. Camera::Camera(float fov, float aspect) {
  5. mFOV = fov;
  6. mAspect = aspect;
  7. mNearPlaneWidth = 0.0f;
  8. mNearPlaneHeight = 0.0f;
  9. }
  10. void Camera::LookAt(const Vector3& position, const Vector3& center, const Vector3& up) {
  11. Vector3 camera_to_view_point = center - position;
  12. float distance = camera_to_view_point.Magnitude();
  13. float half_fov = mFOV / 2.0f;
  14. float radian_half_fov = half_fov * 3.14159f / 180.0f;
  15. float half_height = atanf(radian_half_fov)*distance;
  16. float half_width = half_height * mAspect;
  17. mNearPlaneWidth = half_width * 2.0f;
  18. mNearPlaneHeight = half_height * 2.0f;
  19. camera_to_view_point.Normalize();
  20. Vector3 right_direction = Cross(camera_to_view_point, up);
  21. right_direction.Normalize();
  22. Vector3 up_direction = Cross(right_direction, camera_to_view_point);
  23. up_direction.Normalize();
  24. mUVector = right_direction;
  25. mVVector = up_direction;
  26. mUSpanVector = mNearPlaneWidth * mUVector;
  27. mVSpanVector = mNearPlaneHeight * mVVector;
  28. mCenter = center;
  29. mPosition = position;
  30. mUp = up;
  31. }
  32. Ray Camera::GetRay(float u, float v) {
  33. u = u * 2.0f - 1.0f;
  34. v = v * 2.0f - 1.0f;
  35. Vector3 point_on_near_plane = mCenter + 0.5f*u*mUSpanVector + 0.5f*v*mVSpanVector;
  36. return Ray(mPosition, point_on_near_plane - mPosition, Vector3(1.0f, 1.0f, 1.0f));
  37. }