HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RAY_DemoFile.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  * This is a sample procedural DSO
27  */
28 
29 #include <UT/UT_DSOVersion.h>
30 #include <GU/GU_Detail.h>
31 #include "RAY_DemoFile.h"
33 
34 using namespace HDK_Sample;
35 
36 static RAY_ProceduralArg theArgs[] = {
37  RAY_ProceduralArg("file", "string", ""),
38  RAY_ProceduralArg("blurfile", "string", ""),
39  RAY_ProceduralArg("velocityblur", "int", "0"),
40  RAY_ProceduralArg("shutter", "real", "1"),
42 };
43 
45 {
46 public:
48  : RAY_ProceduralFactory::ProcDefinition("demofile")
49  {
50  }
51  virtual RAY_Procedural *create() const { return new RAY_DemoFile(); }
52  virtual RAY_ProceduralArg *arguments() const { return theArgs; }
53 };
54 
55 void
57 {
58  factory->insert(new ProcDef);
59 }
60 
62  : myShutter(1),
63  myPreBlur(0),
64  myPostBlur(0)
65 {
66  myBox.initBounds(0, 0, 0);
67 }
68 
70 {
71 }
72 
73 const char *
75 {
76  return "RAY_DemoFile";
77 }
78 
79 int
81 {
82  int ival;
83 
84  // The user is required to specify the bounds of the geometry.
85  // Alternatively, if the bounds aren't specified, we could forcibly load
86  // the geometry at this point and compute the bounds ourselves.
87  if (!box)
88  {
89  fprintf(stderr, "The %s procedural needs a bounding box specified\n",
90  className());
91  return 0;
92  }
93 
94  // Stash away the box the user specified
95  myBox = *box;
96 
97  // Import the shutter time. This is a scale between 0 and 1
98  if (!import("shutter", &myShutter, 1))
99  myShutter = 1;
100 
101  // A toggle for whether to use velocityblur or not. Since there's no
102  // "bool" type for parameters, import into an int.
103  if (import("velocityblur", &ival, 1))
104  myVelocityBlur = (ival != 0);
105  else
106  myVelocityBlur = false;
107 
108  // Import the filenames for t0 and t1
109  import("file", myFile);
110  import("blurfile", myBlurFile);
111 
112  // Import the shutter settings for velocity blur. The 'camera:shutter'
113  // stores the start and end of the shutter window in fraction of
114  // a frame. Divide by the current FPS value to get the correct
115  // scaling for velocity blur.
116  fpreal fps = 24.0, shutter[2] = {0};
117  import("global:fps", &fps, 1);
118  import("camera:shutter", shutter, 2);
119  myPreBlur = -(myShutter * shutter[0]) / fps;
120  myPostBlur = (myShutter * shutter[1]) / fps;
121 
122  return 1;
123 }
124 
125 void
127 {
128  box = myBox;
129 }
130 
131 void
133 {
134  // Allocate geometry.
135  // Warning: When allocating geometry for a procedural, do not simply
136  // construct a GU_Detail, but call RAY_Procedural::createGeometry().
138 
139  // Load geometry from disk
140  if (!g0->load(myFile, 0).success())
141  {
142  fprintf(stderr, "Unable to load geometry[0]: '%s'\n",
143  myFile.c_str());
144  return;
145  }
146 
147  // Add geometry to mantra.
148  if (myVelocityBlur)
149  {
150  // If performing velocity blur, then we add velocity blur geometry
151  g0.addVelocityBlur(myPreBlur, myPostBlur);
152  }
153  else
154  {
155  // Otherwise, we check to see if there's a motion blur geometry file
156  if (myBlurFile.isstring())
157  {
158  auto g1 = g0.appendSegmentGeometry(myShutter);
160  if (!wlock.getGdp()->load(myBlurFile, 0).success())
161  {
162  fprintf(stderr, "Unable to load geometry[1]: '%s'\n",
163  myBlurFile.c_str());
164  g0.removeSegmentGeometry(g1);
165  }
166  }
167  }
169  obj->addGeometry(g0);
170 }
GU_Detail * getGdp() const
RAY_ProceduralGeo createGeometry() const
Allocate geometry for this procedural.
Procedural primitive for mantra (RAY)
bool insert(ProcDefinition *def, bool replace_existing=true)
A procedural which does a deferred load of geometry from disk.
Definition: RAY_DemoFile.h:39
bool success() const
Definition: GA_Detail.h:1878
IOStatus load(const char *filename, const GA_LoadOptions *opts=0, UT_StringArray *errors=0)
Load a geometry file.
void registerProcedural(RAY_ProceduralFactory *factory)
Modern interface to register procedurals.
Definition: RAY_DemoFile.C:56
SYS_FORCE_INLINE const char * c_str() const
fpreal addVelocityBlur(fpreal pre_blur, fpreal post_blur, const UT_StringHolder &velocity_attribute=UTmakeUnsafeRef("v"), int acceleration_segments=1, const UT_StringHolder &acceleration_attribute=UT_StringHolder())
virtual void getBoundingBox(UT_BoundingBox &box)
The bounding box is the "object space" bounds of the procedural.
Definition: RAY_DemoFile.C:126
RAY_ProceduralChildPtr createChild() const
Parameter definition for arguments to RAY_Procedural.
virtual RAY_ProceduralArg * arguments() const
Provide a const reference to the arguments for the procedural.
Definition: RAY_DemoFile.C:52
double fpreal
Definition: SYS_Types.h:270
GU_DetailHandle appendSegmentGeometry(fpreal shutter)
SYS_FORCE_INLINE void initBounds()
virtual const char * className() const
Definition: RAY_DemoFile.C:74
virtual int initialize(const UT_BoundingBox *)
Definition: RAY_DemoFile.C:80
bool removeSegmentGeometry(const GU_ConstDetailHandle &geo)
virtual RAY_Procedural * create() const
Create a procedural, and pass ownership of the instance to mantra.
Definition: RAY_DemoFile.C:51
Class to create a procedural.
SYS_FORCE_INLINE bool isstring() const