# This script generates images to be used with EGUIM_AddPushButtonImg3_shadow
# It also expects separate Direct ("*-D.tif") and Indirect ("*-I.tif") illumination images, in order to apply indirect light on the ground plane
#!/bin/sh +xv
#set -o verbose
export IMAGE=$1
export ScrCOMPILER="$2"
export ScrOUTPUT=$3
export VERBOSE=

# Copy alpha from image into a separate file
#$iSPL $VERBOSE  -i Icons/images/$IMAGE.sgi -oA tmp/.sgi

# Multily image with its alpha
#$iCM  $VERBOSE  -M   -i Icons/images/$IMAGE.sgi         -i tmp/$IMAGE.sgi  -o tmp/$IMAGE-RGB.sgi

# Subtract the result from the image
#$iCM  $VERBOSE  -S   -i Icons/images/$IMAGE-shadow.sgi  -i tmp/$IMAGE.sgi  -o tmp/$IMAGE-A.sgi

# Brighten up alpha, so shadows won't crop around the image edges
#$iCN $EITHREADS  $VERBOSE  -c 1.03  tmp/$IMAGE-A.sgi  .sgi

# Merge image channels
#$iCM  $VERBOSE  -iRGB tmp/$IMAGE-RGB.sgi  -iA tmp/$IMAGE-A.sgi  -o tmp/$IMAGE-A.sgi


# Ground rendered with Constant with shadow shader : direct lighting : black ground, indirect : 0.75 Diffuse.
# This preserves indirect light on the ground and eliminates the need for multiplying image by alpha
$iSPL $VERBOSE  -i Icons/images/$IMAGE.sgi -oA tmp/A-.sgi

# Multiply indirect light by 0.1 where alpha = 0 (on the ground)
$iCN $EITHREADS  $VERBOSE  -b 0.1 tmp/A-$IMAGE.sgi tmp/$IMAGE-AO.sgi
$iCM  $VERBOSE  -M -i Icons/images/$IMAGE-I.tif  -i tmp/$IMAGE-AO.sgi  -o tmp/$IMAGE-New-I.tif;rm -f tmp/$IMAGE-AO.sgi

$iCM  $VERBOSE  -i Icons/images/$IMAGE-D.tif  -i tmp/$IMAGE-New-I.tif  -o tmp/$IMAGE.tif;rm -f tmp/$IMAGE-New-I.tif

$iCN $EITHREADS  $VERBOSE  -g 1.3 tmp/$IMAGE.tif tmp/$IMAGE.sgi;rm -f tmp/$IMAGE.tif

# Brighten up alpha, so shadows won't crop around the image edges
$iCM  $VERBOSE  -S   -i Icons/images/$IMAGE-shadow.sgi  -i tmp/A-$IMAGE.sgi  -o tmp/$IMAGE-A.sgi;rm -f tmp/A-$IMAGE.sgi
$iCN $EITHREADS  $VERBOSE  -c 1.15 -g 1.8  tmp/$IMAGE-A.sgi  .sgi
$iCM  $VERBOSE  -iRGB tmp/$IMAGE.sgi  -iA tmp/$IMAGE-A.sgi  -o tmp/.sgi

$iS $EITHREADS   $VERBOSE  -s $4 $5  -gaussian -b 0.8  tmp/$IMAGE.sgi         tmp/$IMAGE.c
$iS $EITHREADS   $VERBOSE  -s $6 $7  -gaussian -b 0.8  tmp/$IMAGE.sgi         tmp/$IMAGE-Big.c
rm -f tmp/$IMAGE.sgi

$iSPL $VERBOSE  -i Icons/images/$IMAGE-Active.sgi -oA tmp/.sgi
$iCM  $VERBOSE  -M   -i Icons/images/$IMAGE-Active.sgi         -i tmp/$IMAGE-Active.sgi  -o tmp/$IMAGE-RGB.sgi
$iCM  $VERBOSE  -S   -i Icons/images/$IMAGE-Active-shadow.sgi  -i tmp/$IMAGE-Active.sgi  -o tmp/$IMAGE-A.sgi
rm -f tmp/$IMAGE-Active.sgi


$iCN $EITHREADS  $VERBOSE  -c 1.03  tmp/$IMAGE-A.sgi  .sgi
$iCM  $VERBOSE  -iRGB tmp/$IMAGE-RGB.sgi  -iA tmp/$IMAGE-A.sgi  -o tmp/A.sgi

$iS $EITHREADS   $VERBOSE  -s $4 $5  tmp/$IMAGE-RGBA.sgi         tmp/$IMAGE-Active.c
$iS $EITHREADS   $VERBOSE  -s $4 $5  tmp/$IMAGE-RGBA.sgi         tmp/$IMAGE-Armed.c

rm -f tmp/$IMAGE-A.sgi tmp/$IMAGE-RGB.sgi tmp/$IMAGE-RGBA.sgi

cat tmp/$IMAGE.c tmp/$IMAGE-Active.c tmp/$IMAGE-Armed.c tmp/$IMAGE-Big.c > tmp/$IMAGE-all.c;rm -f tmp/$IMAGE.c tmp/$IMAGE-Active.c tmp/$IMAGE-Armed.c tmp/$IMAGE-Big.c
$ScrCOMPILER -c tmp/$IMAGE-all.c -o $ScrOUTPUT;rm -f tmp/$IMAGE-all.c
