1 module armos.graphics.camera;
2 
3 import armos.graphics;
4 import armos.math;
5 import armos.events;
6 
7 /++
8 Cameraを表すinterfaceです.Cameraで写したい処理をbegin()とend()の間に記述します.
9 +/
10 interface Camera{
11     public{
12         /++
13             projectionMatrixを取得します.
14         +/
15         Matrix4f projectionMatrix();
16         
17         /++
18             viewMatrixを取得します.
19         +/
20         Matrix4f viewMatrix();
21 
22         /++
23             Cameraの位置を表します.
24         +/
25         Vector3f position();
26         
27         ///
28         Camera position(in Vector3f p);
29         
30         /++
31             Cameraが映す対象の位置を表します.
32         +/
33         Vector3f target();
34         
35         ///
36         Camera target(in Vector3f v);
37 
38         /++
39             Cameraの方向を表します.
40         +/
41         Vector3f up();
42         
43         ///
44         Camera up(in Vector3f v);
45 
46         /**
47           Cameraの視野角を表します.単位はdegreeです.
48          **/
49         double fov();
50         
51         ///
52         Camera fov(in double f);
53 
54         /++
55             描画を行う最短距離です.
56         +/
57         double nearDist();
58         
59         ///
60         Camera nearDist(in double n);
61         
62         /++
63             描画を行う最長距離です.
64         +/
65         double farDist();
66         
67         ///
68         Camera farDist(in double f);
69             
70         /++
71             Cameraで表示する処理を開始します.
72         +/
73         Camera begin();
74         
75         /++
76             Cameraで表示する処理を終了します.
77         +/
78         Camera end();
79     }//public
80 }//interface Camera
81 
82 ///
83 class DefaultCamera: Camera{
84     mixin CameraImpl;
85 }
86 
87 mixin template CameraImpl(){
88     import armos.math;
89     import armos.graphics;
90     import armos.app;
91     private alias T = typeof(this);
92     public{
93         /++
94             projectionMatrixを取得します.
95         +/
96         Matrix4f projectionMatrix()const{return _projectionMatrix;}
97 
98         ///
99         Matrix4f viewMatrix()const{return _viewMatrix;}
100         
101         /++
102             Cameraの位置を表します.
103         +/
104         Vector3f position()const{return _position;}
105         
106         ///
107         T position(in Vector3f p){_position = p; return this;}
108         
109 
110         /++
111             Cameraが映す対象の位置を表します.
112         +/
113         Vector3f target()const{return _target;}
114         
115         ///
116         T target(in Vector3f v){_target = v; return this;}
117 
118         /++
119             Cameraの方向を表します.
120         +/
121         Vector3f up()const{return _up;}
122         
123         ///
124         T up(in Vector3f v){_up = v; return this;}
125 
126         /**
127           Cameraの視野角を表します.単位はdegreeです.
128          **/
129         double fov()const{return _fov;}
130         
131         ///
132         T fov(in double f){_fov = f; return this;}
133 
134         /++
135             描画を行う最短距離です.
136         +/
137         double nearDist()const{return _nearDist;}
138         
139         ///
140         T nearDist(in double n){
141             _nearDist = n;
142             return this;
143         }
144 
145         /++
146             描画を行う最長距離です.
147         +/
148         double farDist()const{return _farDist;}
149         
150         ///
151         T farDist(in double f){
152             _farDist = f;
153             return this;
154         }
155 
156         /++
157             Cameraで表示する処理を開始します.
158         +/
159         T begin(){
160             _viewMatrix = lookAtViewMatrix(
161                     _position, 
162                     _target, 
163                     _up
164                     );
165 
166             _projectionMatrix = perspectiveMatrix(
167                     _fov,
168                     windowAspect,
169                     _nearDist,
170                     _farDist
171                     );
172 
173             pushViewMatrix;
174             loadViewMatrix(_viewMatrix);
175             pushProjectionMatrix;
176             loadProjectionMatrix(_projectionMatrix);
177             multProjectionMatrix(scalingMatrix!float(1f, -1f, 1f));
178             return this;
179         }
180 
181         /++
182             Cameraで表示する処理を終了します.
183         +/
184         T end(){
185             popViewMatrix;
186             popProjectionMatrix;
187             return this;
188         }
189     }
190 
191     private{
192         Matrix4f _projectionMatrix;
193         Matrix4f _viewMatrix;
194         
195         Vector3f _position = Vector3f.zero;
196         Vector3f _target   = Vector3f.zero;
197         Vector3f _up       = Vector3f(0, 1, 0);
198         
199         double   _fov      = 60.0;
200         double   _nearDist = 0.1;
201         double   _farDist  = 10000.0;
202     }
203 }