###############################################################################
# Product:     OpenCTM tools
# File:        Makefile.msvc
# Description: Makefile for the OpenCTM tools, MS Visual Studio version
###############################################################################
# Copyright (c) 2009-2010 Marcus Geelnard
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
#     1. The origin of this software must not be misrepresented; you must not
#     claim that you wrote the original software. If you use this software
#     in a product, an acknowledgment in the product documentation would be
#     appreciated but is not required.
#
#     2. Altered source versions must be plainly marked as such, and must not
#     be misrepresented as being the original software.
#
#     3. This notice may not be removed or altered from any source
#     distribution.
###############################################################################

OPENCTMDIR = ..\lib
GLEWDIR = glew
JPEGDIR = jpeg
RPLYDIR = rply
TINYXMLDIR = tinyxml
ZLIBDIR = zlib
PNGLITEDIR = pnglite

CPP = cl
CPPFLAGS = /nologo /c /Ox /W3 /EHsc /I$(OPENCTMDIR) /I$(RPLYDIR) /I$(JPEGDIR) /I$(TINYXMLDIR) /I$(GLEWDIR) /I$(ZLIBDIR) /I$(PNGLITEDIR) /DGLEW_STATIC /D_CRT_SECURE_NO_WARNINGS
RC = rc

MESHOBJS = mesh.obj meshio.obj ctm.obj ply.obj rply.obj stl.obj 3ds.obj dae.obj obj.obj lwo.obj off.obj wrl.obj
CTMCONVOBJS = ctmconv.obj common.obj systimer.obj convoptions.obj $(MESHOBJS) ctmconv.res
CTMVIEWEROBJS = ctmviewer.obj common.obj image.obj systimer.obj sysdialog_win.obj convoptions.obj glew.obj pnglite.obj $(MESHOBJS) ctmviewer.res
CTMBENCHOBJS = ctmbench.obj systimer.obj

all: ctmconv.exe ctmviewer.exe ctmbench.exe

clean:
	del /Q ctmconv.exe ctmviewer.exe ctmbench.exe $(CTMCONVOBJS) $(CTMVIEWEROBJS) $(CTMBENCHOBJS) bin2c.exe phong_frag.h phong_vert.h
	cd $(JPEGDIR) && $(MAKE) /fmakefile.vc cleanlib
	cd $(TINYXMLDIR) && $(MAKE) /fMakefile.msvc clean
	cd $(ZLIBDIR) && $(MAKE) /fMakefile.msvc clean

openctm.dll: $(OPENCTMDIR)\openctm.dll
	copy $(OPENCTMDIR)\openctm.dll openctm.dll

ctmconv.exe: $(CTMCONVOBJS) $(TINYXMLDIR)\tinyxml.lib openctm.dll
	$(CPP) /nologo /Fe$@ $(CTMCONVOBJS) /link /LIBPATH:$(OPENCTMDIR) /LIBPATH:$(TINYXMLDIR) openctm.lib tinyxml.lib

ctmviewer.exe: $(CTMVIEWEROBJS) $(JPEGDIR)\libjpeg.lib $(TINYXMLDIR)\tinyxml.lib $(ZLIBDIR)\libz.lib openctm.dll
	$(CPP) /nologo /Fe$@ $(CTMVIEWEROBJS) /link /subsystem:windows /entry:mainCRTStartup /LIBPATH:$(OPENCTMDIR) /LIBPATH:$(TINYXMLDIR) /LIBPATH:$(JPEGDIR) /LIBPATH:$(ZLIBDIR) openctm.lib tinyxml.lib glut.lib libjpeg.lib libz.lib opengl32.lib glu32.lib

ctmbench.exe: $(CTMBENCHOBJS) openctm.dll
	$(CPP) /nologo /Fe$@ $(CTMBENCHOBJS) /link /LIBPATH:$(OPENCTMDIR) openctm.lib

.cpp.obj:
	$(CPP) $(CPPFLAGS) /Fo$@ $<

ctmconv.obj: ctmconv.cpp systimer.h convoptions.h mesh.h meshio.h
ctmviewer.obj: ctmviewer.cpp common.h image.h systimer.h sysdialog.h mesh.h meshio.h phong_vert.h phong_frag.h icons\icon_open.h icons\icon_save.h icons\icon_help.h
ctmbench.obj: ctmbench.cpp systimer.h
common.obj: common.cpp common.h
image.obj: image.cpp image.h common.h $(JPEGDIR)\libjpeg.lib
systimer.obj: systimer.cpp systimer.h
sysdialog_win.obj: sysdialog_win.cpp sysdialog.h
convoptions.obj: convoptions.cpp convoptions.h
mesh.obj: mesh.cpp mesh.h convoptions.h
meshio.obj: meshio.cpp common.h convoptions.h mesh.h ctm.h ply.h stl.h 3ds.h dae.h obj.h lwo.h off.h wrl.h
ctm.obj: ctm.cpp ctm.h mesh.h convoptions.h
ply.obj: ply.cpp ply.h mesh.h convoptions.h common.h
stl.obj: stl.cpp stl.h mesh.h convoptions.h
3ds.obj: 3ds.cpp 3ds.h mesh.h convoptions.h
dae.obj: dae.cpp dae.h mesh.h convoptions.h
obj.obj: obj.cpp obj.h mesh.h convoptions.h common.h
lwo.obj: lwo.cpp lwo.h mesh.h convoptions.h
off.obj: off.cpp off.h mesh.h convoptions.h common.h
wrl.obj: wrl.cpp wrl.h mesh.h convoptions.h common.h

phong_vert.h: phong.vert bin2c.exe
	bin2c.exe phong.vert phongVertSrc > $@

phong_frag.h: phong.frag bin2c.exe
	bin2c.exe phong.frag phongFragSrc > $@

bin2c.exe: bin2c.cpp
	$(CPP) /nologo /Ox /W3 /EHsc /Fe$@ bin2c.cpp

ctmconv.res: ctmconv.rc icons\openctm.ico
	$(RC) ctmconv.rc

ctmviewer.res: ctmviewer.rc icons\openctm.ico
	$(RC) ctmviewer.rc

$(JPEGDIR)\libjpeg.lib: 
	cd $(JPEGDIR) && $(MAKE) /fmakefile.vc libjpeg.lib

$(ZLIBDIR)\libz.lib:
	cd $(ZLIBDIR) && $(MAKE) /fMakefile.msvc

glew.obj: $(GLEWDIR)\glew.c
	cl /nologo /c /Ox /W3 /I$(GLEWDIR) /DGLEW_STATIC /Fo$@ $(GLEWDIR)\glew.c

rply.obj: $(RPLYDIR)\rply.c
	cl /nologo /c /Ox /W3 /I$(RPLYDIR) /D_CRT_SECURE_NO_WARNINGS /Fo$@ $(RPLYDIR)\rply.c

pnglite.obj: $(PNGLITEDIR)\pnglite.c
	cl /nologo /c /Ox /W3 /I$(PNGLITEDIR) /D_CRT_SECURE_NO_WARNINGS /Fo$@ $(PNGLITEDIR)\pnglite.c

$(TINYXMLDIR)\tinyxml.lib:
	cd $(TINYXMLDIR) && $(MAKE) /fMakefile.msvc
