HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RAY_DemoMountain.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005
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  virtual RAY_Procedural *create() const
59  { return new RAY_DemoMountain(); }
60  virtual RAY_ProceduralArg *arguments() const { 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  mySplits = 1;
98 
99  // Get parameter values
100  import("p0", myP[0].pos.data(), 3);
101  import("p1", myP[1].pos.data(), 3);
102  import("p2", myP[2].pos.data(), 3);
103  return 1;
104 }
105 
106 void
107 RAY_DemoMountain::computeBounds(UT_BoundingBox &box, bool include_displace)
108 {
109  box.initBounds(myP[0].pos);
110  box.enlargeBounds(myP[1].pos);
111  box.enlargeBounds(myP[2].pos);
112  if (include_displace)
113  {
114  fpreal bounds = 0.5 / (fpreal)mySplits;
115  box.expandBounds(0, bounds);
116  }
117 }
118 
119 
120 void
122 {
123  computeBounds(box, true);
124  fpreal bounds = 0.5 / (fpreal)mySplits;
125  box.initBounds(myP[0].pos);
126  box.enlargeBounds(myP[1].pos);
127  box.enlargeBounds(myP[2].pos);
128  box.expandBounds(0, bounds);
129 }
130 
131 void
133 {
134  fpreal lod;
135  UT_BoundingBox box;
136 
137  // Invoke the measuring code on the bounding box to determine the level of
138  // detail. The level of detail is the square root of the number of pixels
139  // covered by the bounding box (with all shading factors considered).
140  // However, for computing LOD, we do *not* want to include displacement
141  // bounds
142  computeBounds(box, false);
143  lod = getLevelOfDetail(box);
144 
146  {
147  // Render geometry
148  fractalRender();
149  }
150  else
151  {
152  // Split into child procedurals
153  fractalSplit();
154  }
155 }
156 
157 static void
158 edgeSplit(FractalPoint &P, const FractalPoint &p0, const FractalPoint &p1,
159  fpreal scale)
160 {
161  uint seed;
162  fpreal disp;
163 
164  seed = (p0.seed + p1.seed)/2;
165  disp = SYSfastRandomZero(seed)*scale;
166  P.seed = seed;
167  P.pos = (p0.pos + p1.pos) * .5;
168  P.pos(2) += 0.25 * disp * distance3d(p0.pos, p1.pos);
169 }
170 
171 void
173 {
174  RAY_DemoMountain *kids[4];
175  int i;
176  fpreal scale;
177 
178  scale = 1.0 / (fpreal)mySplits;
179  for (i = 0; i < 4; i++)
180  kids[i] = new RAY_DemoMountain(mySplits+1);
181 
182  for (i = 0; i < 3; i++)
183  {
184  kids[i]->myP[0] = myP[i];
185  edgeSplit(kids[i]->myP[1], myP[i], myP[(i+1)%3], scale);
186  }
187  for (i = 0; i < 3; i++)
188  {
189  kids[3]->myP[i] = kids[(i+1)%3]->myP[2] = kids[i]->myP[1];
190  }
191  for (i = 0; i < 4; i++)
192  {
194  child->addProcedural(kids[i]);
195  }
196 }
197 
198 void
200 {
201  // Build some geometry
203  GU_PrimPoly *poly = GU_PrimPoly::build(geo.get(), 3);
204 
205 #if 0
206  // Optionally, create a primitive color attribute "Cd"
207  uint seed = myP[0].seed + myP[1].seed + myP[2].seed;
208  UT_Vector3 clr;
209  clr.x() = SYSfastRandom(seed);
210  clr.y() = SYSfastRandom(seed);
211  clr.z() = SYSfastRandom(seed);
213  Cd.set(poly->getMapOffset(), clr);
214 #endif
215 
216  for (int i = 0; i < 3; i++)
217  {
218  geo->setPos3(poly->getPointOffset(i), myP[i].pos);
219  }
220 
221  // Now, add the geometry to mantra.
223  child->addGeometry(geo);
224 }
SYS_FORCE_INLINE constexpr const T * data() const noexcept
SYS_FORCE_INLINE void setPos3(GA_Offset ptoff, const UT_Vector3 &P)
Set P from a UT_Vector3.
Definition: GA_Detail.h:203
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:974
virtual int initialize(const UT_BoundingBox *)
void fractalSplit()
Split into 4 new procedurals (each rendering a triangle)
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)
SYS_FORCE_INLINE T & x(void)
Definition: UT_Vector3.h:581
png_uint_32 i
Definition: png.h:2877
void expandBounds(T relative, T absolute)
SYS_FORCE_INLINE T & z(void)
Definition: UT_Vector3.h:585
virtual const char * className() const
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:125
SYS_FORCE_INLINE GA_Offset getPointOffset(GA_Size i) const
Definition: GEO_TriMesh.h:164
RAY_ProceduralChildPtr createChild() const
void enlargeBounds(const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
virtual void getBoundingBox(UT_BoundingBox &box)
The bounding box is the "object space" bounds of the procedural.
void assign(fpreal x, fpreal y, fpreal z, uint s)
unsigned int uint
Definition: SYS_Types.h:33
Parameter definition for arguments to RAY_Procedural.
virtual RAY_ProceduralArg * arguments() const
Provide a const reference to the arguments for the procedural.
SYS_FORCE_INLINE T & y(void)
Definition: UT_Vector3.h:583
double fpreal
Definition: SYS_Types.h:263
fpreal getLevelOfDetail(const UT_BoundingBox &box) const
SYS_FORCE_INLINE GA_Offset getMapOffset() const
Gets the offset of this primitive in the detail containing it.
Definition: GA_Primitive.h:137
void fractalRender()
Render triangle geometry.
SYS_FORCE_INLINE void initBounds()
virtual RAY_Procedural * create() const
Create a procedural, and pass ownership of the instance to mantra.
GU_Detail * get(int segment=0) const
Class to create a procedural.
GLint lod
Definition: glcorearb.h:2764
void registerProcedural(RAY_ProceduralFactory *factory)
Modern interface to register procedurals.
#define MAX_SPLITS