1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * @file camera.hh
|
---|
9 | * @author Kornel Kisielewicz epyon@chaosforge.org
|
---|
10 | * @brief Camera interface class
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef NV_INTERFACE_CAMERA_HH
|
---|
14 | #define NV_INTERFACE_CAMERA_HH
|
---|
15 |
|
---|
16 | #include <nv/common.hh>
|
---|
17 | #include <nv/stl/math.hh>
|
---|
18 |
|
---|
19 | namespace nv
|
---|
20 | {
|
---|
21 | class camera
|
---|
22 | {
|
---|
23 | public:
|
---|
24 | camera() {}
|
---|
25 |
|
---|
26 | void set_lookat( const vec3& eye, const vec3& focus, const vec3& up )
|
---|
27 | {
|
---|
28 | m_view = math::look_at( eye, focus, up );
|
---|
29 | m_position = eye;
|
---|
30 | m_direction = math::normalize( focus - eye );
|
---|
31 | }
|
---|
32 |
|
---|
33 | void set_perspective( f32 fov, f32 aspect, f32 near, f32 far )
|
---|
34 | {
|
---|
35 | m_projection = math::perspective( math::radians( fov ), aspect, near, far );
|
---|
36 | m_near = near;
|
---|
37 | m_far = far;
|
---|
38 | }
|
---|
39 | void set_ortho( f32 left, f32 right, f32 bottom, f32 top, f32 near = -1.0f, f32 far = 1.0f )
|
---|
40 | {
|
---|
41 | m_projection = math::ortho( left, right, bottom, top, near, far );
|
---|
42 | m_near = near;
|
---|
43 | m_far = far;
|
---|
44 | }
|
---|
45 | const mat4& get_projection() const
|
---|
46 | {
|
---|
47 | return m_projection;
|
---|
48 | }
|
---|
49 | const mat4& get_view() const
|
---|
50 | {
|
---|
51 | return m_view;
|
---|
52 | }
|
---|
53 | const vec3& get_position() const
|
---|
54 | {
|
---|
55 | return m_position;
|
---|
56 | }
|
---|
57 | const vec3& get_direction() const
|
---|
58 | {
|
---|
59 | return m_direction;
|
---|
60 | }
|
---|
61 | float get_near() const { return m_near; }
|
---|
62 | float get_far() const { return m_far; }
|
---|
63 | private:
|
---|
64 | bool m_dirty;
|
---|
65 | vec3 m_position;
|
---|
66 | vec3 m_direction;
|
---|
67 | mat4 m_projection;
|
---|
68 | mat4 m_view;
|
---|
69 | float m_near;
|
---|
70 | float m_far;
|
---|
71 | };
|
---|
72 |
|
---|
73 | // TODO - this and camera should have dirty states
|
---|
74 | class scene_state
|
---|
75 | {
|
---|
76 | public:
|
---|
77 | const camera& get_camera() const { return m_camera; }
|
---|
78 | camera& get_camera() { return m_camera; }
|
---|
79 | void set_camera( const camera& c ) { m_camera = c; }
|
---|
80 | void set_model( const mat4& m ) { m_model = m; }
|
---|
81 | void set_viewport( const ivec4& v ){ m_viewport = v; }
|
---|
82 | const ivec4& get_viewport() const { return m_viewport; }
|
---|
83 | const mat4& get_model() const { return m_model; }
|
---|
84 | const mat4& get_view() const { return m_camera.get_view(); }
|
---|
85 | const mat4& get_projection() const { return m_camera.get_projection(); }
|
---|
86 | mat4 get_viewprojection() const { return m_camera.get_projection() * get_view(); }
|
---|
87 | mat4 get_modelview() const { return get_view() * m_model; }
|
---|
88 | mat4 get_mvp() const { return m_camera.get_projection() * get_modelview(); }
|
---|
89 |
|
---|
90 | mat4 get_view_inv() const { return math::inverse( get_view() ); }
|
---|
91 | mat4 get_model_inv() const { return math::inverse( get_model() ); }
|
---|
92 | mat4 get_modelview_inv() const { return math::inverse( get_modelview() ); }
|
---|
93 | mat4 get_projection_inv() const { return math::inverse( get_projection() ); }
|
---|
94 | mat4 get_viewprojection_inv() const { return math::inverse( get_viewprojection() ); }
|
---|
95 | mat4 get_mvp_inv() const { return math::inverse( get_mvp() ); }
|
---|
96 | mat3 get_normal() const { return math::transpose( math::inverse( mat3( get_modelview() ) ) ); }
|
---|
97 | protected:
|
---|
98 | mat4 m_model;
|
---|
99 | camera m_camera;
|
---|
100 | ivec4 m_viewport;
|
---|
101 | };
|
---|
102 |
|
---|
103 |
|
---|
104 | }
|
---|
105 |
|
---|
106 | #endif // NV_INTERFACE_CAMERA_HH
|
---|