21 static void * globtex = NULL;
24 int put_material(
void);
25 int mgf2rgb(C_COLOR *cin,
double intensity,
float cout[3]);
29 #define curmatname (c_cmname == NULL ? "mat" : c_cmname) 41 #define CIE_x_w 0.3333 42 #define CIE_y_w 0.3333 44 #define CIE_C_rD ( (1./CIE_y_w) * \ 45 ( CIE_x_w*(CIE_y_g - CIE_y_b) - \ 46 CIE_y_w*(CIE_x_g - CIE_x_b) + \ 47 CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g ) ) 48 #define CIE_C_gD ( (1./CIE_y_w) * \ 49 ( CIE_x_w*(CIE_y_b - CIE_y_r) - \ 50 CIE_y_w*(CIE_x_b - CIE_x_r) - \ 51 CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r ) ) 52 #define CIE_C_bD ( (1./CIE_y_w) * \ 53 ( CIE_x_w*(CIE_y_r - CIE_y_g) - \ 54 CIE_y_w*(CIE_x_r - CIE_x_g) + \ 55 CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r ) ) 58 static float xyz2rgbmat[3][3] = {
59 {(CIE_y_g - CIE_y_b - CIE_x_b*CIE_y_g + CIE_y_b*CIE_x_g)/CIE_C_rD,
60 (CIE_x_b - CIE_x_g - CIE_x_b*CIE_y_g + CIE_x_g*CIE_y_b)/CIE_C_rD,
61 (CIE_x_g*CIE_y_b - CIE_x_b*CIE_y_g)/CIE_C_rD},
62 {(CIE_y_b - CIE_y_r - CIE_y_b*CIE_x_r + CIE_y_r*CIE_x_b)/CIE_C_gD,
63 (CIE_x_r - CIE_x_b - CIE_x_r*CIE_y_b + CIE_x_b*CIE_y_r)/CIE_C_gD,
64 (CIE_x_b*CIE_y_r - CIE_x_r*CIE_y_b)/CIE_C_gD},
65 {(CIE_y_r - CIE_y_g - CIE_y_r*CIE_x_g + CIE_y_g*CIE_x_r)/CIE_C_bD,
66 (CIE_x_g - CIE_x_r - CIE_x_g*CIE_y_r + CIE_x_r*CIE_y_g)/CIE_C_bD,
67 (CIE_x_r*CIE_y_g - CIE_x_g*CIE_y_r)/CIE_C_bD}
75 int mgf2rgb(C_COLOR *cin,
double intensity,
float cout[3]) {
78 cie[0] = intensity*cin->cx/cin->cy;
80 cie[2] = intensity*(1./cin->cy - 1.) - cie[0];
82 cout[0] = xyz2rgbmat[0][0]*cie[0] + xyz2rgbmat[0][1]*cie[1]
83 + xyz2rgbmat[0][2]*cie[2];
84 if(cout[0] < 0.) cout[0] = 0.;
85 cout[1] = xyz2rgbmat[1][0]*cie[0] + xyz2rgbmat[1][1]*cie[1]
86 + xyz2rgbmat[1][2]*cie[2];
87 if(cout[1] < 0.) cout[1] = 0.;
88 cout[2] = xyz2rgbmat[2][0]*cie[0] + xyz2rgbmat[2][1]*cie[1]
89 + xyz2rgbmat[2][2]*cie[2];
90 if(cout[2] < 0.) cout[2] = 0.;
95 int put_material(
void) {
99 char *mname = curmatname;
101 if (!c_cmaterial->clock) {
102 printf(
"USE %s\n", mname);
105 printf(
"%sDEF %s Group {\n", tabs, mname);
107 printf(
"%sMaterial {\n", tabs);
110 memset(&tex, 0,
sizeof(tex));
112 mgf2rgb(&c_cmaterial->rd_c, 1.0, rgbval);
113 tex.
col =
rt_color(rgbval[0], rgbval[1], rgbval[2]);
116 tex.
opacity = 1.0 - (c_cmaterial->td + c_cmaterial->ts);
119 if (c_cmaterial->ed > FTINY) {
120 mgf2rgb(&c_cmaterial->ed_c, 1.0, rgbval);
121 tex.
col =
rt_color(rgbval[0], rgbval[1], rgbval[2]);
133 if (c_cmaterial->rs > 0.0) {
134 tex.
specular = c_cmaterial->rs - (c_cmaterial->rs_a / 2.0);
137 if (c_cmaterial->rs_a > 0.09) {
139 pval = c_cmaterial->rs * 4;
141 pexp = 12.0 / c_cmaterial->rs_a;
146 c_cmaterial->clock = 0;
152 static int myfaceh(
int ac,
char **av) {
153 static char lastmat[256];
163 if (strcmp(lastmat, curmatname) || c_cmaterial->clock) {
165 strcpy(lastmat, curmatname);
168 if ((vp = c_getvert(av[1])) == NULL)
170 xf_xfmpoint(vert, vp->p);
171 v0 =
rt_vector(vert[0], vert[1], vert[2]);
172 xf_rotvect(normal, vp->n);
173 n0 =
rt_vector(normal[0], normal[1], normal[2]);
175 if ((vp = c_getvert(av[2])) == NULL)
177 xf_xfmpoint(vert, vp->p);
178 v1 =
rt_vector(vert[0], vert[1], vert[2]);
179 xf_rotvect(normal, vp->n);
180 n1 =
rt_vector(normal[0], normal[1], normal[2]);
182 for (i = 3; i < ac; i++) {
183 if ((vp = c_getvert(av[i])) == NULL)
185 xf_xfmpoint(vert, vp->p);
186 v2 =
rt_vector(vert[0], vert[1], vert[2]);
187 xf_rotvect(normal, vp->n);
188 n2 =
rt_vector(normal[0], normal[1], normal[2]);
190 if (((n0.
x*n0.
x + n0.
y*n0.
y + n0.
z*n0.
z) < 0.9) ||
191 ((n1.
x*n1.
x + n1.
y*n1.
y + n1.
z*n1.
z) < 0.9) ||
192 ((n2.
x*n2.
x + n2.
y*n2.
y + n2.
z*n2.
z) < 0.9)) {
193 rt_tri(globscene, globtex, v0, v1, v2);
195 rt_stri(globscene, globtex, v0, v1, v2, n0, n1, n2);
238 static void DefaultScene(
char *mgfname,
SceneHandle scene) {
241 float x, y, z, zoom, len;
244 strcpy(fname, mgfname);
245 strcat(fname,
".scn");
246 if ((ifp = fopen(fname,
"r")) == NULL) {
247 printf(
"No scene settings file found, using defaults\n");
250 printf(
"Reading default scene parameters from %s\n", fname);
253 fscanf(ifp,
"%f", &zoom);
255 fscanf(ifp,
"%f %f %f", &x, &y, &z);
258 fscanf(ifp,
"%f %f %f", &x, &y, &z);
259 len = sqrt(x*x + y*y + z*z);
262 fscanf(ifp,
"%f %f %f", &x, &y, &z);
275 if (defaultflag == 1) {
277 DefaultScene(mgfname, scene);
280 printf(
"MGF: loading %s\n", mgfname);
283 mg_ehand[MG_E_FACE] = myfaceh;
284 mg_ehand[MG_E_VERTEX] = c_hvertex;
285 mg_ehand[MG_E_POINT] = c_hvertex;
286 mg_ehand[MG_E_NORMAL] = c_hvertex;
287 mg_ehand[MG_E_XF] = xf_handler;
290 mg_ehand[MG_E_COLOR] = c_hcolor;
291 mg_ehand[MG_E_CMIX] = c_hcolor;
292 mg_ehand[MG_E_CSPEC] = c_hcolor;
293 mg_ehand[MG_E_CXY] = c_hcolor;
294 mg_ehand[MG_E_CCT] = c_hcolor;
295 mg_ehand[MG_E_ED] = c_hmaterial;
296 mg_ehand[MG_E_MATERIAL] = c_hmaterial;
297 mg_ehand[MG_E_RD] = c_hmaterial;
298 mg_ehand[MG_E_RS] = c_hmaterial;
299 mg_ehand[MG_E_SIDES] = c_hmaterial;
300 mg_ehand[MG_E_TD] = c_hmaterial;
301 mg_ehand[MG_E_TS] = c_hmaterial;
304 if (mg_load(mgfname) != MG_OK)
float g
Green color component.
void rt_camera_setup(SceneHandle voidscene, flt zoom, flt aspectratio, int antialiasing, int raydepth, apivector camcent, apivector viewvec, apivector upvec)
Define a camera for a perspective projection, given the specified zoom factor, aspect ratio...
float r
Red color component.
void * rt_texture(SceneHandle sc, apitexture *apitex)
Translate a texture definition into the internal format used by Tachyon, and returns an opaque pointe...
apicolor col
base object color
flt diffuse
diffuse reflection
flt opacity
how opaque the object is
flt specular
specular reflection
apicolor rt_color(flt r, flt g, flt b)
Helper function to make colors.
unsigned int ParseMGF(char *mgfname, SceneHandle scene, int defaultflag)
double flt
generic floating point number, using double
void rt_tri(SceneHandle voidscene, void *tex, apivector v0, apivector v1, apivector v2)
Define a flat-shaded triangle.
flt x
X coordinate or direction component.
flt y
Y coordinate or direction component.
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.
int texturefunc
which texture function to use
apivector rt_vector(flt x, flt y, flt z)
Helper function to make vectors.
flt ambient
ambient lighting
#define RT_PHONG_PLASTIC
Dielectric Phong highlight.
float b
Blue color component.
void rt_tex_phong(void *voidtex, flt phong, flt phongexp, int type)
Set Phong shading parameters for an existing texture.
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
void * rt_light(SceneHandle voidscene, void *tex, apivector ctr, flt rad)
Define a point light with associated texture, position, and radius.
flt z
Z coordinate or direction component.