HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
frameRecorder.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
8 #define PXR_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
9 
10 /// \file usdAppUtils/frameRecorder.h
11 
12 #include "pxr/pxr.h"
14 
15 #include "pxr/base/tf/diagnostic.h"
16 #include "pxr/base/tf/token.h"
17 #include "pxr/usd/usd/stage.h"
18 #include "pxr/usd/usd/timeCode.h"
19 #include "pxr/usd/usdGeom/camera.h"
21 
22 #include <string>
23 
24 
26 
27 
28 /// \class UsdAppUtilsFrameRecorder
29 ///
30 /// A utility class for recording images of USD stages.
31 ///
32 /// UsdAppUtilsFrameRecorder uses Hydra to produce recorded images of a USD
33 /// stage looking through a particular UsdGeomCamera on that stage at a
34 /// particular UsdTimeCode. The images generated will be effectively the same
35 /// as what you would see in the viewer in usdview.
36 ///
37 /// Note that it is assumed that an OpenGL context has already been setup for
38 /// the UsdAppUtilsFrameRecorder if OpenGL is being used as the underlying HGI
39 /// device. This is not required for Metal or Vulkan.
41 {
42 public:
43  /// The \p rendererPluginId argument indicates the renderer plugin that
44  /// Hyrda should use. If the empty token is passed in, a default renderer
45  /// plugin will be chosen depending on the value of \p gpuEnabled.
46  /// The \p gpuEnabled argument determines if the UsdAppUtilsFrameRecorder
47  /// instance will allow Hydra to use the GPU to produce images.
48  /// The \p enableUsdDrawModes argument determines whether the frame
49  /// recorder instance will respect USD draw modes as authored. Setting
50  /// this to false causes the frame recorder to ignore draw modes.
53  const TfToken& rendererPluginId = TfToken(),
54  bool gpuEnabled = true,
55  bool enableUsdDrawModes = true);
56 
57  /// Gets the ID of the Hydra renderer plugin that will be used for
58  /// recording.
60  return _imagingEngine.GetCurrentRendererId();
61  }
62 
63  /// Sets the Hydra renderer plugin to be used for recording.
64  /// This also resets the presentation flag on the HdxPresentTask to false,
65  /// to avoid the need for an OpenGL context.
66  ///
67  /// Note that the renderer plugins that may be set will be restricted if
68  /// this UsdAppUtilsFrameRecorder instance has disabled the GPU.
69  bool SetRendererPlugin(const TfToken& id) {
70  const bool succeeded = _imagingEngine.SetRendererPlugin(id);
71  _imagingEngine.SetEnablePresentation(false);
72 
73  return succeeded;
74  }
75 
76  /// Sets the path to the render pass prim to use.
77  ///
78  /// \note If there is a render settings prim designated by the
79  /// render pass prim via renderSource, it must also be set
80  /// with SetActiveRenderSettingsPrimPath().
83 
84  /// Sets the path to the render settings prim to use.
85  ///
86  /// \see SetActiveRenderPassPrimPath()
89 
90  /// Sets the width of the recorded image.
91  ///
92  /// The height of the recorded image will be computed using this value and
93  /// the aspect ratio of the camera used for recording.
94  ///
95  /// The default image width is 960 pixels.
96  void SetImageWidth(const size_t imageWidth) {
97  if (imageWidth == 0u) {
98  TF_CODING_ERROR("Image width cannot be zero");
99  return;
100  }
101  _imageWidth = imageWidth;
102  }
103 
104  /// Sets the level of refinement complexity.
105  ///
106  /// The default complexity is "low" (1.0).
107  void SetComplexity(const float complexity) {
108  _complexity = complexity;
109  }
110 
111  /// Sets the color correction mode to be used for recording.
112  ///
113  /// By default, color correction is disabled.
115  void SetColorCorrectionMode(const TfToken& colorCorrectionMode);
116 
117  /// Turns the built-in camera light on or off.
118  ///
119  /// When on, this will add a light at the camera's origin.
120  /// This is sometimes called a "headlight".
122  void SetCameraLightEnabled(bool cameraLightEnabled);
123 
124  /// Sets the camera visibility of dome lights.
125  ///
126  /// When on, dome light textures will be drawn to the background as if
127  /// mapped onto a sphere infinitely far away.
129  void SetDomeLightVisibility(bool domeLightsVisible);
130 
131  /// Sets the UsdGeomImageable purposes to be used for rendering
132  ///
133  /// We will __always__ include "default" purpose, and by default,
134  /// we will also include UsdGeomTokens->proxy. Use this method
135  /// to explicitly enumerate an alternate set of purposes to be
136  /// included along with "default".
138  void SetIncludedPurposes(const TfTokenVector& purposes);
139 
140  /// Sets the primary camera prim path.
142  void SetPrimaryCameraPrimPath(const SdfPath& cameraPath);
143 
144  /// Records an image and writes the result to \p outputImagePath.
145  ///
146  /// The recorded image will represent the view from \p usdCamera looking at
147  /// the imageable prims on USD stage \p stage at time \p timeCode.
148  ///
149  /// If \p usdCamera is not a valid camera, a camera will be computed
150  /// to automatically frame the stage geometry.
151  ///
152  /// When we are using a RenderSettings prim, the generated image will be
153  /// written to the file indicated on the connected RenderProducts,
154  /// instead of the given \p outputImagePath. Note that in this case the
155  /// given \p usdCamera will later be overridden by the one authored on the
156  /// RenderSettings Prim.
157  ///
158  /// Returns true if the image was generated and written successfully, or
159  /// false otherwise.
161  bool Record(
162  const UsdStagePtr& stage,
163  const UsdGeomCamera& usdCamera,
164  const UsdTimeCode timeCode,
165  const std::string& outputImagePath);
166 
167 private:
168  UsdImagingGLEngine _imagingEngine;
169  size_t _imageWidth;
170  float _complexity;
171  TfToken _colorCorrectionMode;
172  TfTokenVector _purposes;
173  SdfPath _renderPassPrimPath;
174  SdfPath _renderSettingsPrimPath;
175  bool _cameraLightEnabled;
176  bool _domeLightsVisible;
177 };
178 
179 
181 
182 
183 #endif
USDIMAGINGGL_API TfToken GetCurrentRendererId() const
Return the id of the currently used renderer plugin.
bool SetRendererPlugin(const TfToken &id)
Definition: frameRecorder.h:69
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define TF_CODING_ERROR
#define USDAPPUTILS_API
Definition: api.h:23
USDAPPUTILS_API void SetColorCorrectionMode(const TfToken &colorCorrectionMode)
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Definition: token.h:70
USDAPPUTILS_API UsdAppUtilsFrameRecorder(const TfToken &rendererPluginId=TfToken(), bool gpuEnabled=true, bool enableUsdDrawModes=true)
USDAPPUTILS_API void SetPrimaryCameraPrimPath(const SdfPath &cameraPath)
Sets the primary camera prim path.
USDAPPUTILS_API bool Record(const UsdStagePtr &stage, const UsdGeomCamera &usdCamera, const UsdTimeCode timeCode, const std::string &outputImagePath)
USDAPPUTILS_API void SetCameraLightEnabled(bool cameraLightEnabled)
USDAPPUTILS_API void SetActiveRenderPassPrimPath(SdfPath const &path)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
Definition: path.h:280
USDAPPUTILS_API void SetDomeLightVisibility(bool domeLightsVisible)
TfToken GetCurrentRendererId() const
Definition: frameRecorder.h:59
USDAPPUTILS_API void SetIncludedPurposes(const TfTokenVector &purposes)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void SetImageWidth(const size_t imageWidth)
Definition: frameRecorder.h:96
USDAPPUTILS_API void SetActiveRenderSettingsPrimPath(SdfPath const &path)
USDIMAGINGGL_API void SetEnablePresentation(bool enabled)
void SetComplexity(const float complexity)