# Makefile for RenderPark.
#####################################################################
# Use of GNU make is highly recommended !!
#####################################################################
# Config.common contains platform and site independent configuration
# parameters and default that can be overriden in Config.site.
# There is (should be) no need to change anything in this file
# unless you are adding source files or directories to RenderPark.
#####################################################################
include Config.common
include Config.site

# sources in this directory
CSRCS =  main.c options.c file.c batch.c ipc.c error.c \
	monitor.c canvas.c select.c softids.c \
	rendercommon.c render.c renderhook.c \
	tonemapping.c adaptation.c gamma.c lightmetering.c \
	camera.c scene.c transform.c vector.c vectorlist.c \
	bounds.c material.c materiallist.c vector2d.c \
	xxdf.c bsdf.c splitbsdf.c brdf.c btdf.c edf.c phong.c texture.c \
	hit.c hitlist.c grid.c geom.c geomlist.c compound.c surface.c \
	vertex.c vertexlist.c patch.c patchlist.c jacobian.c background.c \
	readmgf.c namedvertex.c cie.c fix.c cluster.c color.c \
	geometry3d.c topology.c cubature.c shadowcaching.c shaftculling.c \
	spherical.c potential.c radiance.c raytracing.c packedcolor.c \
	globallines.c \
	uit.c ui_defaults.c ui_main.c ui_file.c ui_radiance.c ui_render.c \
	ui_raytracing.c ui_camera.c ui_help.c ui_tonemapping.c ui_config.c \
	ui_canvas.c ui_phong.c ui_debug.c ui_dump.c \
	writevrml.c discretesampling.c \
	$(EXTRASRCS)
CXXSRCS = readvrml.C PhBRML.C PhBRMLBackground.C kdtree.C lightlist.C hemirenderer.C stratification.C samplegrid.C ui_pathdebug.C SoftIds.C balancekdtree.C background_edf.C
SRCS = $(CXXSRCS) $(CSRCS)

# object files in this directory
OBJS = $(CSRCS:.c=.o) $(CXXSRCS:.C=.o)

# Galerkin radiosity flags, sources, targets, libs
# The Makefile variable includeGalekinRadiosity is set in Config.common.
GALDIRS = GALERKIN
ifeq ($(includeGalerkinRadiosity),true)
GALFLAGS = -DGALERKIN
GALTARGETS = libgalerkin
GALLIBS = -L./GALERKIN -lgalerkin

# if Galerkin radiosity is reuiqred, unconditionally make the
# libgalerkin target defined below. Whether something needs to be
# done or not depends on the dependencies in the subdirectory.
.PHONY: libgalerkin

libgalerkin: 
	${MAKE} -C GALERKIN all
endif

# Monte Carlo radiosity
MCRDIRS = MCRAD
ifeq ($(includeMonteCarloRadiosity),true)
MCRTARGETS = libmcrad
MCRLIBS = -L./MCRAD -lmcrad

ifeq ($(includeStochasticRelaxationRadiosity),true)
SRRFLAGS = -DSTOCHRELAX
endif

ifeq ($(includeWDRS),true)
WDRSFLAGS = -DWDRS
endif
ifeq ($(includeStochRay),true)
SRRFLAGS = -DSTOCHRAY
endif
ifeq ($(includeRandomWalkRadiosity),true)
RWRADFLAGS = -DRWRAD
endif
MCRFLAGS = $(WDRSFLAGS) $(SRRFLAGS) $(RWRADFLAGS) $(SRRFLAGS)

.PHONY: libmcrad

libmcrad: 
	${MAKE} -C MCRAD all
endif

# The Ultimate Rendering Algorithm
HURADIRS = HURA
ifeq ($(includeHURA),true)
HURAFLAGS = -DHURA
HURATARGETS = libhura
HURALIBS = -L./HURA -lhura

.PHONY: libhura

libhura: 
	${MAKE} -C HURA all
endif

# Density Estimation
DESTDIRS = DEST SE
ifeq ($(includeDensityEstimation),true)
DESTFLAGS = -DDEST
DESTTARGETS = libdest libply
DESTLIBS = -L./DEST -ldest -L./SE/Plyfile -lply

.PHONY: libdest libply

libdest:
	${MAKE} -C DEST all

libply:
	$(MAKE) -C SE all
endif

# The PhotonMap
PHOTONMAPDIRS = PHOTONMAP
ifeq ($(includePhotonMap),true)
PHOTONMAPFLAGS = -DPHOTONMAP
PHOTONMAPTARGETS = libpmap
PHOTONMAPLIBS = -L./PHOTONMAP -lpmap

.PHONY: libpmap

libpmap: 
	${MAKE} -C PHOTONMAP all
endif

# RADFLAGS are extra compilation flags for radiance.c.
# RADLIBS are command line arguments passed to CXX in order to 
# link in the libraries with the world-space radiance methods
# RADTARGETS is the list targets to make for the libraries mentionned
# in RADLIBS.
RADDIRS = $(GALDIRS) $(MCRDIRS) $(HURADIRS) $(DESTDIRS) $(PHOTONMAPDIRS) $(YUPDIRS)
RADFLAGS = $(GALFLAGS) $(MCRFLAGS) $(HURAFLAGS) $(DESTFLAGS) $(PHOTONMAPFLAGS) $(YUPFLAGS)
RADTARGETS = $(GALTARGETS) $(MCRTARGETS) $(HURATARGETS) $(DESTTARGETS) $(PHOTONMAPTARGETS) $(YUPTARGETS)
RADLIBS = $(GALLIBS) $(MCRLIBS) $(HURALIBS) $(DESTLIBS) $(PHOTONMAPLIBS) $(YUPLIBS)

# Pixel-based radiance computations:

# Stochastic Raytracing and Bidirectional Path Tracing
RAYTRACINGDIRS = RAYTRACING
ifeq ($(includeStochasticRayTracing),true)
SRTFLAGS = -DRT_STOCHASTIC
endif
ifeq ($(includeBidirectionalPathTracing),true)
BIDIRFLAGS = -DRT_BIDIR
endif
RAYTRACINGFLAGS = $(SRTFLAGS) $(BIDIRFLAGS)
RAYTRACINGTARGETS = libraytracing
RAYTRACINGLIBS = -L./RAYTRACING -lraytracing

.PHONY: libraytracing

libraytracing:
	${MAKE} -C RAYTRACING all

# RAYFLAGS are extra compilation flags for raytracing.c
# RAYLIBS determine the libraries to link in for the raytracing methods
# RAYTARGERTS is the list of targets to make the raytracing libs.
RAYDIRS = ${RAYTRACINGDIRS}
RAYFLAGS = $(RAYTRACINGFLAGS)
RAYTARGETS = $(RAYTRACINGTARGETS)
RAYLIBS = $(RAYTRACINGLIBS)


# tool libraries
TOOLDIRS =  MGF POOLS GDT BREP SGL QMC IMAGE PNM TONEMAP # TOOLS
TOOLFLAGS =  -I./POOLS -I./GDT -I./BREP -I./IMAGE -I./SGL
TOOLTARGETS = libmgf libpools libgdt libbrep libsgl libqmc libimage libtonemap
TOOLLIBS = -L./IMAGE -limage -L./MGF -lmgf -L./SGL -lsgl -L./QMC -lqmc -L./BREP -lbrep -L./GDT -lgdt -L./POOLS -lpools -L./TONEMAP -ltonemap

.PHONY: libmgf libpools libgdt libbrep libsgl libqmc libimage libtonemap

libmgf: 
	${MAKE} -C MGF all

libpools: 
	${MAKE} -C POOLS all

libbrep: 
	${MAKE} -C BREP all

libgdt: 
	${MAKE} -C GDT all

libsgl: 
	${MAKE} -C SGL all

libqmc:
	${MAKE} -C QMC all

libimage:
	${MAKE} -C IMAGE all

libtonemap:
	${MAKE} -C TONEMAP all


# even if you are doing implementations in RenderPark, you shouldn't
# need to change anything below here.

SUBDIRS = $(TOOLDIRS) $(RADDIRS) $(RAYDIRS)
LIBTARGETS = $(TOOLTARGETS) $(RADTARGETS) $(RAYTARGETS)

THISDIRLIBS = $(RAYLIBS) $(RADLIBS) $(TOOLLIBS)
THISDIRFLAGS = -I. $(TOOLFLAGS)



# default target: make rpk
all: rpk

list-targets:
	echo "SUBDIRS=" $(SUBDIRS)
	echo "LIBTARGETS=" $(LIBTARGETS)
	echo "THISLIBDIRS=" $(THISLIBDIRS)

# automatically generate source file dependencies

depend_here:
	$(MAKEDEPEND) $(MKDEPFLAGS) $(DRIVERFLAGS) $(SRCS) > deps

depend: render.c ui_defaults.c depend_here
	for i in $(SUBDIRS) ; do { $(MAKE) -C $$i depend || exit 1 ; } ; done

# render.c is a link to opengl.c, gl.c or starbase.c.
render.c: Config.site
	$(SYMLINK) $(DRIVER).c render.c
	$(TOUCH) render.c

# special compilation flags are required to compile render.c
render.o: render.c 
	$(CC) $(CFLAGS) $(DRIVERFLAGS) -c render.c

# and also for ui_help.c
ui_help.o: ui_help.c
	$(CC) $(CFLAGS) -DWEBBROWSER=${WEBBROWSER} -DRPKHOME=${RPKHOME} -DRPKHTMLDOC=${RPKHTMLDOC} -c ui_help.c

# radiance.c as well
radiance.o: radiance.c Config.common
	$(CC) $(CFLAGS) $(RADFLAGS) -c radiance.c

# and raytracing.c too
raytracing.o: raytracing.c Config.common
	$(CC) $(CFLAGS) $(RAYFLAGS) -c raytracing.c

# and also ui_debug.c
ui_debug.o: ui_debug.c Config.common
	$(CC) $(CFLAGS) $(RADFLAGS) $(RAYFLAGS) -c ui_debug.c

# automatically create a C file containing fallback resources from 
# the application defaults in the file 'RenderPark'
ui_defaults.c: RenderPark
	echo "/* ui_defaults.c: automatically generated from the application defaults file 'RenderPark' */" > ui_defaults.c
	echo "" >> ui_defaults.c
	echo "char *fallback_resources[] = {" >> ui_defaults.c
	sh ad2c.script < RenderPark >> ui_defaults.c
	echo "(char *)0" >> ui_defaults.c
	echo "};" >> ui_defaults.c

# make the executable program 'rpk'
rpk: $(LIBTARGETS) $(OBJS)
	$(CXX) $(LDFLAGS) -o rpk $(OBJS) $(LIBS)

install:
	-mkdir $(BINDIR)
	$(INSTALL) $(TOPDIR)/rpk $(BINDIR)

uninstall:
	-$(RM) $(BINDIR)/rpk

# clean up executable and object files
cleanhere: cleangen
	-$(RM) -f ui_defaults.c render.c rpk

clean: cleanhere
	for i in $(SUBDIRS) ; do { $(MAKE) -C $$i clean || exit 1 ; } ; done

distclean: cleanhere
	for i in $(SUBDIRS) ; do { $(MAKE) -C $$i distclean || exit 1 ; } ; done
	-$(RM) *.ppm *.mgf *.gz *.tif *.pic

# include the automatically generated source file dependencies
include deps
