# ************************************************************
#
#  Copyright (c) 2009-2018, Thanos Theo. All rights reserved.
#  Released Under a Simplified BSD (FreeBSD) License
#  for academic, personal & non-commercial use.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  1. Redistributions of source code must retain the above copyright notice, this
#  list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright notice,
#  this list of conditions and the following disclaimer in the documentation
#  and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
#  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  The views and conclusions contained in the software and documentation are those
#  of the author and should not be interpreted as representing official policies,
#  either expressed or implied, of the FreeBSD Project.
#
#  A Commercial License is also available for commercial use with
#  special restrictions and obligations at a one-off fee. See links at:
#  1. http://www.dotredconsultancy.com/openglrenderingenginetoolrelease.php
#  2. http://www.dotredconsultancy.com/openglrenderingenginetoolsourcecodelicence.php
#  Please contact Thanos Theo (thanos.theo@dotredconsultancy.com) for more information.
#
# ************************************************************

# GPU Framework version 14.0.0

cmake_minimum_required(VERSION 3.9.0)
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." ON)

# check if the externally dependent library exist, report an error if not.
# OpenGLRenderingEngine depends on SuperQuadrics
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../SuperQuadrics/CMakeLists.txt")
  set(SuperQuadrics_dir ../SuperQuadrics)
else()
  message(STATUS FATAL_ERROR "External needed SuperQuadrics library is not found. OpenGLRenderingEngine can not be built.")
endif()

# check if the externally dependent library exist, report an error if not.
# OpenGLRenderingEngine depends on ShaderFilesGenerator pre-build execution
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../ShaderFilesGenerator/CMakeLists.txt")
  set(ShaderFilesGenerator_dir ../ShaderFilesGenerator)
else()
  message(STATUS FATAL_ERROR "External needed ShaderFilesGenerator library is not found. OpenGLRenderingEngine can not be built.")
endif()

project(OpenGLRenderingEngine CXX)

# set the module path so include can find modules
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Scripts/CMake")

# CMAKE_MODULE_PATH is set, so include looks for modules in that directory
include(MacroUtils)

# setup the compiler & linker environment
setupCompilerAndLinker()

if (NOT GLEW_FOUND OR NOT FREEGLUT_FOUND OR NOT ASSIMP_FOUND)
  # setup the build preprocessor environment
  setupGPUFrameworkPreprocessorDefines()

  # setup all external libraries
  setupGPUFrameworkExternalLibraries()
endif()

# if dependency is missing, then build them
if (NOT TARGET SuperQuadrics)
  #When specifying an out-of-tree source a binary directory must be explicitly specified
  add_subdirectory(${SuperQuadrics_dir} ${CMAKE_BINARY_DIR}/SuperQuadrics)
endif()

if (NOT TARGET ShaderFilesGenerator)
  # when specifying an out-of-tree source, a binary directory must be explicitly specified
  add_subdirectory(${ShaderFilesGenerator_dir} ${CMAKE_BINARY_DIR}/ShaderFilesGenerator)
endif()

include(include/CMakeLists.txt)
include(src/CMakeLists.txt)

# all the relevant GLSL related headers also visible in VS
include(GLSL/CMakeLists.txt)
# all the relevant GL   related headers also visible in VS
include(../GL/CMakeLists.txt)

#########################################################################
##
## Defines the OpenGLRenderingEngine libraries.

add_library(OpenGLRenderingEngine
  ${OPENGL_RENDERING_ENGINE_SRC_CPP} ${OPENGL_RENDERING_ENGINE_HEADERS}
  ${SHADER_FILES_GENERATOR_SRC_CPP_OPENGL_RENDERING_ENGINE} ${SHADER_FILES_GENERATOR_HEADER_OPENGL_RENDERING_ENGINE}
  # all the relevant GL related headers in VS
  ${GL_ASSIMP_LIBRARY_INCLUDES}
  ${GL_DEVIL_LIBRARY_INCLUDES}
  ${GL_FREEGLUT_LIBRARY_INCLUDES}
  ${GL_GLEW_LIBRARY_INCLUDES}
  # all relevant GLSL Shader files below
  ${OPENGL_RENDERING_ENGINE_GLSL_COMMON_FUNCTIONS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_CUBE_CAPPING_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_FXAA_ANTIALIAS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_GBUFFER_EFFECTS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_ILLUMINATED_LINE_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_ORDER_INDEPEDENT_TRANSPARENCY_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_POINT_CYLINDER_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_POINT_SPHERE_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_POINT_SPHERE_TESS_QUAD_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_SURFACE_LIGHTING_LOD_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_SURFACE_LIGHTING_MODELS_SHADERS}
  ${OPENGL_RENDERING_ENGINE_GLSL_SHADERS}
  )

# pre_build process, run ShaderFilesGenerator executable, OpenGLRenderingEngine source code is dependent on this step
add_custom_command(TARGET OpenGLRenderingEngine PRE_BUILD COMMAND
                   ShaderFilesGenerator -path ${CMAKE_SOURCE_DIR}
                   COMMENT "run ShaderFilesGenerator executable before OpenGLRenderingEngine is built."
  )

target_link_libraries(OpenGLRenderingEngine SuperQuadrics OpenGL::GL OpenGL::GLU ${ASSIMP_LIBRARIES} ${DEVIL_LIBRARY_1} ${DEVIL_LIBRARY_2} ${DEVIL_LIBRARY_3} ${FREEGLUT_LIBRARIES} ${GLEW_LIBRARIES})
add_dependencies(OpenGLRenderingEngine ShaderFilesGenerator)

# for both internal build and parent build (ie being pulled in via add_subdirectory() rather than being a standalone build)
target_include_directories(
  OpenGLRenderingEngine PUBLIC
  ${ASSIMP_INCLUDE_HEADERS}
  ${DEVIL_INCLUDE_HEADERS}
  ${FREEGLUT_INCLUDE_HEADERS}
  ${GLEW_INCLUDE_HEADERS}
  ${OpenGLRenderingEngine_SOURCE_DIR}/include
  ${OpenGLRenderingEngine_SOURCE_DIR}/../ShaderFilesGenerator/include
  ${OpenGLRenderingEngine_SOURCE_DIR}/../SuperQuadrics/include
  )

install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)

if (WIN32)
  install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION .   COMPONENT distributable EXCLUDE_FROM_ALL)
elseif (UNIX)
  install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib COMPONENT distributable EXCLUDE_FROM_ALL)
endif()