1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 #if __MINGW32__
47 #include <malloc.h>
48 #elif _WIN32
49 #include <malloc.h>
50 #define alloca _alloca
51 #elif __FreeBSD__ || __NetBSD__ || __OpenBSD__
52 #include <stdlib.h>
53 #else
54 #include <alloca.h>
55 #endif
56 
57 /*
58  * types.h - This file contains all of the type definitions for the raytracer
59  *
60  *  $Id: types.h,v 1.2 2007-02-22 17:54:16 Exp $
61  */
62 
63 #define MAXOCTNODES 25 /* subdivide octants /w > # of children */
64 #define SPEPSILON   0.000001 /* amount to crawl down a ray           */
65 #define EPSILON     0.000001 /* amount to crawl down a ray           */
66 #define TWOPI       6.2831853 /* guess                               */
67 #define FHUGE       1e18 /* biggest fp number we can represent       */
68 
69 /* Maximum internal table sizes */
70 /* Use prime numbers for best memory system performance */
71 #define INTTBSIZE 1024 /* maximum intersections we can hold */
72 #define MAXLIGHTS 39 /* maximum number of lights in a scene */
73 #define MAXIMGS   39 /* maximum number of distinct images   */
74 #define RPCQSIZE  113 /* number of RPC messages to queue    */
75 
76 /* Parameter values for rt_boundmode() */
77 #define RT_BOUNDING_DISABLED 0 /* spatial subdivision/bounding disabled */
78 #define RT_BOUNDING_ENABLED  1 /* spatial subdivision/bounding enabled  */
79 
80 /* Parameter values for rt_displaymode() */
81 #define RT_DISPLAY_DISABLED 0 /* video output enabled  */
82 #define RT_DISPLAY_ENABLED  1 /* video output disabled */
83 
84 /* Ray flags */
85 #define RT_RAY_REGULAR  1
86 #define RT_RAY_SHADOW   2
87 #define RT_RAY_BOUNDED  4
88 #define RT_RAY_FINISHED 8
89 
90 #ifdef USESINGLEFLT
91 typedef float flt; /* generic floating point number, using float */
92 #else
93 typedef double flt; /* generic floating point number, using double */
94 #endif
95 
96 typedef unsigned char byte_t; /* 1 byte */
97 typedef signed int word_t; /* 32 bit integer */
98 
99 typedef struct {
100     flt x; /* X coordinate value */
101     flt y; /* Y coordinate value */
102     flt z; /* Z coordinate value */
103 } vector;
104 
105 typedef struct {
106     flt r; /* Red component   */
107     flt g; /* Green component */
108     flt b; /* Blue component  */
109 } color;
110 
111 typedef struct {
112     byte_t r; /* Red component   */
113     byte_t g; /* Green component */
114     byte_t b; /* Blue component  */
115 } bytecolor;
116 
117 typedef struct { /* Raw 24 bit image structure, for tga, ppm etc */
118     int loaded; /* image memory residence flag */
119     int xres; /* image X axis size */
120     int yres; /* image Y axis size */
121     int bpp; /* image bits per pixel */
122     char name[96]; /* image filename (with path)     */
123     unsigned char *data; /* pointer to raw byte image data */
124 } rawimage;
125 
126 typedef struct { /* Scalar Volume Data */
127     int loaded; /* Volume data memory residence flag */
128     int xres; /* volume X axis size */
129     int yres; /* volume Y axis size */
130     int zres; /* volume Z axis size */
131     flt opacity; /* opacity per unit length */
132     char name[96]; /* Volume data filename */
133     unsigned char *data; /* pointer to raw byte volume data */
134 } scalarvol;
135 
136 typedef struct {
137     color (*texfunc)(void *, void *, void *);
138     int shadowcast; /* does the object cast a shadow */
139     int islight; /* light flag... */
140     color col; /* base object color */
141     flt ambient; /* ambient lighting */
142     flt diffuse; /* diffuse reflection */
143     flt phong; /* phong specular highlights */
144     flt phongexp; /* phong exponent/shininess factor */
145     int phongtype; /* phong type: 0 == plastic, nonzero == metal */
146     flt specular; /* specular reflection */
147     flt opacity; /* how opaque the object is */
148     vector ctr; /* origin of texture */
149     vector rot; /* rotation of texture about origin */
150     vector scale; /* scale of texture in x,y,z */
151     vector uaxs; /* planar map U axis */
152     vector vaxs; /* planar map V axis */
153     void *img; /* pointer to image for image mapping */
154     void *obj; /* object ptr, hack for volume shaders for now */
155 } texture;
156 
157 typedef struct {
158     void (*intersect)(void *, void *); /* intersection func ptr  */
159     void (*normal)(void *, void *, void *, void *); /* normal function ptr */
160     int (*bbox)(void *, vector *, vector *); /* return the object bbox */
161     void (*free)(void *); /* free the object */
162 } object_methods;
163 
164 typedef struct {
165     unsigned int id; /* Unique Object serial number */
166     void *nextobj; /* pointer to next object in list */
167     object_methods *methods; /* this object's methods */
168     texture *tex; /* object texture */
169 } object;
170 
171 typedef struct {
172     object *obj; /* to object we hit */
173     flt t; /* distance along the ray to the hit point */
174 } intersection;
175 
176 typedef struct {
177     int num; /* number of intersections    */
178     intersection closest; /* closest intersection > 0.0 */
179     intersection list[INTTBSIZE]; /* list of all intersections  */
180 } intersectstruct;
181 
182 typedef struct {
183     char outfilename[200]; /* name of the output image */
184     unsigned char *rawimage; /* pointer to a raw rgb image to be stored */
185     int hres; /* horizontal output image resolution */
186     int vres; /* vertical output image resolution */
187     flt aspectratio; /* aspect ratio of output image */
188     int raydepth; /* maximum recursion depth */
189     int antialiasing; /* number of antialiasing rays to fire */
190     int verbosemode; /* verbose reporting flag */
191     int boundmode; /* automatic spatial subdivision flag */
192     int boundthresh; /* threshold number of subobjects */
193     int displaymode; /* run-time X11 display flag */
194     vector camcent; /* center of the camera in world coords */
195     vector camviewvec; /* view direction of the camera (Z axis) */
196     vector camrightvec; /* right axis for the camera (X axis) */
197     vector camupvec; /* up axis for the camera (Y axis) */
198     flt camzoom; /* zoom factor for the camera */
199     color background; /* scene background color */
200 } scenedef;
201 
202 typedef struct {
203     intersectstruct *intstruct; /* ptr to thread's intersection data */
204     unsigned int depth; /* levels left to recurse.. (maxdepth - curdepth) */
205     unsigned int flags; /* ray flags, any special treatment needed etc */
206     unsigned int serial; /* serial number of the ray */
207     unsigned int *mbox; /* mailbox array for optimizing intersections */
208     vector o; /* origin of the ray X,Y,Z */
209     vector d; /* normalized direction of the ray */
210     flt maxdist; /* maximum distance to search for intersections */
211     vector s; /* startpoint of the ray (may differ from origin */
212     vector e; /* endpoint of the ray if bounded */
213     scenedef *scene; /* pointer to the scene, for global parms such as */
214     /* background colors etc */
215 } ray;
216 
217 typedef struct {
218     int type; /* RPC call type */
219     int from; /* Sending processor */
220     int len; /* length of parms in bytes */
221     void *parms; /* Parameters to RPC */
222 } rpcmsg;
223