Community/Tasks/osgNature: render_pov_to_textures.sh.v2.txt.sh

Line 
1 #!/bin/sh
2 #render a arbaro generated pov file into textures
3 #author: Fred Marmond <fmarmond@users.sourceforge.net>
4 #2005-01-09
5 #It uses ARBARO (http://arbaro.sourceforge.net/), a wonderfull tree generetor in GPL.
6 #My final goal: automagically generate tree mesh for OSG with LOD
7 #
8 #This script uses ARBARO and POVRAY to render trees to textures (to be used in a 2 quads tree or billboard one)
9
10 #
11 #param: $1: the .pov filename
12 #output: will generate mesh_object_name (token from the .pov file) _face.tga, .side.tga
13 #       (for exemple, if mesh name is 'lombardy_poplar_13', output file will be 'lombardy_poplar_13.face.tga' and 'lombardy_poplar_13.side.tga')
14 #       output files are already RGBA, so, you may use them directly as textures! :-))
15
16 #todo:
17 #       1: make better parameters (texture size, light, leaves, ...)
18 #       2: add texture for leaves and branches (actually, I only put basic colors for leaves and branches)
19
20 #requirements:
21 #- povray must be compiled with TGA support
22 #- povray must have writing permissions to /tmp/
23
24 textureX=512
25 textureY=512
26
27 leaves=true
28 depthmap=true
29
30 output_dir="textures/"
31
32 #some povray installation gives a bad name for povray ("povray35" for exemple)
33 #change it here if it is your case
34 #try to see if we can use megapov => depthmap
35 megapov="megapov"
36 defaultpov="povray"
37 if which $megapov ;
38 then 
39         povray="$megapov"
40 else
41         povray="$defaultpov"
42         depthmap="\'$megapov\' not found, we'll use '$defaultpov' instead. We can't compute depthmap on default povray. Please use MegaPov if you want depthmaps (can be found at http://megapov.inetart.net/megapov-1.1.tar.bz2, or try mirror http://mirror.isurf.ca/download.sourcemage.org/mirror/megapov-1.1.tar.bz2)."
43 fi
44
45
46 povname=$1
47 filename=`echo $povname | sed 's/\.pov$//'`
48
49 #search the base object name
50 #       search "union {" line and get the line just after
51 #       this line is "object { foo_bar_NUMBER_stems"
52 #               remove the "object { " and "stems"
53 #       so, objname="foo_bar_NUMBER"
54
55 objname=`grep "union {" $povname -A 1 | tail -n 1 | sed 's/^.*{ \(.*[0-9]*\)_stems/\1/' `
56
57 echo "objname=[$objname]"
58
59 tmppov="/tmp/$objname.$$.pov"
60 tmptex="/tmp/$objname.$$.tga"
61 tmpdepth="/tmp/$objname.$$.depth.tga"
62
63
64 result="/tmp/result.$$.txt"
65 (
66         echo "ARBARO to POVRAY: automatic texture generation, by Fred Marmond, fmarmond@users.sf.net"
67         echo "Input file :      $povname"
68         echo "Object name:      $objname"
69         echo
70         echo "Options :"
71         echo "textureX   :      $textureX"
72         echo "textureY   :      $textureY"
73         echo "leaves     :      $leaves"
74         echo "depthmap   :      $depthmap"
75         echo
76         echo "Output files :"
77 )>$result
78
79 base_pov="      #include \"${filename}.inc\"
80                 //we set 'full blue' background, in order to make the transparent texture easily later
81                 background {rgb <0.0,0.0,0.0,0.0>}
82
83                 //comment it if you don't want lighting
84                 light_source { <5000,5000,-3000>, rgb 1.2 }
85                 light_source { <-5000,2000,3000>, rgb 0.5 shadowless }
86
87                 
88                 //#declare HEIGHT = ${objname}_height * 1.02;
89                 #declare HEIGHT = ${objname}_height * 1.3;
90                 #declare WIDTH = HEIGHT*${textureX}/${textureY};
91         "
92
93 camera_face="
94                 camera { orthographic location <0, HEIGHT*0.5, -40 >
95                         right <WIDTH, 0, 0> up <0, HEIGHT, 0.0>
96                         look_at <0, HEIGHT*0.5, 0> }   
97         "
98 camera_side="
99                 camera { orthographic location <40, HEIGHT*0.5, 0>
100                         right <WIDTH, 0, 0> up <0, HEIGHT, 0.0>
101                         look_at <0, HEIGHT*0.5, 0> }
102          "
103 camera_top="
104                 camera { orthographic location <0, HEIGHT, 0>
105                         right <WIDTH, 0, 0> up <0, 0, WIDTH>
106                         look_at <0, HEIGHT*0.5, 0> }
107          "
108
109 obj_pov="       
110                 union {
111                          object { ${objname}_stems
112                                 pigment {color rgb <1.0,1.0,0.0>} }
113         "
114
115      
116 if [ "$leaves" = "true" ];
117 then    
118         obj_pov="$obj_pov
119                          object { ${objname}_leaves
120                                 texture { pigment {color rgb <0.0,1.0,0.0>}
121                                           finish { ambient 0.15 diffuse 0.8 }}}
122         "
123 fi      
124 obj_pov="$obj_pov }"
125
126
127 if [ "$depthmap" = "true" ];
128 then
129 depth_map="
130         #version unofficial MegaPov 1.1;
131         #include \"pprocess.inc\"
132         #declare DepthMin=35;
133         #declare DepthMax=45;
134         global_settings {
135           assumed_gamma 1.0
136             post_process{
137                 PP_Depth(DepthMin,DepthMax)
138                 save_file \"${tmpdepth}\"
139                 }
140         }
141         "
142 fi
143
144
145
146 function render()
147 #param1: $camera_view
148 #param2: view name
149 #exemple: render($camera_face,face)
150 {
151         camera_view="$1"
152         view="$2"
153         output_rgba="${output_dir}${objname}.${view}.tga"
154         output_depth="${output_dir}${objname}.${view}.depth.tga"
155 ( echo "$depth_map $base_pov $camera_view $obj_pov" ) > $tmppov
156 $povray -I${tmppov} -O${tmptex} +UA +W${textureX} +H${textureY} -D +Ft && ( mv ${tmptex} ${output_rgba} && echo "out RGBA file: $output_rgba" >> $result ;if [ -f ${tmpdepth} ]; then mv ${tmpdepth} ${output_depth}; echo "out DEPTH file: $output_depth">> $result;  fi ) || ( echo " FAILED..."; exit )
157
158
159
160 }
161
162 render "$camera_face" "face"
163 #render "$camera_side" "side"
164 #render top
165
166
167 rm $tmppov
168 echo
169 echo "---------------------------------------------------------------------------"
170 echo
171 cat $result
172 echo
173 rm $result