1 #pragma once
2 
3 #include "icamera.h"
4 #include "math/frustum.h"
5 #include "Camera.h"
6 
7 #include "view.h"
8 
9 const Matrix4 g_radiant2opengl(
10   0,-1, 0, 0,
11   0, 0, 1, 0,
12  -1, 0, 0, 0,
13   0, 0, 0, 1
14 );
15 
16 const Matrix4 g_opengl2radiant(
17   0, 0,-1, 0,
18  -1, 0, 0, 0,
19   0, 1, 0, 0,
20   0, 0, 0, 1
21 );
22 
projection_for_camera(float near_z,float far_z,float fieldOfView,int width,int height)23 inline Matrix4 projection_for_camera(float near_z, float far_z, float fieldOfView, int width, int height) {
24   const float half_width = static_cast<float>(near_z * tan(degrees_to_radians(fieldOfView * 0.5)));
25   const float half_height = half_width * (static_cast<float>(height) / static_cast<float>(width));
26 
27   return matrix4_frustum(
28     -half_width,
29     half_width,
30     -half_height,
31     half_height,
32     near_z,
33     far_z
34   );
35 }
36 
37 class RadiantCameraView : public CameraView
38 {
39   Camera& m_camera;
40   View* m_view;
41   Callback m_update;
42 public:
RadiantCameraView(Camera & camera,View * view,const Callback & update)43   RadiantCameraView(Camera& camera, View* view, const Callback& update) : m_camera(camera), m_view(view), m_update(update)
44   {
45   }
update()46   void update()
47   {
48     m_view->Construct(m_camera.projection, m_camera.modelview, m_camera.width, m_camera.height);
49     m_update();
50   }
setModelview(const Matrix4 & modelview)51   void setModelview(const Matrix4& modelview)
52   {
53     m_camera.modelview = modelview;
54     matrix4_multiply_by_matrix4(m_camera.modelview, g_radiant2opengl);
55     matrix4_affine_invert(m_camera.modelview);
56     m_camera.updateVectors();
57     update();
58   }
setFieldOfView(float fieldOfView)59   void setFieldOfView(float fieldOfView)
60   {
61     float farClip = m_camera.getFarClipPlane();
62     m_camera.projection = projection_for_camera(farClip / 4096.0f, farClip, fieldOfView, m_camera.width, m_camera.height);
63     update();
64   }
65 };
66