1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_CAMERA_H
25 #define PXR_IMAGING_HD_CAMERA_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/sprim.h"
31 
32 #include "pxr/imaging/cameraUtil/conformWindow.h"
33 
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/base/tf/staticTokens.h"
36 #include "pxr/base/gf/matrix4d.h"
37 #include "pxr/base/gf/range1f.h"
38 
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 /// Camera state that can be requested from the scene delegate via
44 /// GetCameraParamValue(id, token). The parameters below mimic the
45 /// USD camera schema and GfCamera (with the exception of window
46 /// policy). All spatial units are in world units though and
47 /// projection is HdCamera::Projection rather than a token.
48 #define HD_CAMERA_TOKENS                            \
49     /* frustum */                                   \
50     (projection)                                    \
51     (horizontalAperture)                            \
52     (verticalAperture)                              \
53     (horizontalApertureOffset)                      \
54     (verticalApertureOffset)                        \
55     (focalLength)                                   \
56     (clippingRange)                                 \
57     (clipPlanes)                                    \
58                                                     \
59     /* depth of field */                            \
60     (fStop)                                         \
61     (focusDistance)                                 \
62                                                     \
63     /* shutter/lighting */                          \
64     (shutterOpen)                                   \
65     (shutterClose)                                  \
66     (exposure)                                      \
67                                                     \
68     /* how to match window with different aspect */ \
69     (windowPolicy)                                  \
70                                                     \
71     /* OpenGL-style matrices, deprecated */         \
72     (worldToViewMatrix)                             \
73     (projectionMatrix)
74 
75 
76 TF_DECLARE_PUBLIC_TOKENS(HdCameraTokens, HD_API, HD_CAMERA_TOKENS);
77 
78 /// \class HdCamera
79 ///
80 /// Hydra schema for a camera that pulls the params (see above) during
81 /// Sync.
82 /// Backends that use additional camera parameters can inherit from HdCamera and
83 /// pull on them.
84 ///
85 class HdCamera : public HdSprim
86 {
87 public:
88     using ClipPlanesVector = std::vector<GfVec4d>;
89 
90     HD_API
91     HdCamera(SdfPath const & id);
92     HD_API
93     ~HdCamera() override;
94 
95     // change tracking for HdCamera
96     enum DirtyBits : HdDirtyBits
97     {
98         Clean                 = 0,
99         DirtyTransform        = 1 << 0,
100         DirtyViewMatrix       = DirtyTransform, // deprecated
101         DirtyProjMatrix       = 1 << 1,         // deprecated
102         DirtyWindowPolicy     = 1 << 2,
103         DirtyClipPlanes       = 1 << 3,
104         DirtyParams           = 1 << 4,
105         AllDirty              = (DirtyTransform
106                                 |DirtyProjMatrix
107                                 |DirtyWindowPolicy
108                                 |DirtyClipPlanes
109                                 |DirtyParams)
110     };
111 
112     enum Projection {
113         Perspective = 0,
114         Orthographic
115     };
116 
117     // ---------------------------------------------------------------------- //
118     /// Sprim API
119     // ---------------------------------------------------------------------- //
120 
121     /// Synchronizes state from the delegate to this object.
122     HD_API
123     void Sync(HdSceneDelegate *sceneDelegate,
124               HdRenderParam   *renderParam,
125               HdDirtyBits     *dirtyBits) override;
126 
127 
128     /// Returns the minimal set of dirty bits to place in the
129     /// change tracker for use in the first sync of this prim.
130     /// Typically this would be all dirty bits.
131     HD_API
132     HdDirtyBits GetInitialDirtyBitsMask() const override;
133 
134     // ---------------------------------------------------------------------- //
135     /// Camera parameters accessor API
136     // ---------------------------------------------------------------------- //
137 
138     /// Returns camera transform
GetTransform()139     GfMatrix4d const& GetTransform() const {
140         return _transform;
141     }
142 
143     /// Returns whether camera is orthographic and perspective
GetProjection()144     Projection GetProjection() const {
145         return _projection;
146     }
147 
148     /// Returns horizontal aperture in world units.
GetHorizontalAperture()149     float GetHorizontalAperture() const {
150         return _horizontalAperture;
151     }
152 
153     /// Returns vertical aperture in world units.
GetVerticalAperture()154     float GetVerticalAperture() const {
155         return _verticalAperture;
156     }
157 
158     /// Returns horizontal aperture offset in world units.
GetHorizontalApertureOffset()159     float GetHorizontalApertureOffset() const {
160         return _horizontalApertureOffset;
161     }
162 
163     /// Returns vertical aperture offset in world units.
GetVerticalApertureOffset()164     float GetVerticalApertureOffset() const {
165         return _verticalApertureOffset;
166     }
167 
168     /// Returns focal length in world units.
GetFocalLength()169     float GetFocalLength() const {
170         return _focalLength;
171     }
172 
173     /// Returns near and far plane in world units
GetClippingRange()174     GfRange1f const &GetClippingRange() const {
175         return _clippingRange;
176     }
177 
178     /// Returns any additional clipping planes defined in camera space.
GetClipPlanes()179     std::vector<GfVec4d> const& GetClipPlanes() const {
180         return _clipPlanes;
181     }
182 
183     /// Returns fstop of camera
GetFStop()184     float GetFStop() const {
185         return _fStop;
186     }
187 
188     /// Returns focus distance in world units.
GetFocusDistance()189     float GetFocusDistance() const {
190         return _focusDistance;
191     }
192 
GetShutterOpen()193     double GetShutterOpen() const {
194         return _shutterOpen;
195     }
196 
GetShutterClose()197     double GetShutterClose() const {
198         return _shutterClose;
199     }
200 
GetExposure()201     float GetExposure() const {
202         return _exposure;
203     }
204 
205     /// Returns the window policy of the camera. If no opinion is authored, we
206     /// default to "CameraUtilFit"
GetWindowPolicy()207     CameraUtilConformWindowPolicy const& GetWindowPolicy() const {
208         return _windowPolicy;
209     }
210 
211     // ---------------------------------------------------------------------- //
212     /// Legacy camera parameters accessor API
213     // ---------------------------------------------------------------------- //
214 
215     /// Returns the matrix transformation from world to camera space.
216     /// \deprecated Use GetTransform instead
217     HD_API
218     GfMatrix4d GetViewMatrix() const;
219 
220     /// Returns the matrix transformation from camera to world space.
221     /// \deprecated Use GetTransform and invert instead
222     HD_API
223     GfMatrix4d GetViewInverseMatrix() const;
224 
225     /// Returns the projection matrix for the camera.
226     /// \deprecated Compute from above physically based attributes
227     HD_API
228     GfMatrix4d GetProjectionMatrix() const;
229 
230 protected:
231     // frustum
232     GfMatrix4d              _transform;
233     Projection              _projection;
234     float                   _horizontalAperture;
235     float                   _verticalAperture;
236     float                   _horizontalApertureOffset;
237     float                   _verticalApertureOffset;
238     float                   _focalLength;
239     GfRange1f               _clippingRange;
240     std::vector<GfVec4d>    _clipPlanes;
241 
242     // focus
243     float                   _fStop;
244     float                   _focusDistance;
245 
246     // shutter/lighting
247     double                  _shutterOpen;
248     double                  _shutterClose;
249     float                   _exposure;
250 
251     // Camera's opinion how it display in a window with
252     // a different aspect ratio
253     CameraUtilConformWindowPolicy _windowPolicy;
254 
255     // OpenGL-style matrices
256     GfMatrix4d _worldToViewMatrix;
257     GfMatrix4d _worldToViewInverseMatrix;
258     GfMatrix4d _projectionMatrix;
259 };
260 
261 PXR_NAMESPACE_CLOSE_SCOPE
262 
263 #endif // PXR_IMAGING_HD_CAMERA_H
264