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-2011, 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 <vlGraphics/Applet.hpp> 00033 #include <vlGraphics/Rendering.hpp> 00034 #include <vlCore/VisualizationLibrary.hpp> 00035 #include <vlGraphics/SceneManager.hpp> 00036 #include <vlGraphics/RenderQueue.hpp> 00037 #include <vlCore/Time.hpp> 00038 #include <vlCore/Say.hpp> 00039 #include <vlCore/Log.hpp> 00040 00041 using namespace vl; 00042 00043 //----------------------------------------------------------------------------- 00044 Applet::Applet() 00045 { 00046 VL_DEBUG_SET_OBJECT_NAME() 00047 mFrameCount = 0; 00048 mStartTime = Time::currentTime(); 00049 mFPS = 0; 00050 mReadPixels = new ReadPixels; 00051 mAppletName = "AppletNoName"; 00052 } 00053 //----------------------------------------------------------------------------- 00054 void Applet::initialize() 00055 { 00056 // if the user didn't provide one use the one installed by default 00057 ref<Rendering> rend = rendering() && rendering()->as<Rendering>() ? rendering()->as<Rendering>() : new Rendering; 00058 setRendering(rend.get()); 00059 00060 // installs a SceneManagerActorTree as the default scene manager 00061 mSceneManagerActorTree = new SceneManagerActorTree; 00062 rend->sceneManagers()->push_back(sceneManager()); 00063 00064 // render target attached externally 00065 // ... 00066 00067 mFly = new GhostCameraManipulator; 00068 mTrackball = new TrackballManipulator; 00069 mFly->setEnabled(false); 00070 mTrackball->setEnabled(true); 00071 00072 bindManipulators( rend->camera() ); 00073 } 00074 //----------------------------------------------------------------------------- 00075 void Applet::updateEvent() 00076 { 00077 // FPS counter 00078 if (Time::currentTime() - mStartTime > 0.500f) 00079 { 00080 double secs = (Time::currentTime() - mStartTime); 00081 mFPS = mFrameCount / secs; 00082 mFrameCount = 0; 00083 mStartTime = Time::currentTime(); 00084 } 00085 mFrameCount++; 00086 00087 // update the scene content 00088 updateScene(); 00089 00090 // set frame time for all the rendering 00091 real now_time = Time::currentTime(); 00092 rendering()->setFrameClock( now_time ); 00093 00094 // execute rendering 00095 rendering()->render(); 00096 00097 // show rendering 00098 if ( openglContext()->hasDoubleBuffer() ) 00099 openglContext()->swapBuffers(); 00100 00101 VL_CHECK_OGL(); 00102 00103 // useful for debugging 00104 // wglMakeCurrent(NULL,NULL); 00105 } 00106 //----------------------------------------------------------------------------- 00107 void Applet::keyReleaseEvent(unsigned short, EKey key) 00108 { 00109 if (key == Key_Escape) 00110 openglContext()->quitApplication(); 00111 else 00112 if (key == Key_T) 00113 { 00114 mFly->setEnabled(false); 00115 mTrackball->setEnabled(true); 00116 } 00117 else 00118 if (key == Key_F) 00119 { 00120 mTrackball->setEnabled(false); 00121 mFly->setEnabled(true); 00122 } 00123 else 00124 if (key == Key_F1) 00125 openglContext()->setFullscreen( !openglContext()->fullscreen() ); 00126 else 00127 if (key == Key_F5) 00128 { 00129 mReadPixels->setup( 0, 0, openglContext()->width(), openglContext()->height(), RDB_BACK_LEFT, false ); 00130 rendering()->onFinishedCallbacks()->push_back( mReadPixels.get() ); 00131 mReadPixels->setRemoveAfterCall(true); 00132 Time time; 00133 String filename = Say( appletName() + " - %n%02n%02n%02n%02n.png") << time.year() << time.month() << time.dayOfMonth() << time.hour() << time.second(); 00134 mReadPixels->setSavePath( filename ); 00135 Log::print( Say("Saved screenshot: '%s'\n") << filename ); 00136 openglContext()->update(); 00137 } 00138 else 00139 if (key == Key_U) 00140 openglContext()->update(); 00141 else 00142 if (key == Key_C) 00143 openglContext()->setContinuousUpdate( !openglContext()->continuousUpdate() ); 00144 } 00145 //----------------------------------------------------------------------------- 00146 void Applet::resizeEvent(int w, int h) 00147 { 00148 // if a simple Rendering is attached as the rendering root than update viewport and projection matrix. 00149 Rendering* rend = cast<Rendering>(rendering()); 00150 if (rend) 00151 { 00152 VL_CHECK( w == rend->renderer()->framebuffer()->width() ); 00153 VL_CHECK( h == rend->renderer()->framebuffer()->height() ); 00154 rend->camera()->viewport()->setWidth( w ); 00155 rend->camera()->viewport()->setHeight( h ); 00156 rend->camera()->setProjectionPerspective(); 00157 } 00158 } 00159 //----------------------------------------------------------------------------- 00160 void Applet::bindManipulators(Camera* camera) 00161 { 00162 mFly->setCamera( camera ); 00163 mTrackball->setCamera( camera ); 00164 mTrackball->setTransform( NULL ); 00165 mTrackball->setPivot( vec3(0,0,0) ); 00166 } 00167 //----------------------------------------------------------------------------- 00168 void Applet::addedListenerEvent(OpenGLContext* ogl_context) 00169 { 00170 VL_CHECK(ogl_context) 00171 VL_CHECK(mFly->openglContext() == NULL); 00172 ogl_context->addEventListener( mFly.get() ); 00173 00174 VL_CHECK(mTrackball->openglContext() == NULL); 00175 mTrackball->setPivot( vec3(0,0,0) ); 00176 ogl_context->addEventListener( mTrackball.get() ); 00177 } 00178 //----------------------------------------------------------------------------- 00179 void Applet::removedListenerEvent(OpenGLContext* ogl_context) 00180 { 00181 ogl_context->removeEventListener( mTrackball.get() ); 00182 ogl_context->removeEventListener( mFly.get() ); 00183 } 00184 //----------------------------------------------------------------------------- 00185 void Applet::destroyEvent() 00186 { 00187 mFly->setCamera(NULL); 00188 mTrackball->setCamera(NULL); 00189 mTrackball->setTransform(NULL); 00190 mRendering = NULL; 00191 } 00192 //-----------------------------------------------------------------------------