#!/bin/sh
#render a arbaro generated pov file into textures
#author: Fred Marmond <fmarmond@users.sourceforge.net>
#2005-01-09
#It uses ARBARO (http://arbaro.sourceforge.net/), a wonderfull tree generetor in GPL.
#My final goal: automagically generate tree mesh for OSG with LOD
#
#This script uses ARBARO and POVRAY to render trees to textures (to be used in a 2 quads tree or billboard one)

#
#param: $1: the .pov filename
#output: will generate mesh_object_name (token from the .pov file) _face.tga, .side.tga
#	(for exemple, if mesh name is 'lombardy_poplar_13', output file will be 'lombardy_poplar_13.face.tga' and 'lombardy_poplar_13.side.tga')
#	output files are already RGBA, so, you may use them directly as textures! :-))

#todo: 
#	1: make better parameters (texture size, light, leaves, ...)
#	2: add texture for leaves and branches (actually, I only put basic colors for leaves and branches)

#requirements:
#- povray must be compiled with TGA support
#- povray must have writing permissions to /tmp/

textureX=512
textureY=512

leaves=true
depthmap=true

output_dir="textures/"

#some povray installation gives a bad name for povray ("povray35" for exemple)
#change it here if it is your case
#try to see if we can use megapov => depthmap
megapov="megapov"
defaultpov="povray"
if which $megapov ; 
then 
	povray="$megapov" 
else
	povray="$defaultpov"
	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)."
fi


povname=$1
filename=`echo $povname | sed 's/\.pov$//'`

#search the base object name
#	search "union {" line and get the line just after
#	this line is "object { foo_bar_NUMBER_stems"
#		remove the "object { " and "stems"
#	so, objname="foo_bar_NUMBER"

objname=`grep "union {" $povname -A 1 | tail -n 1 | sed 's/^.*{ \(.*[0-9]*\)_stems/\1/' `

echo "objname=[$objname]"

tmppov="/tmp/$objname.$$.pov"
tmptex="/tmp/$objname.$$.tga"
tmpdepth="/tmp/$objname.$$.depth.tga"


result="/tmp/result.$$.txt"
(
	echo "ARBARO to POVRAY: automatic texture generation, by Fred Marmond, fmarmond@users.sf.net"
	echo "Input file :	$povname"
	echo "Object name:	$objname"
	echo
	echo "Options :"
	echo "textureX   :	$textureX"
	echo "textureY   :	$textureY"
	echo "leaves     :	$leaves"
	echo "depthmap   :	$depthmap"
	echo
	echo "Output files :"
)>$result

base_pov="	#include \"${filename}.inc\"
		//we set 'full blue' background, in order to make the transparent texture easily later
		background {rgb <0.0,0.0,0.0,0.0>}

		//comment it if you don't want lighting
		light_source { <5000,5000,-3000>, rgb 1.2 }
		light_source { <-5000,2000,3000>, rgb 0.5 shadowless }

		
		//#declare HEIGHT = ${objname}_height * 1.02;
		#declare HEIGHT = ${objname}_height * 1.3;
		#declare WIDTH = HEIGHT*${textureX}/${textureY};
	"

camera_face="
		camera { orthographic location <0, HEIGHT*0.5, -40 >
         		right <WIDTH, 0, 0> up <0, HEIGHT, 0.0>
         		look_at <0, HEIGHT*0.5, 0> }	
	"
camera_side="
		camera { orthographic location <40, HEIGHT*0.5, 0>
        		right <WIDTH, 0, 0> up <0, HEIGHT, 0.0>
		        look_at <0, HEIGHT*0.5, 0> }
	 "
camera_top="
		camera { orthographic location <0, HEIGHT, 0>
        		right <WIDTH, 0, 0> up <0, 0, WIDTH>
        		look_at <0, HEIGHT*0.5, 0> }
	 "

obj_pov="	
		union { 
        		 object { ${objname}_stems
                		pigment {color rgb <1.0,1.0,0.0>} }
	"

      
if [ "$leaves" = "true" ];
then	
	obj_pov="$obj_pov
		         object { ${objname}_leaves
        		        texture { pigment {color rgb <0.0,1.0,0.0>} 
                		          finish { ambient 0.15 diffuse 0.8 }}}
	"
fi	
obj_pov="$obj_pov }"


if [ "$depthmap" = "true" ];
then
depth_map="
	#version unofficial MegaPov 1.1;
	#include \"pprocess.inc\"
	#declare DepthMin=35;
	#declare DepthMax=45;
	global_settings {
	  assumed_gamma 1.0
	    post_process{
	        PP_Depth(DepthMin,DepthMax)
		save_file \"${tmpdepth}\"
		}
	}
	"
fi



function render()
#param1: $camera_view
#param2: view name 
#exemple: render($camera_face,face)
{
	camera_view="$1"
	view="$2"
	output_rgba="${output_dir}${objname}.${view}.tga"
	output_depth="${output_dir}${objname}.${view}.depth.tga"
( echo "$depth_map $base_pov $camera_view $obj_pov" ) > $tmppov
$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 )



}

render "$camera_face" "face"
#render "$camera_side" "side"
#render top


rm $tmppov
echo
echo "---------------------------------------------------------------------------"
echo
cat $result
echo
rm $result
