Renderer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Renderer/Renderer.h>
  6. Renderer::~Renderer()
  7. {
  8. if (mWindow != nullptr)
  9. mWindow->SetWindowResizeListener({});
  10. }
  11. void Renderer::Initialize(ApplicationWindow *inWindow)
  12. {
  13. // Store window
  14. mWindow = inWindow;
  15. mWindow->SetWindowResizeListener([this]() { OnWindowResize(); });
  16. }
  17. static Mat44 sPerspectiveInfiniteReverseZ(float inFovY, float inAspect, float inNear, float inYSign)
  18. {
  19. float height = 1.0f / Tan(0.5f * inFovY);
  20. float width = height / inAspect;
  21. return Mat44(Vec4(width, 0.0f, 0.0f, 0.0f), Vec4(0.0f, inYSign * height, 0.0f, 0.0f), Vec4(0.0f, 0.0f, 0.0f, -1.0f), Vec4(0.0f, 0.0f, inNear, 0.0f));
  22. }
  23. void Renderer::BeginFrame(const CameraState &inCamera, float inWorldScale)
  24. {
  25. // Mark that we're in the frame
  26. JPH_ASSERT(!mInFrame);
  27. mInFrame = true;
  28. // Store state
  29. mCameraState = inCamera;
  30. // Light properties
  31. Vec3 light_pos = inWorldScale * Vec3(250, 250, 250);
  32. Vec3 light_tgt = Vec3::sZero();
  33. Vec3 light_up = Vec3(0, 1, 0);
  34. Vec3 light_fwd = (light_tgt - light_pos).Normalized();
  35. float light_fov = DegreesToRadians(20.0f);
  36. float light_near = 1.0f;
  37. // Camera properties
  38. Vec3 cam_pos = Vec3(inCamera.mPos - mBaseOffset);
  39. float camera_fovy = inCamera.mFOVY;
  40. float camera_aspect = static_cast<float>(mWindow->GetWindowWidth()) / mWindow->GetWindowHeight();
  41. float camera_fovx = 2.0f * ATan(camera_aspect * Tan(0.5f * camera_fovy));
  42. float camera_near = 0.01f * inWorldScale;
  43. // Calculate camera frustum
  44. mCameraFrustum = Frustum(cam_pos, inCamera.mForward, inCamera.mUp, camera_fovx, camera_fovy, camera_near);
  45. // Calculate light frustum
  46. mLightFrustum = Frustum(light_pos, light_fwd, light_up, light_fov, light_fov, light_near);
  47. // Camera projection and view
  48. mVSBuffer.mProjection = sPerspectiveInfiniteReverseZ(camera_fovy, camera_aspect, camera_near, mPerspectiveYSign);
  49. Vec3 tgt = cam_pos + inCamera.mForward;
  50. mVSBuffer.mView = Mat44::sLookAt(cam_pos, tgt, inCamera.mUp);
  51. // Light projection and view
  52. mVSBuffer.mLightProjection = sPerspectiveInfiniteReverseZ(light_fov, 1.0f, light_near, mPerspectiveYSign);
  53. mVSBuffer.mLightView = Mat44::sLookAt(light_pos, light_tgt, light_up);
  54. // Camera ortho projection and view
  55. mVSBufferOrtho.mProjection = Mat44(Vec4(2.0f / mWindow->GetWindowWidth(), 0.0f, 0.0f, 0.0f), Vec4(0.0f, -mPerspectiveYSign * 2.0f / mWindow->GetWindowHeight(), 0.0f, 0.0f), Vec4(0.0f, 0.0f, -1.0f, 0.0f), Vec4(-1.0f, mPerspectiveYSign * 1.0f, 0.0f, 1.0f));
  56. mVSBufferOrtho.mView = Mat44::sIdentity();
  57. // Light projection and view are unused in ortho mode
  58. mVSBufferOrtho.mLightView = Mat44::sIdentity();
  59. mVSBufferOrtho.mLightProjection = Mat44::sIdentity();
  60. // Set constants for pixel shader
  61. mPSBuffer.mCameraPos = Vec4(cam_pos, 0);
  62. mPSBuffer.mLightPos = Vec4(light_pos, 0);
  63. }
  64. void Renderer::EndFrame()
  65. {
  66. // Mark that we're no longer in the frame
  67. JPH_ASSERT(mInFrame);
  68. mInFrame = false;
  69. }