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