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

File render_pov_to_textures.sh.v2.txt.sh, 4.8 kB (added by martin, 7 years ago)
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
24textureX=512
25textureY=512
26
27leaves=true
28depthmap=true
29
30output_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
35megapov="megapov"
36defaultpov="povray"
37if which $megapov ;
38then 
39        povray="$megapov" 
40else
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)."
43fi
44
45
46povname=$1
47filename=`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
55objname=`grep "union {" $povname -A 1 | tail -n 1 | sed 's/^.*{ \(.*[0-9]*\)_stems/\1/' `
56
57echo "objname=[$objname]"
58
59tmppov="/tmp/$objname.$$.pov"
60tmptex="/tmp/$objname.$$.tga"
61tmpdepth="/tmp/$objname.$$.depth.tga"
62
63
64result="/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
79base_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
93camera_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        "
98camera_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         "
103camera_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
109obj_pov="       
110                union {
111                         object { ${objname}_stems
112                                pigment {color rgb <1.0,1.0,0.0>} }
113        "
114
115     
116if [ "$leaves" = "true" ];
117then   
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        "
123fi     
124obj_pov="$obj_pov }"
125
126
127if [ "$depthmap" = "true" ];
128then
129depth_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        "
142fi
143
144
145
146function 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
162render "$camera_face" "face"
163#render "$camera_side" "side"
164#render top
165
166
167rm $tmppov
168echo
169echo "---------------------------------------------------------------------------"
170echo
171cat $result
172echo
173rm $result