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 /*
47  * apitrigeom.cpp - This file contains code for generating triangle tessellated
48  *                geometry, for use with OpenGL, XGL, etc.
49  */
50 
51 #include "machine.hpp"
52 #include "types.hpp"
53 #include "api.hpp"
54 #include "macros.hpp"
55 #include "vector.hpp"
56 
57 #define MyVNorm(a)          VNorm((vector *)a)
58 #define MyVCross(a, b, c)   VCross((vector *)a, (vector *)b, (vector *)c)
59 #define MyVAddS(x, a, b, c) VAddS((flt)x, (vector *)a, (vector *)b, (vector *)c)
60 
61 #define CYLFACETS    36
62 #define RINGFACETS   36
63 #define SPHEREFACETS 25
64 
rt_tri_fcylinder(void * tex,vector ctr,vector axis,apiflt rad)65 void rt_tri_fcylinder(void *tex, vector ctr, vector axis, apiflt rad) {
66     vector x, y, z, tmp;
67     double u, v, u2, v2;
68     int j;
69     vector p1, p2, p3, p4;
70     vector n1, n2;
71 
72     z = axis;
73     MyVNorm(&z);
74     tmp.x = z.y - 2.1111111;
75     tmp.y = -z.z + 3.14159267;
76     tmp.z = z.x - 3.915292342341;
77     MyVNorm(&z);
78     MyVNorm(&tmp);
79     MyVCross(&z, &tmp, &x);
80     MyVNorm(&x);
81     MyVCross(&x, &z, &y);
82     MyVNorm(&y);
83 
84     for (j = 0; j < CYLFACETS; j++) {
85         u = rad * sin((6.28 * j) / (CYLFACETS - 1.0));
86         v = rad * cos((6.28 * j) / (CYLFACETS - 1.0));
87         u2 = rad * sin((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
88         v2 = rad * cos((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
89 
90         p1.x = p1.y = p1.z = 0.0;
91         p4 = p3 = p2 = p1;
92 
93         MyVAddS(u, &x, &p1, &p1);
94         MyVAddS(v, &y, &p1, &p1);
95         n1 = p1;
96         MyVNorm(&n1);
97         MyVAddS(1.0, &ctr, &p1, &p1);
98 
99         MyVAddS(u2, &x, &p2, &p2);
100         MyVAddS(v2, &y, &p2, &p2);
101         n2 = p2;
102         MyVNorm(&n2);
103         MyVAddS(1.0, &ctr, &p2, &p2);
104 
105         MyVAddS(1.0, &axis, &p1, &p3);
106         MyVAddS(1.0, &axis, &p2, &p4);
107 
108         rt_stri(tex, p1, p2, p3, n1, n2, n1);
109         rt_stri(tex, p3, p2, p4, n1, n2, n2);
110     }
111 }
112 
rt_tri_cylinder(void * tex,vector ctr,vector axis,apiflt rad)113 void rt_tri_cylinder(void *tex, vector ctr, vector axis, apiflt rad) {
114     rt_fcylinder(tex, ctr, axis, rad);
115 }
116 
rt_tri_ring(void * tex,vector ctr,vector norm,apiflt a,apiflt b)117 void rt_tri_ring(void *tex, vector ctr, vector norm, apiflt a, apiflt b) {
118     vector x, y, z, tmp;
119     double u, v, u2, v2;
120     int j;
121     vector p1, p2, p3, p4;
122     vector n1, n2;
123 
124     z = norm;
125     MyVNorm(&z);
126     tmp.x = z.y - 2.1111111;
127     tmp.y = -z.z + 3.14159267;
128     tmp.z = z.x - 3.915292342341;
129     MyVNorm(&z);
130     MyVNorm(&tmp);
131     MyVCross(&z, &tmp, &x);
132     MyVNorm(&x);
133     MyVCross(&x, &z, &y);
134     MyVNorm(&y);
135 
136     for (j = 0; j < RINGFACETS; j++) {
137         u = sin((6.28 * j) / (RINGFACETS - 1.0));
138         v = cos((6.28 * j) / (RINGFACETS - 1.0));
139         u2 = sin((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
140         v2 = cos((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
141 
142         p1.x = p1.y = p1.z = 0.0;
143         p4 = p3 = p2 = p1;
144 
145         MyVAddS(u, &x, &p1, &p1);
146         MyVAddS(v, &y, &p1, &p1);
147         n1 = p1;
148         MyVNorm(&n1);
149         MyVAddS(a, &n1, &ctr, &p1);
150         MyVAddS(b, &n1, &ctr, &p3);
151 
152         MyVAddS(u2, &x, &p2, &p2);
153         MyVAddS(v2, &y, &p2, &p2);
154         n2 = p2;
155         MyVNorm(&n2);
156         MyVAddS(a, &n2, &ctr, &p2);
157         MyVAddS(b, &n2, &ctr, &p4);
158 
159         rt_stri(tex, p1, p2, p3, norm, norm, norm);
160         rt_stri(tex, p3, p2, p4, norm, norm, norm);
161     }
162 }
163 
rt_tri_box(void * tex,vector min,vector max)164 void rt_tri_box(void *tex, vector min, vector max) {
165     /* -XY face */
166     rt_tri(tex,
167            rt_vector(min.x, min.y, min.z),
168            rt_vector(min.x, max.y, min.z),
169            rt_vector(max.x, max.y, min.z));
170     rt_tri(tex,
171            rt_vector(min.x, min.y, min.z),
172            rt_vector(max.x, max.y, min.z),
173            rt_vector(max.x, min.y, min.z));
174 
175     /* +XY face */
176     rt_tri(tex,
177            rt_vector(min.x, min.y, max.z),
178            rt_vector(max.x, max.y, max.z),
179            rt_vector(min.x, max.y, max.z));
180     rt_tri(tex,
181            rt_vector(min.x, min.y, max.z),
182            rt_vector(max.x, min.y, max.z),
183            rt_vector(max.x, max.y, max.z));
184 
185     /* -YZ face */
186     rt_tri(tex,
187            rt_vector(min.x, min.y, min.z),
188            rt_vector(min.x, max.y, max.z),
189            rt_vector(min.x, min.y, max.z));
190     rt_tri(tex,
191            rt_vector(min.x, min.y, min.z),
192            rt_vector(min.x, max.y, min.z),
193            rt_vector(min.x, max.y, max.z));
194 
195     /* +YZ face */
196     rt_tri(tex,
197            rt_vector(max.x, min.y, min.z),
198            rt_vector(max.x, min.y, max.z),
199            rt_vector(max.x, max.y, max.z));
200     rt_tri(tex,
201            rt_vector(max.x, min.y, min.z),
202            rt_vector(max.x, max.y, max.z),
203            rt_vector(max.x, max.y, min.z));
204 
205     /* -XZ face */
206     rt_tri(tex,
207            rt_vector(min.x, min.y, min.z),
208            rt_vector(min.x, min.y, max.z),
209            rt_vector(max.x, min.y, max.z));
210     rt_tri(tex,
211            rt_vector(min.x, min.y, min.z),
212            rt_vector(max.x, min.y, max.z),
213            rt_vector(max.x, min.y, min.z));
214 
215     /* +XZ face */
216     rt_tri(tex,
217            rt_vector(min.x, max.y, min.z),
218            rt_vector(max.x, max.y, max.z),
219            rt_vector(min.x, max.y, max.z));
220     rt_tri(tex,
221            rt_vector(min.x, max.y, min.z),
222            rt_vector(max.x, max.y, min.z),
223            rt_vector(max.x, max.y, max.z));
224 }
225 
rt_tri_sphere(void * tex,vector ctr,apiflt rad)226 void rt_tri_sphere(void *tex, vector ctr, apiflt rad) {}
227 
rt_tri_plane(void * tex,vector ctr,vector norm)228 void rt_tri_plane(void *tex, vector ctr, vector norm) {
229     rt_tri_ring(tex, ctr, norm, 0.0, 10000.0);
230 }
231