File: create.c

package info (click to toggle)
cmix 2.0.12-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,920 kB
  • ctags: 6,063
  • sloc: cpp: 27,139; ansic: 11,924; sh: 2,795; exp: 2,270; yacc: 1,724; makefile: 1,247; lex: 488; perl: 278
file content (277 lines) | stat: -rw-r--r-- 8,481 bytes parent folder | download | duplicates (4)
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
/*
 * Time-stamp: <99/02/19 08:49:23 panic>
 * Author:	The C-Mix Project <cmix@diku.dk>
 *         	Peter Holst Andersen <txix@diku.dk>
 *              Arne John Glenstrup  <panic@diku.dk>
 * Contents:	Functions for creating the scene: create_*
 */

#include "ray.h"
#include <math.h>
#include <stdio.h>

#ifndef __CMIX
#  define __CMIX(X)
#endif

#define NEXT(OBJ) (OBJ.last + 1 >= OBJ.max ? \
  (fprintf(stderr, \
	   "error: too many %s in the scene, please increase %s in 'ray.h'.\n", \
	   OBJ.name, OBJ.maxname), \
   exit(-1), 0) : ++OBJ.last)

static void
compute_surface_normal(vectorType* n, vectorType* v1, vectorType* v2) {
  /* n is the normal to the surface, v1 is given and is the orientation
   * of the surface (for checked discs: the pattern orientation).
   * v2 is computed as a vector normal to both n and v1
   */
  myfloat a, b, c;
  a = n->x * v1->y - n->y * v1->x; if (isnan(a)) a = 0.0;
  if (fzero(a))
    v2->z = 0.0;
  else {
    b = (n->z * v1->x - n->x * v1->z) / a; if (isnan(b)) b = 0.0;
    c = (n->y * v1->z - n->z * v1->y) / a; if (isnan(c)) c = 0.0;
    v2->z = 1 / sqrt(b * b + c * c + 1.0);
  };
  a = n->y * v1->z - n->z * v1->y; if (isnan(a)) a = 0.0;
  if (fzero(a))
    v2->x = 0.0;
  else {
    b = (n->x * v1->y - n->y * v1->x) / a; if (isnan(b)) b = 0.0;
    c = (n->z * v1->x - n->x * v1->z) / a; if (isnan(c)) c = 0.0;
    v2->x = 1 / sqrt(b * b + c * c + 1.0);
    if (!fzero(v2->z) && !fzero(b))
      v2->x = copysign(v2->x, copysign(1.0, v2->z) * copysign(1.0, b));
  };
  a = n->z * v1->x - n->x * v1->z; if (isnan(a)) a = 0.0;
  if (fzero(a))
    v2->y = 0.0;
  else {
    b = (n->y * v1->z - n->z * v1->y) / a; if (isnan(b)) b = 0.0;
    c = (n->x * v1->y - n->y * v1->x) / a; if (isnan(c)) c = 0.0;
    v2->y = 1 / sqrt(b * b + c * c + 1.0);
    if (!fzero(v2->x) && !fzero(b))
      v2->y = copysign(v2->y, copysign(1.0, v2->x) * copysign(1.0, b));
    else if (!fzero(v2->z) && !fzero(c))
      v2->y = copysign(v2->y, copysign(1.0, v2->z) * copysign(1.0, c));
  };
#ifdef DEBUG
  __CMIX(pure)fprintf
    (stderr, "/* Created surface: normal = (%g, %g, %g) */\n",
     n->x, n->y, n->z);
  __CMIX(pure)fprintf
    (stderr, "/*                  x-unit = (%g, %g, %g) */\n",
     v1->x, v1->y, v1->z);
  __CMIX(pure)fprintf
    (stderr, "/*                  y-unit = (%g, %g, %g) */\n",
     v2->x, v2->y, v2->z);
#endif
  __CMIX(pure)fprintf
    (stderr, "/* Check: n*x = %g, n*y = %g, x*y = %g */\n",
     n->x *v1->x + n->y *v1->y + n->z *v1->z,
     n->x *v2->x + n->y *v2->y + n->z *v2->z,
     v1->x*v2->x + v1->y*v2->y + v1->z*v2->z);
}

surfaceT create_simple_surface(myfloat am, myfloat di,
			       myfloat re, myfloat tr, myfloat ref, int specpow)
{
  int i;
  i = NEXT(surfaceIdx);
  surface[i].tag = SIMPLE;
  surface[i].u.sinfo.ambient.red = am;
  surface[i].u.sinfo.ambient.green = am;
  surface[i].u.sinfo.ambient.blue = am;
  surface[i].u.sinfo.diffuse.red = di;
  surface[i].u.sinfo.diffuse.green = di;
  surface[i].u.sinfo.diffuse.blue = di;
  surface[i].u.sinfo.reflectivity = re;
  surface[i].u.sinfo.transparency = tr;
  surface[i].u.sinfo.refrindex = ref;
  surface[i].u.sinfo.specpow = specpow;
  return i;
}

surfaceT create_color_surface(myfloat am, myfloat di,
			      myfloat re, myfloat tr, myfloat ref, int specpow,
			      myfloat red, myfloat green, myfloat blue)
{
  int i;
  i = NEXT(surfaceIdx);
  surface[i].tag = SIMPLE;
  surface[i].u.sinfo.ambient.red = am * red;
  surface[i].u.sinfo.ambient.green = am * green;
  surface[i].u.sinfo.ambient.blue = am * blue;
  surface[i].u.sinfo.diffuse.red = di * red;
  surface[i].u.sinfo.diffuse.green = di * green;
  surface[i].u.sinfo.diffuse.blue = di * blue;
  surface[i].u.sinfo.reflectivity = re;
  surface[i].u.sinfo.transparency = tr;
  surface[i].u.sinfo.refrindex = ref;
  surface[i].u.sinfo.specpow = specpow;
  return i;
}

surfaceT create_special_color_surface(myfloat amred, myfloat amgreen, myfloat amblue,
				      myfloat dired, myfloat digreen, myfloat diblue, 
				      myfloat re, myfloat tr,
				      myfloat ref, int specpow)
{
  int i;
  i = NEXT(surfaceIdx);
  surface[i].tag = SIMPLE;
  surface[i].u.sinfo.ambient.red = amred;
  surface[i].u.sinfo.ambient.green = amgreen;
  surface[i].u.sinfo.ambient.blue = amblue;
  surface[i].u.sinfo.diffuse.red = dired;
  surface[i].u.sinfo.diffuse.green = digreen;
  surface[i].u.sinfo.diffuse.blue = diblue;
  surface[i].u.sinfo.reflectivity = re;
  surface[i].u.sinfo.transparency = tr;
  surface[i].u.sinfo.refrindex = ref;
  surface[i].u.sinfo.specpow = specpow;
  return i;
}

surfaceT create_checked_surface(surfaceT s1, surfaceT s2, myfloat checksize)
{
  int i;
  i = NEXT(surfaceIdx);
  surface[i].tag = CHECKED;
  surface[i].u.checked.s1 = s1;
  surface[i].u.checked.s2 = s2;
  surface[i].u.checked.checksize = checksize;
  surface[i].u.checked.dbl_checksize = checksize * 2.0;
  return i;
}

objectT create_disc(myfloat cx, myfloat cy, myfloat cz,
			myfloat nx, myfloat ny, myfloat nz,
			myfloat vx, myfloat vy, myfloat vz,
			myfloat r, int s)
{
  objectType *obj;
  vectorType n, v, v2;
  obj = scene + NEXT(objectIdx); (obj + 1)->tag = NONE;
  obj->tag = DISC;
  obj->prop.surface = s;	
  obj->obj.disc.c.x = cx;
  obj->obj.disc.c.y = cy;
  obj->obj.disc.c.z = cz;
  obj->obj.disc.n.x = nx;
  obj->obj.disc.n.y = ny;
  obj->obj.disc.n.z = nz;
  obj->obj.disc.v1.x = vx;
  obj->obj.disc.v1.y = vy;
  obj->obj.disc.v1.z = vz;
  obj->obj.disc.r = r;
  obj->obj.disc.r2 = r*r;
  vector_norm_S(&obj->obj.disc.n);
  vector_norm_S(&obj->obj.disc.v1);
  compute_surface_normal(&obj->obj.disc.n, &obj->obj.disc.v1,
			 &obj->obj.disc.v2);
  obj->obj.disc.d = obj->obj.disc.n.x*cx + obj->obj.disc.n.y*cy + 
    obj->obj.disc.n.z*cz;
  return obj;
}

objectT create_square(myfloat cx, myfloat cy, myfloat cz,
		   myfloat nx, myfloat ny, myfloat nz,
		   myfloat vx, myfloat vy, myfloat vz,
		   myfloat r, int s)
{
  objectType *obj;
  vectorType n, v, v2;
  obj = scene + NEXT(objectIdx); (obj + 1)->tag = NONE;
  obj->tag = SQUARE;
  obj->prop.surface = s;
  obj->obj.square.c.x = cx;
  obj->obj.square.c.y = cy;
  obj->obj.square.c.z = cz;
  obj->obj.square.n.x = nx;
  obj->obj.square.n.y = ny;
  obj->obj.square.n.z = nz;
  obj->obj.square.v1.x = vx;
  obj->obj.square.v1.y = vy;
  obj->obj.square.v1.z = vz;
  vector_norm_S(&obj->obj.square.n);
  vector_norm_S(&obj->obj.square.v1);
  compute_surface_normal(&obj->obj.square.n, &obj->obj.square.v1,
			 &obj->obj.square.v2);
  obj->obj.square.r2 = r*r;
  obj->obj.square.d = obj->obj.square.n.x*cx + obj->obj.square.n.y*cy +
    obj->obj.square.n.z*cz;
  return obj;
}

objectT create_sphere(myfloat x, myfloat y, myfloat z,
			  myfloat nx, myfloat ny, myfloat nz,
			  myfloat r, int s)
{
  objectType *obj;
  vectorType n, v, v2;
  myfloat l;
  obj = scene + NEXT(objectIdx); (obj + 1)->tag = NONE;
  obj->tag = SPHERE;
  obj->prop.surface = s;
  obj->obj.sphere.r = r;
  obj->obj.sphere.r2 = r*r;
  obj->obj.sphere.c.x = x;
  obj->obj.sphere.c.y = y;
  obj->obj.sphere.c.z = z;
  n.x = nx; n.y = ny; n.z = nz;
  vector_norm_S(&n);
  if (fzero(n.z)) {
    v.x = 0.0; v.y = 0.0; v.z = 1.0;
    v2.x = -n.y; v2.y = n.x; v2.z = 0.0;
  } else {
    if (fzero(n.x) && fzero(n.y)) {
      v.x = 1.0; v.y = 0.0;
    } else {
      v.x = -n.y; v.y = n.x;
    }
    v.z = 0.0;
    vector_norm_S(&v);
    l = sqrt(n.x * n.x + n.y * n.y);
    if (fzero(l)) {
      v2.x = -v.y; v2.y = v.x; v2.z = 0.0;
    } else {
      v2.x = -n.x * n.z / l; v2.y = -n.y * n.z / l;
      v2.z = sqrt(n.x * n.x + n.y * n.y);
    }
  }
  obj->obj.sphere.n = n;
  obj->obj.sphere.v1 = v;
  obj->obj.sphere.v2 = v2;
  fprintf(stderr, "sphere s%d %lf %lf %lf %lf\n", s, r, x, y, z);
#ifdef DEBUG
  __CMIX(pure)fprintf
    (stderr, "/* Sphere: pole (%g, %g, %g) */\n", n.x, n.y, n.z);
  __CMIX(pure)fprintf
    (stderr, "/* Sphere: equator (%g, %g, %g) */\n", v.x, v.y, v.z);
  __CMIX(pure)fprintf
    (stderr, "/* Sphere: normal (%g, %g, %g) */\n", v2.x, v2.y, v2.z);
#endif
  __CMIX(pure)fprintf
    (stderr, "/* Check: p*e = %g, p*n = %g, e*n = %g */\n",
     n.x*v.x  + n.y*v.y  + n.z*v.z,
     n.x*v2.x + n.y*v2.y + n.z*v2.z,
     v.x*v2.x + v.y*v2.y + v.z*v2.z);
  return obj;
}    

lightT create_light(myfloat r, myfloat g, myfloat b,
		  myfloat x, myfloat y, myfloat z)
{
  lightType *li;
  li = light + NEXT(lightIdx);
  li->p.x = x;
  li->p.y = y;
  li->p.z = z;
  li->c.red = r;
  li->c.green = g;
  li->c.blue = b;
  return li;
}