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 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_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
25 #define PXR_USD_IMAGING_USD_APP_UTILS_FRAME_RECORDER_H
26 
27 /// \file usdAppUtils/frameRecorder.h
28 
29 #include "pxr/pxr.h"
31 
32 #include "pxr/base/tf/diagnostic.h"
33 #include "pxr/base/tf/token.h"
34 #include "pxr/usd/usd/stage.h"
35 #include "pxr/usd/usd/timeCode.h"
36 #include "pxr/usd/usdGeom/camera.h"
38 
39 #include <string>
40 
41 
43 
44 
45 /// \class UsdAppUtilsFrameRecorder
46 ///
47 /// A utility class for recording images of USD stages.
48 ///
49 /// UsdAppUtilsFrameRecorder uses Hydra to produce recorded images of a USD
50 /// stage looking through a particular UsdGeomCamera on that stage at a
51 /// particular UsdTimeCode. The images generated will be effectively the same
52 /// as what you would see in the viewer in usdview.
53 ///
54 /// Note that it is assumed that an OpenGL context has already been setup for
55 /// the UsdAppUtilsFrameRecorder if OpenGL is being used as the underlying HGI
56 /// device. This is not required for Metal or Vulkan.
58 {
59 public:
60  /// The \p rendererPluginId argument indicates the renderer plugin that
61  /// Hyrda should use. If the empty token is passed in, a default renderer
62  /// plugin will be chosen depending on the value of \p gpuEnabled.
63  /// The \p gpuEnabled argument determines if the UsdAppUtilsFrameRecorder
64  /// instance will allow Hydra to use the GPU to produce images.
67  const TfToken& rendererPluginId = TfToken(),
68  bool gpuEnabled = true);
69 
70  /// Gets the ID of the Hydra renderer plugin that will be used for
71  /// recording.
73  return _imagingEngine.GetCurrentRendererId();
74  }
75 
76  /// Sets the Hydra renderer plugin to be used for recording.
77  /// This also resets the presentation flag on the HdxPresentTask to false,
78  /// to avoid the need for an OpenGL context.
79  ///
80  /// Note that the renderer plugins that may be set will be restricted if
81  /// this UsdAppUtilsFrameRecorder instance has disabled the GPU.
82  bool SetRendererPlugin(const TfToken& id) {
83  const bool succeeded = _imagingEngine.SetRendererPlugin(id);
84  _imagingEngine.SetEnablePresentation(false);
85 
86  return succeeded;
87  }
88 
89  /// Sets the width of the recorded image.
90  ///
91  /// The height of the recorded image will be computed using this value and
92  /// the aspect ratio of the camera used for recording.
93  ///
94  /// The default image width is 960 pixels.
95  void SetImageWidth(const size_t imageWidth) {
96  if (imageWidth == 0u) {
97  TF_CODING_ERROR("Image width cannot be zero");
98  return;
99  }
100  _imageWidth = imageWidth;
101  }
102 
103  /// Sets the level of refinement complexity.
104  ///
105  /// The default complexity is "low" (1.0).
106  void SetComplexity(const float complexity) {
107  _complexity = complexity;
108  }
109 
110  /// Sets the color correction mode to be used for recording.
111  ///
112  /// By default, color correction is disabled.
114  void SetColorCorrectionMode(const TfToken& colorCorrectionMode);
115 
116  /// Sets the UsdGeomImageable purposes to be used for rendering
117  ///
118  /// We will __always__ include "default" purpose, and by default,
119  /// we will also include UsdGeomTokens->proxy. Use this method
120  /// to explicitly enumerate an alternate set of purposes to be
121  /// included along with "default".
123  void SetIncludedPurposes(const TfTokenVector& purposes);
124 
125  /// Records an image and writes the result to \p outputImagePath.
126  ///
127  /// The recorded image will represent the view from \p usdCamera looking at
128  /// the imageable prims on USD stage \p stage at time \p timeCode.
129  ///
130  /// If \p usdCamera is not a valid camera, a camera will be computed
131  /// to automatically frame the stage geometry.
132  ///
133  /// Returns true if the image was generated and written successfully, or
134  /// false otherwise.
136  bool Record(
137  const UsdStagePtr& stage,
138  const UsdGeomCamera& usdCamera,
139  const UsdTimeCode timeCode,
140  const std::string& outputImagePath);
141 
142 private:
143  UsdImagingGLEngine _imagingEngine;
144  size_t _imageWidth;
145  float _complexity;
146  TfToken _colorCorrectionMode;
147  TfTokenVector _purposes;
148 };
149 
150 
152 
153 
154 #endif
USDIMAGINGGL_API TfToken GetCurrentRendererId() const
Return the id of the currently used renderer plugin.
USDAPPUTILS_API UsdAppUtilsFrameRecorder(const TfToken &rendererPluginId=TfToken(), bool gpuEnabled=true)
bool SetRendererPlugin(const TfToken &id)
Definition: frameRecorder.h:82
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define TF_CODING_ERROR
#define USDAPPUTILS_API
Definition: api.h:40
USDAPPUTILS_API void SetColorCorrectionMode(const TfToken &colorCorrectionMode)
USDIMAGINGGL_API bool SetRendererPlugin(TfToken const &id)
Definition: token.h:87
USDAPPUTILS_API bool Record(const UsdStagePtr &stage, const UsdGeomCamera &usdCamera, const UsdTimeCode timeCode, const std::string &outputImagePath)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
TfToken GetCurrentRendererId() const
Definition: frameRecorder.h:72
USDAPPUTILS_API void SetIncludedPurposes(const TfTokenVector &purposes)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
void SetImageWidth(const size_t imageWidth)
Definition: frameRecorder.h:95
USDIMAGINGGL_API void SetEnablePresentation(bool enabled)
void SetComplexity(const float complexity)