Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #include <vlCore/VisualizationLibrary.hpp> 00033 #include <vlCore/GlobalSettings.hpp> 00034 #include <vlCore/VLXRegistry.hpp> 00035 #include <vlCore/FileSystem.hpp> 00036 #include <vlCore/LoadWriterManager.hpp> 00037 #include <vlCore/Log.hpp> 00038 #include <vlCore/Say.hpp> 00039 #include <vlCore/Time.hpp> 00040 #include <vlCore/Quaternion.hpp> 00041 #include <vlCore/AABB.hpp> 00042 #include <vlCore/Sphere.hpp> 00043 #include <vlCore/version.hpp> 00044 #include <vlCore/MersenneTwister.hpp> 00045 #include <cassert> 00046 00047 using namespace vl; 00048 00049 #if defined(VL_IO_2D_JPG) 00050 #include "plugins/ioJPG.hpp" 00051 #endif 00052 #if defined(VL_IO_2D_PNG) 00053 #include "plugins/ioPNG.hpp" 00054 #endif 00055 #if defined(VL_IO_2D_TIFF) 00056 #include "plugins/ioTIFF.hpp" 00057 #endif 00058 #if defined(VL_IO_2D_TGA) 00059 #include "plugins/ioTGA.hpp" 00060 #endif 00061 #if defined(VL_IO_2D_DAT) 00062 #include "plugins/ioDAT.hpp" 00063 #endif 00064 #if defined(VL_IO_2D_DDS) 00065 #include "plugins/ioDDS.hpp" 00066 #endif 00067 #if defined(VL_IO_2D_BMP) 00068 #include "plugins/ioBMP.hpp" 00069 #endif 00070 #if defined(VL_IO_2D_DICOM) 00071 #include "plugins/ioDICOM.hpp" 00072 #endif 00073 00074 //------------------------------------------------------------------------------ 00075 VL_COMPILE_TIME_CHECK( sizeof(double) == 8 ) 00076 VL_COMPILE_TIME_CHECK( sizeof(float) == 4 ) 00077 VL_COMPILE_TIME_CHECK( sizeof(long long) == 8 ) 00078 VL_COMPILE_TIME_CHECK( sizeof(int) == 4 ) 00079 VL_COMPILE_TIME_CHECK( sizeof(short) == 2 ) 00080 VL_COMPILE_TIME_CHECK( sizeof(char) == 1 ) 00081 VL_COMPILE_TIME_CHECK( sizeof(wchar_t) >= 2 ) 00082 VL_COMPILE_TIME_CHECK( sizeof(vec2) == sizeof(real)*2 ) 00083 VL_COMPILE_TIME_CHECK( sizeof(vec3) == sizeof(real)*3 ) 00084 VL_COMPILE_TIME_CHECK( sizeof(vec4) == sizeof(real)*4 ) 00085 VL_COMPILE_TIME_CHECK( sizeof(mat2) == sizeof(real)*2*2 ) 00086 VL_COMPILE_TIME_CHECK( sizeof(mat3) == sizeof(real)*3*3 ) 00087 VL_COMPILE_TIME_CHECK( sizeof(mat4) == sizeof(real)*4*4 ) 00088 VL_COMPILE_TIME_CHECK( sizeof(quat) == sizeof(real)*4 ) 00089 VL_COMPILE_TIME_CHECK( sizeof(AABB) == sizeof(real)*6 ) 00090 VL_COMPILE_TIME_CHECK( sizeof(Sphere) == sizeof(real)*4 ) 00091 //------------------------------------------------------------------------------ 00092 // VL misc 00093 //------------------------------------------------------------------------------ 00094 namespace 00095 { 00096 std::string gVersionString = String( Say("%n.%n.%s") << VL_Major << VL_Minor << VL_Patch ).toStdString(); 00097 bool gInitializedCore = false; 00098 }; 00099 //------------------------------------------------------------------------------ 00100 bool VisualizationLibrary::isCoreInitialized() { return gInitializedCore; } 00101 //------------------------------------------------------------------------------ 00102 const char* VisualizationLibrary::versionString() { return gVersionString.c_str(); } 00103 //------------------------------------------------------------------------------ 00104 // Global GlobalSettings 00105 //------------------------------------------------------------------------------ 00106 namespace 00107 { 00108 ref<GlobalSettings> gSettings = NULL; 00109 } 00110 GlobalSettings* vl::globalSettings() 00111 { 00112 return gSettings.get(); 00113 } 00114 //------------------------------------------------------------------------------ 00115 // Default logger 00116 //------------------------------------------------------------------------------ 00117 namespace 00118 { 00119 ref<Log> gDefaultLogger; 00120 } 00121 void vl::setDefLogger(Log* logger) 00122 { 00123 gDefaultLogger = logger; 00124 } 00125 Log* vl::defLogger() 00126 { 00127 return gDefaultLogger.get(); 00128 } 00129 //------------------------------------------------------------------------------ 00130 // Default LoadWriterManager 00131 //------------------------------------------------------------------------------ 00132 namespace 00133 { 00134 ref<LoadWriterManager> gDefaultLoadWriterManager = NULL; 00135 } 00136 LoadWriterManager* vl::defLoadWriterManager() 00137 { 00138 return gDefaultLoadWriterManager.get(); 00139 } 00140 void vl::setDefLoadWriterManager(LoadWriterManager* lwm) 00141 { 00142 gDefaultLoadWriterManager = lwm; 00143 } 00144 //----------------------------------------------------------------------------- 00145 // Default FileSystem 00146 //----------------------------------------------------------------------------- 00147 namespace 00148 { 00149 ref<FileSystem> gDefaultFileSystem = NULL;; 00150 } 00151 FileSystem* vl::defFileSystem() 00152 { 00153 return gDefaultFileSystem.get(); 00154 } 00155 void vl::setDefFileSystem(FileSystem* fs) 00156 { 00157 gDefaultFileSystem = fs; 00158 } 00159 //----------------------------------------------------------------------------- 00160 // Default VLXRegistry 00161 //----------------------------------------------------------------------------- 00162 namespace 00163 { 00164 ref<VLXRegistry> gDefaultVLXRegistry = NULL; 00165 } 00166 VLXRegistry* vl::defVLXRegistry() 00167 { 00168 return gDefaultVLXRegistry.get(); 00169 } 00170 void vl::setDefVLXRegistry(VLXRegistry* reg) 00171 { 00172 gDefaultVLXRegistry = reg; 00173 } 00174 //----------------------------------------------------------------------------- 00175 // Default MersenneTwister 00176 //----------------------------------------------------------------------------- 00177 namespace 00178 { 00179 ref<MersenneTwister> gDefaultMersenneTwister = NULL; 00180 } 00181 MersenneTwister* vl::defMersenneTwister() 00182 { 00183 return gDefaultMersenneTwister.get(); 00184 } 00185 void vl::setDefMersenneTwister(MersenneTwister* reg) 00186 { 00187 gDefaultMersenneTwister= reg; 00188 } 00189 //------------------------------------------------------------------------------ 00190 void VisualizationLibrary::initCore(bool log_info) 00191 { 00192 VL_CHECK(!gInitializedCore); 00193 if (gInitializedCore) 00194 { 00195 Log::bug("VisualizationLibrary::initCore(): Visualization Library Core is already initialized!\n"); 00196 return; 00197 } 00198 00199 // --- Init Core --- 00200 00201 // Install globabl settings 00202 gSettings = new GlobalSettings; 00203 00204 // Install default logger 00205 ref<StandardLog> logger = new StandardLog; 00206 logger->setLogFile( globalSettings()->defaultLogPath() ); 00207 setDefLogger( logger.get() ); 00208 00209 // Install default LoadWriterManager 00210 gDefaultLoadWriterManager = new LoadWriterManager; 00211 00212 // Install default FileSystem 00213 gDefaultFileSystem = new FileSystem; 00214 gDefaultFileSystem->directories().push_back( new DiskDirectory( globalSettings()->defaultDataPath() ) ); 00215 00216 // Install default VLXRegistry 00217 gDefaultVLXRegistry = new VLXRegistry; 00218 00219 // Install default MersenneTwister (seed done automatically) 00220 gDefaultMersenneTwister = new MersenneTwister; 00221 00222 // Register 2D modules 00223 #if defined(VL_IO_2D_JPG) 00224 registerLoadWriter(new LoadWriterJPG); 00225 #endif 00226 #if defined(VL_IO_2D_PNG) 00227 registerLoadWriter(new LoadWriterPNG); 00228 #endif 00229 #if defined(VL_IO_2D_TIFF) 00230 registerLoadWriter(new LoadWriterTIFF); 00231 #endif 00232 #if defined(VL_IO_2D_TGA) 00233 registerLoadWriter(new LoadWriterTGA); 00234 #endif 00235 #if defined(VL_IO_2D_BMP) 00236 registerLoadWriter(new LoadWriterBMP); 00237 #endif 00238 #if defined(VL_IO_2D_DDS) 00239 registerLoadWriter(new LoadWriterDDS); 00240 #endif 00241 #if defined(VL_IO_2D_DAT) 00242 registerLoadWriter(new LoadWriterDAT); 00243 #endif 00244 #if defined(VL_IO_2D_DICOM) 00245 registerLoadWriter(new LoadWriterDICOM); 00246 #endif 00247 00248 // Log VL and system information. 00249 if (globalSettings()->verbosityLevel() && log_info) 00250 Log::logSystemInfo(); 00251 00252 // Initialized = on 00253 gInitializedCore = true; 00254 } 00255 //------------------------------------------------------------------------------ 00256 void VisualizationLibrary::shutdownCore() 00257 { 00258 // Initialized = off 00259 gInitializedCore = false; 00260 00261 // --- Dispose Core --- 00262 00263 // Dispose default MersenneTwister 00264 gDefaultMersenneTwister = NULL; 00265 00266 // Dispose default VLXRegistry 00267 gDefaultVLXRegistry = NULL; 00268 00269 // Dispose default LoadWriterManager 00270 gDefaultLoadWriterManager->loadCallbacks().clear(); 00271 gDefaultLoadWriterManager->writeCallbacks().clear(); 00272 gDefaultLoadWriterManager->loadWriters().clear(); 00273 gDefaultLoadWriterManager = NULL; 00274 00275 // Dispose default FileSystem 00276 gDefaultFileSystem->directories().clear(); 00277 gDefaultFileSystem = NULL; 00278 00279 // Dispose default logger 00280 if (globalSettings()->verbosityLevel()) 00281 { 00282 Log::print("Visualization Library shutdown.\n"); 00283 } 00284 // we keep the logger alive as much as we can. 00285 // setDefLogger( NULL ); 00286 00287 // keep global settings (used by logger) 00288 // gSettings = NULL; 00289 } 00290 //------------------------------------------------------------------------------ 00291 void vl::abort_vl() 00292 { 00293 vl::Time::sleep(3000); 00294 exit(1); 00295 } 00296 //------------------------------------------------------------------------------ 00297 #if defined(VL_PLATFORM_WINDOWS) 00298 // console includes 00299 #include <io.h> 00300 #include <fcntl.h> 00301 #endif 00302 00303 void vl::showWin32Console() 00304 { 00305 #if defined(VL_PLATFORM_WINDOWS) 00306 AllocConsole(); 00307 // stdout 00308 HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE); 00309 int hCrt = _open_osfhandle((intptr_t)handle_out, _O_TEXT); 00310 FILE* hf_out = _fdopen(hCrt, "w"); 00311 setvbuf(hf_out, NULL, _IONBF, 1); 00312 *stdout = *hf_out; 00313 // stderr 00314 handle_out = GetStdHandle(STD_ERROR_HANDLE); 00315 hCrt = _open_osfhandle((intptr_t)handle_out, _O_TEXT); 00316 hf_out = _fdopen(hCrt, "w"); 00317 setvbuf(hf_out, NULL, _IONBF, 1); 00318 *stderr = *hf_out; 00319 #endif 00320 } 00321 //------------------------------------------------------------------------------