ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/3ds2mgf.c
Revision: 1.6
Committed: Fri Feb 28 20:11:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 1.5: +3 -4 lines
Log Message:
Updates for 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 3DS2POV.C Copyright (c) 1996 Steve Anger and Jeff Bowermaster
6 MGF output added by Greg Ward
7
8 Reads a 3D Studio .3DS file and writes a POV-Ray, Vivid,
9 Polyray, MGF or raw scene file.
10
11 Version 2.0 Written Feb/96
12
13 Compiled with MSDOS GNU C++ 2.4.1 or generic ANSI-C compiler
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include "vect.h"
22 #include "rayopt.h"
23
24 #ifdef __TURBOC__
25 #include <alloc.h>
26 extern unsigned _stklen = 16384;
27 #endif
28
29
30 #define FALSE 0
31 #define TRUE 1
32
33 /* Internal bounding modes */
34 #define OFF 0
35 #define ON 1
36 #define AUTO 2
37
38 #define MAX_LIB 10
39 #define ASPECT 1.333
40
41 /* Output formats */
42 #define POV10 0
43 #define POV20 1
44 #define VIVID 2
45 #define POLYRAY 3
46 #define MGF 4
47 #define RAW 99
48
49 #define DEG(x) ((double)(180.0/M_PI)*(x))
50 #define RAD(x) ((double)(M_PI/180.0)*(x))
51
52 #ifndef M_PI
53 #define M_PI (3.14159265358979323846)
54 #endif
55
56 #ifndef MAXFLOAT
57 #define MAXFLOAT (1e37)
58 #endif
59 /* RGB chromaticity definitions for MGF */
60 #define CIE_x_r 0.640
61 #define CIE_y_r 0.330
62 #define CIE_x_g 0.290
63 #define CIE_y_g 0.600
64 #define CIE_x_b 0.150
65 #define CIE_y_b 0.060
66 /* computed luminances from above */
67 #define CIE_Y_r 0.265
68 #define CIE_Y_g 0.670
69 #define CIE_Y_b 0.065
70
71 /* A generic list type */
72 #define LIST_INSERT(root, node) list_insert ((List **)&root, (List *)node)
73 #define LIST_FIND(root, name) list_find ((List **)&root, name)
74 #define LIST_DELETE(root, node) list_delete ((List **)&root, (List *)node)
75 #define LIST_KILL(root) list_kill ((List **)&root)
76
77 #define LIST_FIELDS \
78 char name[80]; \
79 void *next;
80
81
82 typedef unsigned char byte;
83 typedef unsigned short word;
84 typedef unsigned long dword;
85
86 typedef struct {
87 LIST_FIELDS
88 } List;
89
90
91 typedef struct {
92 int a, b, c;
93 } Face;
94
95
96 typedef struct {
97 float red, green, blue;
98 } Colour;
99
100
101 /* Transformation command */
102 typedef struct {
103 LIST_FIELDS
104
105 Matrix matrix;
106 } Transform;
107
108
109 /* Morph command */
110 typedef struct {
111 LIST_FIELDS
112
113 int count; /* Number of objects in morph */
114 char names[4][80]; /* Name of n'th object in average */
115 float weight[4]; /* Weight applied to n'th object */
116
117 Matrix matrix;
118 } Morph;
119
120
121 /* Omni light command */
122 typedef struct {
123 LIST_FIELDS
124
125 Vector pos; /* Light position */
126 Colour col; /* Light colour */
127 } OmniLight;
128
129
130 /* Spotlight command */
131 typedef struct {
132 LIST_FIELDS
133
134 Vector pos; /* Spotlight position */
135 Vector target; /* Spotlight target location */
136 Colour col; /* Spotlight colour */
137 float hotspot; /* Hotspot angle (degrees) */
138 float falloff; /* Falloff angle (degrees) */
139 int shadow_flag; /* Shadow flag (not used) */
140 } Spotlight;
141
142
143 /* Camera command */
144 typedef struct {
145 LIST_FIELDS
146
147 Vector pos; /* Camera location */
148 Vector target; /* Camera target */
149 float bank; /* Banking angle (degrees) */
150 float lens; /* Camera lens size (mm) */
151 } Camera;
152
153
154 /* Material list */
155 typedef struct {
156 LIST_FIELDS
157
158 int external; /* Externally defined material? */
159 } Material;
160
161
162 /* Object summary */
163 typedef struct {
164 LIST_FIELDS
165
166 Vector center; /* Min value of object extents */
167 Vector lengths; /* Max value of object extents */
168 } Summary;
169
170
171 /* Material property */
172 typedef struct {
173 LIST_FIELDS
174
175 Colour ambient;
176 Colour diffuse;
177 Colour specular;
178 float shininess;
179 float transparency;
180 float reflection;
181 int self_illum;
182 int two_side;
183 char tex_map[40];
184 float tex_strength;
185 char bump_map[40];
186 float bump_strength;
187 } MatProp;
188
189
190 /* Default material property */
191 MatProp DefaultMaterial = { "Default", NULL, {1.0, 1.0, 1.0}, {1.0, 1.0, 1.0},
192 {1.0, 1.0, 1.0}, 70.0, 0.0, 0.0, FALSE, FALSE };
193
194 /* A mesh object */
195 typedef struct {
196 LIST_FIELDS
197
198 int vertices; /* Number of vertices */
199 Vector *vertex; /* List of object vertices */
200
201 int faces; /* Number of faces */
202 Face *face; /* List of object faces */
203 Material **mtl; /* Materials for each face */
204
205 Matrix matrix; /* Local mesh matrix */
206 Matrix invmatrix;
207 Vector center; /* Center of object */
208 Vector lengths; /* Dimensions of object */
209
210 int hidden; /* Hidden flag */
211 int shadow; /* Shadow flag */
212 } Mesh;
213
214
215 typedef struct {
216 dword start;
217 dword end;
218 dword length;
219 word tag;
220 } Chunk;
221
222
223 typedef struct {
224 byte red;
225 byte green;
226 byte blue;
227 } Colour_24;
228
229
230 Colour Black = {0.0, 0.0, 0.0};
231
232 OmniLight *omni_list = NULL;
233 Spotlight *spot_list = NULL;
234 Camera *cam_list = NULL;
235 Mesh *mesh_list = NULL;
236 Transform *trans_list = NULL;
237 Morph *morph_list = NULL;
238 Material *mtl_list = NULL;
239 List *excl_list = NULL;
240 List *box_list = NULL;
241 MatProp *mprop_list = NULL;
242 Summary *summary = NULL;
243
244
245 FILE *in;
246 FILE *out;
247 char inname[80];
248 char outname[80];
249 char vuename[80];
250 char obj_name[80] = "";
251 Colour fog_colour = {0.0, 0.0, 0.0};
252 Colour col = {0.0, 0.0, 0.0};
253 Colour global_amb = {0.1, 0.1, 0.1};
254 Vector pos = {0.0, 0.0, 0.0};
255 Vector target = {0.0, 0.0, 0.0};
256 float fog_distance = 0.0;
257 float hotspot = -1;
258 float falloff = -1;
259 Mesh *mesh = NULL;
260 int frame = -1;
261 char libname[MAX_LIB][80];
262 float smooth = 60.0;
263 int bound = 0;
264 int verbose = 0;
265 int format = POV20;
266 int internal_bounding = AUTO;
267 int box_all = FALSE;
268 int cameras = 0;
269 int libs = 0;
270 float vue_version = 1.0;
271 Matrix *ani_matrix = NULL;
272 int no_opt = FALSE;
273 FILE *meshf = NULL;
274
275
276 void process_args (int argc, char *argv[]);
277 void parse_option (char *option);
278 void list_insert (List **root, List *new_node);
279 void *list_find (List **root, char *name);
280 void list_delete (List **root, List *node);
281 void list_kill (List **root);
282 Material *update_materials (char *new_material, int ext);
283 MatProp *create_mprop (void);
284 void read_library (char *fname);
285 void write_intro (FILE *f);
286 void write_summary (FILE *f);
287 void write_bgsolid (FILE *f, Colour col);
288 void write_light (FILE *f, char *name, Vector pos, Colour col);
289 void write_spot (FILE *f, char *name, Vector pos, Vector target, Colour col,
290 float hotspot, float falloff);
291 void write_fog (FILE *f, Colour col, float dist);
292 void write_camera (FILE *f, char *name, Vector pos, Vector target, float lens,
293 float bank);
294 void write_material (FILE *f, char *mat);
295 void write_pov10_material (FILE *f, MatProp *m);
296 void write_pov20_material (FILE *f, MatProp *m);
297 void write_vivid_material (FILE *f, MatProp *m);
298 void write_polyray_material (FILE *f, MatProp *m);
299 void write_mgf_material (FILE *f, MatProp *m);
300 void write_mesh (FILE *f, Mesh *mesh);
301 Transform *parse_transform (char *string);
302 Morph *parse_morph (char *string);
303 OmniLight *parse_omnilight (char *string);
304 Spotlight *parse_spotlight (char *string);
305 Camera *parse_camera (char *string);
306 void read_frame (char *filename, int frame_no);
307 void find_frame (FILE *f, int frame_no);
308 void save_animation (void);
309 Mesh *create_mesh (char *name, int vertices, int faces);
310 Mesh *copy_mesh (Mesh *mesh);
311 void free_mesh_data (Mesh *mesh);
312 void update_limits (Mesh *mesh);
313 char *before (char *str, char *target);
314 char *after (char *str, char *target);
315 char *between (char *str, char *target1, char *target2);
316 char *parse_string (char *str);
317 char upcase (char c);
318 float colour_intens (Colour *colour);
319 void parse_file (void);
320 void parse_3ds (Chunk *mainchunk);
321 void parse_mdata (Chunk *mainchunk);
322 void parse_fog (Chunk *mainchunk);
323 void parse_fog_bgnd (void);
324 void parse_mat_entry (Chunk *mainchunk);
325 char *parse_mapname (Chunk *mainchunk);
326 void parse_named_object (Chunk *mainchunk);
327 void parse_n_tri_object (Chunk *mainchunk);
328 void parse_point_array (void);
329 void parse_face_array (Chunk *mainchunk);
330 void parse_msh_mat_group (void);
331 void parse_smooth_group (void);
332 void parse_mesh_matrix (void);
333 void parse_n_direct_light (Chunk *mainchunk);
334 void parse_dl_spotlight (void);
335 void parse_n_camera (void);
336 void parse_colour (Colour *colour);
337 void parse_colour_f (Colour *colour);
338 void parse_colour_24 (Colour_24 *colour);
339 float parse_percentage (void);
340 short parse_int_percentage (void);
341 float parse_float_percentage (void);
342 void start_chunk (Chunk *chunk);
343 void end_chunk (Chunk *chunk);
344 byte read_byte (void);
345 word read_word (void);
346 dword read_dword (void);
347 float read_float (void);
348 void read_point (Vector v);
349 char *read_string (void);
350 float findfov (float lens);
351 int read_mgfmatname (char *s, int n, FILE *f);
352
353 char *progname;
354
355
356 int main (int argc, char *argv[])
357 {
358 char meshfname[128];
359 Material *m;
360 int i;
361
362 process_args (argc, argv);
363
364 if (!no_opt) {
365 opt_set_format (format);
366 opt_set_dec (4);
367 opt_set_bound (bound);
368 opt_set_smooth (smooth);
369 opt_set_quiet (!verbose);
370 opt_set_fname (outname, "");
371 } else if (format == MGF) {
372 strcpy(meshfname, outname);
373 add_ext(meshfname, "inc", 1);
374 if (!strcmp(meshfname, outname)) {
375 printf ("Output and mesh file names are identical!\n");
376 exit (1);
377 }
378 if ((meshf = fopen (meshfname, "w")) == NULL) {
379 printf ("Cannot open mesh output file %s!\n", meshfname);
380 exit (1);
381 }
382 }
383
384 if ((in = fopen (inname, "rb")) == NULL) {
385 printf ("Cannot open input file %s!\n", inname);
386 exit (1);
387 }
388
389 if ((out = fopen (outname, "w")) == NULL) {
390 printf ("Cannot open output file %s!\n", outname);
391 exit (1);
392 }
393
394 /* Load the names of pre-defined materials */
395 for (i = 0; i < MAX_LIB; i++) {
396 if (strlen(libname[i]) > 0)
397 read_library (libname[i]);
398 }
399
400 /* Load the instructions for the current frame */
401 if (strlen(vuename) > 0)
402 read_frame (vuename, frame);
403
404 printf("Output to: %s\n", outname);
405
406 if (frame >= 0)
407 printf ("Generating frame #%d\n", frame);
408
409 printf("\nPlease wait; Processing...\n");
410
411 write_intro(out);
412
413 parse_file();
414
415 fclose(in);
416
417 for (m = mtl_list; m != NULL; m = m->next) {
418 if (!m->external)
419 write_material (out, m->name);
420 }
421
422 if (frame >= 0)
423 save_animation();
424
425 if (!no_opt) {
426 write_summary (out);
427 fflush (out);
428
429 opt_finish();
430 } else if (meshf != NULL) {
431 fclose(meshf);
432 fprintf (out, "i %s\n", meshfname);
433 }
434
435 fclose (out);
436
437 LIST_KILL (omni_list);
438 LIST_KILL (spot_list);
439 LIST_KILL (cam_list);
440 LIST_KILL (mesh_list);
441 LIST_KILL (trans_list);
442 LIST_KILL (morph_list);
443 LIST_KILL (mtl_list);
444 LIST_KILL (excl_list);
445 LIST_KILL (box_list);
446 LIST_KILL (mprop_list);
447 LIST_KILL (summary);
448
449 return 0;
450 }
451
452
453 /* Handle the command line args */
454 void process_args (int argc, char *argv[])
455 {
456 int i;
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\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 = POV20; /* 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 100000.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 100000.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 if (feof(in)) {
2380 fprintf(stderr, "%s: unexpected EOF\n", progname);
2381 break;
2382 }
2383 if (chunk.end <= mainchunk->end) {
2384 switch (chunk.tag) {
2385 case 0x3D3D: parse_mdata (&chunk);
2386 break;
2387 }
2388 }
2389
2390 end_chunk (&chunk);
2391 } while (chunk.end <= mainchunk->end);
2392 }
2393
2394
2395 void parse_mdata (Chunk *mainchunk)
2396 {
2397 Chunk chunk;
2398 Colour bgnd_colour;
2399
2400 do {
2401 start_chunk (&chunk);
2402
2403 if (chunk.end <= mainchunk->end) {
2404 switch (chunk.tag) {
2405 case 0x2100: parse_colour (&global_amb);
2406 break;
2407 case 0x1200: parse_colour (&bgnd_colour);
2408 break;
2409 case 0x1201: write_bgsolid (out, bgnd_colour);
2410 break;
2411 case 0x2200: parse_fog (&chunk);
2412 break;
2413 case 0x2210: parse_fog_bgnd();
2414 break;
2415 case 0x2201: write_fog (out, fog_colour, fog_distance);
2416 break;
2417 case 0xAFFF: parse_mat_entry (&chunk);
2418 break;
2419 case 0x4000: parse_named_object (&chunk);
2420 break;
2421 }
2422 }
2423
2424 end_chunk (&chunk);
2425 } while (chunk.end <= mainchunk->end);
2426 }
2427
2428
2429 void parse_fog (Chunk *mainchunk)
2430 {
2431 Chunk chunk;
2432
2433 (void)read_float();
2434 (void)read_float();
2435 fog_distance = read_float();
2436 (void)read_float();
2437
2438 parse_colour (&fog_colour);
2439
2440 do {
2441 start_chunk (&chunk);
2442
2443 if (chunk.end <= mainchunk->end) {
2444 switch (chunk.tag) {
2445 case 0x2210: parse_fog_bgnd();
2446 break;
2447 }
2448 }
2449
2450 end_chunk (&chunk);
2451 } while (chunk.end <= mainchunk->end);
2452 }
2453
2454
2455 void parse_fog_bgnd()
2456 {
2457
2458 }
2459
2460
2461 void parse_mat_entry (Chunk *mainchunk)
2462 {
2463 Chunk chunk;
2464 MatProp *mprop;
2465
2466 mprop = create_mprop();
2467
2468 do {
2469 start_chunk (&chunk);
2470
2471 if (chunk.end <= mainchunk->end) {
2472 switch (chunk.tag) {
2473 case 0xA000: strcpy (mprop->name, read_string());
2474 cleanup_name (mprop->name);
2475 break;
2476
2477 case 0xA010: parse_colour (&mprop->ambient);
2478 break;
2479
2480 case 0xA020: parse_colour (&mprop->diffuse);
2481 break;
2482
2483 case 0xA030: parse_colour (&mprop->specular);
2484 break;
2485
2486 case 0xA040: mprop->shininess = 100.0*parse_percentage();
2487 break;
2488
2489 case 0xA050: mprop->transparency = parse_percentage();
2490 break;
2491
2492 case 0xA080: mprop->self_illum = TRUE;
2493 break;
2494
2495 case 0xA081: mprop->two_side = TRUE;
2496 break;
2497
2498 case 0xA220: mprop->reflection = parse_percentage();
2499 (void)parse_mapname (&chunk);
2500 break;
2501
2502 case 0xA310: if (mprop->reflection == 0.0)
2503 mprop->reflection = 1.0;
2504 break;
2505
2506 case 0xA200: mprop->tex_strength = parse_percentage();
2507 strcpy (mprop->tex_map, parse_mapname (&chunk));
2508 break;
2509
2510 case 0xA230: mprop->bump_strength = parse_percentage();
2511 strcpy (mprop->bump_map, parse_mapname (&chunk));
2512 break;
2513 }
2514 }
2515
2516 end_chunk (&chunk);
2517 } while (chunk.end <= mainchunk->end);
2518
2519 LIST_INSERT (mprop_list, mprop);
2520 }
2521
2522
2523 char *parse_mapname (Chunk *mainchunk)
2524 {
2525 static char name[80] = "";
2526 Chunk chunk;
2527
2528 do {
2529 start_chunk (&chunk);
2530
2531 if (chunk.end <= mainchunk->end) {
2532 switch (chunk.tag) {
2533 case 0xA300: strcpy (name, read_string());
2534 break;
2535 }
2536 }
2537
2538 end_chunk (&chunk);
2539 } while (chunk.end <= mainchunk->end);
2540
2541 return name;
2542 }
2543
2544
2545 void parse_named_object (Chunk *mainchunk)
2546 {
2547 Chunk chunk;
2548
2549 strcpy (obj_name, read_string());
2550 cleanup_name (obj_name);
2551
2552 printf ("Working on: %s\n", obj_name);
2553
2554 mesh = NULL;
2555
2556 do {
2557 start_chunk (&chunk);
2558
2559 if (chunk.end <= mainchunk->end) {
2560 switch (chunk.tag) {
2561 case 0x4100: parse_n_tri_object (&chunk);
2562 break;
2563 case 0x4600: parse_n_direct_light (&chunk);
2564 break;
2565 case 0x4700: parse_n_camera();
2566 break;
2567 case 0x4010: if (mesh != NULL) mesh->hidden = TRUE;
2568 break;
2569 case 0x4012: if (mesh != NULL) mesh->shadow = FALSE;
2570 break;
2571 }
2572 }
2573
2574 end_chunk (&chunk);
2575 } while (chunk.end <= mainchunk->end);
2576
2577 if (mesh != NULL) {
2578 update_limits (mesh);
2579
2580 if (frame >= 0)
2581 LIST_INSERT (mesh_list, mesh);
2582 else {
2583 write_mesh (out, mesh);
2584
2585 free_mesh_data (mesh);
2586 free (mesh);
2587 }
2588 }
2589 }
2590
2591
2592 void parse_n_tri_object (Chunk *mainchunk)
2593 {
2594 Chunk chunk;
2595
2596 mesh = create_mesh (obj_name, 0, 0);
2597
2598 do {
2599 start_chunk (&chunk);
2600
2601 if (chunk.end <= mainchunk->end) {
2602 switch (chunk.tag) {
2603 case 0x4110: parse_point_array();
2604 break;
2605 case 0x4120: parse_face_array (&chunk);
2606 break;
2607 case 0x4160: parse_mesh_matrix();
2608 break;
2609 }
2610 }
2611
2612 end_chunk (&chunk);
2613 } while (chunk.end <= mainchunk->end);
2614 }
2615
2616
2617 void parse_point_array()
2618 {
2619 int i;
2620
2621 mesh->vertices = read_word();
2622 mesh->vertex = malloc (mesh->vertices * sizeof(*(mesh->vertex)));
2623 if (mesh->vertex == NULL)
2624 abortmsg ("Out of memory allocating mesh", 1);
2625
2626 for (i = 0; i < mesh->vertices; i++)
2627 read_point (mesh->vertex[i]);
2628 }
2629
2630
2631 void parse_face_array (Chunk *mainchunk)
2632 {
2633 Chunk chunk;
2634 int i;
2635
2636 mesh->faces = read_word();
2637 mesh->face = malloc (mesh->faces * sizeof(*(mesh->face)));
2638 if (mesh->face == NULL)
2639 abortmsg ("Out of memory allocating mesh", 1);
2640
2641 mesh->mtl = malloc (mesh->faces * sizeof(*(mesh->mtl)));
2642 if (mesh->mtl == NULL)
2643 abortmsg ("Out of memory allocating mesh", 1);
2644
2645 for (i = 0; i < mesh->faces; i++) {
2646 mesh->face[i].a = read_word();
2647 mesh->face[i].b = read_word();
2648 mesh->face[i].c = read_word();
2649 (void)read_word();
2650
2651 mesh->mtl[i] = NULL;
2652 }
2653
2654 do {
2655 start_chunk (&chunk);
2656
2657 if (chunk.end <= mainchunk->end) {
2658 switch (chunk.tag) {
2659 case 0x4130: parse_msh_mat_group();
2660 break;
2661 case 0x4150: parse_smooth_group();
2662 break;
2663 }
2664 }
2665
2666 end_chunk (&chunk);
2667 } while (chunk.end <= mainchunk->end);
2668
2669 for (i = 0; i < mesh->faces; i++) {
2670 if (mesh->mtl[i] == NULL)
2671 mesh->mtl[i] = update_materials ("Default", 0);
2672 }
2673 }
2674
2675
2676 void parse_msh_mat_group()
2677 {
2678 Material *new_mtl;
2679 char mtlname[80];
2680 int mtlcnt;
2681 int i, face;
2682
2683 strcpy (mtlname, read_string());
2684 cleanup_name (mtlname);
2685
2686 new_mtl = update_materials (mtlname, 0);
2687
2688 mtlcnt = read_word();
2689
2690 for (i = 0; i < mtlcnt; i++) {
2691 face = read_word();
2692 mesh->mtl[face] = new_mtl;
2693 }
2694 }
2695
2696
2697 void parse_smooth_group()
2698 {
2699
2700 }
2701
2702
2703 void parse_mesh_matrix()
2704 {
2705 int i, j;
2706
2707 if (mesh != NULL) {
2708 for (i = 0; i < 4; i++) {
2709 for (j = 0; j < 3; j++)
2710 mesh->matrix[i][j] = read_float();
2711 }
2712
2713 mat_inv (mesh->invmatrix, mesh->matrix);
2714 }
2715 }
2716
2717
2718 void parse_n_direct_light (Chunk *mainchunk)
2719 {
2720 Chunk chunk;
2721 Spotlight *s;
2722 OmniLight *o;
2723 int light_off = FALSE;
2724 int spot_flag = FALSE;
2725
2726 read_point (pos);
2727 parse_colour (&col);
2728
2729 do {
2730 start_chunk (&chunk);
2731
2732 if (chunk.end <= mainchunk->end) {
2733 switch (chunk.tag) {
2734 case 0x4620: light_off = TRUE;
2735 break;
2736 case 0x4610: parse_dl_spotlight();
2737 spot_flag = TRUE;
2738 break;
2739 }
2740 }
2741
2742 end_chunk (&chunk);
2743 } while (chunk.end <= mainchunk->end);
2744
2745 if (light_off)
2746 return;
2747
2748 if (!spot_flag) {
2749 if (frame >= 0) {
2750 o = LIST_FIND (omni_list, obj_name);
2751
2752 if (o != NULL) {
2753 pos[X] = o->pos[X];
2754 pos[Y] = o->pos[Y];
2755 pos[Z] = o->pos[Z];
2756 col = o->col;
2757 }
2758 }
2759
2760 write_light (out, obj_name, pos, col);
2761 }
2762 else {
2763 if (frame >= 0) {
2764 s = LIST_FIND (spot_list, obj_name);
2765
2766 if (s != NULL) {
2767 pos[X] = s->pos[X];
2768 pos[Y] = s->pos[Y];
2769 pos[Z] = s->pos[Z];
2770 target[X] = s->target[X];
2771 target[Y] = s->target[Y];
2772 target[Z] = s->target[Z];
2773 col = s->col;
2774 hotspot = s->hotspot;
2775 falloff = s->falloff;
2776 }
2777 }
2778
2779 if (falloff <= 0.0)
2780 falloff = 180.0;
2781
2782 if (hotspot <= 0.0)
2783 hotspot = 0.7*falloff;
2784
2785 write_spot (out, obj_name, pos, target, col, hotspot, falloff);
2786 }
2787 }
2788
2789
2790 void parse_dl_spotlight()
2791 {
2792 read_point (target);
2793
2794 hotspot = read_float();
2795 falloff = read_float();
2796 }
2797
2798
2799 void parse_n_camera()
2800 {
2801 float bank;
2802 float lens;
2803
2804 read_point (pos);
2805 read_point (target);
2806 bank = read_float();
2807 lens = read_float();
2808
2809 if (frame >= 0 && cam_list != NULL) {
2810 pos[X] = cam_list->pos[X];
2811 pos[Y] = cam_list->pos[Y];
2812 pos[Z] = cam_list->pos[Z];
2813 target[X] = cam_list->target[X];
2814 target[Y] = cam_list->target[Y];
2815 target[Z] = cam_list->target[Z];
2816 lens = cam_list->lens;
2817 bank = cam_list->bank;
2818 }
2819
2820 write_camera (out, obj_name, pos, target, lens, bank);
2821 }
2822
2823
2824 void parse_colour (Colour *colour)
2825 {
2826 Chunk chunk;
2827 Colour_24 colour_24;
2828
2829 start_chunk (&chunk);
2830
2831 switch (chunk.tag) {
2832 case 0x0010: parse_colour_f (colour);
2833 break;
2834
2835 case 0x0011: parse_colour_24 (&colour_24);
2836 colour->red = colour_24.red/255.0;
2837 colour->green = colour_24.green/255.0;
2838 colour->blue = colour_24.blue/255.0;
2839 break;
2840
2841 default: abortmsg ("Error parsing colour", 1);
2842 }
2843
2844 end_chunk (&chunk);
2845 }
2846
2847
2848 void parse_colour_f (Colour *colour)
2849 {
2850 colour->red = read_float();
2851 colour->green = read_float();
2852 colour->blue = read_float();
2853 }
2854
2855
2856 void parse_colour_24 (Colour_24 *colour)
2857 {
2858 colour->red = read_byte();
2859 colour->green = read_byte();
2860 colour->blue = read_byte();
2861 }
2862
2863
2864 float parse_percentage()
2865 {
2866 Chunk chunk;
2867 float percent = 0.0;
2868
2869 start_chunk (&chunk);
2870
2871 switch (chunk.tag) {
2872 case 0x0030: percent = parse_int_percentage()/100.0;
2873 break;
2874
2875 case 0x0031: percent = parse_float_percentage();
2876 break;
2877
2878 default: printf ("WARNING: Error parsing percentage");
2879 }
2880
2881 end_chunk (&chunk);
2882
2883 return percent;
2884 }
2885
2886
2887 short parse_int_percentage()
2888 {
2889 word percent = read_word();
2890
2891 return percent;
2892 }
2893
2894
2895 float parse_float_percentage()
2896 {
2897 float percent = read_float();
2898
2899 return percent;
2900 }
2901
2902
2903 void start_chunk (Chunk *chunk)
2904 {
2905 chunk->start = ftell(in);
2906 chunk->tag = read_word();
2907 chunk->length = read_dword();
2908 if (chunk->length < sizeof(word)+sizeof(dword))
2909 chunk->length = sizeof(word) + sizeof(dword);
2910 chunk->end = chunk->start + chunk->length;
2911 }
2912
2913
2914 void end_chunk (Chunk *chunk)
2915 {
2916 fseek (in, chunk->end, 0);
2917 }
2918
2919
2920 byte read_byte()
2921 {
2922 byte data;
2923
2924 data = fgetc (in);
2925
2926 return data;
2927 }
2928
2929
2930 word read_word()
2931 {
2932 word data;
2933
2934 data = fgetc (in);
2935 data |= fgetc (in) << 8;
2936
2937 return data;
2938 }
2939
2940
2941 dword read_dword()
2942 {
2943 dword data;
2944
2945 data = read_word();
2946 data |= read_word() << 16;
2947
2948 return data;
2949 }
2950
2951
2952 float read_float()
2953 {
2954 dword data;
2955
2956 data = read_dword();
2957
2958 return *(float *)&data;
2959 }
2960
2961
2962 void read_point (Vector v)
2963 {
2964 v[X] = read_float();
2965 v[Y] = read_float();
2966 v[Z] = read_float();
2967 }
2968
2969
2970 char *read_string()
2971 {
2972 static char string[80];
2973 int i;
2974
2975 for (i = 0; i < 80; i++) {
2976 string[i] = read_byte();
2977
2978 if (string[i] == '\0')
2979 break;
2980 }
2981
2982 return string;
2983 }
2984
2985
2986 float findfov (float lens)
2987 {
2988 static float lens_table[13] =
2989 { 15.0, 17.0, 24.0, 35.0, 50.0, 85.0, 100.0, 135.0, 200.0,
2990 500.0, 625.0, 800.0, 1000.0 };
2991 static float fov_table[13] =
2992 { 115.0, 102.0, 84.0, 63.0, 46.0, 28.0, 24.0, 18.0,
2993 12.0, 5.0, 4.0, 3.125, 2.5 };
2994
2995 float fov, f1, f2, l1, l2;
2996 int i;
2997
2998 if (lens < 15.0)
2999 lens = 15.0;
3000 else if (lens > 1000.0)
3001 lens = 1000.0;
3002
3003 for (i = 0; i < 13; i++)
3004 if (lens < lens_table[i])
3005 break;
3006
3007 if (i == 13)
3008 i = 12;
3009 else if (i == 0)
3010 i = 1;
3011
3012 f1 = fov_table[i-1];
3013 f2 = fov_table[i];
3014 l1 = lens_table[i-1];
3015 l2 = lens_table[i];
3016
3017 fov = f1 + (lens - l1) * (f2 - f1) / (l2 - l1);
3018
3019 return fov;
3020 }
3021
3022