]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/save_vrml.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / save_vrml.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@egdn.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## save_vrml(outname,[options],s1,...)    - Save vrml code
17 ## 
18 ## Makes a vrml2 file from strings of vrml code. A "background" node is
19 ## added.
20 ## 
21 ## Options :
22 ## "nobg"
23 ## "nolight"
24 ## 
25 ## Bugs :
26 ## - "outname" should not contain the substring ".wrl" anywhere else
27 ##   than as a suffix.
28 ## - "outname" should only contain the character ">" as ">>" at the
29 ##   beginning , to indicate append rather than overwriting the
30 ##   file.
31
32 function save_vrml(outname, varargin)
33
34 verbose = 0;
35 append = 0;
36
37 if ! ischar(outname) ,
38   error "save_vrml wants a string as first arg"
39 end
40
41 if strcmp(outname(1:2),">>"),
42   append = 1;
43 end
44 outname = strrep(outname,">","");
45
46 outname = strrep(outname,".wrl",'');                    # No suffix.
47
48 fname = sprintf("%s.wrl",outname);                      # Add suffix.
49 bg_col = [0.4 0.4 0.7];
50 ## bg_col = [1 1 1];
51 l_intensity = 0.3 ;
52 l_ambientIntensity = 0.5 ;
53 l_direction = [0.57735  -0.57735   0.57735] ;
54
55 bg_node = sprintf (["Background {\n",...
56                     "  skyColor    %8.3g %8.3g %8.3g\n",...
57                     "}\n"],\
58                    bg_col);
59 bg_node = "";
60
61 lightstr = sprintf (["PointLight {\n",\
62                      "  intensity         %8.3g\n",\
63                      "  ambientIntensity  %8.3g\n",\
64                      "  direction         %8.3g %8.3g %8.3g\n",\
65                      "}\n"],\
66                     l_intensity, l_ambientIntensity, l_direction);
67 lightstr = "";
68
69                                 # Read eventual options
70 ninit = nargin;
71
72 i = 1;
73 args = nargin; # nargin is now a function
74 while --args,
75
76   tmp = varargin{i++};
77   if     strcmp (tmp, "nobg"),
78     bg_node = "";
79   elseif strcmp (tmp, "nolight"),
80     lightstr = "";
81   else                          # Reached non-options
82     ## beginpre 2.1.39
83     # va_start ();
84     # n = ++args ;
85     # while n++ < ninit, va_arg (); end
86     ## args, ninit
87     ## endpre 2.1.39
88     i--;                        # pos 2.1.39
89     break;
90   end
91 end
92 bg_node = [bg_node, lightstr];
93 ## No path.
94 if findstr(outname,"/"),
95   outname = outname(max(findstr(outname,"/"))+1:size(outname,2)) ;
96 end
97
98 if append, fid = fopen(fname,"at");             # Saving.
99 else       fid = fopen(fname,"wt"); 
100 end ;
101
102 if fid == -1 , error(sprintf("save_vrml : unable to open %s",fname)); end
103  
104 ## Put header.
105 fprintf(fid,"#VRML V2.0 utf8 \n# %s , created by save_vrml.m on %s \n%s",
106         fname,datestr(now),bg_node);
107
108 while i <= length (varargin) ,
109
110   if verbose, printf ("save_vrml : %i'th string\n",i); end
111
112   fprintf (fid,"%s", varargin{i}) ;
113   i++ ;
114 end
115
116 fprintf(fid,"\n");
117 fclose(fid);
118 endfunction
119