#
# author: Nicolas Docquier
#
# OUtput 3D view to image, movies, etc.

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                       PROJECT MAIN CONFIGURATIONS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


# CMake minimum version
cmake_minimum_required(VERSION 2.8.12)

# project name
project (ScreenCapture)

# Retriev the MBsysC base directory
set (ROBOTRAN_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../..")


# message to display the project name
message(STATUS "Processing ${PROJECT_NAME}")

# for Unix: display all the warnings, except the ones related to /* -- */
if (UNIX)
  set(CMAKE_C_FLAGS "-g -Wall -Wno-comment")
endif (UNIX)

# additional CMake functions
set(CMAKE_AUX ${ROBOTRAN_SOURCE_DIR}/cmake_aux)
set(CMAKE_AUX_BIN ${PROJECT_BINARY_DIR}/cmake_aux)

add_subdirectory ( ${CMAKE_AUX}/listing/   ${CMAKE_AUX_BIN}/listing/   )
add_subdirectory ( ${CMAKE_AUX}/libraries/ ${CMAKE_AUX_BIN}/libraries/ )

# Windows M_PI definitions
if (WIN32)
    add_definitions(-D _USE_MATH_DEFINES)
endif (WIN32)

# project own definition
add_definitions(-D SEPARATE_OPEN_GL)


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           PROJECT OPTIONS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


include(CMakeDependentOption)
option (FLAG_FRAME_CAPTURE "Frame capture" ON)

# subflag of frame capture -> output to png
cmake_dependent_option(FLAG_FRAME_CAPTURE_PNG "Enable frame capture to PNG (need libpng)" OFF "FLAG_FRAME_CAPTURE" OFF)

# subflag of frame capture -> output to mpeg
cmake_dependent_option(FLAG_FRAME_CAPTURE_MPEG "Enable frame capture to PNG (need libmpeg)" OFF "FLAG_FRAME_CAPTURE" OFF)

# subflag of frame capture -> output to opencv
cmake_dependent_option(FLAG_FRAME_CAPTURE_OPENCV "Enable frame capture to PNG (need opencv)" OFF "FLAG_FRAME_CAPTURE" OFF)

add_definitions( -DFRAME_CAPTURE )
if(FLAG_FRAME_CAPTURE_PNG)
    add_definitions( -DFRAME_CAPTURE_PNG )
    #set(LIB_MBSYSC_DEFINITIONS ${LIB_MBSYSC_DEFINITIONS} -DFRAME_CAPTURE_PNG)
endif( )

if(FLAG_FRAME_CAPTURE_MPEG)
    add_definitions( -DFRAME_CAPTURE_MPEG )
    #set(LIB_MBSYSC_DEFINITIONS ${LIB_MBSYSC_DEFINITIONS} -DFRAME_CAPTURE_MPEG)
endif( )

if(FLAG_FRAME_CAPTURE_OPENCV)
    add_definitions( -DFRAME_CAPTURE_OPENCV )
    #set(LIB_MBSYSC_DEFINITIONS ${LIB_MBSYSC_DEFINITIONS} -DFRAME_CAPTURE_OPENCV)
endif( )


option (FLAG_IPY_FRAME_CAPTURE "Python interface to the frame capture app" OFF)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           USER ABSOLUTE PATHS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# configure header files to pass some of the CMake settings to the source code

configure_file (
  "${ROBOTRAN_SOURCE_DIR}/conf/cmake_config.h.in"
  "${PROJECT_BINARY_DIR}/conf/cmake_config.h"
)

set(OPEN_GL_FILES_PATH ${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_realtime/open_gl/)

configure_file (
    "${OPEN_GL_FILES_PATH}/conf/cmake_open_gl_config.h.in"
    "${PROJECT_BINARY_DIR}/conf/cmake_open_gl_config.h"
)

# 'cmake_config.h.in' and 'cmake_open_gl_config.h.in' is in the 'conf' folder
include_directories (${PROJECT_BINARY_DIR}/conf)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           EXTERNAL LIBRARIES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

set(CMAKE_MODULE_PATH ${ROBOTRAN_SOURCE_DIR}/conf)

# Libxml2
xml_lib(main)

# OpenGL
open_gl_lib(main)

# Frame capture
if(FLAG_FRAME_CAPTURE)
    include_directories("./open_gl/frame_capture")
    if(FLAG_FRAME_CAPTURE_PNG)
        find_package(PNG)
    endif()
    
    if(FLAG_FRAME_CAPTURE_MPEG)
        set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE)
        find_package(FFMPEG REQUIRED)
    endif()
    
    if(FLAG_FRAME_CAPTURE_OPENCV)
        find_package( OpenCV REQUIRED )
        include_directories( ${OpenCV_INCLUDE_DIRS} )    
    endif()
    
endif()


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           LIST FILES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 
# list source files to compile
init_src()

increment_src("${PROJECT_SOURCE_DIR}/src/renderer")

increment_src("${OPEN_GL_FILES_PATH}/anim_read")
increment_src("${OPEN_GL_FILES_PATH}/components")
increment_src("${OPEN_GL_FILES_PATH}/lights")
increment_src("${OPEN_GL_FILES_PATH}/lights/specific")
increment_src("${OPEN_GL_FILES_PATH}/loader")
increment_src("${OPEN_GL_FILES_PATH}/mbs_read")
increment_src("${OPEN_GL_FILES_PATH}/shapes")
increment_src("${OPEN_GL_FILES_PATH}/viewpoint")
increment_src("${OPEN_GL_FILES_PATH}/renderer/default")
increment_src("${OPEN_GL_FILES_PATH}/renderer/ogl")
increment_src("${OPEN_GL_FILES_PATH}/world")
increment_src("${OPEN_GL_FILES_PATH}/window/glfw")
increment_src("${OPEN_GL_FILES_PATH}/shaders")

increment_src("${OPEN_GL_FILES_PATH}/frame_capture")

set(SOURCE_FILES ${SOURCE_FILES} "${OPEN_GL_FILES_PATH}/interface/shader_multisample.cc")

set(SOURCE_FILES ${SOURCE_FILES} "./src/ScreenCapture.cc")


# list include directories (to find headers)
init_include()

increment_include("${PROJECT_SOURCE_DIR}/src/")

include_directories("${OPEN_GL_FILES_PATH}/")
include_directories("${OPEN_GL_FILES_PATH}/anim_read")
include_directories("${OPEN_GL_FILES_PATH}/components")
include_directories("${OPEN_GL_FILES_PATH}/interface")
include_directories("${OPEN_GL_FILES_PATH}/lights")
include_directories("${OPEN_GL_FILES_PATH}/lights/specific")
include_directories("${OPEN_GL_FILES_PATH}/loader")
include_directories("${OPEN_GL_FILES_PATH}/loader/vrml")
include_directories("${OPEN_GL_FILES_PATH}/loader/obj")
include_directories("${OPEN_GL_FILES_PATH}/loader/stl")
include_directories("${OPEN_GL_FILES_PATH}/mbs_read")
include_directories("${OPEN_GL_FILES_PATH}/shaders")
include_directories("${OPEN_GL_FILES_PATH}/shapes")
include_directories("${OPEN_GL_FILES_PATH}/shapes/specific")
include_directories("${OPEN_GL_FILES_PATH}/viewpoint")
include_directories("${OPEN_GL_FILES_PATH}/viewpoint/specific")
include_directories("${OPEN_GL_FILES_PATH}/world")
include_directories("${OPEN_GL_FILES_PATH}/renderer")
include_directories("${OPEN_GL_FILES_PATH}/renderer/default")
include_directories("${OPEN_GL_FILES_PATH}/renderer/ogl")
include_directories("${OPEN_GL_FILES_PATH}/window")
include_directories("${OPEN_GL_FILES_PATH}/window/glfw")
include_directories("${OPEN_GL_FILES_PATH}/specific_include")

include_directories("${OPEN_GL_FILES_PATH}/frame_capture")
#add_subdirectory("${OPEN_GL_FILES_PATH}/frame_capture" "${CMAKE_BINARY_DIR}/frame_capture")


add_definitions( -DOPEN_GL ) 


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE main library
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# include these directories
include_directories (${INCLUDE_DIR})

# name of the library
set (LibScreenCapture ScreenCapture)

add_library(${LibScreenCapture} SHARED  ${SOURCE_FILES})

target_link_libraries ( ${LibScreenCapture} ${LIBXML2_LIBRARIES})

target_link_libraries ( ${LibScreenCapture} ${OPEN_GL_LIBS})

# math external library (for Unix): linkage
if (UNIX)
    target_link_libraries (${LibScreenCapture} m)
endif (UNIX)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE standalone executable
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


# name of the executable
set (Executable exe_ScreenCapture)

# generate the executable
add_executable (${Executable} src/main.cc)

# link library: Libxml2
target_link_libraries ( ${Executable} ${LibScreenCapture} )
# target_link_libraries ( ${Executable} ${LIBXML2_LIBRARIES} ${OPEN_GL_LIBS} ) # Is it necessary ??? -> seems not for ubuntu but for other platform (mac...)?

# math external library (for Unix): linkage
if (UNIX)
    target_link_libraries (${Executable} m)
endif (UNIX)


# Frame capture
if(FLAG_FRAME_CAPTURE)
    if(FLAG_FRAME_CAPTURE_PNG)
        target_link_libraries (${LibScreenCapture} ${PNG_LIBRARIES})
        #target_link_libraries (${Executable} ${PNG_LIBRARIES}) # Is it necessary ??? -> seems not for ubuntu but for other platform (mac...)?
    endif()
    
    if(FLAG_FRAME_CAPTURE_MPEG)
        target_link_libraries (${LibScreenCapture} ${FFMPEG_LIBRARIES}) # ${SWSCALE_LIBRARIES})
        #target_link_libraries (${Executable} ${FFMPEG_LIBRARIES}) # ${SWSCALE_LIBRARIES}) # Is it necessary ??? -> seems not for ubuntu but for other platform (mac...)?
    endif()
    
    if(FLAG_FRAME_CAPTURE_OPENCV)
        target_link_libraries( ${LibScreenCapture} ${OpenCV_LIBS} )
        #target_link_libraries( ${Executable} ${OpenCV_LIBS} ) # Is it necessary ??? -> seems not for ubuntu but for other platform (mac...)?
    endif()
    
endif()


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         Python interface
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

if(FLAG_IPY_FRAME_CAPTURE)
  
    find_package(Python REQUIRED)  

    include_directories(${PYTHON_INCLUDE_DIR} 
                      ${NUMPY_INCLUDE_DIR}
                      ${CMAKE_CURRENT_SOURCE_DIR}
                     )  

    # Set library extension
    if(UNIX)
      set(IPY_LIB_EXT ".so")
    elseif(WIN32)
      set(IPY_LIB_EXT ".pyd")
    else()
      message("Unknow operating system, extension for cython library arbitrary choosen: .so")
      set(IPY_LIB_EXT ".so")
    endif()

    set( cxx_arg "--cplus" )
    set( pyx_lang "CXX" )

    set(LibPyMbsVtk CyScreenCapture)

    file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${LibPyMbsVtk}.cpp)
    add_custom_command(OUTPUT ${LibPyMbsVtk}.cpp
                     COMMAND ${CYTHON_EXECUTABLE} -f ${CMAKE_CURRENT_SOURCE_DIR}/src/${LibPyMbsVtk}.pyx  -o ${CMAKE_CURRENT_BINARY_DIR}/${LibPyMbsVtk}.cpp
                     ARGS ${cxx_arg}
                     IMPLICIT_DEPENDS ${pyx_lang}
                     DEPENDS src/${LibPyMbsVtk}.pyx
                     )
    add_library(${LibPyMbsVtk} SHARED  ${CMAKE_CURRENT_BINARY_DIR}/${LibPyMbsVtk}.cpp)
    set_target_properties(${LibPyMbsVtk}   PROPERTIES PREFIX ""      )
    set_target_properties(${LibPyMbsVtk}   PROPERTIES SUFFIX "${IPY_LIB_EXT}"   )
    target_link_libraries(${LibPyMbsVtk} ${LibScreenCapture} ${LIBXML2_LIBRARIES} m ${PYTHON_LIBRARIES})

endif()


