Tachyon (current)  Current Main Branch
apitrigeom.c
Go to the documentation of this file.
1 /*
2  * apitrigeom.c - This file contains code for generating triangle tesselated
3  * geometry, for use with OpenGL, XGL, etc.
4  *
5  * (C) Copyright 1994-2022 John E. Stone
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * $Id: apitrigeom.c,v 1.12 2022/02/18 17:55:28 johns Exp $
9  *
10  */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <math.h>
16 
17 #define TACHYON_INTERNAL 1
18 #include "tachyon.h"
19 #include "apitrigeom.h"
20 
21 /* XXX Hack! This needs cleanup.. */
22 void VNorm(apivector * a);
23 void VCross(apivector * a, apivector * b, apivector * c);
24 void VAddS(flt a, apivector * A, apivector * B, apivector * C);
25 void VAdd(apivector * a, apivector * b, apivector * c);
26 void VSub(apivector * a, apivector * b, apivector * c);
27 flt VDot(apivector *a, apivector *b);
28 
29 #define CYLFACETS 36
30 #define RINGFACETS 36
31 #define SPHEREFACETS 25
32 
33 void rt_tri_fcylinder(SceneHandle scene, void * tex, apivector ctr, apivector axis, flt rad) {
34  apivector x, y, z, tmp;
35  double u, v, u2, v2;
36  int j;
37  apivector p1, p2, p3, p4;
38  apivector n1, n2;
39 
40  z = axis;
41  VNorm(&z);
42  tmp.x = z.y - 2.1111111;
43  tmp.y = -z.z + 3.14159267;
44  tmp.z = z.x - 3.915292342341;
45  VNorm(&z);
46  VNorm(&tmp);
47  VCross(&z, &tmp, &x);
48  VNorm(&x);
49  VCross(&x, &z, &y);
50  VNorm(&y);
51 
52  for (j=0; j<CYLFACETS; j++) {
53  u = rad * SIN((6.28 * j) / (CYLFACETS - 1.0));
54  v = rad * COS((6.28 * j) / (CYLFACETS - 1.0));
55  u2 = rad * SIN((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
56  v2 = rad * COS((6.28 * (j + 1.0)) / (CYLFACETS - 1.0));
57 
58  p1.x = p1.y = p1.z = 0.0;
59  p4 = p3 = p2 = p1;
60 
61  VAddS(u, &x, &p1, &p1);
62  VAddS(v, &y, &p1, &p1);
63  n1 = p1;
64  VNorm(&n1);
65  VAddS(1.0, &ctr, &p1, &p1);
66 
67 
68  VAddS(u2, &x, &p2, &p2);
69  VAddS(v2, &y, &p2, &p2);
70  n2 = p2;
71  VNorm(&n2);
72  VAddS(1.0, &ctr, &p2, &p2);
73 
74  VAddS(1.0, &axis, &p1, &p3);
75  VAddS(1.0, &axis, &p2, &p4);
76 
77  rt_stri(scene, tex, p1, p2, p3, n1, n2, n1);
78  rt_stri(scene, tex, p3, p2, p4, n1, n2, n2);
79  }
80 }
81 
82 void rt_tri_cylinder(SceneHandle scene, void * tex, apivector ctr, apivector axis, flt rad) {
83  rt_fcylinder(scene, tex, ctr, axis, rad);
84 }
85 
86 void rt_tri_ring(SceneHandle scene, void * tex, apivector ctr, apivector norm, flt a, flt b) {
87  apivector x, y, z, tmp;
88  double u, v, u2, v2;
89  int j;
90  apivector p1, p2, p3, p4;
91  apivector n1, n2;
92 
93  z = norm;
94  VNorm(&z);
95  tmp.x = z.y - 2.1111111;
96  tmp.y = -z.z + 3.14159267;
97  tmp.z = z.x - 3.915292342341;
98  VNorm(&z);
99  VNorm(&tmp);
100  VCross(&z, &tmp, &x);
101  VNorm(&x);
102  VCross(&x, &z, &y);
103  VNorm(&y);
104 
105  for (j=0; j<RINGFACETS; j++) {
106  u = SIN((6.28 * j) / (RINGFACETS - 1.0));
107  v = COS((6.28 * j) / (RINGFACETS - 1.0));
108  u2 = SIN((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
109  v2 = COS((6.28 * (j + 1.0)) / (RINGFACETS - 1.0));
110 
111  p1.x = p1.y = p1.z = 0.0;
112  p4 = p3 = p2 = p1;
113 
114  VAddS(u, &x, &p1, &p1);
115  VAddS(v, &y, &p1, &p1);
116  n1 = p1;
117  VNorm(&n1);
118  VAddS(a, &n1, &ctr, &p1);
119  VAddS(b, &n1, &ctr, &p3);
120 
121  VAddS(u2, &x, &p2, &p2);
122  VAddS(v2, &y, &p2, &p2);
123  n2 = p2;
124  VNorm(&n2);
125  VAddS(a, &n2, &ctr, &p2);
126  VAddS(b, &n2, &ctr, &p4);
127 
128  rt_stri(scene, tex, p1, p2, p3, norm, norm, norm);
129  rt_stri(scene, tex, p3, p2, p4, norm, norm, norm);
130 
131  }
132 }
133 
134 void rt_tri_box(SceneHandle scene, void * tex, apivector min, apivector max) {
135  /* -XY face */
136  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
137  rt_vector(min.x, max.y, min.z),
138  rt_vector(max.x, max.y, min.z));
139  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
140  rt_vector(max.x, max.y, min.z),
141  rt_vector(max.x, min.y, min.z));
142 
143  /* +XY face */
144  rt_tri(scene, tex, rt_vector(min.x, min.y, max.z),
145  rt_vector(max.x, max.y, max.z),
146  rt_vector(min.x, max.y, max.z));
147  rt_tri(scene, tex, rt_vector(min.x, min.y, max.z),
148  rt_vector(max.x, min.y, max.z),
149  rt_vector(max.x, max.y, max.z));
150 
151  /* -YZ face */
152  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
153  rt_vector(min.x, max.y, max.z),
154  rt_vector(min.x, min.y, max.z));
155  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
156  rt_vector(min.x, max.y, min.z),
157  rt_vector(min.x, max.y, max.z));
158 
159  /* +YZ face */
160  rt_tri(scene, tex, rt_vector(max.x, min.y, min.z),
161  rt_vector(max.x, min.y, max.z),
162  rt_vector(max.x, max.y, max.z));
163  rt_tri(scene, tex, rt_vector(max.x, min.y, min.z),
164  rt_vector(max.x, max.y, max.z),
165  rt_vector(max.x, max.y, min.z));
166 
167  /* -XZ face */
168  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
169  rt_vector(min.x, min.y, max.z),
170  rt_vector(max.x, min.y, max.z));
171  rt_tri(scene, tex, rt_vector(min.x, min.y, min.z),
172  rt_vector(max.x, min.y, max.z),
173  rt_vector(max.x, min.y, min.z));
174 
175  /* +XZ face */
176  rt_tri(scene, tex, rt_vector(min.x, max.y, min.z),
177  rt_vector(max.x, max.y, max.z),
178  rt_vector(min.x, max.y, max.z));
179  rt_tri(scene, tex, rt_vector(min.x, max.y, min.z),
180  rt_vector(max.x, max.y, min.z),
181  rt_vector(max.x, max.y, max.z));
182 }
183 
184 void rt_tri_sphere(SceneHandle scene, void * tex, apivector ctr, flt rad) {
185 }
186 
187 void rt_tri_plane(SceneHandle scene, void * tex, apivector ctr, apivector norm) {
188  rt_tri_ring(scene, tex, ctr, norm, 0.0, 10000.0);
189 }
190 
void rt_tri_plane(SceneHandle scene, void *tex, apivector ctr, apivector norm)
Definition: apitrigeom.c:187
void rt_tri_fcylinder(SceneHandle scene, void *tex, apivector ctr, apivector axis, flt rad)
Definition: apitrigeom.c:33
#define CYLFACETS
Definition: apitrigeom.c:29
#define RINGFACETS
Definition: apitrigeom.c:30
void VSub(apivector *a, apivector *b, apivector *c)
void VAddS(flt a, apivector *A, apivector *B, apivector *C)
flt VDot(apivector *a, apivector *b)
void rt_tri_sphere(SceneHandle scene, void *tex, apivector ctr, flt rad)
Definition: apitrigeom.c:184
double flt
generic floating point number, using double
Definition: tachyon.h:47
void rt_tri(SceneHandle voidscene, void *tex, apivector v0, apivector v1, apivector v2)
Define a flat-shaded triangle.
Definition: api.c:1224
#define SIN(x)
Definition: util.h:30
void VCross(apivector *a, apivector *b, apivector *c)
flt x
X coordinate or direction component.
Definition: tachyon.h:54
#define COS(x)
Definition: util.h:26
flt y
Y coordinate or direction component.
Definition: tachyon.h:55
void rt_fcylinder(SceneHandle scene, void *tex, apivector ctr, apivector axis, flt rad)
Define a finite-length cylinder.
Definition: api.c:1173
void rt_tri_ring(SceneHandle scene, void *tex, apivector ctr, apivector norm, flt a, flt b)
Definition: apitrigeom.c:86
void * SceneHandle
Definition: tachyon.h:51
void rt_stri(SceneHandle voidscene, void *tex, apivector v0, apivector v1, apivector v2, apivector n0, apivector n1, apivector n2)
Define a smooth-shaded triangle using interpolated vertex normals.
Definition: api.c:1252
apivector rt_vector(flt x, flt y, flt z)
Helper function to make vectors.
Definition: api.c:159
void rt_tri_cylinder(SceneHandle scene, void *tex, apivector ctr, apivector axis, flt rad)
Definition: apitrigeom.c:82
void rt_tri_box(SceneHandle scene, void *tex, apivector min, apivector max)
Definition: apitrigeom.c:134
void VAdd(apivector *a, apivector *b, apivector *c)
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
flt z
Z coordinate or direction component.
Definition: tachyon.h:56
void VNorm(apivector *a)