All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
legacyEngine.h
1 //
2 // Copyright 2016 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 USDIMAGINGGL_LEGACY_ENGINE_H
25 #define USDIMAGINGGL_LEGACY_ENGINE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usdImaging/usdImagingGL/api.h"
29 
30 #include "pxr/usdImaging/usdImagingGL/renderParams.h"
31 
32 #include "pxr/imaging/glf/drawTarget.h"
33 #include "pxr/imaging/glf/glContext.h"
34 #include "pxr/imaging/glf/simpleLight.h"
35 #include "pxr/imaging/glf/simpleMaterial.h"
36 
37 #include "pxr/imaging/garch/gl.h"
38 #include "pxr/imaging/cameraUtil/conformWindow.h"
39 
40 #include "pxr/usd/usdGeom/gprim.h"
41 #include "pxr/usd/usd/notice.h"
42 
43 #include "pxr/base/tf/declarePtrs.h"
44 
45 #include "pxr/base/tf/hashmap.h"
46 #include "pxr/base/tf/hashset.h"
47 
48 #include <boost/unordered_map.hpp>
49 
50 
51 PXR_NAMESPACE_OPEN_SCOPE
52 
53 TF_DECLARE_WEAK_PTRS(UsdImagingGLLegacyEngine);
54 
55 class UsdImagingGLLegacyEngine : public TfWeakBase
56 {
57 
58  typedef UsdImagingGLLegacyEngine This;
59 
60 public:
61 
62  USDIMAGINGGL_API
63  UsdImagingGLLegacyEngine(const SdfPathVector& excludedPaths);
64 
65  USDIMAGINGGL_API
66  ~UsdImagingGLLegacyEngine();
67 
68  // Entry point for kicking off a render
69  USDIMAGINGGL_API
70  void Render(const UsdPrim& root,
71  const UsdImagingGLRenderParams& params);
72 
73 
76  USDIMAGINGGL_API
77  void SetFreeCameraMatrices(const GfMatrix4d& viewMatrix,
78  const GfMatrix4d& projectionMatrix);
79 
81  USDIMAGINGGL_API
82  void SetCameraPath(const SdfPath& id);
83 
85  void SetWindowPolicy(CameraUtilConformWindowPolicy policy) {
86  _windowPolicy = policy;
87  }
88  void SetRenderViewport(const GfVec4d& viewport) {
89  _viewport = viewport;
90  }
91 
93  USDIMAGINGGL_API
94  void SetLightingState(GlfSimpleLightVector const &lights,
95  GlfSimpleMaterial const &material,
96  GfVec4f const &sceneAmbient);
97 
98  USDIMAGINGGL_API
99  void InvalidateBuffers();
100 
101  USDIMAGINGGL_API
102  SdfPath GetRprimPathFromPrimId(int primId) const;
103 
104  USDIMAGINGGL_API
105  bool TestIntersection(
106  const GfMatrix4d &viewMatrix,
107  const GfMatrix4d &projectionMatrix,
108  const GfMatrix4d &worldToLocalSpace,
109  const UsdPrim& root,
110  const UsdImagingGLRenderParams& params,
111  GfVec3d *outHitPoint,
112  SdfPath *outHitPrimPath = NULL,
113  SdfPath *outInstancerPath = NULL,
114  int *outHitInstanceIndex = NULL,
115  int *outHitElementIndex = NULL);
116 
117 private:
118  bool _SupportsPrimitiveRestartIndex();
119 
120 private:
121 
122  // Extracts all data necessary for drawing the stage.
123  void _TraverseStage(const UsdPrim& root);
124 
125  // Update GL camera state
126  void _ResolveCamera();
127  void _UpdateGLCameraFramingState(const GfMatrix4d& viewMatrix,
128  const GfMatrix4d& projectionMatrix,
129  const GfVec4d& viewport);
130 
131  // Common logic for extracting color information for all gprims.
132  void _ProcessGprimColor(const UsdGeomGprim *gprimSchema,
133  const UsdPrim &prim,
134  bool *doubleSided,
135  VtArray<GfVec3f> *color,
136  TfToken *interpolation);
137 
138  // Standard geometry callbacks
139  void _HandleXform(const UsdPrim &prim);
140  void _HandleMesh(const UsdPrim &prim);
141  void _HandleCurves(const UsdPrim& prim);
142  void _HandleCube(const UsdPrim &prim);
143  void _HandleSphere(const UsdPrim &prim);
144  void _HandleCone(const UsdPrim &prim);
145  void _HandleCylinder(const UsdPrim &prim);
146  void _HandleCapsule(const UsdPrim &prim);
147  void _HandlePoints(const UsdPrim &prim);
148  void _HandleNurbsPatch(const UsdPrim &prim);
149  void _RenderPrimitive(const UsdPrim &prim, const UsdGeomGprim *gprimSchema,
150  const VtArray<GfVec3f> &pts, const VtIntArray &nmvts,
151  const VtIntArray &vts);
152 
153  // Generates GPU buffers for raw float and index data.
154  void _PopulateBuffers();
155 
156  // Sets up and issues draw call(s) for polygons.
157  // When drawID is true, draw with ID color instead of vertex color.
158  void _DrawPolygons(bool drawID);
159 
160  // Sets up and issues draw call(s) for lines.
161  // When drawID is true, draw with ID color instead of vertex color.
162  void _DrawLines(bool drawID);
163 
164  // Allocates a new ID and color and associates it with the given path,
165  // returns the ID color.
166  GfVec4f _IssueID(SdfPath const& path);
167 
168  // Append a copy of the ID to the ID color buffer (one per vertex).
169  void _AppendIDColor(GfVec4f const& ID, std::vector<GLfloat>* vec) {
170  vec->push_back(ID[0]);
171  vec->push_back(ID[1]);
172  vec->push_back(ID[2]);
173  }
174 
175  // USD Change notice handler.
176  void _OnObjectsChanged(UsdNotice::ObjectsChanged const&,
177  UsdStageWeakPtr const& sender);
178 
179 private:
180  UsdImagingGLRenderParams _params;
181 
182  GfMatrix4d _ctm;
183 
184  std::vector<std::pair<UsdPrim, GfMatrix4d> > _xformStack;
185 
186  // Raw float data for all points to be drawn as polygons.
187  std::vector<GLfloat> _points;
188 
189  // Raw float data for all normals on polygons (1 per point).
190  std::vector<GLfloat> _normals;
191 
192  // Raw float data for all polygon colors (1 per point).
193  std::vector<GLfloat> _colors;
194  std::vector<GLfloat> _IDColors;
195 
196  // Indexes into _points to define polygons to be drawn.
197  std::vector<GLuint> _verts;
198 
199  // The number of points on each polygon-- not needed if prim restart is
200  // supported.
201  std::vector<GLuint> _numVerts;
202 
203  // The byte-offsets into the element array buffer indicating the start of
204  // each polygon-- not needed if prim restart is supported.
205  std::vector<GLvoid*> _vertIdxOffsets;
206 
207  // A rolling count of points, to assist in providing buffer offsets for the
208  // raw data of all meshes.
209  int _vertCount;
210 
211  // Raw float data for all points to be drawn as lines.
212  std::vector<GLfloat> _linePoints;
213 
214  // Raw float data for all line colors (1 per point).
215  std::vector<GLfloat> _lineColors;
216  std::vector<GLfloat> _lineIDColors;
217 
218  // Indexes into _linePoints to define individual segments. If prim restart
219  // is supported, each segment will be separated by a prim restart index;
220  // otherwise we need to manually account for each segment and break up the
221  // draw calls accordingly.
222  std::vector<GLuint> _lineVerts;
223 
224  // The number of points on each line segment-- not needed if prim restart is
225  // supported.
226  std::vector<GLuint> _numLineVerts;
227 
228  // The byte-offsets into the element array buffer indicating the start of
229  // each line segment-- not needed if prim restart is supported.
230  std::vector<GLvoid*> _lineVertIdxOffsets;
231 
232  // A rolling count of points, to assist in providing buffer offsets for the
233  // raw data of all lines.
234  int _lineVertCount;
235 
236  // The identifiers for the physical buffers on the card.
237  GLuint _attribBuffer;
238  GLuint _indexBuffer;
239 
240  UsdPrim _root;
241 
242  TfHashSet<SdfPath, SdfPath::Hash> _excludedSet;
243 
244  GfMatrix4d _freeCamViewMatrix;
245  GfMatrix4d _freeCamProjMatrix;
246  GfVec4d _viewport;
247  CameraUtilConformWindowPolicy _windowPolicy;
248  bool _usingSceneCam;
249  SdfPath _sceneCamId;
250  UsdPrim _sceneCam;
251 
252  // Define a type for PrimID along with some helper methods to pack a vec4i
253  // into a single value (and the inverse).
254  struct _PrimID {
255  typedef int32_t ValueType;
256 
257  static ValueType Pack(GfVec4i primIdColor) {
258  ValueType primID = {((primIdColor[0] & 0xff) << 0) |
259  ((primIdColor[1] & 0xff) << 8) |
260  ((primIdColor[2] & 0xff) << 16) };
261  return primID;
262  }
263 
264  static GfVec4f Unpack(ValueType primId) {
265  GfVec4f primIdColor;
266  // Convert the instance Id into an rbg color. We could potentially
267  // use alpha if we want to extend the range.
268  primIdColor[0] = ((float)((primId & 0x000000ff) >> 0)) / 255.0f;
269  primIdColor[1] = ((float)((primId & 0x0000ff00) >> 8)) / 255.0f;
270  primIdColor[2] = ((float)((primId & 0x00ff0000) >> 16)) / 255.0f;
271  primIdColor[3] = 1;
272 
273  return primIdColor;
274  }
275  };
276 
277  // A running counter of prim IDs.
278  _PrimID::ValueType _primIDCounter;
279 
280  // A mapping from ID to SdfPath, used for picking.
281  typedef TfHashMap<_PrimID::ValueType, SdfPath> _PrimIDMap;
282  _PrimIDMap _primIDMap;
283 
284  // For changes from UsdStage.
285  TfNotice::Key _objectsChangedNoticeKey;
286 
287  typedef boost::unordered_map<GlfGLContextSharedPtr, GlfDrawTargetRefPtr> _DrawTargetPerContextMap;
288  _DrawTargetPerContextMap _drawTargets;
289 };
290 
291 
292 PXR_NAMESPACE_CLOSE_SCOPE
293 
294 #endif // USDIMAGINGGL_LEGACY_ENGINE_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
Handle-object returned by TfNotice::Register().
Definition: notice.h:256
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:62
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Notice sent in response to authored changes that affect UsdObjects.
Definition: notice.h:107
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
Basic type for a vector of 4 float components.
Definition: vec4f.h:63
Used as an arguments class for various methods in UsdImagingGLEngine.
Definition: renderParams.h:72
Base class for all geometric primitives.
Definition: gprim.h:66
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142