1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __CAMERA_H__
18 #define __CAMERA_H__
19 
20 #include "kernel/kernel_types.h"
21 
22 #include "graph/node.h"
23 
24 #include "util/util_array.h"
25 #include "util/util_boundbox.h"
26 #include "util/util_projection.h"
27 #include "util/util_transform.h"
28 #include "util/util_types.h"
29 
30 CCL_NAMESPACE_BEGIN
31 
32 class Device;
33 class DeviceScene;
34 class Scene;
35 
36 /* Camera
37  *
38  * The camera parameters are quite standard, tested to be both compatible with
39  * Renderman, and Blender after remapping.
40  */
41 
42 class Camera : public Node {
43  public:
44   NODE_DECLARE
45 
46   /* Specifies an offset for the shutter's time interval. */
47   enum MotionPosition {
48     /* Shutter opens at the current frame. */
49     MOTION_POSITION_START = 0,
50     /* Shutter is fully open at the current frame. */
51     MOTION_POSITION_CENTER = 1,
52     /* Shutter closes at the current frame. */
53     MOTION_POSITION_END = 2,
54 
55     MOTION_NUM_POSITIONS,
56   };
57 
58   /* Specifies rolling shutter effect. */
59   enum RollingShutterType {
60     /* No rolling shutter effect. */
61     ROLLING_SHUTTER_NONE = 0,
62     /* Sensor is being scanned vertically from top to bottom. */
63     ROLLING_SHUTTER_TOP = 1,
64 
65     ROLLING_SHUTTER_NUM_TYPES,
66   };
67 
68   /* Stereo Type */
69   enum StereoEye {
70     STEREO_NONE,
71     STEREO_LEFT,
72     STEREO_RIGHT,
73   };
74 
75   /* motion blur */
76   float shuttertime;
77   MotionPosition motion_position;
78   array<float> shutter_curve;
79   size_t shutter_table_offset;
80 
81   /* ** Rolling shutter effect. ** */
82   /* Defines rolling shutter effect type. */
83   RollingShutterType rolling_shutter_type;
84   /* Specifies exposure time of scanlines when using
85    * rolling shutter effect.
86    */
87   float rolling_shutter_duration;
88 
89   /* depth of field */
90   float focaldistance;
91   float aperturesize;
92   uint blades;
93   float bladesrotation;
94 
95   /* type */
96   CameraType type;
97   float fov;
98 
99   /* panorama */
100   PanoramaType panorama_type;
101   float fisheye_fov;
102   float fisheye_lens;
103   float latitude_min;
104   float latitude_max;
105   float longitude_min;
106   float longitude_max;
107 
108   /* panorama stereo */
109   StereoEye stereo_eye;
110   bool use_spherical_stereo;
111   float interocular_distance;
112   float convergence_distance;
113   bool use_pole_merge;
114   float pole_merge_angle_from;
115   float pole_merge_angle_to;
116 
117   /* anamorphic lens bokeh */
118   float aperture_ratio;
119 
120   /* sensor */
121   float sensorwidth;
122   float sensorheight;
123 
124   /* clipping */
125   float nearclip;
126   float farclip;
127 
128   /* screen */
129   int width, height;
130   int resolution;
131   BoundBox2D viewplane;
132   /* width and height change during preview, so we need these for calculating dice rates. */
133   int full_width, full_height;
134   /* controls how fast the dicing rate falls off for geometry out side of view */
135   float offscreen_dicing_scale;
136 
137   /* border */
138   BoundBox2D border;
139   BoundBox2D viewport_camera_border;
140 
141   /* transformation */
142   Transform matrix;
143 
144   /* motion */
145   array<Transform> motion;
146   bool use_perspective_motion;
147   float fov_pre, fov_post;
148 
149   /* computed camera parameters */
150   ProjectionTransform screentoworld;
151   ProjectionTransform rastertoworld;
152   ProjectionTransform ndctoworld;
153   Transform cameratoworld;
154 
155   ProjectionTransform worldtoraster;
156   ProjectionTransform worldtoscreen;
157   ProjectionTransform worldtondc;
158   Transform worldtocamera;
159 
160   ProjectionTransform rastertocamera;
161   ProjectionTransform cameratoraster;
162 
163   ProjectionTransform full_rastertocamera;
164 
165   float3 dx;
166   float3 dy;
167 
168   float3 full_dx;
169   float3 full_dy;
170 
171   float3 frustum_right_normal;
172   float3 frustum_top_normal;
173   float3 frustum_left_normal;
174   float3 frustum_bottom_normal;
175 
176   /* update */
177   bool need_update;
178   bool need_device_update;
179   bool need_flags_update;
180   int previous_need_motion;
181 
182   /* Kernel camera data, copied here for dicing. */
183   KernelCamera kernel_camera;
184   array<DecomposedTransform> kernel_camera_motion;
185 
186   /* functions */
187   Camera();
188   ~Camera();
189 
190   void compute_auto_viewplane();
191 
192   void update(Scene *scene);
193 
194   void device_update(Device *device, DeviceScene *dscene, Scene *scene);
195   void device_update_volume(Device *device, DeviceScene *dscene, Scene *scene);
196   void device_free(Device *device, DeviceScene *dscene, Scene *scene);
197 
198   bool modified(const Camera &cam);
199   bool motion_modified(const Camera &cam);
200   void tag_update();
201 
202   /* Public utility functions. */
203   BoundBox viewplane_bounds_get();
204 
205   /* Calculates the width of a pixel at point in world space. */
206   float world_to_raster_size(float3 P);
207 
208   /* Motion blur. */
209   float motion_time(int step) const;
210   int motion_step(float time) const;
211   bool use_motion() const;
212 
213  private:
214   /* Private utility functions. */
215   float3 transform_raster_to_world(float raster_x, float raster_y);
216 };
217 
218 CCL_NAMESPACE_END
219 
220 #endif /* __CAMERA_H__ */
221