HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i3dsphere.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  */
27 
28 #include <stdio.h>
29 #include <iostream>
30 #include <IMG3D/IMG3D_Manager.h>
31 #include <UT/UT_Vector3.h>
32 #include <UT/UT_BoundingBox.h>
33 #include <UT/UT_Exit.h>
34 #include <UT/UT_Main.h>
35 #include <UT/UT_Options.h>
36 
37 namespace HDK_Sample {
38 
39 static void
40 error(const char *msg)
41 {
42  fprintf(stderr, "Error: %s\n", msg);
44 }
45 
46 static void
47 i3dSphere(int nvalues,
48  const UT_Vector3 *P,
49  fpreal32 *result[],
50  const char *names[],
51  int sizes[],
52  int for_aa)
53 {
54  fpreal32 *density;
55  fpreal32 *color;
56  int i;
57  fpreal d;
58 
59  // Verify correct channels
60  UT_ASSERT(!strcmp(names[0], "density") && sizes[0] == 1);
61  UT_ASSERT(!strcmp(names[1], "color") && sizes[1] == 3);
62  density = result[0];
63  color = result[1];
64 
65  for (i = 0; i < nvalues; i++)
66  {
67  // Density is 1 inside the sphere and 0 outside
68  d = P[i].length() > 1 ? 0 : 1;
69  *density = d;
70  color[0] = d * SYSfit(P[i].x(), -1, 1, 0, 1);
71  color[1] = d * SYSfit(P[i].y(), -1, 1, 0, 1);
72  color[2] = d * SYSfit(P[i].z(), -1, 1, 0, 1);
73 
74  // Now, move the result buffers to the next pixel
75  density += 1; // Skip to the next float
76  color += 3; // Skip three floats
77  }
78 }
79 
80 } // End of HDK_Sample namespace
81 
82 using namespace HDK_Sample;
83 
84 int
85 theMain(int argc, char *argv[])
86 {
87  IMG3D_Manager fp;
88  UT_BoundingBox bounds;
89  const char *chnames[2] = { "density", "color" };
90  int chsizes[2] = { 1, 3 };
91 
92  printf("Generating sphere.i3d\n");
93  bounds.initBounds(-1, -1, -1);
94  bounds.enlargeBounds(1, 1, 1);
95  if (!fp.createTexture("sphere.i3d", bounds, 10, 10, 10))
96  error("Unable to createTexture()");
97 
98  if (!fp.fillTexture(2, chnames, chsizes, i3dSphere, 1))
99  error("Unable to fill the texture");
100 
101  if (!fp.exportTag("software", "hdk_isosphere"))
102  error("Unable to save software tag");
103 
104  if (!fp.closeTexture())
105  error("Unable to close texture");
106 
107  return 0;
108 }
109 UT_MAIN(theMain);// exit with proper tear down
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
Definition: glcorearb.h:2621
int fillTexture(int num_channels, const char *channel_names[], int channel_sizes[], IMG3D_TextureEval evaluator, int max_samples=4, fpreal variance=0.005, fpreal filter_width=1, fpreal jitter=1)
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
int closeTexture()
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLint y
Definition: glcorearb.h:103
constexpr SYS_FORCE_INLINE T length() const noexcept
Definition: UT_Vector3.h:361
**But if you need a result
Definition: thread.h:613
float fpreal32
Definition: SYS_Types.h:200
int theMain(int argc, char *argv[])
Definition: i3dsphere.C:85
catch-all error code
Definition: UT_Exit.h:42
static SYS_FUNC_NORETURN void exit(UT_ExitCode exit_code=EXIT_OK)
Calls exit(), which implicitly leads to our callbacks being called.
< returns > If no error
Definition: snippets.dox:2
bool exportTag(const char *name, int value)
UT_MAIN(theMain)
void enlargeBounds(const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
GLint GLenum GLint x
Definition: glcorearb.h:409
auto fprintf(std::FILE *f, const S &fmt, const T &...args) -> int
Definition: printf.h:602
GLuint color
Definition: glcorearb.h:1261
int createTexture(const char *filename, const UT_BoundingBox &box, int xres, int yres, int zres, int compression=5)
fpreal64 fpreal
Definition: SYS_Types.h:277
SYS_FORCE_INLINE void initBounds()
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Class to handle reading/writing 3D texture images.
Definition: IMG3D_Manager.h:69