Commit 77554841 authored by Nicolas Van der Noot's avatar Nicolas Van der Noot
Browse files

open_gl files added

parent aac9830c
......@@ -48,19 +48,44 @@ endif ( )
if (WIN32)
## --- WIN32 or WIN64 DETECTION --- ##
# avoid warnings
if(COMMAND cmake_policy)
cmake_policy(SET CMP0054 NEW)
endif()
# Visual studio version
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 18.0)
message("Visual Studio version set to VS2012 (${CMAKE_CXX_COMPILER_VERSION})")
set(MSVC_VERSION lib-vc2012)
elseif (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 19.0)
message("Visual Studio version set to VS2013 (${CMAKE_CXX_COMPILER_VERSION})")
set(MSVC_VERSION lib-vc2013)
else ( )
message("Visual Studio version set to VS2015 (${CMAKE_CXX_COMPILER_VERSION})")
set(MSVC_VERSION lib-vc2015)
endif ( )
else ( )
message(FATAL_ERROR "CMake error: only Visual Studio projects can currently be used on Windows !")
endif ( )
## --- WIN32 or WIN64 DETECTION --- ##
if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) # 64bit Windows
SET(WIN_LIB_DIRECTORY win64_include_lib)
else( ) # 32bit Windows
SET(WIN_LIB_DIRECTORY win32_include_lib)
endif( )
## ---- WINDOWS DLL FILES ---- ##
## ---- WINDOWS DLL FILES ---- ##
# copy all the dll (except 'jvm.dll') used for Windows
# these dll files are copied in the Executable directory (Debug or Release)
# copy all the dll (except 'jvm.dll') used for Windows
# these dll files are copied in the Executable directory (Debug or Release)
file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Release)
# GLFW
file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Release)
endif ( )
SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/Debug)
......
......@@ -41,10 +41,10 @@ function(mbsysc_specific_flags) # flag for lib_mbsysC only (not set by project)
cmake_dependent_option(FLAG_VISU "Enable 3D visualization (need Java or OpenGL)" ON "FLAG_REAL_TIME" OFF)
# use the Java library to display in real-time the animation
cmake_dependent_option(FLAG_JAVA "Enable 3D visualization (need Java)" ON "FLAG_VISU" OFF)
cmake_dependent_option(FLAG_JAVA "Enable 3D visualization (need Java)" OFF "FLAG_VISU" OFF)
# use the OpenGL library to display in real-time the animation
cmake_dependent_option(FLAG_OPEN_GL "Enable 3D visualization (need OpenGL)" OFF "FLAG_VISU" OFF)
cmake_dependent_option(FLAG_OPEN_GL "Enable 3D visualization (need OpenGL)" ON "FLAG_VISU" OFF)
endfunction()
......
......@@ -111,3 +111,23 @@ function(java_lib arg0)
set(JNI_LIBRARIES ${JNI_LIBRARIES} PARENT_SCOPE)
set(J3D_PATH ${J3D_PATH} PARENT_SCOPE)
endfunction()
# OpenGL
function(open_gl_lib arg0)
add_definitions( -DOPEN_GL )
find_package(Opengl REQUIRED)
if(OPEN_GL_FOUND)
include_directories (${OPEN_GL_INCLUDE_PATH})
message( "\n >> ${arg0}: OpenGL found")
else ( )
message( "\n >> ${arg0}: OpenGL not found")
endif ( )
message(" -> INC : ${OPEN_GL_INCLUDE_PATH}")
message(" -> LIB : ${OPEN_GL_LIBS}\n")
set(OPEN_GL_INCLUDE_PATH ${OPEN_GL_INCLUDE_PATH} PARENT_SCOPE)
set(OPEN_GL_LIBS ${OPEN_GL_LIBS} PARENT_SCOPE)
endfunction()
#
# author: Nicolas Van der Noot
#
# This file finds the include folders (header files) and the libraries
# related to OpenGL and the other required components
#
# OPEN_GL_FOUND : 1 if all required files found (0 otherwise)
# OPEN_GL_INCLUDE_PATH : include paths (for the header files) -> for compilation
# OPEN_GL_LIBS : libraries -> for linkage
#
if (UNIX)
if (APPLE)
## ---- MAC OS ---- ##
# possible paths for: 'gl.h'
set(TRIAL_PATHS_GL
/System/Library/Frameworks/OpenGL.framework/Versions/A
)
# possible paths for: 'glm.hpp'
set(TRIAL_PATHS_GLM
/usr/local/include/glm/
)
# possible paths for: 'glew.h'
set(TRIAL_PATHS_GLEW
/usr/local/include/GL
)
# possible paths for: 'glfw3.h'
set(TRIAL_PATHS_GLFW3
/usr/local/include/GLFW
)
# possible paths for: 'libGL.dylib'
set(TRIAL_PATHS_LIB_OPEN_GL
/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries
)
# possible paths for: 'libglfw3.a'
set(TRIAL_PATHS_LIB_GLFW
/usr/local/lib
)
# possible paths for: 'libGLEW.dylib'
set(TRIAL_PATHS_LIB_GLEW
/usr/local/lib
)
find_path(PATH_GL gl.h ${TRIAL_PATHS_GL} )
find_path(PATH_GLM glm.hpp ${TRIAL_PATHS_GLM} )
find_path(PATH_GLEW glew.h ${TRIAL_PATHS_GLEW} )
find_path(PATH_GLFW3 glfw3.h ${TRIAL_PATHS_GLFW3})
find_library(PATH_LIB_OPEN_GL libGL.dylib ${TRIAL_PATHS_LIB_OPEN_GL})
find_library(PATH_LIB_GLFW libglfw3.a ${TRIAL_PATHS_LIB_GLFW} )
find_library(PATH_LIB_GLEW libGLEW.dylib ${TRIAL_PATHS_LIB_GLEW} )
set(OPEN_GL_INCLUDE_PATH ${PATH_GL} ${PATH_GLM} ${PATH_GLEW} ${PATH_GLFW3})
set(OPEN_GL_LIBS ${PATH_LIB_OPEN_GL} ${PATH_LIB_GLFW} ${PATH_LIB_GLEW} "-framework Cocoa" "-framework OpenGL" "-framework CoreVideo" "-framework IOKit")
## ---------------- ##
else (APPLE)
## ---- LINUX ---- ##
# main OpenGL libraries
find_package(OpenGL REQUIRED)
# possible paths for: 'glm.hpp'
set(TRIAL_PATHS_GLM
/usr/include/glm
)
# possible paths for: 'glew.h'
set(TRIAL_PATHS_GLEW
/usr/include/GL
)
# possible paths for: 'glfw3.h'
set(TRIAL_PATHS_GLFW3
/usr/include/GLFW
/usr/local/include/GLFW
)
# possible paths for: 'libglfw.so'
set(TRIAL_PATHS_LIB_GLFW
/usr/lib/x86_64-linux-gnu
/usr/local/lib
)
# possible paths for: 'libGLEW.so'
set(TRIAL_PATHS_LIB_GLEW
/usr/lib/x86_64-linux-gnu
/usr/local/lib
)
find_path(PATH_GLM glm.hpp ${TRIAL_PATHS_GLM} )
find_path(PATH_GLEW glew.h ${TRIAL_PATHS_GLEW} )
find_path(PATH_GLFW3 glfw3.h ${TRIAL_PATHS_GLFW3})
find_library(PATH_LIB_GLFW libglfw.so ${TRIAL_PATHS_LIB_GLFW})
find_library(PATH_LIB_GLEW libGLEW.so ${TRIAL_PATHS_LIB_GLEW})
set(OPEN_GL_INCLUDE_PATH ${OPENGL_INCLUDE_DIR} ${PATH_GLM} ${PATH_GLEW} ${PATH_GLFW3})
set(OPEN_GL_LIBS ${OPENGL_LIBRARIES} ${PATH_LIB_GLFW} ${PATH_LIB_GLEW})
## --------------- ##
endif (APPLE)
else (UNIX)
## ---- WINDOWS ---- ##
# possible paths for: 'glm.hpp'
set(TRIAL_PATHS_GLM
${PROJECT_SOURCE_DIR}/lib/${WIN_LIB_DIRECTORY}/include/glm
)
# possible paths for: 'glew.h'
set(TRIAL_PATHS_GLEW
${PROJECT_SOURCE_DIR}/lib/${WIN_LIB_DIRECTORY}/include/GL
)
# possible paths for: 'glfw3.h'
set(TRIAL_PATHS_GLFW3
${PROJECT_SOURCE_DIR}/lib/${WIN_LIB_DIRECTORY}/include/GLFW
)
# possible paths for: 'OpenGL32.lib'
set(TRIAL_PATHS_LIB_OPEN_GL
C:/Program\ Files\ \(x86\)/Windows\ Kits/8.1/Lib/winv6.3/um/x64
C:/Program\ Files\ \(x86\)/Windows\ Kits/8.1/Lib/winv6.3/um/x86
)
# possible paths for: 'glfw3.lib'
set(TRIAL_PATHS_LIB_GLFW
${PROJECT_SOURCE_DIR}/lib/${WIN_LIB_DIRECTORY}/lib/GLFW/${MSVC_VERSION}
)
# possible paths for: 'glew32.lib'
set(TRIAL_PATHS_LIB_GLEW
${PROJECT_SOURCE_DIR}/lib/${WIN_LIB_DIRECTORY}/lib/GL
)
find_path(PATH_GLM glm.hpp ${TRIAL_PATHS_GLM} )
find_path(PATH_GLEW glew.h ${TRIAL_PATHS_GLEW} )
find_path(PATH_GLFW3 glfw3.h ${TRIAL_PATHS_GLFW3})
find_library(PATH_LIB_GLFW glfw3.lib ${TRIAL_PATHS_LIB_GLFW})
find_library(PATH_LIB_GLEW glew32.lib ${TRIAL_PATHS_LIB_GLEW})
find_library(PATH_LIB_OPEN_GL OpenGL32.lib ${TRIAL_PATHS_LIB_OPEN_GL})
set(OPEN_GL_INCLUDE_PATH ${PATH_GLM} ${PATH_GLEW} ${PATH_GLFW3})
set(OPEN_GL_LIBS ${PATH_LIB_OPEN_GL} ${PATH_LIB_GLFW} ${PATH_LIB_GLEW})
## ----------------- ##
endif (UNIX)
# flag put to 1 if all required files are found
if (OPEN_GL_INCLUDE_PATH AND OPEN_GL_LIBS)
set(OPEN_GL_FOUND 1)
else ( )
set(OPEN_GL_FOUND 0)
endif ( )
# outputs
mark_as_advanced(
OPEN_GL_FOUND
OPEN_GL_INCLUDE_PATH
OPEN_GL_LIBS
)
......@@ -50,7 +50,7 @@ if (FLAG_JAVA)
# find java and java 3d
java_lib(mbs_realtime)
set(JNI_LIBRARIES ${JNI_LIBRARIES} PARENT_SCOPE)
set (J3D_PATH ${J3D_PATH} PARENT_SCOPE)
set(J3D_PATH ${J3D_PATH} PARENT_SCOPE)
include_directories("./java")
increment_src( ${PROJECT_SOURCE_DIR}/java )
......@@ -71,6 +71,39 @@ endif ()
# Get OpenGL lib
if (FLAG_OPEN_GL)
# find Libxml2
xml_lib(mbs_realtime)
include_directories(${LIBXML2_INCLUDE_DIR})
# find OpenGL
open_gl_lib(mbs_realtime)
set(OPEN_GL_INCLUDE_PATH ${OPEN_GL_INCLUDE_PATH} PARENT_SCOPE)
set(OPEN_GL_LIBS ${OPEN_GL_LIBS} PARENT_SCOPE)
include_directories("./open_gl")
include_directories("./open_gl/anim_read")
include_directories("./open_gl/components")
include_directories("./open_gl/lights")
include_directories("./open_gl/lights/specific")
include_directories("./open_gl/loader")
include_directories("./open_gl/loader/manual/vrml")
include_directories("./open_gl/loader/obj")
include_directories("./open_gl/shaders")
include_directories("./open_gl/shapes")
include_directories("./open_gl/shapes/specific")
include_directories("./open_gl/viewpoint")
include_directories("./open_gl/viewpoint/specific")
include_directories("./open_gl/world")
increment_src( ${PROJECT_SOURCE_DIR}/open_gl )
set(OPEN_GL_FILES_PATH ${CMAKE_CURRENT_SOURCE_DIR}/open_gl/)
configure_file (
"./open_gl/conf/cmake_open_gl_config.h.in"
"${PROJECT_BINARY_DIR}/conf/cmake_open_gl_config.h"
)
include_directories ("${PROJECT_BINARY_DIR}/conf")
endif ()
# list include directories (to find headers)
......@@ -113,7 +146,7 @@ endif ( )
# OpenGl external library: linkage
if (FLAG_OPEN_GL)
target_link_libraries (${LIB_MBSYSC_REALTIME} ${OPEN_GL_LIBS})
endif ( )
set(LIB_MBSYSC_REALTIME_PATH ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
......
#include "AnimReader.hh"
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <cmath>
namespace OpenGLMbs{
inline int limit_rande(int x, int min, int max) { return (x <= min) ? min : ((x >= max) ? max : x); }
/*! \brief constructor
*/
AnimReader::AnimReader(const char* anim_filename)
{
int cur_i;
std::string cur_line, token;
std::ifstream f(anim_filename);
nb_q = NbQCompute(anim_filename);
while(std::getline(f, cur_line))
{
std::istringstream iss(cur_line);
cur_i = -1;
q_tab.push_back(new double[nb_q]);
while(std::getline(iss, token, ' '))
{
if (IsFloat(token))
{
// time
if (cur_i == -1)
{
t_vec.push_back(StringToFloat(token));
}
else
{
q_tab.back()[cur_i] = StringToFloat(token);
}
cur_i++;
}
}
if (cur_i != nb_q)
{
std::cout << "AnimReader error: number of joints not consistend (" << cur_i << " >< " << nb_q << ") !" << std::endl;
exit(EXIT_FAILURE);
}
}
f.close();
t_size = t_vec.size();
if (t_size <= 0)
{
std::cout << "AnimReader error: t_vec size (" << t_size << ") should be strictly positive !" << std::endl;
exit(EXIT_FAILURE);
}
if (t_size != q_tab.size())
{
std::cout << "AnimReader error: t_vec size (" << t_size << ") different from q_tab size (" << q_tab.size() << ") !" << std::endl;
exit(EXIT_FAILURE);
}
init_t = t_vec[0];
end_t = t_vec[t_size-1];
diff_t = end_t - init_t;
if (diff_t < 0.0)
{
std::cout << "AnimReader error: final time should be bigger than initial time !" << std::endl;
exit(EXIT_FAILURE);
}
}
/*! \brief destructor
*/
AnimReader::~AnimReader()
{
for(unsigned int i=0; i<q_tab.size(); i++)
{
delete[] q_tab[i];
}
}
bool AnimReader::IsFloat(std::string cur_str)
{
float f;
std::istringstream iss(cur_str);
iss >> std::noskipws >> f;
return !iss.fail();
}
float AnimReader::StringToFloat(std::string cur_str)
{
float f;
std::istringstream iss(cur_str);
iss >> std::noskipws >> f;
return f;
}
int AnimReader::NbQCompute(const char* anim_filename)
{
int cur_nb_q;
std::string first_line, token;
std::ifstream f(anim_filename);
std::getline(f, first_line);
std::istringstream iss(first_line);
cur_nb_q = -1;
while(std::getline(iss, token, ' '))
{
if (IsFloat(token))
{
cur_nb_q++;
}
}
f.close();
return cur_nb_q;
}
void AnimReader::FillQ(double target_t, int nb_q, double *q_vec)
{
int i0, i1;
double alpha, alpha_comp, diff_t_vec;
// safety
if (this->nb_q != nb_q)
{
std::cout << "AnimReader error: nb_q (" << nb_q << ") should be " << this->nb_q << " !" << std::endl;
exit(EXIT_FAILURE);
}
IndexTime(target_t, i0, i1);
diff_t_vec = t_vec[i1] - t_vec[i0];
if (i0 == i1 || diff_t <= 0.0)
{
for(int i=0; i<nb_q; i++)
{
q_vec[i] = q_tab[i0][i];
}
}
else
{
alpha = (target_t - t_vec[i0]) / diff_t_vec;
alpha = limit_rande(alpha, 0.0, 1.0);
alpha_comp = 1.0 - alpha;
for(int i=0; i<nb_q; i++)
{
q_vec[i] = alpha_comp * q_tab[i0][i] + alpha * q_tab[i1][i];
}
}
}
void AnimReader::IndexTime(double target_t, int &i0, int &i1)
{
int guess_i, cur_i;
if (diff_t <= 0.0)
{
i0 = 0;
i1 = 0;
return;
}
guess_i = round(((target_t - init_t) / diff_t) * t_size);
guess_i = limit_rande(guess_i, 0, t_size-1);
if (target_t < t_vec[guess_i])
{
if (guess_i > 0)
{
i0 = guess_i-1;
i1 = guess_i;
}
else
{
i0 = 0;
i1 = 0;
return;
}
cur_i = guess_i - 1;
while (cur_i > 0 && target_t < t_vec[cur_i])
{
cur_i--;
}
if (cur_i > 0 && target_t >= t_vec[cur_i])
{
i0 = cur_i;
i1 = cur_i+1;
}
else
{
i0 = 0;
i1 = 0;
return;
}
}
else
{
if (guess_i < t_size-1)
{
i0 = guess_i;
i1 = guess_i+1;
}
else
{
i0 = t_size-1;
i1 = t_size-1;
return;
}
cur_i = guess_i + 1;
while (cur_i < t_size-1 && target_t > t_vec[cur_i])
{
cur_i++;
}
if (cur_i < t_size-1 && target_t <= t_vec[cur_i])
{
i0 = cur_i;
i1 = cur_i+1;
}
else
{
i0 = t_size-1;
i1 = t_size-1;
return;
}
}
i0 = limit_rande(i0, 0, t_size-1);
i1 = limit_rande(i1, 0, t_size-1);
}
}
/*!
* \author Nicolas Van der Noot
* \file AnimReader.hh
* \brief AnimReader class
*/
#ifndef _ANIM_READER_HH_
#define _ANIM_READER_HH_
#include <string>
#include <vector>
namespace OpenGLMbs{