-
Notifications
You must be signed in to change notification settings - Fork 0
/
sset.c
341 lines (320 loc) · 9.68 KB
/
sset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "sset.h"
static void
die(char * f, ...){
va_list ap;
va_start(ap, f);
vfprintf(stderr, f, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
v4_to_float3(void *out, v4 in){
*(float *)(out) = in.x;
*(float *)(out+sizeof(float)) = in.y;
*(float *)(out+2*sizeof(float)) = in.z;
}
/* read into a v4 from something like 1 1 1.
* returns 1 if all OK, 0 if not all OK. */
int
sreadv4(char *in, v4 *v){
double x, y, z;
if(sscanf(in, "%lf %lf %lf", &x, &y, &z) != 3) return 0;
v4 tmp = mkv4(x, y, z, 0);
memcpy((void *)v, (void *)&tmp, sizeof(v4));
return 1;
}
/* rtrt: read only the first cam */
void
mkcam(XMLNode * node){
v4 tmp;
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Position", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(o, tmp);
}else if(strcmp("Gaze", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(gaze, norv4(tmp));
vec3_norm(gaze, gaze);
}else if(strcmp("Up", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(up, norv4(tmp));
vec3_norm(up, up);
}else if(strcmp("NearDistance", n->tag) == 0){
sscanf(n->text, "%f", &d);
}else if(strcmp("NearPlane", n->tag) == 0){
sscanf(n->text, "%f %f %f %f", &nplane[0], &nplane[1], &nplane[2], &nplane[3]);
}
}
/* orthonormalize */
vec3_mul_cross(right, gaze, up);
vec3_norm(right, right);
vec3_mul_cross(up, right, gaze);
vec3_norm(up, up);
}
void
mkptlight(XMLNode * node){
Light q;
v4 tmp;
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Position", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.o, tmp);
}else if(strcmp("Intensity", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.lum, tmp);
}
}
scene.lights[scene.lightc++] = q;
}
void
mklights(XMLNode * node){
v4 tmp;
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("AmbientLight", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&scene.amb, tmp);
}else if(strcmp("PointLight", n->tag) == 0){
mkptlight(n);
}else if(strcmp("AreaLight", n->tag) == 0){
mkptlight(n);
}
}
}
void
mkmatl(XMLNode * node){
Matl q;
v4 tmp;
memset(&q, 0, sizeof(Matl));
int i, mirror = 0, transp = 0;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("AmbientReflectance", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.amb, tmp);
}else if(strcmp("DiffuseReflectance", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.diff, tmp);
}else if(strcmp("SpecularReflectance", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.spec, tmp);
}else if(strcmp("PhongExponent", n->tag) == 0){
sscanf(n->text, "%d", (int *)&q.phong);
}else if(strcmp("MirrorReflectance", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.mirror, tmp);
if(q.mirror[0] != 0 && q.mirror[1] != 0 && q.mirror[2] != 0) mirror = 1;
}else if(strcmp("Transparency", n->tag) == 0){
sreadv4(n->text, &tmp);
v4_to_float3(&q.transp, tmp);
if(q.transp[0] != 0 && q.transp[1] != 0 && q.transp[2] != 0) transp = 1;
}else if(strcmp("RefractionIndex", n->tag) == 0){
sscanf(n->text, "%f", (float *)&q.eta);
}
}
if(mirror) if(transp) die("mkmatl: Undefined material %d has both MirrorReflectance and Transparency\n");
else q.type = M_MIRROR;
else if(transp) q.type = M_DIELECTRIC;
else q.type = M_PHONG;
scene.matls[scene.matlc++] = q;
}
void
mkmatls(XMLNode * node){
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Material", n->tag) == 0) mkmatl(n);
else die("mkmatls: Unknown tag name in Materials: %s\n", n->tag);
}
}
/* vertices are points. their t is 1 */
void
mkvertices(XMLNode * node){
for(char * v = strtok(node->text, "\n"); v != NULL; v = strtok(NULL, "\n")){
if(!sreadv4(v, &(vertices[vertexc++]))) vertexc--;
vertices[vertexc-1].t = 1;
}
}
/* first we reverse the string, then parse like 1t 1s 1r 2t. this way we can go along multiplying matrices */
m4
mktransmtx(XMLNode * node){
int i, j, x; char t; m4 out = mkm4(mkv4(1, 0, 0, 0), mkv4(0, 1, 0, 0), mkv4(0, 0, 1, 0), mkv4(0, 0, 0, 1));
char * s = node->text;
if(s && strlen(s)) for(i=0, j=strlen(node->text)-1; i < j; i++,j--){ t = s[i]; s[i] = s[j]; s[j] = t; }
for(char * v = strtok(s, " "); v != NULL; v = strtok(NULL, " ")){
if(sscanf(v, "%d%c", &x, &t) == 2){
switch(t){
case 't': out = mulm4(out, trs[x-1]); break;
case 'r': out = mulm4(out, rots[x-1]); break;
case 's': out = mulm4(out, scs[x-1]); break;
default: die("mktransmtx: bogus transformation type: %c\n", t);
}
}
}
return out;
}
/* we precompute trgl normal N, d and perpendicular planes to trgl N1, d1, N2, d2.
* see rt.c:/^trglxray/ also see dat.h:/^struct Trgl/ */
Obj
preptrgl(int matl_idx, v4 v1, v4 v2, v4 v3){
Obj q;
q.type = O_TRGL;
q.matl_idx = matl_idx - 1;
v4_to_float3(&q.trgl.v1, v1);
v4_to_float3(&q.trgl.v2, v2);
v4_to_float3(&q.trgl.v3, v3);
return q;
}
void
mktrgl(XMLNode * node){
int matl_idx;
int i, a, b, c;
m4 q = mkm4(mkv4(1, 0, 0, 0), mkv4(0, 1, 0, 0), mkv4(0, 0, 1, 0), mkv4(0, 0, 0, 1));
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Material", n->tag) == 0){
sscanf(n->text, "%d", &matl_idx);
}else if(strcmp("Indices", n->tag) == 0){
sscanf(n->text, "%d %d %d", &a, &b, &c);
}else if(strcmp("Transformations", n->tag) == 0){
q = mktransmtx(n);
}else{
die("mktrgl: Unknown tag name in Triangle: %s\n", n->tag);
}
}
v4 p1 = vecm4(q, vertices[a-1]), p2 = vecm4(q, vertices[b-1]), p3 = vecm4(q, vertices[c-1]);
scene.objs[scene.objc++] = preptrgl(matl_idx, p1, p2, p3);
}
/* first precompute given triangles. then loop through them, add their normals to respective vertex normals, normalize
* see dat.h:/Mesh/ */
int trgls[MESHOBJ_MAX][3];
Obj outobjs[MESHOBJ_MAX];
void
mkmesh(XMLNode * node){
int matl_idx;
int i, trglc=0, off;
m4 q = mkm4(mkv4(1, 0, 0, 0), mkv4(0, 1, 0, 0), mkv4(0, 0, 1, 0), mkv4(0, 0, 0, 1));
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Material", n->tag) == 0){
sscanf(n->text, "%d", &matl_idx);
}else if(strcmp("Faces", n->tag) == 0){
off=0;
if(n->n_attributes && strcmp(n->attributes[0].name, "vertexOffset") == 0) sscanf(n->attributes[0].value, "%d", &off);
int a, b, c;
for(char * v = strtok(n->text, "\n"); v != NULL; v = strtok(NULL, "\n")){
if(sscanf(v, "%d %d %d", &a, &b, &c) == 3){
trgls[trglc][0] = a+off; trgls[trglc][1] = b+off; trgls[trglc][2] = c+off;
trglc++;
}
}
}else if(strcmp("Transformations", n->tag) == 0){
q = mktransmtx(n);
}else{
die("mkmesh: Unknown tag name in Mesh: %s\n", n->tag);
}
}
for(i=0; i<trglc; i++){
v4 p1 = vecm4(q, vertices[trgls[i][0]-1]), p2 = vecm4(q, vertices[trgls[i][1]-1]), p3 = vecm4(q, vertices[trgls[i][2]-1]);
outobjs[i] = preptrgl(matl_idx, p1, p2, p3);
}
for(i=0; i<trglc; i++) scene.objs[scene.objc++] = outobjs[i];
}
void
mksph(XMLNode * node){
Obj q;
q.type = O_SPH;
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Material", n->tag) == 0){
sscanf(n->text, "%d", &q.matl_idx);
q.matl_idx--;
}else if(strcmp("Center", n->tag) == 0){
int a;
sscanf(n->text, "%d", &a);
v4_to_float3(&q.sph.o, vertices[a-1]);
}else if(strcmp("Radius", n->tag) == 0){
sscanf(n->text, "%f", &q.sph.r);
}
}
scene.objs[scene.objc++] = q;
}
void
mkobjs(XMLNode * node){
int i;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Mesh", n->tag) == 0){
mkmesh(n);
}else if(strcmp("Triangle", n->tag) == 0){
mktrgl(n);
}else if(strcmp("Sphere", n->tag) == 0){
mksph(n);
}else{
die("mkobjs: Unknown tag name in Objects: %s\n", n->tag);
}
}
}
/* see http://ksuweb.kennesaw.edu/~plaval/math4490/rotgen.pdf */
void
mktrans(XMLNode * node){
int i; double phi, x, y, z;
for(i=0; i<node->n_children; i++){
XMLNode * n = node->children[i];
if(strcmp("Translation", n->tag) == 0){
sscanf(n->text, "%lf %lf %lf", &x, &y, &z);
m4 q = mkm4(mkv4(1, 0, 0, x), mkv4(0, 1, 0, y), mkv4(0, 0, 1, z), mkv4(0, 0, 0, 1));
trs[trc++] = q;
}else if(strcmp("Scaling", n->tag) == 0){
sscanf(n->text, "%lf %lf %lf", &x, &y, &z);
m4 q = mkm4(mkv4(x, 0, 0, 0), mkv4(0, y, 0, 0), mkv4(0, 0, z, 0), mkv4(0, 0, 0, 1));
scs[scc++] = q;
}else if(strcmp("Rotation", n->tag) == 0){
sscanf(n->text, "%lf %lf %lf %lf", &phi, &x, &y, &z);
v4 nor = norv4(mkv4(x, y, z, 0));
phi = phi * M_PI / 180.0; double C = cos(phi), S = sin(phi), t = 1-C;
m4 q = mkm4(mkv4(t*nor.x*nor.x + C, t*nor.x*nor.y - S*nor.z, t*nor.x*nor.z + S*nor.y, 0),
mkv4(t*nor.x*nor.y + S*nor.z, t*nor.y*nor.y + C, t*nor.y*nor.z - S*nor.x, 0),
mkv4(t*nor.x*nor.z - S*nor.y, t*nor.y*nor.z + S*nor.x, t*nor.z*nor.z + C, 0),
mkv4(0, 0, 0, 1));
rots[rotc++] = q;
}else{
die("mkobjs: Unknown tag name in Transformations: %s\n", n->tag);
}
}
}
void
sset(char * f){
scene.lightc = vertexc = scene.matlc = scene.objc = 0;
XMLDoc doc;
XMLDoc_init(&doc);
XMLDoc_parse_file_DOM(f, &doc);
int i;
XMLNode * root = doc.nodes[doc.i_root];
for(i=0; i<root->n_children; i++){
XMLNode * node = root->children[i];
if(strcmp("Camera", node->tag) == 0){
mkcam(node);
}else if(strcmp("Lights", node->tag) == 0){
mklights(node);
}else if(strcmp("Materials", node->tag) == 0){
mkmatls(node);
}else if(strcmp("Textures", node->tag) == 0){
printf("Ignoring Textures.\n");
}else if(strcmp("Transformations", node->tag) == 0){
mktrans(node);
}else if(strcmp("VertexData", node->tag) == 0){
mkvertices(node);
}else if(strcmp("TexCoordData", node->tag) == 0){
printf("Ignoring TexCoordData.\n");
}else if(strcmp("Objects", node->tag) == 0){
mkobjs(node);
}
}
}