ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/3ds2mgf.c
Revision: 1.2
Committed: Thu Feb 8 13:29:33 1996 UTC (28 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +47 -3 lines
Log Message:
added better MGF output for smoothing turned off

File Contents

# Content
1 /*
2 3DS2POV.C Copyright (c) 1996 Steve Anger and Jeff Bowermaster
3 MGF output added by Greg Ward
4
5 Reads a 3D Studio .3DS file and writes a POV-Ray, Vivid,
6 Polyray, MGF or raw scene file.
7
8 Version 2.0 Written Feb/96
9
10 Compiled with MSDOS GNU C++ 2.4.1 or generic ANSI-C compiler
11 */
12
13 #ifndef lint
14 static char SCCSid[] = "$SunId$ LBL";
15 #endif
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include "vect.h"
23 #include "rayopt.h"
24
25 #ifdef __TURBOC__
26 #include <alloc.h>
27 extern unsigned _stklen = 16384;
28 #endif
29
30
31 #define FALSE 0
32 #define TRUE 1
33
34 /* Internal bounding modes */
35 #define OFF 0
36 #define ON 1
37 #define AUTO 2
38
39 #define MAX_LIB 10
40 #define ASPECT 1.333
41
42 /* Output formats */
43 #define POV10 0
44 #define POV20 1
45 #define VIVID 2
46 #define POLYRAY 3
47 #define MGF 4
48 #define RAW 99
49
50 #define DEG(x) ((180.0/M_PI)*(x))
51 #define RAD(x) ((M_PI/180.0)*(x))
52
53 #ifndef M_PI
54 #define M_PI (3.14159265358979323846)
55 #endif
56
57 #ifndef MAXFLOAT
58 #define MAXFLOAT (1e37)
59 #endif
60 /* RGB chromaticity definitions for MGF */
61 #define CIE_x_r 0.640
62 #define CIE_y_r 0.330
63 #define CIE_x_g 0.290
64 #define CIE_y_g 0.600
65 #define CIE_x_b 0.150
66 #define CIE_y_b 0.060
67 /* computed luminances from above */
68 #define CIE_Y_r 0.265
69 #define CIE_Y_g 0.670
70 #define CIE_Y_b 0.065
71
72 /* A generic list type */
73 #define LIST_INSERT(root, node) list_insert ((List **)&root, (List *)node)
74 #define LIST_FIND(root, name) list_find ((List **)&root, name)
75 #define LIST_DELETE(root, node) list_delete ((List **)&root, (List *)node)
76 #define LIST_KILL(root) list_kill ((List **)&root)
77
78 #define LIST_FIELDS \
79 char name[80]; \
80 void *next;
81
82
83 typedef unsigned char byte;
84 typedef unsigned short word;
85 typedef unsigned long dword;
86
87 typedef struct {
88 LIST_FIELDS
89 } List;
90
91
92 typedef struct {
93 int a, b, c;
94 } Face;
95
96
97 typedef struct {
98 float red, green, blue;
99 } Colour;
100
101
102 /* Transformation command */
103 typedef struct {
104 LIST_FIELDS
105
106 Matrix matrix;
107 } Transform;
108
109
110 /* Morph command */
111 typedef struct {
112 LIST_FIELDS
113
114 int count; /* Number of objects in morph */
115 char names[4][80]; /* Name of n'th object in average */
116 float weight[4]; /* Weight applied to n'th object */
117
118 Matrix matrix;
119 } Morph;
120
121
122 /* Omni light command */
123 typedef struct {
124 LIST_FIELDS
125
126 Vector pos; /* Light position */
127 Colour col; /* Light colour */
128 } OmniLight;
129
130
131 /* Spotlight command */
132 typedef struct {
133 LIST_FIELDS
134
135 Vector pos; /* Spotlight position */
136 Vector target; /* Spotlight target location */
137 Colour col; /* Spotlight colour */
138 float hotspot; /* Hotspot angle (degrees) */
139 float falloff; /* Falloff angle (degrees) */
140 int shadow_flag; /* Shadow flag (not used) */
141 } Spotlight;
142
143
144 /* Camera command */
145 typedef struct {
146 LIST_FIELDS
147
148 Vector pos; /* Camera location */
149 Vector target; /* Camera target */
150 float bank; /* Banking angle (degrees) */
151 float lens; /* Camera lens size (mm) */
152 } Camera;
153
154
155 /* Material list */
156 typedef struct {
157 LIST_FIELDS
158
159 int external; /* Externally defined material? */
160 } Material;
161
162
163 /* Object summary */
164 typedef struct {
165 LIST_FIELDS
166
167 Vector center; /* Min value of object extents */
168 Vector lengths; /* Max value of object extents */
169 } Summary;
170
171
172 /* Material property */
173 typedef struct {
174 LIST_FIELDS
175
176 Colour ambient;
177 Colour diffuse;
178 Colour specular;
179 float shininess;
180 float transparency;
181 float reflection;
182 int self_illum;
183 int two_side;
184 char tex_map[40];
185 float tex_strength;
186 char bump_map[40];
187 float bump_strength;
188 } MatProp;
189
190
191 /* Default material property */
192 MatProp DefaultMaterial = { "Default", NULL, {1.0, 1.0, 1.0}, {1.0, 1.0, 1.0},
193 {1.0, 1.0, 1.0}, 70.0, 0.0, 0.0, FALSE, FALSE };
194
195 /* A mesh object */
196 typedef struct {
197 LIST_FIELDS
198
199 int vertices; /* Number of vertices */
200 Vector *vertex; /* List of object vertices */
201
202 int faces; /* Number of faces */
203 Face *face; /* List of object faces */
204 Material **mtl; /* Materials for each face */
205
206 Matrix matrix; /* Local mesh matrix */
207 Matrix invmatrix;
208 Vector center; /* Center of object */
209 Vector lengths; /* Dimensions of object */
210
211 int hidden; /* Hidden flag */
212 int shadow; /* Shadow flag */
213 } Mesh;
214
215
216 typedef struct {
217 dword start;
218 dword end;
219 dword length;
220 word tag;
221 } Chunk;
222
223
224 typedef struct {
225 byte red;
226 byte green;
227 byte blue;
228 } Colour_24;
229
230
231 Colour Black = {0.0, 0.0, 0.0};
232
233 OmniLight *omni_list = NULL;
234 Spotlight *spot_list = NULL;
235 Camera *cam_list = NULL;
236 Mesh *mesh_list = NULL;
237 Transform *trans_list = NULL;
238 Morph *morph_list = NULL;
239 Material *mtl_list = NULL;
240 List *excl_list = NULL;
241 List *box_list = NULL;
242 MatProp *mprop_list = NULL;
243 Summary *summary = NULL;
244
245
246 FILE *in;
247 FILE *out;
248 char inname[80];
249 char outname[80];
250 char vuename[80];
251 char obj_name[80] = "";
252 Colour fog_colour = {0.0, 0.0, 0.0};
253 Colour col = {0.0, 0.0, 0.0};
254 Colour global_amb = {0.1, 0.1, 0.1};
255 Vector pos = {0.0, 0.0, 0.0};
256 Vector target = {0.0, 0.0, 0.0};
257 float fog_distance = 0.0;
258 float hotspot = -1;
259 float falloff = -1;
260 Mesh *mesh = NULL;
261 int frame = -1;
262 char libname[MAX_LIB][80];
263 float smooth = 60.0;
264 int bound = 0;
265 int verbose = 0;
266 int format = POV20;
267 int internal_bounding = AUTO;
268 int box_all = FALSE;
269 int cameras = 0;
270 int libs = 0;
271 float vue_version = 1.0;
272 Matrix *ani_matrix = NULL;
273 int no_opt = FALSE;
274 FILE *meshf = NULL;
275
276
277 void process_args (int argc, char *argv[]);
278 void parse_option (char *option);
279 void list_insert (List **root, List *new_node);
280 void *list_find (List **root, char *name);
281 void list_delete (List **root, List *node);
282 void list_kill (List **root);
283 Material *update_materials (char *new_material, int ext);
284 MatProp *create_mprop (void);
285 void read_library (char *fname);
286 void write_intro (FILE *f);
287 void write_summary (FILE *f);
288 void write_bgsolid (FILE *f, Colour col);
289 void write_light (FILE *f, char *name, Vector pos, Colour col);
290 void write_spot (FILE *f, char *name, Vector pos, Vector target, Colour col,
291 float hotspot, float falloff);
292 void write_fog (FILE *f, Colour col, float dist);
293 void write_camera (FILE *f, char *name, Vector pos, Vector target, float lens,
294 float bank);
295 void write_material (FILE *f, char *mat);
296 void write_pov10_material (FILE *f, MatProp *m);
297 void write_pov20_material (FILE *f, MatProp *m);
298 void write_vivid_material (FILE *f, MatProp *m);
299 void write_polyray_material (FILE *f, MatProp *m);
300 void write_mgf_material (FILE *f, MatProp *m);
301 void write_mesh (FILE *f, Mesh *mesh);
302 Transform *parse_transform (char *string);
303 Morph *parse_morph (char *string);
304 OmniLight *parse_omnilight (char *string);
305 Spotlight *parse_spotlight (char *string);
306 Camera *parse_camera (char *string);
307 void read_frame (char *filename, int frame_no);
308 void find_frame (FILE *f, int frame_no);
309 void save_animation (void);
310 Mesh *create_mesh (char *name, int vertices, int faces);
311 Mesh *copy_mesh (Mesh *mesh);
312 void free_mesh_data (Mesh *mesh);
313 void update_limits (Mesh *mesh);
314 char *before (char *str, char *target);
315 char *after (char *str, char *target);
316 char *between (char *str, char *target1, char *target2);
317 char *parse_string (char *str);
318 char upcase (char c);
319 float colour_intens (Colour *colour);
320 void parse_file (void);
321 void parse_3ds (Chunk *mainchunk);
322 void parse_mdata (Chunk *mainchunk);
323 void parse_fog (Chunk *mainchunk);
324 void parse_fog_bgnd (void);
325 void parse_mat_entry (Chunk *mainchunk);
326 char *parse_mapname (Chunk *mainchunk);
327 void parse_named_object (Chunk *mainchunk);
328 void parse_n_tri_object (Chunk *mainchunk);
329 void parse_point_array (void);
330 void parse_face_array (Chunk *mainchunk);
331 void parse_msh_mat_group (void);
332 void parse_smooth_group (void);
333 void parse_mesh_matrix (void);
334 void parse_n_direct_light (Chunk *mainchunk);
335 void parse_dl_spotlight (void);
336 void parse_n_camera (void);
337 void parse_colour (Colour *colour);
338 void parse_colour_f (Colour *colour);
339 void parse_colour_24 (Colour_24 *colour);
340 float parse_percentage (void);
341 short parse_int_percentage (void);
342 float parse_float_percentage (void);
343 void start_chunk (Chunk *chunk);
344 void end_chunk (Chunk *chunk);
345 byte read_byte (void);
346 word read_word (void);
347 dword read_dword (void);
348 float read_float (void);
349 void read_point (Vector v);
350 char *read_string (void);
351 float findfov (float lens);
352 int read_mgfmatname (char *s, int n, FILE *f);
353
354
355 int main (int argc, char *argv[])
356 {
357 char meshfname[128];
358 Material *m;
359 int i;
360
361 process_args (argc, argv);
362
363 if (!no_opt) {
364 opt_set_format (format);
365 opt_set_dec (4);
366 opt_set_bound (bound);
367 opt_set_smooth (smooth);
368 opt_set_quiet (!verbose);
369 opt_set_fname (outname, "");
370 } else if (format == MGF) {
371 strcpy(meshfname, outname);
372 add_ext(meshfname, "inc", 1);
373 if (!strcmp(meshfname, outname)) {
374 printf ("Output and mesh file names are identical!\n");
375 exit (1);
376 }
377 if ((meshf = fopen (meshfname, "w")) == NULL) {
378 printf ("Cannot open mesh output file %s!\n", meshfname);
379 exit (1);
380 }
381 }
382
383 if ((in = fopen (inname, "rb")) == NULL) {
384 printf ("Cannot open input file %s!\n", inname);
385 exit (1);
386 }
387
388 if ((out = fopen (outname, "w")) == NULL) {
389 printf ("Cannot open output file %s!\n", outname);
390 exit (1);
391 }
392
393 /* Load the names of pre-defined materials */
394 for (i = 0; i < MAX_LIB; i++) {
395 if (strlen(libname[i]) > 0)
396 read_library (libname[i]);
397 }
398
399 /* Load the instructions for the current frame */
400 if (strlen(vuename) > 0)
401 read_frame (vuename, frame);
402
403 printf("Output to: %s\n", outname);
404
405 if (frame >= 0)
406 printf ("Generating frame #%d\n", frame);
407
408 printf("\nPlease wait; Processing...\n");
409
410 write_intro(out);
411
412 parse_file();
413
414 fclose(in);
415
416 for (m = mtl_list; m != NULL; m = m->next) {
417 if (!m->external)
418 write_material (out, m->name);
419 }
420
421 if (frame >= 0)
422 save_animation();
423
424 if (!no_opt) {
425 write_summary (out);
426 fflush (out);
427
428 opt_finish();
429 } else if (meshf != NULL) {
430 fclose(meshf);
431 fprintf (out, "i %s\n", meshfname);
432 }
433
434 fclose (out);
435
436 LIST_KILL (omni_list);
437 LIST_KILL (spot_list);
438 LIST_KILL (cam_list);
439 LIST_KILL (mesh_list);
440 LIST_KILL (trans_list);
441 LIST_KILL (morph_list);
442 LIST_KILL (mtl_list);
443 LIST_KILL (excl_list);
444 LIST_KILL (box_list);
445 LIST_KILL (mprop_list);
446 LIST_KILL (summary);
447
448 return 0;
449 }
450
451
452 /* Handle the command line args */
453 void process_args (int argc, char *argv[])
454 {
455 int i;
456 char *progname;
457 char *env_opt, *option;
458
459 printf("\n\nAutodesk 3D Studio to Raytracer file Translator. Feb/96\n");
460 printf("Version 2.0 Copyright (c) 1996 Steve Anger and Jeff Bowermaster\n");
461 #ifdef __GNUC__
462 printf ("32 bit version. DOS extender Copyright (c) 1991 DJ Delorie\n");
463 #endif
464 printf ("\n");
465
466 if (argc < 2) {
467 printf ("Usage: %s inputfile[.3ds] [outputfile] [options]\n\n", argv[0]);
468 printf ("Options: -snnn - Smooth triangles with angles < nnn\n");
469 printf (" -l<filename> - Specifies native material library\n");
470 printf (" -a<filename> - Use animation information in specified file\n");
471 printf (" -fnnn - Generate frame nnn of animation\n");
472 printf (" -x<object> - Exclude this object from conversion\n");
473 printf (" -b<object> - Convert this object as a box\n");
474 printf (" +i, -i - Turn internal bounding on or off\n");
475 printf (" +v, -v - Turn verbose status messages on or off\n");
476 printf (" -op - Output to POV-Ray 2.0 format (default)\n");
477 printf (" -op1 - Output to POV-Ray 1.0 format\n");
478 printf (" -ov - Output to Vivid format\n");
479 printf (" -ol - Output to poLyray format\n");
480 printf (" -om - Output to MGF\n");
481 printf (" -or - Output to RAW triangle format\n\n");
482 printf ("ex. %s birdshow +v -lmaterials.inc\n\n", argv[0]);
483 exit(1);
484 }
485 /* figure default format from name */
486 progname = strrchr(argv[0], '/');
487 if (progname == NULL) progname = argv[0];
488 else progname++;
489 if (!strcmp(progname, "3ds2pov"))
490 format = POV20;
491 else if (!strcmp(progname, "3ds2viv"))
492 format = VIVID;
493 else if (!strcmp(progname, "3ds2pi"))
494 format = POLYRAY;
495 else if (!strcmp(progname, "3ds2mgf"))
496 format = MGF;
497 else if (!strcmp(progname, "3ds2raw"))
498 format = RAW;
499 else
500 format = MGF; /* default if program name strange */
501
502 strcpy (inname, "");
503 strcpy (outname, "");
504 strcpy (vuename, "");
505
506 for (i = 0; i < MAX_LIB; i++)
507 strcpy (libname[i], "");
508
509 frame = -1;
510 smooth = 70.0;
511 bound = 0;
512 verbose = 0;
513 internal_bounding = AUTO;
514 box_all = FALSE;
515 libs = 0;
516
517 /* Parse the enviroment string options */
518 env_opt = getenv ("3DS2POV");
519
520 if (env_opt != NULL) {
521 option = parse_string (env_opt);
522
523 while (strlen(option) > 0) {
524 parse_option (option);
525 option = parse_string (NULL);
526 }
527 }
528
529 /* Parse the command line options */
530 for (i = 1; i < argc; i++)
531 parse_option (argv[i]);
532
533 if (strlen(inname) == 0)
534 abortmsg ("No input file specified", 1);
535
536 if (strlen(outname) == 0)
537 strcpy (outname, inname);
538
539 switch (format) {
540 case POV10:
541 case POV20: add_ext (outname, "pov", 1); break;
542 case VIVID: add_ext (outname, "v", 1); break;
543 case POLYRAY: add_ext (outname, "pi", 1); break;
544 case MGF: add_ext (outname, "mgf", 1); break;
545 case RAW: add_ext (outname, "raw", 1); break;
546 }
547
548 switch (internal_bounding) {
549 case OFF: bound = 2; break;
550 case ON: bound = 0; break;
551 case AUTO: bound = (format == POV10) ? 0 : 2; break;
552 }
553
554 if ((strlen(vuename) > 0) != (frame >= 0))
555 abortmsg ("The -a and -f parameters must be used together", 1);
556
557 if (format == RAW || (format == MGF && smooth < 0.1))
558 no_opt = TRUE;
559 }
560
561
562 void parse_option (char *option)
563 {
564 List *excl, *box;
565 char name[80];
566
567 if (option[0] == '-' || option[0] == '+') {
568 switch (upcase(option[1])) {
569 case 'A': strcpy (vuename, &option[2]);
570 break;
571
572 case 'B': strcpy (name, parse_string (&option[2]));
573 if (strlen(name) == 0)
574 box_all = TRUE;
575 else {
576 cleanup_name (name);
577
578 box = malloc (sizeof (*box));
579 strcpy (box->name, name);
580
581 LIST_INSERT (box_list, box);
582 }
583 break;
584
585 case 'F': if (option[2] != '\0')
586 frame = atoi (&option[2]);
587 break;
588
589 case 'I': if (option[0] == '-')
590 internal_bounding = OFF;
591 else
592 internal_bounding = ON;
593 break;
594
595 case 'L': if (libs == MAX_LIB)
596 abortmsg ("Too many libraries specified", 1);
597
598 strcpy (libname[libs++], &option[2]);
599 break;
600
601 case 'O': switch (upcase(option[2])) {
602 case 'P': if (option[3] == '1')
603 format = POV10;
604 else
605 format = POV20;
606 break;
607
608 case 'V': format = VIVID;
609 break;
610
611 case 'L': format = POLYRAY;
612 break;
613
614 case 'R': format = RAW;
615 break;
616
617 case 'M': format = MGF;
618 break;
619
620 default: printf ("Invalid output format %s specified\n", option);
621 exit(1);
622 }
623 break;
624
625 case 'S': if (option[2] != '\0')
626 smooth = atof (&option[2]);
627 break;
628
629 case 'U': printf ("Warning: -u parameter no long has any effect\n");
630 printf (" use +i or -i instead.\n");
631 break;
632
633 case 'V': if (option[0] == '-')
634 verbose = 0;
635 else
636 verbose = 1;
637 break;
638
639 case 'X': strcpy (name, parse_string (&option[2]));
640 cleanup_name (name);
641
642 excl = malloc (sizeof (*excl));
643 strcpy (excl->name, name);
644
645 LIST_INSERT (excl_list, excl);
646 break;
647
648 default : printf ("\nInvalid option %s specified\n", option);
649 exit (1);
650 }
651 }
652 else if (strlen (inname) == 0) {
653 strcpy (inname, option);
654 add_ext (inname, "3ds", 0);
655 }
656 else if (strlen (outname) == 0)
657 strcpy (outname, option);
658 else
659 abortmsg ("Too many file names specified.\n", 1);
660 }
661
662
663 /* Insert a new node into the list */
664 void list_insert (List **root, List *new_node)
665 {
666 new_node->next = *root;
667
668 *root = new_node;
669 }
670
671
672 /* Find the node with the specified name */
673 void *list_find (List **root, char *name)
674 {
675 List *p;
676
677 for (p = *root; p != NULL; p = p->next) {
678 if (strcmp (p->name, name) == 0)
679 break;
680 }
681
682 return (void *)p;
683 }
684
685
686 /* Delete the indicated node from the list */
687 void list_delete (List **root, List *node)
688 {
689 List *prev;
690
691 prev = *root;
692 while (prev != NULL && prev->next != node)
693 prev = prev->next;
694
695 if (prev == NULL)
696 *root = node->next;
697 else
698 prev->next = node->next;
699
700 free (node);
701 }
702
703
704 /* Delete the entire list */
705 void list_kill (List **root)
706 {
707 List *temp;
708
709 while (*root != NULL) {
710 temp = *root;
711 *root = (*root)->next;
712 free (temp);
713 }
714 }
715
716
717 /* Add a new material to the material list */
718 Material *update_materials (char *new_material, int ext)
719 {
720 Material *p;
721
722 p = LIST_FIND (mtl_list, new_material);
723
724 if (p == NULL) {
725 p = malloc (sizeof (*p));
726
727 if (p == NULL)
728 abortmsg ("Out of memory adding material", 1);
729
730 strcpy (p->name, new_material);
731 p->external = ext;
732
733 LIST_INSERT (mtl_list, p);
734 }
735
736 return p;
737 }
738
739
740 MatProp *create_mprop()
741 {
742 MatProp *new_mprop;
743
744 new_mprop = malloc (sizeof(*new_mprop));
745 if (new_mprop == NULL)
746 abortmsg ("Out of memory adding material", 1);
747
748 strcpy (new_mprop->name, "");
749 new_mprop->ambient = Black;
750 new_mprop->diffuse = Black;
751 new_mprop->specular = Black;
752 new_mprop->shininess = 0.0;
753 new_mprop->transparency = 0.0;
754 new_mprop->reflection = 0.0;
755 new_mprop->self_illum = FALSE;
756 new_mprop->two_side = FALSE;
757
758 strcpy (new_mprop->tex_map, "");
759 new_mprop->tex_strength = 0.0;
760
761 strcpy (new_mprop->bump_map, "");
762 new_mprop->bump_strength = 0.0;
763
764 return new_mprop;
765 }
766
767
768 /* Load in any predefined materials */
769 void read_library (char *fname)
770 {
771 FILE *lib;
772 char string[256], name[80];
773
774 if ((lib = fopen (fname, "r")) == NULL) {
775 printf ("Cannot open texture library file %s!\n", fname);
776 exit(1);
777 }
778
779 switch (format) {
780 case POV10:
781 case POV20:
782 while (fgets (string, 256, lib) != NULL) {
783 if (strstr (string, "#declare")) {
784 strcpy (name, between (string, "#declare", "="));
785 cleanup_name (name);
786 (void)update_materials (name, TRUE);
787 }
788 }
789 break;
790
791 case VIVID:
792 while (fgets (string, 256, lib) != NULL) {
793 if (strstr (string, "#define")) {
794 (void)parse_string (string);
795 strcpy (name, parse_string (NULL));
796 cleanup_name (name);
797 (void)update_materials (name, TRUE);
798 }
799 }
800 break;
801
802 case POLYRAY:
803 while (fgets (string, 256, lib) != NULL) {
804 if (strstr (string, "define")) {
805 (void)parse_string (string);
806 strcpy (name, parse_string (NULL));
807 cleanup_name (name);
808 (void)update_materials (name, TRUE);
809 }
810 }
811 break;
812
813 case MGF:
814 while (read_mgfmatname(name, 80, lib))
815 (void)update_materials (name, TRUE);
816 break;
817 }
818
819 fclose (lib);
820 }
821
822
823 /* parse the next MGF material name from f, return FALSE if EOF */
824 int read_mgfmatname (char *s, int n, FILE *f)
825 {
826 char inpline[128];
827 register char *cp, *cp2;
828 register int i;
829 /* find material */
830 while (fgets(inpline, sizeof(inpline), f) != NULL) {
831 for (cp = inpline; isspace(*cp); cp++)
832 ;
833 if (*cp++ != 'm' || !isspace(*cp++))
834 continue;
835 while (isspace(*cp))
836 cp++;
837 if (!*cp)
838 continue;
839 for (i=n, cp2=s; *cp && !isspace(*cp); cp++) /* get name */
840 if (--i > 0)
841 *cp2++ = *cp;
842 *cp2 = '\0';
843 while (isspace(*cp))
844 cp++;
845 if (*cp++ != '=' || !isspace(*cp)) /* not defined? */
846 continue;
847 return TRUE;
848 }
849 return FALSE;
850 }
851
852
853 void write_intro (FILE *f)
854 {
855 int i;
856
857 switch (format) {
858 case POV10:
859 case POV20:
860 fprintf (f, "#include \"colors.inc\"\n");
861 fprintf (f, "#include \"shapes.inc\"\n");
862 fprintf (f, "#include \"textures.inc\"\n");
863
864 for (i = 0; i < MAX_LIB; i++) {
865 if (strlen(libname[i]) > 0)
866 fprintf (f, "#include \"%s\"\n", libname[i]);
867 }
868
869 fprintf (f, "\n");
870 break;
871
872 case VIVID:
873 fprintf (f, "#include color.vc\n");
874
875 for (i = 0; i < MAX_LIB; i++) {
876 if (strlen(libname[i]) > 0)
877 fprintf (f, "#include %s\n", libname[i]);
878 }
879
880 fprintf (f, "\n");
881 break;
882
883 case POLYRAY:
884 fprintf (f, "include \"colors.inc\"\n");
885
886 for (i = 0; i < MAX_LIB; i++) {
887 if (strlen(libname[i]) > 0)
888 fprintf (f, "include \"%s\"\n", libname[i]);
889 }
890
891 fprintf (f, "\n");
892 break;
893
894 case MGF:
895 fprintf (f, "c R =\n\tcxy %.3f %.3f\n", CIE_x_r, CIE_y_r);
896 fprintf (f, "c G =\n\tcxy %.3f %.3f\n", CIE_x_g, CIE_y_g);
897 fprintf (f, "c B =\n\tcxy %.3f %.3f\n", CIE_x_b, CIE_y_b);
898
899 for (i = 0; i < MAX_LIB; i++) {
900 if (strlen(libname[i]) > 0)
901 fprintf (f, "i %s\n", libname[i]);
902 }
903
904 fprintf (f, "\n");
905 break;
906 }
907 }
908
909
910 /* Write the object summary */
911 void write_summary (FILE *f)
912 {
913 char *comstr;
914 Summary *s;
915
916 if (summary == NULL)
917 return;
918
919 switch (format) {
920 case POV10:
921 case POV20:
922 case VIVID:
923 case POLYRAY:
924 comstr = "//";
925 break;
926 case MGF:
927 comstr = "# ";
928 break;
929 }
930 fprintf (f, "%s Object CenterX CenterY CenterZ LengthX LengthY LengthZ\n", comstr);
931 fprintf (f, "%s ---------- ---------- ---------- ---------- ---------- ---------- ----------\n", comstr);
932
933 for (s = summary; s != NULL; s = s->next) {
934 fprintf (f, "%s %-10s%11.2f%11.2f%11.2f%11.2f%11.2f%11.2f\n",
935 comstr, s->name, s->center[X], s->center[Y], s->center[Z],
936 s->lengths[X], s->lengths[Y], s->lengths[Z]);
937 }
938
939 fprintf (f, "\n");
940 }
941
942
943 /* Write background solid colour */
944 void write_bgsolid (FILE *f, Colour col)
945 {
946 switch (format) {
947 case POV10:
948 fprintf (f, "/* Background colour */\n");
949 fprintf (f, "object {\n");
950 fprintf (f, " sphere { <0.0 0.0 0.0> 1e6 }\n");
951 fprintf (f, " texture {\n");
952 fprintf (f, " ambient 1.0\n");
953 fprintf (f, " diffuse 0.0\n");
954 fprintf (f, " color red %4.2f green %4.2f blue %4.2f\n",
955 col.red, col.green, col.blue);
956 fprintf (f, " }\n");
957 fprintf (f, "}\n\n");
958 break;
959
960 case POV20:
961 fprintf (f, "background { color red %4.2f green %4.2f blue %4.2f }\n\n",
962 col.red, col.green, col.blue);
963 break;
964
965 case POLYRAY:
966 fprintf (f, "background <%4.2f, %4.2f, %4.2f>\n\n",
967 col.red, col.green, col.blue);
968 break;
969 }
970 }
971
972
973 void write_light (FILE *f, char *name, Vector pos, Colour col)
974 {
975 switch (format) {
976 case POV10:
977 fprintf (f, "/* Light: %s */\n", name);
978 fprintf (f, "object {\n");
979 fprintf (f, " light_source { <%.4f %.4f %.4f> color red %4.2f green %4.2f blue %4.2f }\n",
980 pos[X], pos[Y], pos[Z], col.red, col.green, col.blue);
981 fprintf (f, "}\n\n");
982 break;
983
984 case POV20:
985 fprintf (f, "/* Light: %s */\n", name);
986 fprintf (f, "light_source {\n");
987 fprintf (f, " <%.4f, %.4f, %.4f> color rgb <%4.2f, %4.2f, %4.2f>\n",
988 pos[X], pos[Y], pos[Z], col.red, col.green, col.blue);
989 fprintf (f, "}\n\n");
990 break;
991
992 case VIVID:
993 fprintf (f, "/* Light: %s */\n", name);
994 fprintf (f, "light {\n");
995 fprintf (f, " type point\n");
996 fprintf (f, " position %.4f %.4f %.4f\n",
997 pos[X], pos[Y], pos[Z]);
998 fprintf (f, " color %4.2f %4.2f %4.2f\n",
999 col.red, col.green, col.blue);
1000 fprintf (f, "}\n\n");
1001 break;
1002
1003 case POLYRAY:
1004 fprintf (f, "// Light: %s\n", name);
1005 fprintf (f, "light <%4.2f, %4.2f, %4.2f>, <%.4f, %.4f, %.4f>\n\n",
1006 col.red, col.green, col.blue, pos[X], pos[Y], pos[Z]);
1007 break;
1008
1009 case MGF:
1010 fprintf (f, "\n# Light\n");
1011 if (name[0]) fprintf (f, "o %s\n", name);
1012 fprintf (f, "m\n\tsides 1\n\tc\n\t\t\tcmix %.3f R %.3f G %.3f B\n\ted %e\n",
1013 CIE_Y_r*col.red, CIE_Y_g*col.green, CIE_Y_b*col.blue,
1014 10000.0*(CIE_Y_r*col.red + CIE_Y_g*col.green + CIE_Y_b*col.blue));
1015 fprintf (f, "v c =\n\tp %.4f %.4f %.4f\nsph c .01\n",
1016 pos[X], pos[Y], pos[Z]);
1017 if (name[0]) fprintf (f, "o\n");
1018 fprintf (f, "\n");
1019 break;
1020 }
1021 }
1022
1023
1024 void write_spot (FILE *f, char *name, Vector pos, Vector target, Colour col,
1025 float hotspot, float falloff)
1026 {
1027 switch (format) {
1028 case POV10:
1029 fprintf (f, "/* Spotlight: %s */\n", name);
1030 fprintf (f, "object {\n");
1031 fprintf (f, " light_source {\n");
1032 fprintf (f, " <%.4f %.4f %.4f> color red %4.2f green %4.2f blue %4.2f\n",
1033 pos[X], pos[Y], pos[Z],
1034 col.red, col.green, col.blue);
1035 fprintf (f, " spotlight\n");
1036 fprintf (f, " point_at <%.4f %.4f %.4f>\n",
1037 target[X], target[Y], target[Z]);
1038 fprintf (f, " tightness 0\n");
1039 fprintf (f, " radius %.2f\n", 0.5*hotspot);
1040 fprintf (f, " falloff %.2f\n", 0.5*falloff);
1041 fprintf (f, " }\n");
1042 fprintf (f, "}\n\n");
1043 break;
1044
1045 case POV20:
1046 fprintf (f, "/* Spotlight: %s */\n", name);
1047 fprintf (f, "light_source {\n");
1048 fprintf (f, " <%.4f, %.4f, %.4f> color rgb <%4.2f, %4.2f, %4.2f>\n",
1049 pos[X], pos[Y], pos[Z],
1050 col.red, col.green, col.blue);
1051 fprintf (f, " spotlight\n");
1052 fprintf (f, " point_at <%.4f, %.4f, %.4f>\n",
1053 target[X], target[Y], target[Z]);
1054 fprintf (f, " tightness 0\n");
1055 fprintf (f, " radius %.2f\n", 0.5*hotspot);
1056 fprintf (f, " falloff %.2f\n", 0.5*falloff);
1057 fprintf (f, "}\n\n");
1058 break;
1059
1060 case VIVID:
1061 fprintf (f, "/* Spotlight: %s */\n", name);
1062 fprintf (f, "light {\n");
1063 fprintf (f, " type spot\n");
1064 fprintf (f, " position %.4f %.4f %.4f\n",
1065 pos[X], pos[Y], pos[Z]);
1066 fprintf (f, " at %.4f %.4f %.4f\n",
1067 target[X], target[Y], target[Z]);
1068 fprintf (f, " color %4.2f %4.2f %4.2f\n",
1069 col.red, col.green, col.blue);
1070 fprintf (f, " min_angle %.2f\n", hotspot);
1071 fprintf (f, " max_angle %.2f\n", falloff);
1072 fprintf (f, "}\n\n");
1073 break;
1074
1075 case POLYRAY:
1076 fprintf (f, "// Spotlight: %s\n", name);
1077 fprintf (f, "spot_light <%4.2f, %4.2f, %4.2f>, <%.4f, %.4f, %.4f>,\n",
1078 col.red, col.green, col.blue, pos[X], pos[Y], pos[Z]);
1079 fprintf (f, " <%.4f, %.4f, %.4f>, 0.0, %.2f, %.2f\n\n",
1080 target[X], target[Y], target[Z], hotspot/2.0, falloff/2.0);
1081 break;
1082
1083 case MGF:
1084 fprintf (f, "\n# Spotlight\n");
1085 if (name[0]) fprintf (f, "o %s\n", name);
1086 fprintf (f, "# hotspot: %.2f\n# falloff: %.2f\n", hotspot, falloff);
1087 fprintf (f, "m\n\tsides 1\n\tc\n\t\t\tcmix %.3f R %.3f G %.3f B\n\ted %e\n",
1088 CIE_Y_r*col.red, CIE_Y_g*col.green, CIE_Y_b*col.blue,
1089 10000.0*(CIE_Y_r*col.red + CIE_Y_g*col.green + CIE_Y_b*col.blue));
1090 fprintf (f, "v c =\n\tp %.4f %.4f %.4f\n\tn %.4f %.4f %.4f\n",
1091 pos[X], pos[Y], pos[Z],
1092 target[X]-pos[X], target[Y]-pos[Y], target[Z]-pos[Z]);
1093 fprintf (f, "ring c 0 .01\n");
1094 if (name[0]) fprintf (f, "o\n");
1095 fprintf (f, "\n");
1096 break;
1097 }
1098 }
1099
1100
1101 void write_fog (FILE *f, Colour col, float dist)
1102 {
1103 if (dist <= 0.0)
1104 return;
1105
1106 switch (format) {
1107 case POV10:
1108 fprintf (f, "fog {\n");
1109 fprintf (f, " color red %4.2f green %4.2f blue %4.2f %.4f\n",
1110 col.red, col.green, col.blue, dist/2.0);
1111 fprintf (f, "}\n\n");
1112 break;
1113
1114 case POV20:
1115 fprintf (f, "fog {\n");
1116 fprintf (f, " color red %4.2f green %4.2f blue %4.2f distance %.4f\n",
1117 col.red, col.green, col.blue, dist/2.0);
1118 fprintf (f, "}\n\n");
1119 break;
1120 }
1121 }
1122
1123
1124 void write_camera (FILE *f, char *name, Vector pos, Vector target,
1125 float lens, float bank)
1126 {
1127 float fov;
1128
1129 cameras++;
1130
1131 fov = findfov (lens);
1132
1133 switch (format) {
1134 case POV10:
1135 /* Comment out multiple cameras */
1136 if (cameras > 1)
1137 fprintf (f, "/*\n");
1138
1139 fprintf (f, "/* Camera: %s */\n", name);
1140 fprintf (f, "camera {\n");
1141 fprintf (f, " location <%.4f %.4f %.4f>\n",
1142 pos[X], pos[Y], pos[Z]);
1143 fprintf (f, " direction <0 %.3f 0>\n", 0.60/tan(0.5*RAD(fov)) );
1144 fprintf (f, " up <0 0 1>\n");
1145 fprintf (f, " sky <0 0 1>\n");
1146 fprintf (f, " right <%.3f 0 0>\n", ASPECT);
1147 fprintf (f, " look_at <%.4f %.4f %.4f>\n",
1148 target[X], target[Y], target[Z]);
1149 if (bank != 0.0)
1150 fprintf (f, " /* Bank angle = %.2f */\n", bank);
1151
1152 fprintf (f, "}\n");
1153
1154 if (cameras > 1)
1155 fprintf (f, "*/\n");
1156
1157 fprintf (f, "\n");
1158 break;
1159
1160 case POV20:
1161 /* Comment out multiple cameras */
1162 if (cameras > 1)
1163 fprintf (f, "/*\n");
1164
1165 fprintf (f, "/* Camera: %s */\n", name);
1166 fprintf (f, "camera {\n");
1167 fprintf (f, " location <%.4f, %.4f, %.4f>\n",
1168 pos[X], pos[Y], pos[Z]);
1169 fprintf (f, " direction <0, %.3f, 0>\n", 0.60/tan(0.5*RAD(fov)) );
1170 fprintf (f, " up <0, 0, 1>\n");
1171 fprintf (f, " sky <0, 0, 1>\n");
1172 fprintf (f, " right <%.3f, 0, 0>\n", ASPECT);
1173 fprintf (f, " look_at <%.4f, %.4f, %.4f>\n",
1174 target[X], target[Y], target[Z]);
1175 if (bank != 0.0)
1176 fprintf (f, " /* Bank angle = %.2f */\n", bank);
1177
1178 fprintf (f, "}\n");
1179
1180 if (cameras > 1)
1181 fprintf (f, "*/\n");
1182
1183 fprintf (f, "\n");
1184 break;
1185
1186 case VIVID:
1187 fprintf (f, "/* Camera: %s */\n", name);
1188
1189 if (cameras > 1)
1190 fprintf (f, "/*\n");
1191
1192 fprintf (f, "studio {\n");
1193 fprintf (f, " from %.4f %.4f %.4f\n",
1194 pos[X], pos[Y], pos[Z]);
1195 fprintf (f, " at %.4f %.4f %.4f\n",
1196 target[X], target[Y], target[Z]);
1197 fprintf (f, " up 0 0 1\n");
1198 fprintf (f, " angle %.2f\n", 1.1*fov);
1199 fprintf (f, " aspect %.3f\n", ASPECT);
1200 fprintf (f, " resolution 320 200\n");
1201 fprintf (f, " antialias none\n");
1202 fprintf (f, "}\n");
1203
1204 if (cameras > 1)
1205 fprintf (f, "*/\n");
1206
1207 fprintf (f, "\n");
1208 break;
1209
1210 case POLYRAY:
1211 if (cameras == 1) {
1212 fprintf (f, "// Camera: %s\n", name);
1213 fprintf (f, "viewpoint {\n");
1214 fprintf (f, " from <%.4f, %.4f, %.4f>\n",
1215 pos[X], pos[Y], pos[Z]);
1216 fprintf (f, " at <%.4f, %.4f, %.4f>\n",
1217 target[X], target[Y], target[Z]);
1218 fprintf (f, " up <0, 0, 1>\n");
1219 fprintf (f, " angle %.2f\n", 0.85*fov);
1220 fprintf (f, " aspect %.3f\n", -(ASPECT));
1221 fprintf (f, " resolution 320, 200\n");
1222 fprintf (f, "}\n");
1223 }
1224
1225 fprintf (f, "\n");
1226 break;
1227
1228 case MGF:
1229 fprintf (f, "# Camera %s\n", name);
1230 fprintf (f, "# from: %.4f %.4f %.4f\n", pos[X], pos[Y], pos[Z]);
1231 fprintf (f, "# to: %.4f %.4f %.4f\n", target[X], target[Y], target[Z]);
1232 fprintf (f, "# lens length: %.2f\n", lens);
1233 fprintf (f, "# bank: %.2f\n", bank);
1234 break;
1235 }
1236 }
1237
1238
1239 void write_material (FILE *f, char *mat)
1240 {
1241 MatProp *mprop = LIST_FIND (mprop_list, mat);
1242
1243 if (mprop == NULL) {
1244 mprop = &DefaultMaterial;
1245 (void)strcpy(mprop->name, mat);
1246 }
1247
1248 switch (format) {
1249 case POV10:
1250 write_pov10_material (f, mprop);
1251 break;
1252
1253 case POV20:
1254 write_pov20_material (f, mprop);
1255 break;
1256
1257 case VIVID:
1258 write_vivid_material (f, mprop);
1259 break;
1260
1261 case POLYRAY:
1262 write_polyray_material (f, mprop);
1263 break;
1264
1265 case MGF:
1266 write_mgf_material (f, mprop);
1267 break;
1268 }
1269 }
1270
1271
1272 void write_pov10_material (FILE *f, MatProp *m)
1273 {
1274 float amb = 0.1, dif = 0.9, spec = 1.0;
1275 float dist_white, dist_diff, phong, phong_size;
1276 float red, green, blue;
1277
1278 /* amb = get_ambient (m); */
1279
1280 if (m->self_illum) {
1281 amb = 0.9;
1282 dif = 0.1;
1283 }
1284
1285 dist_white = fabs(1.0 - m->specular.red) +
1286 fabs(1.0 - m->specular.green) +
1287 fabs(1.0 - m->specular.blue);
1288
1289 dist_diff = fabs(m->diffuse.red - m->specular.red) +
1290 fabs(m->diffuse.green - m->specular.green) +
1291 fabs(m->diffuse.blue - m->specular.blue);
1292
1293
1294 phong_size = 0.7*m->shininess;
1295 if (phong_size < 1.0) phong_size = 1.0;
1296
1297 if (phong_size > 30.0)
1298 phong = 1.0;
1299 else
1300 phong = phong_size/30.0;
1301
1302 fprintf (f, "#declare %s = texture {\n", m->name);
1303 fprintf (f, " ambient %.2f\n", amb);
1304 fprintf (f, " diffuse %.2f\n", dif);
1305 fprintf (f, " phong %.2f\n", phong);
1306 fprintf (f, " phong_size %.1f\n", phong_size);
1307
1308 if (dist_diff < dist_white)
1309 fprintf (f, " metallic\n");
1310
1311 if (m->reflection > 0.0) {
1312 spec = (m->specular.red + m->specular.green + m->specular.blue)/3.0;
1313 fprintf (f, " reflection %.3f\n", spec * m->reflection);
1314 }
1315
1316 if (m->transparency > 0.0) {
1317 red = m->diffuse.red;
1318 green = m->diffuse.green;
1319 blue = m->diffuse.blue;
1320
1321 /* Saturate the colour towards white as the transparency increases */
1322 red = ((1.0 - m->transparency) * red) + m->transparency;
1323 green = ((1.0 - m->transparency) * green) + m->transparency;
1324 blue = ((1.0 - m->transparency) * blue) + m->transparency;
1325
1326 fprintf (f, " color red %.3f green %.3f blue %.3f alpha %.3f\n",
1327 red, green, blue, m->transparency);
1328 fprintf (f, " ior 1.1\n");
1329 fprintf (f, " refraction 1.0\n");
1330 }
1331 else
1332 fprintf (f, " color red %.3f green %.3f blue %.3f\n",
1333 m->diffuse.red, m->diffuse.green, m->diffuse.blue);
1334
1335 if (strlen (m->tex_map) > 0) {
1336 fprintf (f, " /* Image map: %s, Strength: %.2f */\n",
1337 m->tex_map, m->tex_strength);
1338 }
1339
1340 if (strlen (m->bump_map) > 0) {
1341 fprintf (f, " /* Bump map: %s, Strength: %.2f */\n",
1342 m->bump_map, m->bump_strength);
1343 }
1344
1345 fprintf (f, "}\n\n");
1346 }
1347
1348
1349 void write_pov20_material (FILE *f, MatProp *m)
1350 {
1351 float amb = 0.1, dif = 0.9, spec = 1.0;
1352 float dist_white, dist_diff, phong, phong_size;
1353 float red, green, blue;
1354
1355 /* amb = get_ambient (m); */
1356
1357 if (m->self_illum) {
1358 amb = 0.9;
1359 dif = 0.1;
1360 }
1361
1362 dist_white = fabs(1.0 - m->specular.red) +
1363 fabs(1.0 - m->specular.green) +
1364 fabs(1.0 - m->specular.blue);
1365
1366 dist_diff = fabs(m->diffuse.red - m->specular.red) +
1367 fabs(m->diffuse.green - m->specular.green) +
1368 fabs(m->diffuse.blue - m->specular.blue);
1369
1370 phong_size = 0.7*m->shininess;
1371 if (phong_size < 1.0) phong_size = 1.0;
1372
1373 if (phong_size > 30.0)
1374 phong = 1.0;
1375 else
1376 phong = phong_size/30.0;
1377
1378 fprintf (f, "#declare %s = texture {\n", m->name);
1379 fprintf (f, " finish {\n");
1380 fprintf (f, " ambient %.2f\n", amb);
1381 fprintf (f, " diffuse %.2f\n", dif);
1382 fprintf (f, " phong %.2f\n", phong);
1383 fprintf (f, " phong_size %.1f\n", phong_size);
1384
1385 if (dist_diff < dist_white)
1386 fprintf (f, " metallic\n");
1387
1388 if (m->reflection > 0.0) {
1389 spec = (m->specular.red + m->specular.green + m->specular.blue)/3.0;
1390 fprintf (f, " reflection %.3f\n", spec * m->reflection);
1391 }
1392
1393 if (m->transparency > 0.0) {
1394 fprintf (f, " ior 1.1\n");
1395 fprintf (f, " refraction 1.0\n");
1396 }
1397
1398 fprintf (f, " }\n");
1399
1400 if (m->transparency > 0.0) {
1401 red = m->diffuse.red;
1402 green = m->diffuse.green;
1403 blue = m->diffuse.blue;
1404
1405 /* Saturate the colour towards white as the transparency increases */
1406 red = ((1.0 - m->transparency) * red) + m->transparency;
1407 green = ((1.0 - m->transparency) * green) + m->transparency;
1408 blue = ((1.0 - m->transparency) * blue) + m->transparency;
1409
1410 fprintf (f, " pigment { rgbf <%.3f, %.3f, %.3f, %.3f> }\n",
1411 red, green, blue, m->transparency);
1412 }
1413 else
1414 fprintf (f, " pigment { rgb <%.3f, %.3f, %.3f> }\n",
1415 m->diffuse.red, m->diffuse.green, m->diffuse.blue);
1416
1417 if (strlen (m->tex_map) > 0) {
1418 fprintf (f, " /* Image map: %s, Strength: %.2f */\n",
1419 m->tex_map, m->tex_strength);
1420 }
1421
1422 if (strlen (m->bump_map) > 0) {
1423 fprintf (f, " /* Bump map: %s, Strength: %.2f */\n",
1424 m->bump_map, m->bump_strength);
1425 }
1426
1427 fprintf (f, "}\n\n");
1428 }
1429
1430
1431 void write_vivid_material (FILE *f, MatProp *m)
1432 {
1433 float amb = 0.1, dif = 0.9;
1434
1435 /* amb = get_ambient (m); */
1436
1437 if (m->self_illum) {
1438 amb = 0.9;
1439 dif = 0.1;
1440 }
1441
1442 if (m->transparency > 0.0) {
1443 dif = dif - m->transparency;
1444 if (dif < 0.0) dif = 0.0;
1445 }
1446
1447 fprintf (f, "#define %s \\ \n", m->name);
1448 fprintf (f, " surface { \\ \n");
1449 fprintf (f, " ambient %.3f %.3f %.3f \\ \n",
1450 amb*m->ambient.red, amb*m->ambient.green, amb*m->ambient.blue);
1451
1452 fprintf (f, " diffuse %.3f %.3f %.3f \\ \n",
1453 dif*m->diffuse.red, dif*m->diffuse.green, dif*m->diffuse.blue);
1454
1455 fprintf (f, " shine %.1f %.3f %.3f %.3f \\ \n",
1456 0.7*m->shininess, m->specular.red, m->specular.green, m->specular.blue);
1457
1458 if (m->transparency > 0.0) {
1459 fprintf (f, " transparent %.3f*white \\ \n", 1.0 - (1.0 - m->transparency)/14.0);
1460 fprintf (f, " ior 1.1 \\ \n");
1461 }
1462
1463 if (m->reflection > 0.0)
1464 fprintf (f, " specular %.3f*white \\ \n", m->reflection);
1465
1466 if (strlen (m->tex_map) > 0) {
1467 fprintf (f, " /* Image map: %s, Strength: %.2f */ \\ \n",
1468 m->tex_map, m->tex_strength);
1469 }
1470
1471 if (strlen (m->bump_map) > 0) {
1472 fprintf (f, " /* Bump map: %s, Strength: %.2f */ \\ \n",
1473 m->bump_map, m->bump_strength);
1474 }
1475
1476 fprintf (f, " }\n\n");
1477 }
1478
1479
1480 void write_polyray_material (FILE *f, MatProp *m)
1481 {
1482 float amb = 0.1, dif = 0.9, spec;
1483
1484 /* amb = get_ambient (m); */
1485
1486 if (m->self_illum) {
1487 amb = 0.9;
1488 dif = 0.1;
1489 }
1490
1491 if (m->transparency > 0.0) {
1492 dif = dif - m->transparency;
1493 if (dif < 0.0) dif = 0.0;
1494 }
1495
1496 if (m->shininess == 0.0)
1497 m->shininess = 0.1;
1498
1499 if (m->shininess > 40.0)
1500 spec = 1.0;
1501 else
1502 spec = m->shininess/40.0;
1503
1504 fprintf (f, "define %s\n", m->name);
1505 fprintf (f, "texture {\n");
1506 fprintf (f, " surface {\n");
1507 fprintf (f, " ambient <%.3f, %.3f, %.3f>, %.1f\n",
1508 m->ambient.red, m->ambient.green, m->ambient.blue, amb);
1509
1510 fprintf (f, " diffuse <%.3f, %.3f, %.3f>, %.1f\n",
1511 m->diffuse.red, m->diffuse.green, m->diffuse.blue, dif);
1512
1513 fprintf (f, " specular <%.3f, %.3f, %.3f>, %.2f\n",
1514 m->specular.red, m->specular.green, m->specular.blue, spec);
1515
1516 fprintf (f, " microfacet Reitz %.1f\n", 400.0/m->shininess);
1517
1518 if (m->transparency > 0.0)
1519 fprintf (f, " transmission %.3f, 1.1\n", m->transparency);
1520
1521 if (m->reflection > 0.0)
1522 fprintf (f, " reflection %.3f\n", m->reflection);
1523
1524 if (strlen (m->tex_map) > 0) {
1525 fprintf (f, " // Image map: %s, Strength: %.2f\n",
1526 m->tex_map, m->tex_strength);
1527 }
1528
1529 if (strlen (m->bump_map) > 0) {
1530 fprintf (f, " // Bump map: %s, Strength: %.2f\n",
1531 m->bump_map, m->bump_strength);
1532 }
1533
1534 fprintf (f, " }\n");
1535 fprintf (f, "}\n\n");
1536 }
1537
1538
1539 void write_mgf_material (FILE *f, MatProp *m)
1540 {
1541 float dmag, smag, rdmag, rsmag, tdmag, tsmag, total;
1542
1543 fprintf (f, "m %s =\n", m->name);
1544 fprintf (f, "\tsides %d\n", m->two_side ? 2 : 1);
1545 dmag = CIE_Y_r*m->diffuse.red + CIE_Y_g*m->diffuse.green
1546 + CIE_Y_b*m->diffuse.blue;
1547 smag = CIE_Y_r*m->specular.red + CIE_Y_g*m->specular.green
1548 + CIE_Y_b*m->specular.blue;
1549 rdmag = dmag;
1550 rsmag = smag * m->reflection;
1551 tdmag = 0.0;
1552 tsmag = m->transparency;
1553 total = rdmag + rsmag + tdmag + tsmag;
1554 if (total > 0.99) {
1555 total = 0.9/total;
1556 dmag *= total;
1557 smag *= total;
1558 rdmag *= total;
1559 rsmag *= total;
1560 tdmag *= total;
1561 tsmag *= total;
1562 total = 0.9;
1563 }
1564 if (dmag > 0.005) {
1565 fprintf (f, "\tc\n\t\tcmix %.3f R %.3f G %.3f B\n",
1566 CIE_Y_r*m->diffuse.red,
1567 CIE_Y_g*m->diffuse.green,
1568 CIE_Y_b*m->diffuse.blue);
1569 if (rdmag > 0.005)
1570 fprintf (f, "\trd %.4f\n", rdmag);
1571 if (tdmag > 0.005)
1572 fprintf (f, "\ttd %.4f\n", tdmag);
1573 if (m->self_illum)
1574 fprintf (f, "\ted %.4f\n", dmag);
1575 }
1576 if (m->shininess > 1.1 && rsmag > 0.005) {
1577 fprintf (f, "\tc\n\t\tcmix %.3f R %.3f G %.3f B\n",
1578 CIE_Y_r*m->specular.red,
1579 CIE_Y_g*m->specular.green,
1580 CIE_Y_b*m->specular.blue);
1581 fprintf (f, "\trs %.4f %.4f\n", rsmag, 0.6/sqrt(m->shininess));
1582 }
1583 if (tsmag > 0.005)
1584 fprintf (f, "\tc\n\tts %.4f 0\n", tsmag);
1585
1586 if (strlen (m->tex_map) > 0) {
1587 fprintf (f, "# image map: %s, strength: %.2f\n",
1588 m->tex_map, m->tex_strength);
1589 }
1590
1591 if (strlen (m->bump_map) > 0) {
1592 fprintf (f, "# bump map: %s, strength: %.2f\n",
1593 m->bump_map, m->bump_strength);
1594 }
1595
1596 fprintf (f, "\n");
1597 }
1598
1599
1600 /* Write a mesh file */
1601 void write_mesh (FILE *f, Mesh *mesh)
1602 {
1603 FILE *fi;
1604 int i;
1605 char curmat[80];
1606 Vector va, vb, vc;
1607 Summary *new_summary;
1608 Matrix obj_matrix;
1609
1610 if (mesh->hidden || LIST_FIND (excl_list, mesh->name))
1611 return;
1612
1613 /* Add this object's stats to the summary */
1614 new_summary = malloc (sizeof(*new_summary));
1615 if (new_summary == NULL)
1616 abortmsg ("Out of memory adding summary", 1);
1617
1618 strcpy (new_summary->name, mesh->name);
1619 vect_copy (new_summary->center, mesh->center);
1620 vect_copy (new_summary->lengths, mesh->lengths);
1621
1622 LIST_INSERT (summary, new_summary);
1623
1624 /* Compute the object transformation matrix for animations */
1625 if (ani_matrix != NULL) {
1626 mat_copy (obj_matrix, *ani_matrix);
1627 if (vue_version > 2.0)
1628 mat_mult (obj_matrix, mesh->invmatrix, obj_matrix);
1629 }
1630
1631 switch (format) {
1632 case MGF:
1633 if (no_opt) {
1634 if (mesh->name[0]) fprintf (meshf, "o %s\n", mesh->name);
1635 for (i = 0; i < mesh->vertices; i++) {
1636 vect_copy(va, mesh->vertex[i]);
1637 if (ani_matrix != NULL)
1638 vect_transform (va, va, obj_matrix);
1639 fprintf (meshf, "v v%d =\n\tp %.5f %.5f %.5f\n",
1640 i, va[X], va[Y], va[Z]);
1641 }
1642 curmat[0] = '\0';
1643 for (i = 0; i < mesh->faces; i++) {
1644 if (strcmp(mesh->mtl[i]->name, curmat)) {
1645 strcpy(curmat, mesh->mtl[i]->name);
1646 fprintf (meshf, "m %s\n", curmat);
1647 }
1648 fprintf (meshf, "f v%d v%d v%d\n", mesh->face[i].a,
1649 mesh->face[i].b, mesh->face[i].c);
1650 }
1651 if (mesh->name[0]) fprintf (meshf, "o\n");
1652 break;
1653 }
1654 /*FALL THROUGH*/
1655 case POV10:
1656 case POV20:
1657 case VIVID:
1658 case POLYRAY:
1659 opt_set_vert (mesh->vertices);
1660
1661 for (i = 0; i < mesh->faces; i++) {
1662 vect_copy (va, mesh->vertex[mesh->face[i].a]);
1663 vect_copy (vb, mesh->vertex[mesh->face[i].b]);
1664 vect_copy (vc, mesh->vertex[mesh->face[i].c]);
1665
1666 opt_set_texture (mesh->mtl[i]->name);
1667
1668 opt_add_tri (va[X], va[Y], va[Z], vc[X], vc[Y], vc[Z],
1669 vb[X], vb[Y], vb[Z]);
1670 }
1671
1672 fflush (f);
1673
1674 if (ani_matrix != NULL)
1675 opt_set_transform (obj_matrix);
1676
1677 if (box_all || LIST_FIND (box_list, mesh->name))
1678 opt_write_box (mesh->name);
1679 else
1680 opt_write_file (mesh->name);
1681
1682 break;
1683
1684 case RAW:
1685 fprintf (f, "%s\n", mesh->name);
1686
1687 for (i = 0; i < mesh->faces; i++) {
1688 vect_copy (va, mesh->vertex[mesh->face[i].a]);
1689 vect_copy (vb, mesh->vertex[mesh->face[i].b]);
1690 vect_copy (vc, mesh->vertex[mesh->face[i].c]);
1691
1692 if (ani_matrix != NULL) {
1693 vect_transform (va, va, obj_matrix);
1694 vect_transform (vb, vb, obj_matrix);
1695 vect_transform (vc, vc, obj_matrix);
1696 }
1697
1698 fprintf (f, "%f %f %f %f %f %f %f %f %f\n",
1699 va[X], va[Y], va[Z], vb[X], vb[Y], vb[Z],
1700 vc[X], vc[Y], vc[Z]);
1701 }
1702
1703 break;
1704 }
1705 }
1706
1707
1708 /* Parses an object transformation and returns a pointer to the
1709 newly allocated transformation */
1710 Transform *parse_transform (char *string)
1711 {
1712 Transform *t;
1713 char *token;
1714 int token_no;
1715
1716 t = (Transform *)malloc (sizeof(*t));
1717 if (t == NULL)
1718 abortmsg ("Out of memory allocating transform", 1);
1719
1720 mat_identity (t->matrix);
1721
1722 token = parse_string (string);
1723 token_no = 0;
1724
1725 while (strlen(token) > 0) {
1726 switch (token_no) {
1727 case 0: break;
1728 case 1: strcpy (t->name, token); break;
1729 case 2: t->matrix[0][0] = atof(token); break;
1730 case 3: t->matrix[0][1] = atof(token); break;
1731 case 4: t->matrix[0][2] = atof(token); break;
1732 case 5: t->matrix[1][0] = atof(token); break;
1733 case 6: t->matrix[1][1] = atof(token); break;
1734 case 7: t->matrix[1][2] = atof(token); break;
1735 case 8: t->matrix[2][0] = atof(token); break;
1736 case 9: t->matrix[2][1] = atof(token); break;
1737 case 10: t->matrix[2][2] = atof(token); break;
1738 case 11: t->matrix[3][0] = atof(token); break;
1739 case 12: t->matrix[3][1] = atof(token); break;
1740 case 13: t->matrix[3][2] = atof(token); break;
1741
1742 default: abortmsg ("Error parsing transform", 1);
1743 }
1744
1745 token = parse_string (NULL);
1746 token_no++;
1747 }
1748
1749 t->matrix[0][3] = 0.0;
1750 t->matrix[1][3] = 0.0;
1751 t->matrix[2][3] = 0.0;
1752 t->matrix[3][3] = 1.0;
1753
1754 cleanup_name (t->name);
1755
1756 return t;
1757 }
1758
1759
1760 /* Parses a morph command and returns a pointer to the
1761 newly allocated morph */
1762 Morph *parse_morph (char *string)
1763 {
1764 Morph *m;
1765 char *token;
1766 int i, token_no;
1767
1768 m = (Morph *)malloc (sizeof(*m));
1769 if (m == NULL)
1770 abortmsg ("Out of memory allocating morph", 1);
1771
1772 mat_identity (m->matrix);
1773
1774 token = parse_string (string);
1775
1776 token = parse_string (NULL);
1777 strcpy (m->name, token);
1778
1779 token = parse_string (NULL);
1780 m->count = atoi (token);
1781
1782 if (strlen (m->name) == 0 || m->count < 1 || m->count > 4)
1783 abortmsg ("Error parsing morph command", 1);
1784
1785 cleanup_name (m->name);
1786
1787 for (i = 0; i < m->count; i++) {
1788 token = parse_string (NULL);
1789 strcpy (m->names[i], token);
1790
1791 token = parse_string (NULL);
1792 m->weight[i] = atof (token);
1793
1794 if (strlen (m->names[i]) == 0)
1795 abortmsg ("Error parsing morph command", 1);
1796
1797 cleanup_name (m->names[i]);
1798 }
1799
1800 token = parse_string (NULL);
1801 token_no = 0;
1802
1803 while (strlen(token) > 0) {
1804 switch (token_no) {
1805 case 0: m->matrix[0][0] = atof(token); break;
1806 case 1: m->matrix[0][1] = atof(token); break;
1807 case 2: m->matrix[0][2] = atof(token); break;
1808 case 3: m->matrix[1][0] = atof(token); break;
1809 case 4: m->matrix[1][1] = atof(token); break;
1810 case 5: m->matrix[1][2] = atof(token); break;
1811 case 6: m->matrix[2][0] = atof(token); break;
1812 case 7: m->matrix[2][1] = atof(token); break;
1813 case 8: m->matrix[2][2] = atof(token); break;
1814 case 9: m->matrix[3][0] = atof(token); break;
1815 case 10: m->matrix[3][1] = atof(token); break;
1816 case 11: m->matrix[3][2] = atof(token); break;
1817
1818 default: abortmsg ("Error parsing morph command", 1);
1819 }
1820
1821 token = parse_string (NULL);
1822 token_no++;
1823 }
1824
1825 m->matrix[0][3] = 0.0;
1826 m->matrix[1][3] = 0.0;
1827 m->matrix[2][3] = 0.0;
1828 m->matrix[3][3] = 1.0;
1829
1830 return m;
1831 }
1832
1833
1834 /* Parses an omni light and returns a pointer to the
1835 newly allocated light */
1836 OmniLight *parse_omnilight (char *string)
1837 {
1838 OmniLight *o;
1839 char *token;
1840 int token_no;
1841
1842 o = (OmniLight *)malloc (sizeof(*o));
1843 if (o == NULL)
1844 abortmsg ("Out of memory allocating omnilight", 1);
1845
1846 token = parse_string (string);
1847 token_no = 0;
1848
1849 while (strlen(token) > 0) {
1850 switch (token_no) {
1851 case 0: break;
1852 case 1: strcpy (o->name, token); break;
1853 case 2: o->pos[X] = atof (token); break;
1854 case 3: o->pos[Y] = atof (token); break;
1855 case 4: o->pos[Z] = atof (token); break;
1856 case 5: o->col.red = atof (token); break;
1857 case 6: o->col.green = atof (token); break;
1858 case 7: o->col.blue = atof (token); break;
1859
1860 default: abortmsg ("Error parsing omnilight", 1);
1861 }
1862
1863 token = parse_string (NULL);
1864 token_no++;
1865 }
1866
1867 cleanup_name (o->name);
1868
1869 return o;
1870 }
1871
1872
1873 /* Parses a spotlight and returns a pointer to the
1874 newly allocated spotlight */
1875 Spotlight *parse_spotlight (char *string)
1876 {
1877 Spotlight *s;
1878 char *token;
1879 int token_no;
1880
1881 s = (Spotlight *)malloc (sizeof(*s));
1882 if (s == NULL)
1883 abortmsg ("Out of memory allocating spotlight", 1);
1884
1885 token = parse_string (string);
1886 token_no = 0;
1887
1888 while (strlen(token) > 0) {
1889 switch (token_no) {
1890 case 0: break;
1891 case 1: strcpy (s->name, token); break;
1892 case 2: s->pos[X] = atof (token); break;
1893 case 3: s->pos[Y] = atof (token); break;
1894 case 4: s->pos[Z] = atof (token); break;
1895 case 5: s->target[X] = atof (token); break;
1896 case 6: s->target[Y] = atof (token); break;
1897 case 7: s->target[Z] = atof (token); break;
1898 case 8: s->col.red = atof (token); break;
1899 case 9: s->col.green = atof (token); break;
1900 case 10: s->col.blue = atof (token); break;
1901 case 11: s->hotspot = atof (token); break;
1902 case 12: s->falloff = atof (token); break;
1903 case 13: break;
1904
1905 default: abortmsg ("Error parsing spotlight", 1);
1906 }
1907
1908 token = parse_string (NULL);
1909 token_no++;
1910 }
1911
1912 cleanup_name (s->name);
1913
1914 return s;
1915 }
1916
1917
1918 /* Parses a camera command and returns a pointer to the
1919 newly allocated camera */
1920 Camera *parse_camera (char *string)
1921 {
1922 Camera *c;
1923 char *token;
1924 int token_no;
1925
1926 c = (Camera *)malloc (sizeof(*c));
1927 if (c == NULL)
1928 abortmsg ("Out of memory allocating camera", 1);
1929
1930 token = parse_string (string);
1931 token_no = 0;
1932
1933 while (strlen(token) > 0) {
1934 switch (token_no) {
1935 case 0: break;
1936 case 1: c->pos[X] = atof (token); break;
1937 case 2: c->pos[Y] = atof (token); break;
1938 case 3: c->pos[Z] = atof (token); break;
1939 case 4: c->target[X] = atof (token); break;
1940 case 5: c->target[Y] = atof (token); break;
1941 case 6: c->target[Z] = atof (token); break;
1942 case 7: c->bank = atof (token); break;
1943 case 8: c->lens = atof (token); break;
1944
1945 default: abortmsg ("Error parsing camera", 1);
1946 }
1947
1948 token = parse_string (NULL);
1949 token_no++;
1950 }
1951
1952 return c;
1953 }
1954
1955
1956 /* Load the transforms, camera movements, etc for the specified frame */
1957 void read_frame (char *filename, int frame_no)
1958 {
1959 FILE *f;
1960 char fname[80];
1961 char string[256];
1962 char *token;
1963
1964 /* Open the .vue file */
1965 strcpy (fname, filename); /* Make a copy we can mess with */
1966 add_ext (fname, "vue", 0);
1967
1968 f = fopen (fname, "r");
1969 if (f == NULL) {
1970 printf ("Error opening file '%s'\n", fname);
1971 exit(1);
1972 }
1973
1974 /* Load the specified frame */
1975 find_frame (f, frame_no);
1976
1977 while (fgets (string, 256, f) != NULL) {
1978 token = parse_string (string);
1979
1980 if (strcmp (token, "frame") == 0)
1981 break;
1982 else if (strcmp (token, "transform") == 0) {
1983 LIST_INSERT (trans_list, parse_transform (string));
1984 }
1985 else if (strcmp (token, "morph") == 0) {
1986 LIST_INSERT (morph_list, parse_morph (string));
1987 }
1988 else if (strcmp (token, "light") == 0) {
1989 LIST_INSERT (omni_list, parse_omnilight (string));
1990 }
1991 else if (strcmp (token, "spotlight") == 0) {
1992 LIST_INSERT (spot_list, parse_spotlight (string));
1993 }
1994 else if (strcmp (token, "camera") == 0) {
1995 if (cam_list != NULL)
1996 abortmsg ("ERROR - Multiple cameras in .vue file", 1);
1997
1998 LIST_INSERT (cam_list, parse_camera (string));
1999 }
2000 else if (strcmp (token, "top") == 0)
2001 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2002 else if (strcmp (token, "bottom") == 0)
2003 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2004 else if (strcmp (token, "left") == 0)
2005 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2006 else if (strcmp (token, "right") == 0)
2007 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2008 else if (strcmp (token, "front") == 0)
2009 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2010 else if (strcmp (token, "back") == 0)
2011 abortmsg ("ERROR - Orthogonal viewports are not supported", 1);
2012 else if (strcmp (token, "user") == 0)
2013 abortmsg ("ERROR - User viewports are not supported", 1);
2014 }
2015
2016 fclose(f);
2017 }
2018
2019
2020 void find_frame (FILE *f, int frame_no)
2021 {
2022 char string[256];
2023 char *token;
2024 int frame = 0;
2025
2026 /* Search the .vue file for the required frame */
2027 while (1) {
2028 /* Read the next line in the file */
2029 if (fgets (string, 256, f) == NULL) {
2030 printf ("Unable to locate frame #%d in .vue file\n", frame_no);
2031 exit(1);
2032 }
2033
2034 token = parse_string (string);
2035
2036 if (strcmp (token, "frame") == 0) {
2037 token = parse_string (NULL);
2038
2039 if (strlen(token) == 0) {
2040 printf ("Unable to locate frame #%d in .vue file\n", frame_no);
2041 exit(1);
2042 }
2043
2044 frame = atoi (token);
2045
2046 if (frame == frame_no)
2047 break;
2048 }
2049 else if (strcmp (token, "VERSION") == 0) {
2050 token = parse_string (NULL);
2051
2052 vue_version = atoi(token) / 100.0;
2053 }
2054 }
2055 }
2056
2057
2058 void save_animation()
2059 {
2060 Mesh *mesh, *master;
2061 Transform *t;
2062 Morph *m;
2063 Vector temp;
2064 int i, j;
2065
2066 printf ("\n");
2067
2068 for (t = trans_list; t != NULL; t = t->next) {
2069 printf ("Transforming object: %s\n", t->name);
2070
2071 ani_matrix = &(t->matrix);
2072
2073 mesh = LIST_FIND (mesh_list, t->name);
2074
2075 if (mesh == NULL) {
2076 printf ("Unable to locate mesh object %s\n", t->name);
2077 exit(1);
2078 }
2079
2080 write_mesh (out, mesh);
2081 }
2082
2083 for (m = morph_list; m != NULL; m = m->next) {
2084 printf ("Morphing object: %s\n", m->name);
2085
2086 ani_matrix = &(m->matrix);
2087
2088 mesh = LIST_FIND (mesh_list, m->name);
2089 if (mesh == NULL) {
2090 printf ("Unable to locate mesh object %s\n", m->name);
2091 exit(1);
2092 }
2093
2094 /* Make a copy to mess with */
2095 master = copy_mesh (mesh);
2096 master->hidden = FALSE;
2097
2098 strcpy (master->name, m->name);
2099
2100 for (i = 0; i < master->vertices; i++)
2101 vect_init (master->vertex[i], 0.0, 0.0, 0.0);
2102
2103 for (i = 0; i < m->count; i++) {
2104 mesh = LIST_FIND (mesh_list, m->names[i]);
2105 if (mesh == NULL) {
2106 printf ("Unable to locate mesh object %s\n", m->names[0]);
2107 exit(1);
2108 }
2109
2110 if (mesh->vertices != master->vertices)
2111 abortmsg ("Morphed objects do not contain the same number of vertices", 1);
2112
2113 if (mesh->faces != master->faces)
2114 abortmsg ("Morphed objects do not contain the same number of faces", 1);
2115
2116 for (j = 0; j < master->vertices; j++) {
2117 vect_transform (temp, mesh->vertex[j], mesh->invmatrix);
2118 vect_scale (temp, temp, m->weight[i]);
2119 vect_add (master->vertex[j], master->vertex[j], temp);
2120 }
2121 }
2122
2123 for (i = 0; i < master->vertices; i++)
2124 vect_transform (master->vertex[i], master->vertex[i], master->matrix);
2125
2126 write_mesh (out, master);
2127
2128 free_mesh_data (master);
2129 free (master);
2130 }
2131
2132 for (mesh = mesh_list; mesh != NULL; mesh = mesh->next)
2133 free_mesh_data (mesh);
2134 }
2135
2136
2137 /* Create a new mesh */
2138 Mesh *create_mesh (char *name, int vertices, int faces)
2139 {
2140 Mesh *new_mesh;
2141
2142 new_mesh = malloc (sizeof(*new_mesh));
2143 if (new_mesh == NULL)
2144 abortmsg ("Out of memory allocating mesh", 1);
2145
2146 strcpy (new_mesh->name, name);
2147
2148 new_mesh->vertices = vertices;
2149
2150 if (vertices <= 0)
2151 new_mesh->vertex = NULL;
2152 else {
2153 new_mesh->vertex = malloc (vertices * sizeof(*new_mesh->vertex));
2154 if (new_mesh->vertex == NULL)
2155 abortmsg ("Out of memory allocating mesh", 1);
2156 }
2157
2158 new_mesh->faces = faces;
2159
2160 if (faces <= 0) {
2161 new_mesh->face = NULL;
2162 new_mesh->mtl = NULL;
2163 }
2164 else {
2165 new_mesh->face = malloc (faces * sizeof(*new_mesh->face));
2166 if (new_mesh->face == NULL)
2167 abortmsg ("Out of memory allocating mesh", 1);
2168
2169 new_mesh->mtl = malloc (faces * sizeof(*new_mesh->mtl));
2170 if (new_mesh->mtl == NULL)
2171 abortmsg ("Out of memory allocating mesh", 1);
2172 }
2173
2174 vect_init (new_mesh->center, 0.0, 0.0, 0.0);
2175 vect_init (new_mesh->lengths, 0.0, 0.0, 0.0);
2176
2177 mat_identity (new_mesh->matrix);
2178 mat_identity (new_mesh->invmatrix);
2179
2180 new_mesh->hidden = FALSE;
2181 new_mesh->shadow = TRUE;
2182
2183 return new_mesh;
2184 }
2185
2186
2187 /* Creates a duplicate copy of a mesh */
2188 Mesh *copy_mesh (Mesh *mesh)
2189 {
2190 Mesh *new_mesh;
2191 int i;
2192
2193 new_mesh = create_mesh (mesh->name, mesh->vertices, mesh->faces);
2194
2195 if (new_mesh == NULL)
2196 abortmsg ("Out of memory allocating mesh", 1);
2197
2198 for (i = 0; i < mesh->vertices; i++)
2199 vect_copy (new_mesh->vertex[i], mesh->vertex[i]);
2200
2201 for (i = 0; i < mesh->faces; i++) {
2202 new_mesh->face[i] = mesh->face[i];
2203 new_mesh->mtl[i] = mesh->mtl[i];
2204 }
2205
2206 mat_copy (new_mesh->matrix, mesh->matrix);
2207 mat_copy (new_mesh->invmatrix, mesh->invmatrix);
2208
2209 vect_copy (new_mesh->center, mesh->center);
2210 vect_copy (new_mesh->lengths, mesh->lengths);
2211
2212 new_mesh->hidden = mesh->hidden;
2213 new_mesh->shadow = mesh->shadow;
2214
2215 return new_mesh;
2216 }
2217
2218
2219 /* Free all data associated with mesh object */
2220 void free_mesh_data (Mesh *mesh)
2221 {
2222 if (mesh->vertex != NULL)
2223 free (mesh->vertex);
2224
2225 if (mesh->face != NULL)
2226 free (mesh->face);
2227
2228 if (mesh->mtl != NULL)
2229 free (mesh->mtl);
2230 }
2231
2232
2233 /* Updates the center (pivot) point of the mesh */
2234 void update_limits (Mesh *mesh)
2235 {
2236 Vector vmin = {+MAXFLOAT, +MAXFLOAT, +MAXFLOAT};
2237 Vector vmax = {-MAXFLOAT, -MAXFLOAT, -MAXFLOAT};
2238 int i;
2239
2240 for (i = 0; i < mesh->vertices; i++) {
2241 vect_min (vmin, vmin, mesh->vertex[i]);
2242 vect_max (vmax, vmax, mesh->vertex[i]);
2243 }
2244
2245 vect_add (mesh->center, vmin, vmax);
2246 vect_scale (mesh->center, mesh->center, 0.5);
2247
2248 vect_sub (mesh->lengths, vmax, vmin);
2249 }
2250
2251
2252 /* Return the sub-string of 'str' that is before 'target' */
2253 char *before (char *str, char *target)
2254 {
2255 static char result[256];
2256 char *search;
2257
2258 strncpy (result, str, 256);
2259 result[255] = '\0';
2260
2261 search = strstr (result, target);
2262
2263 if (search != NULL)
2264 *search = '\0';
2265
2266 return result;
2267 }
2268
2269
2270 /* Return the sub-string of 'str' that is after 'target' */
2271 char *after (char *str, char *target)
2272 {
2273 static char result[256];
2274 char *search;
2275
2276 search = strstr (str, target);
2277
2278 if (search == NULL)
2279 strncpy (result, "", 256);
2280 else
2281 strncpy (result, search + strlen(target), 256);
2282
2283 result[255] = '\0';
2284
2285 return result;
2286 }
2287
2288
2289 /* Return the sub-string of 'str' that is between 'target1' and 'target2' */
2290 char *between (char *str, char *target1, char *target2)
2291 {
2292 static char result[256];
2293
2294 strcpy (result, after (str, target1));
2295 strcpy (result, before (result, target2));
2296
2297 return result;
2298 }
2299
2300
2301 /* Works like the C strtok() function except that it can handle */
2302 /* tokens enclosed in double quotes */
2303 char *parse_string (char *str)
2304 {
2305 static char result[256];
2306 static char *p;
2307 char QUOTE = '\"';
2308 int index;
2309
2310 strcpy (result, "");
2311 index = 0;
2312
2313 if (str != NULL)
2314 p = str;
2315
2316 /* Find the start of the next token */
2317 while (isspace (*p))
2318 p++;
2319
2320 if (*p == QUOTE) {
2321 p++;
2322
2323 while (*p != '\0' && *p != QUOTE)
2324 result[index++] = *p++;
2325
2326 if (*p == QUOTE)
2327 p++;
2328 }
2329 else {
2330 while (*p != '\0' && !isspace(*p))
2331 result[index++] = *p++;
2332 }
2333
2334 result[index] = '\0';
2335
2336 return result;
2337 }
2338
2339
2340 /* Convert character 'c' to upper case */
2341 char upcase (char c)
2342 {
2343 if (c >= 'a' && c <= 'z')
2344 c = c - 'a' + 'A';
2345
2346 return c;
2347 }
2348
2349
2350 float colour_intens (Colour *colour)
2351 {
2352 return sqrt (colour->red * colour->red +
2353 colour->green * colour->green +
2354 colour->blue * colour->blue);
2355 }
2356
2357
2358 void parse_file()
2359 {
2360 Chunk chunk;
2361
2362 start_chunk(&chunk);
2363
2364 if (chunk.tag == 0x4D4D)
2365 parse_3ds (&chunk);
2366 else
2367 abortmsg ("Error: Input file is not .3DS format", 1);
2368
2369 end_chunk (&chunk);
2370 }
2371
2372
2373 void parse_3ds (Chunk *mainchunk)
2374 {
2375 Chunk chunk;
2376
2377 do {
2378 start_chunk (&chunk);
2379
2380 if (chunk.end <= mainchunk->end) {
2381 switch (chunk.tag) {
2382 case 0x3D3D: parse_mdata (&chunk);
2383 break;
2384 }
2385 }
2386
2387 end_chunk (&chunk);
2388 } while (chunk.end <= mainchunk->end);
2389 }
2390
2391
2392 void parse_mdata (Chunk *mainchunk)
2393 {
2394 Chunk chunk;
2395 Colour bgnd_colour;
2396
2397 do {
2398 start_chunk (&chunk);
2399
2400 if (chunk.end <= mainchunk->end) {
2401 switch (chunk.tag) {
2402 case 0x2100: parse_colour (&global_amb);
2403 break;
2404 case 0x1200: parse_colour (&bgnd_colour);
2405 break;
2406 case 0x1201: write_bgsolid (out, bgnd_colour);
2407 break;
2408 case 0x2200: parse_fog (&chunk);
2409 break;
2410 case 0x2210: parse_fog_bgnd();
2411 break;
2412 case 0x2201: write_fog (out, fog_colour, fog_distance);
2413 break;
2414 case 0xAFFF: parse_mat_entry (&chunk);
2415 break;
2416 case 0x4000: parse_named_object (&chunk);
2417 break;
2418 }
2419 }
2420
2421 end_chunk (&chunk);
2422 } while (chunk.end <= mainchunk->end);
2423 }
2424
2425
2426 void parse_fog (Chunk *mainchunk)
2427 {
2428 Chunk chunk;
2429
2430 (void)read_float();
2431 (void)read_float();
2432 fog_distance = read_float();
2433 (void)read_float();
2434
2435 parse_colour (&fog_colour);
2436
2437 do {
2438 start_chunk (&chunk);
2439
2440 if (chunk.end <= mainchunk->end) {
2441 switch (chunk.tag) {
2442 case 0x2210: parse_fog_bgnd();
2443 break;
2444 }
2445 }
2446
2447 end_chunk (&chunk);
2448 } while (chunk.end <= mainchunk->end);
2449 }
2450
2451
2452 void parse_fog_bgnd()
2453 {
2454
2455 }
2456
2457
2458 void parse_mat_entry (Chunk *mainchunk)
2459 {
2460 Chunk chunk;
2461 MatProp *mprop;
2462
2463 mprop = create_mprop();
2464
2465 do {
2466 start_chunk (&chunk);
2467
2468 if (chunk.end <= mainchunk->end) {
2469 switch (chunk.tag) {
2470 case 0xA000: strcpy (mprop->name, read_string());
2471 cleanup_name (mprop->name);
2472 break;
2473
2474 case 0xA010: parse_colour (&mprop->ambient);
2475 break;
2476
2477 case 0xA020: parse_colour (&mprop->diffuse);
2478 break;
2479
2480 case 0xA030: parse_colour (&mprop->specular);
2481 break;
2482
2483 case 0xA040: mprop->shininess = 100.0*parse_percentage();
2484 break;
2485
2486 case 0xA050: mprop->transparency = parse_percentage();
2487 break;
2488
2489 case 0xA080: mprop->self_illum = TRUE;
2490 break;
2491
2492 case 0xA081: mprop->two_side = TRUE;
2493 break;
2494
2495 case 0xA220: mprop->reflection = parse_percentage();
2496 (void)parse_mapname (&chunk);
2497 break;
2498
2499 case 0xA310: if (mprop->reflection == 0.0)
2500 mprop->reflection = 1.0;
2501 break;
2502
2503 case 0xA200: mprop->tex_strength = parse_percentage();
2504 strcpy (mprop->tex_map, parse_mapname (&chunk));
2505 break;
2506
2507 case 0xA230: mprop->bump_strength = parse_percentage();
2508 strcpy (mprop->bump_map, parse_mapname (&chunk));
2509 break;
2510 }
2511 }
2512
2513 end_chunk (&chunk);
2514 } while (chunk.end <= mainchunk->end);
2515
2516 LIST_INSERT (mprop_list, mprop);
2517 }
2518
2519
2520 char *parse_mapname (Chunk *mainchunk)
2521 {
2522 static char name[80] = "";
2523 Chunk chunk;
2524
2525 do {
2526 start_chunk (&chunk);
2527
2528 if (chunk.end <= mainchunk->end) {
2529 switch (chunk.tag) {
2530 case 0xA300: strcpy (name, read_string());
2531 break;
2532 }
2533 }
2534
2535 end_chunk (&chunk);
2536 } while (chunk.end <= mainchunk->end);
2537
2538 return name;
2539 }
2540
2541
2542 void parse_named_object (Chunk *mainchunk)
2543 {
2544 Chunk chunk;
2545
2546 strcpy (obj_name, read_string());
2547 cleanup_name (obj_name);
2548
2549 printf ("Working on: %s\n", obj_name);
2550
2551 mesh = NULL;
2552
2553 do {
2554 start_chunk (&chunk);
2555
2556 if (chunk.end <= mainchunk->end) {
2557 switch (chunk.tag) {
2558 case 0x4100: parse_n_tri_object (&chunk);
2559 break;
2560 case 0x4600: parse_n_direct_light (&chunk);
2561 break;
2562 case 0x4700: parse_n_camera();
2563 break;
2564 case 0x4010: if (mesh != NULL) mesh->hidden = TRUE;
2565 break;
2566 case 0x4012: if (mesh != NULL) mesh->shadow = FALSE;
2567 break;
2568 }
2569 }
2570
2571 end_chunk (&chunk);
2572 } while (chunk.end <= mainchunk->end);
2573
2574 if (mesh != NULL) {
2575 update_limits (mesh);
2576
2577 if (frame >= 0)
2578 LIST_INSERT (mesh_list, mesh);
2579 else {
2580 write_mesh (out, mesh);
2581
2582 free_mesh_data (mesh);
2583 free (mesh);
2584 }
2585 }
2586 }
2587
2588
2589 void parse_n_tri_object (Chunk *mainchunk)
2590 {
2591 Chunk chunk;
2592
2593 mesh = create_mesh (obj_name, 0, 0);
2594
2595 do {
2596 start_chunk (&chunk);
2597
2598 if (chunk.end <= mainchunk->end) {
2599 switch (chunk.tag) {
2600 case 0x4110: parse_point_array();
2601 break;
2602 case 0x4120: parse_face_array (&chunk);
2603 break;
2604 case 0x4160: parse_mesh_matrix();
2605 break;
2606 }
2607 }
2608
2609 end_chunk (&chunk);
2610 } while (chunk.end <= mainchunk->end);
2611 }
2612
2613
2614 void parse_point_array()
2615 {
2616 int i;
2617
2618 mesh->vertices = read_word();
2619 mesh->vertex = malloc (mesh->vertices * sizeof(*(mesh->vertex)));
2620 if (mesh->vertex == NULL)
2621 abortmsg ("Out of memory allocating mesh", 1);
2622
2623 for (i = 0; i < mesh->vertices; i++)
2624 read_point (mesh->vertex[i]);
2625 }
2626
2627
2628 void parse_face_array (Chunk *mainchunk)
2629 {
2630 Chunk chunk;
2631 int i;
2632
2633 mesh->faces = read_word();
2634 mesh->face = malloc (mesh->faces * sizeof(*(mesh->face)));
2635 if (mesh->face == NULL)
2636 abortmsg ("Out of memory allocating mesh", 1);
2637
2638 mesh->mtl = malloc (mesh->faces * sizeof(*(mesh->mtl)));
2639 if (mesh->mtl == NULL)
2640 abortmsg ("Out of memory allocating mesh", 1);
2641
2642 for (i = 0; i < mesh->faces; i++) {
2643 mesh->face[i].a = read_word();
2644 mesh->face[i].b = read_word();
2645 mesh->face[i].c = read_word();
2646 (void)read_word();
2647
2648 mesh->mtl[i] = NULL;
2649 }
2650
2651 do {
2652 start_chunk (&chunk);
2653
2654 if (chunk.end <= mainchunk->end) {
2655 switch (chunk.tag) {
2656 case 0x4130: parse_msh_mat_group();
2657 break;
2658 case 0x4150: parse_smooth_group();
2659 break;
2660 }
2661 }
2662
2663 end_chunk (&chunk);
2664 } while (chunk.end <= mainchunk->end);
2665
2666 for (i = 0; i < mesh->faces; i++) {
2667 if (mesh->mtl[i] == NULL)
2668 mesh->mtl[i] = update_materials ("Default", 0);
2669 }
2670 }
2671
2672
2673 void parse_msh_mat_group()
2674 {
2675 Material *new_mtl;
2676 char mtlname[80];
2677 int mtlcnt;
2678 int i, face;
2679
2680 strcpy (mtlname, read_string());
2681 cleanup_name (mtlname);
2682
2683 new_mtl = update_materials (mtlname, 0);
2684
2685 mtlcnt = read_word();
2686
2687 for (i = 0; i < mtlcnt; i++) {
2688 face = read_word();
2689 mesh->mtl[face] = new_mtl;
2690 }
2691 }
2692
2693
2694 void parse_smooth_group()
2695 {
2696
2697 }
2698
2699
2700 void parse_mesh_matrix()
2701 {
2702 int i, j;
2703
2704 if (mesh != NULL) {
2705 for (i = 0; i < 4; i++) {
2706 for (j = 0; j < 3; j++)
2707 mesh->matrix[i][j] = read_float();
2708 }
2709
2710 mat_inv (mesh->invmatrix, mesh->matrix);
2711 }
2712 }
2713
2714
2715 void parse_n_direct_light (Chunk *mainchunk)
2716 {
2717 Chunk chunk;
2718 Spotlight *s;
2719 OmniLight *o;
2720 int light_off = FALSE;
2721 int spot_flag = FALSE;
2722
2723 read_point (pos);
2724 parse_colour (&col);
2725
2726 do {
2727 start_chunk (&chunk);
2728
2729 if (chunk.end <= mainchunk->end) {
2730 switch (chunk.tag) {
2731 case 0x4620: light_off = TRUE;
2732 break;
2733 case 0x4610: parse_dl_spotlight();
2734 spot_flag = TRUE;
2735 break;
2736 }
2737 }
2738
2739 end_chunk (&chunk);
2740 } while (chunk.end <= mainchunk->end);
2741
2742 if (light_off)
2743 return;
2744
2745 if (!spot_flag) {
2746 if (frame >= 0) {
2747 o = LIST_FIND (omni_list, obj_name);
2748
2749 if (o != NULL) {
2750 pos[X] = o->pos[X];
2751 pos[Y] = o->pos[Y];
2752 pos[Z] = o->pos[Z];
2753 col = o->col;
2754 }
2755 }
2756
2757 write_light (out, obj_name, pos, col);
2758 }
2759 else {
2760 if (frame >= 0) {
2761 s = LIST_FIND (spot_list, obj_name);
2762
2763 if (s != NULL) {
2764 pos[X] = s->pos[X];
2765 pos[Y] = s->pos[Y];
2766 pos[Z] = s->pos[Z];
2767 target[X] = s->target[X];
2768 target[Y] = s->target[Y];
2769 target[Z] = s->target[Z];
2770 col = s->col;
2771 hotspot = s->hotspot;
2772 falloff = s->falloff;
2773 }
2774 }
2775
2776 if (falloff <= 0.0)
2777 falloff = 180.0;
2778
2779 if (hotspot <= 0.0)
2780 hotspot = 0.7*falloff;
2781
2782 write_spot (out, obj_name, pos, target, col, hotspot, falloff);
2783 }
2784 }
2785
2786
2787 void parse_dl_spotlight()
2788 {
2789 read_point (target);
2790
2791 hotspot = read_float();
2792 falloff = read_float();
2793 }
2794
2795
2796 void parse_n_camera()
2797 {
2798 float bank;
2799 float lens;
2800
2801 read_point (pos);
2802 read_point (target);
2803 bank = read_float();
2804 lens = read_float();
2805
2806 if (frame >= 0 && cam_list != NULL) {
2807 pos[X] = cam_list->pos[X];
2808 pos[Y] = cam_list->pos[Y];
2809 pos[Z] = cam_list->pos[Z];
2810 target[X] = cam_list->target[X];
2811 target[Y] = cam_list->target[Y];
2812 target[Z] = cam_list->target[Z];
2813 lens = cam_list->lens;
2814 bank = cam_list->bank;
2815 }
2816
2817 write_camera (out, obj_name, pos, target, lens, bank);
2818 }
2819
2820
2821 void parse_colour (Colour *colour)
2822 {
2823 Chunk chunk;
2824 Colour_24 colour_24;
2825
2826 start_chunk (&chunk);
2827
2828 switch (chunk.tag) {
2829 case 0x0010: parse_colour_f (colour);
2830 break;
2831
2832 case 0x0011: parse_colour_24 (&colour_24);
2833 colour->red = colour_24.red/255.0;
2834 colour->green = colour_24.green/255.0;
2835 colour->blue = colour_24.blue/255.0;
2836 break;
2837
2838 default: abortmsg ("Error parsing colour", 1);
2839 }
2840
2841 end_chunk (&chunk);
2842 }
2843
2844
2845 void parse_colour_f (Colour *colour)
2846 {
2847 colour->red = read_float();
2848 colour->green = read_float();
2849 colour->blue = read_float();
2850 }
2851
2852
2853 void parse_colour_24 (Colour_24 *colour)
2854 {
2855 colour->red = read_byte();
2856 colour->green = read_byte();
2857 colour->blue = read_byte();
2858 }
2859
2860
2861 float parse_percentage()
2862 {
2863 Chunk chunk;
2864 float percent = 0.0;
2865
2866 start_chunk (&chunk);
2867
2868 switch (chunk.tag) {
2869 case 0x0030: percent = parse_int_percentage()/100.0;
2870 break;
2871
2872 case 0x0031: percent = parse_float_percentage();
2873 break;
2874
2875 default: printf ("WARNING: Error parsing percentage");
2876 }
2877
2878 end_chunk (&chunk);
2879
2880 return percent;
2881 }
2882
2883
2884 short parse_int_percentage()
2885 {
2886 word percent = read_word();
2887
2888 return percent;
2889 }
2890
2891
2892 float parse_float_percentage()
2893 {
2894 float percent = read_float();
2895
2896 return percent;
2897 }
2898
2899
2900 void start_chunk (Chunk *chunk)
2901 {
2902 chunk->start = ftell(in);
2903 chunk->tag = read_word();
2904 chunk->length = read_dword();
2905 if (chunk->length < sizeof(word)+sizeof(dword))
2906 chunk->length = sizeof(word) + sizeof(dword);
2907 chunk->end = chunk->start + chunk->length;
2908 }
2909
2910
2911 void end_chunk (Chunk *chunk)
2912 {
2913 fseek (in, chunk->end, 0);
2914 }
2915
2916
2917 byte read_byte()
2918 {
2919 byte data;
2920
2921 data = fgetc (in);
2922
2923 return data;
2924 }
2925
2926
2927 word read_word()
2928 {
2929 word data;
2930
2931 data = fgetc (in);
2932 data |= fgetc (in) << 8;
2933
2934 return data;
2935 }
2936
2937
2938 dword read_dword()
2939 {
2940 dword data;
2941
2942 data = read_word();
2943 data |= read_word() << 16;
2944
2945 return data;
2946 }
2947
2948
2949 float read_float()
2950 {
2951 dword data;
2952
2953 data = read_dword();
2954
2955 return *(float *)&data;
2956 }
2957
2958
2959 void read_point (Vector v)
2960 {
2961 v[X] = read_float();
2962 v[Y] = read_float();
2963 v[Z] = read_float();
2964 }
2965
2966
2967 char *read_string()
2968 {
2969 static char string[80];
2970 int i;
2971
2972 for (i = 0; i < 80; i++) {
2973 string[i] = read_byte();
2974
2975 if (string[i] == '\0')
2976 break;
2977 }
2978
2979 return string;
2980 }
2981
2982
2983 float findfov (float lens)
2984 {
2985 static float lens_table[13] =
2986 { 15.0, 17.0, 24.0, 35.0, 50.0, 85.0, 100.0, 135.0, 200.0,
2987 500.0, 625.0, 800.0, 1000.0 };
2988 static float fov_table[13] =
2989 { 115.0, 102.0, 84.0, 63.0, 46.0, 28.0, 24.0, 18.0,
2990 12.0, 5.0, 4.0, 3.125, 2.5 };
2991
2992 float fov, f1, f2, l1, l2;
2993 int i;
2994
2995 if (lens < 15.0)
2996 lens = 15.0;
2997 else if (lens > 1000.0)
2998 lens = 1000.0;
2999
3000 for (i = 0; i < 13; i++)
3001 if (lens < lens_table[i])
3002 break;
3003
3004 if (i == 13)
3005 i = 12;
3006 else if (i == 0)
3007 i = 1;
3008
3009 f1 = fov_table[i-1];
3010 f2 = fov_table[i];
3011 l1 = lens_table[i-1];
3012 l2 = lens_table[i];
3013
3014 fov = f1 + (lens - l1) * (f2 - f1) / (l2 - l1);
3015
3016 return fov;
3017 }
3018
3019