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 | }
|
---|
37 | void set_ortho( f32 left, f32 right, f32 bottom, f32 top, f32 near = -1.0f, f32 far = 1.0f )
|
---|
38 | {
|
---|
39 | m_projection = math::ortho( left, right, bottom, top, near, far );
|
---|
40 | }
|
---|
41 | const mat4& get_projection() const
|
---|
42 | {
|
---|
43 | return m_projection;
|
---|
44 | }
|
---|
45 | const mat4& get_view() const
|
---|
46 | {
|
---|
47 | return m_view;
|
---|
48 | }
|
---|
49 | const vec3& get_position() const
|
---|
50 | {
|
---|
51 | return m_position;
|
---|
52 | }
|
---|
53 | const vec3& get_direction() const
|
---|
54 | {
|
---|
55 | return m_direction;
|
---|
56 | }
|
---|
57 | private:
|
---|
58 | bool m_dirty;
|
---|
59 | vec3 m_position;
|
---|
60 | vec3 m_direction;
|
---|
61 | mat4 m_projection;
|
---|
62 | mat4 m_view;
|
---|
63 | };
|
---|
64 |
|
---|
65 | // TODO - this and camera should have dirty states
|
---|
66 | class scene_state
|
---|
67 | {
|
---|
68 | public:
|
---|
69 | const camera& get_camera() const { return m_camera; }
|
---|
70 | camera& get_camera() { return m_camera; }
|
---|
71 | void set_camera( const camera& c ) { m_camera = c; }
|
---|
72 | void set_model( const mat4& m ) { m_model = m; }
|
---|
73 | void set_viewport( const ivec4& v ){ m_viewport = v; }
|
---|
74 | const ivec4& get_viewport() const { return m_viewport; }
|
---|
75 | const mat4& get_model() const { return m_model; }
|
---|
76 | const mat4& get_view() const { return m_camera.get_view(); }
|
---|
77 | const mat4& get_projection() const { return m_camera.get_projection(); }
|
---|
78 | mat4 get_modelview() const { return get_view() * m_model; }
|
---|
79 | mat4 get_mvp() const { return m_camera.get_projection() * get_modelview(); }
|
---|
80 |
|
---|
81 | mat4 get_view_inv() const { return math::inverse( get_view() ); }
|
---|
82 | mat4 get_model_inv() const { return math::inverse( get_model() ); }
|
---|
83 | mat3 get_normal() const { return math::transpose( math::inverse( mat3( get_modelview() ) ) ); }
|
---|
84 | protected:
|
---|
85 | mat4 m_model;
|
---|
86 | camera m_camera;
|
---|
87 | ivec4 m_viewport;
|
---|
88 | };
|
---|
89 |
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | #endif // NV_INTERFACE_CAMERA_HH
|
---|