HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RAY_DemoMountain.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
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 "RAY_DemoMountain.h"
30 #include <UT/UT_DSOVersion.h>
31 #include <GU/GU_Detail.h>
32 #include <GU/GU_PrimPoly.h>
34 
35 using namespace HDK_Sample;
36 
37 #define MAX_SPLITS 8
38 
39 #define UT_DEBUG 1
40 #include <UT/UT_Counter.h>
41 static UT_Counter theCount("Mountains");
42 static UT_Counter theTotal("TotalMountains");
43 
44 static RAY_ProceduralArg theArgs[] = {
45  RAY_ProceduralArg("p0", "real", "-1 -1 0"),
46  RAY_ProceduralArg("p1", "real", " 1 -1 0"),
47  RAY_ProceduralArg("p2", "real", " 0 1 0"),
49 };
50 
52 {
53 public:
55  : RAY_ProceduralFactory::ProcDefinition("demomountain")
56  {
57  }
58  RAY_Procedural *create() const override
59  { return new RAY_DemoMountain(); }
60  RAY_ProceduralArg *arguments() const override { return theArgs; }
61 };
62 
63 void
65 {
66  factory->insert(new ProcDef);
67 }
68 
70  : mySplits(splits)
71 {
72  theCount++;
73  theTotal++;
74 }
75 
77 {
78  theCount--;
79 }
80 
81 const char *
83 {
84  return "RAY_DemoMountain";
85 }
86 
87 int
89 {
90  uint seed = 0xbabecafe;
91 
92  myP[0].assign(-1, -1, 0, seed);
93  SYSfastRandom(seed); // Permute seed
94  myP[1].assign( 1, -1, 0, seed);
95  SYSfastRandom(seed); // Permute seed
96  myP[2].assign( 0, -1, 0, seed);
97 
98  // Get parameter values
99  import("p0", myP[0].pos.data(), 3);
100  import("p1", myP[1].pos.data(), 3);
101  import("p2", myP[2].pos.data(), 3);
102  return 1;
103 }
104 
105 void
106 RAY_DemoMountain::computeBounds(UT_BoundingBox &box, bool include_displace)
107 {
108  box.initBounds(myP[0].pos);
109  box.enlargeBounds(myP[1].pos);
110  box.enlargeBounds(myP[2].pos);
111  if (include_displace)
112  {
113  fpreal bounds = 0.5 / (fpreal)mySplits;
114  box.expandBounds(0, bounds);
115  }
116 }
117 
118 
119 void
121 {
122  computeBounds(box, true);
123  fpreal bounds = 0.5 / (fpreal)mySplits;
124  box.initBounds(myP[0].pos);
125  box.enlargeBounds(myP[1].pos);
126  box.enlargeBounds(myP[2].pos);
127  box.expandBounds(0, bounds);
128 }
129 
130 void
132 {
133  fpreal lod;
134  UT_BoundingBox box;
135 
136  // Invoke the measuring code on the bounding box to determine the level of
137  // detail. The level of detail is the square root of the number of pixels
138  // covered by the bounding box (with all shading factors considered).
139  // However, for computing LOD, we do *not* want to include displacement
140  // bounds
141  computeBounds(box, false);
142  lod = getLevelOfDetail(box);
143 
145  {
146  // Render geometry
147  fractalRender();
148  }
149  else
150  {
151  // Split into child procedurals
152  fractalSplit();
153  }
154 }
155 
156 static void
157 edgeSplit(FractalPoint &P, const FractalPoint &p0, const FractalPoint &p1,
158  fpreal scale)
159 {
160  uint seed;
161  fpreal disp;
162 
163  seed = (p0.seed + p1.seed)/2;
164  disp = SYSfastRandomZero(seed)*scale;
165  P.seed = seed;
166  P.pos = (p0.pos + p1.pos) * .5;
167  P.pos(2) += 0.25 * disp * distance3d(p0.pos, p1.pos);
168 }
169 
170 void
172 {
173  RAY_DemoMountain *kids[4];
174  int i;
175  fpreal scale;
176 
177  scale = 1.0 / (fpreal)mySplits;
178  for (i = 0; i < 4; i++)
179  kids[i] = new RAY_DemoMountain(mySplits+1);
180 
181  for (i = 0; i < 3; i++)
182  {
183  kids[i]->myP[0] = myP[i];
184  edgeSplit(kids[i]->myP[1], myP[i], myP[(i+1)%3], scale);
185  }
186  for (i = 0; i < 3; i++)
187  {
188  kids[3]->myP[i] = kids[(i+1)%3]->myP[2] = kids[i]->myP[1];
189  }
190  for (i = 0; i < 4; i++)
191  {
193  child->addProcedural(kids[i]);
194  }
195 }
196 
197 void
199 {
200  // Build some geometry
202  GU_PrimPoly *poly = GU_PrimPoly::build(geo.get(), 3);
203 
204 #if 0
205  // Optionally, create a primitive color attribute "Cd"
206  uint seed = myP[0].seed + myP[1].seed + myP[2].seed;
207  UT_Vector3 clr;
208  clr.x() = SYSfastRandom(seed);
209  clr.y() = SYSfastRandom(seed);
210  clr.z() = SYSfastRandom(seed);
212  Cd.set(poly->getMapOffset(), clr);
213 #endif
214 
215  for (int i = 0; i < 3; i++)
216  {
217  geo->setPos3(poly->getPointOffset(i), myP[i].pos);
218  }
219 
220  // Now, add the geometry to mantra.
222  child->addGeometry(geo);
223 }
SYS_FORCE_INLINE GA_Offset getPointOffset(GA_Size i) const
Definition: GA_Primitive.h:254
RAY_ProceduralGeo createGeometry() const
Allocate geometry for this procedural.
T distance3d(const UT_Vector3T< T > &p1, const UT_Vector3T< T > &p2)
Compute the distance between two points.
Definition: UT_Vector3.h:1116
int initialize(const UT_BoundingBox *) override
void fractalSplit()
Split into 4 new procedurals (each rendering a triangle)
constexpr SYS_FORCE_INLINE T & z() noexcept
Definition: UT_Vector3.h:667
Procedural primitive for mantra (RAY)
GA_Attribute * addDiffuseAttribute(GA_AttributeOwner who, GA_Storage s=GA_STORE_INVALID)
bool insert(ProcDefinition *def, bool replace_existing=true)
void getBoundingBox(UT_BoundingBox &box) override
The bounding box is the "object space" bounds of the procedural.
constexpr SYS_FORCE_INLINE const T * data() const noexcept
Definition: UT_Vector3.h:292
SYS_FORCE_INLINE void expandBounds(T relative, T absolute)
GA_API const UT_StringHolder Cd
GA_API const UT_StringHolder scale
static GU_PrimPoly * build(GA_Detail *gdp, int npts, int open=0, int appendpts=1)
Definition: GU_PrimPoly.h:126
RAY_ProceduralChildPtr createChild() const
void enlargeBounds(const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
void assign(fpreal x, fpreal y, fpreal z, uint s)
Parameter definition for arguments to RAY_Procedural.
const char * className() const override
SYS_FORCE_INLINE void setPos3(GA_Offset ptoff, const UT_Vector3 &pos)
Set P from a UT_Vector3.
Definition: GA_Detail.h:237
fpreal getLevelOfDetail(const UT_BoundingBox &box) const
fpreal64 fpreal
Definition: SYS_Types.h:277
SYS_FORCE_INLINE GA_Offset getMapOffset() const
Gets the offset of this primitive in the detail containing it.
Definition: GA_Primitive.h:146
OIIO_UTIL_API std::vector< std::string > splits(string_view str, string_view sep="", int maxsplit=-1)
void fractalRender()
Render triangle geometry.
SYS_FORCE_INLINE void initBounds()
RAY_ProceduralArg * arguments() const override
Provide a const reference to the arguments for the procedural.
RAY_Procedural * create() const override
Create a procedural, and pass ownership of the instance to mantra.
constexpr SYS_FORCE_INLINE T & y() noexcept
Definition: UT_Vector3.h:665
GU_Detail * get(int segment=0) const
unsigned int uint
Definition: SYS_Types.h:45
Class to create a procedural.
GLint lod
Definition: glcorearb.h:2765
void registerProcedural(RAY_ProceduralFactory *factory)
Modern interface to register procedurals.
#define MAX_SPLITS
constexpr SYS_FORCE_INLINE T & x() noexcept
Definition: UT_Vector3.h:663