Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Applet.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGraphics/Applet.hpp>
33 #include <vlGraphics/Rendering.hpp>
37 #include <vlCore/Time.hpp>
38 #include <vlCore/Say.hpp>
39 #include <vlCore/Log.hpp>
40 
41 using namespace vl;
42 
43 //-----------------------------------------------------------------------------
45 {
46  VL_DEBUG_SET_OBJECT_NAME()
47  mFrameCount = 0;
49  mFPS = 0;
50  mReadPixels = new ReadPixels;
51  mAppletName = "AppletNoName";
52 }
53 //-----------------------------------------------------------------------------
55 {
56  // if the user didn't provide one use the one installed by default
58  setRendering(rend.get());
59 
60  // installs a SceneManagerActorTree as the default scene manager
62  rend->sceneManagers()->push_back(sceneManager());
63 
64  // render target attached externally
65  // ...
66 
69  mFly->setEnabled(false);
70  mTrackball->setEnabled(true);
71 
72  bindManipulators( rend->camera() );
73 }
74 //-----------------------------------------------------------------------------
76 {
77  // FPS counter
78  if (Time::currentTime() - mStartTime > 0.500f)
79  {
80  double secs = (Time::currentTime() - mStartTime);
81  mFPS = mFrameCount / secs;
82  mFrameCount = 0;
84  }
85  mFrameCount++;
86 
87  // update the scene content
88  updateScene();
89 
90  // set frame time for all the rendering
91  real now_time = Time::currentTime();
92  rendering()->setFrameClock( now_time );
93 
94  // execute rendering
95  rendering()->render();
96 
97  // show rendering
98  if ( openglContext()->hasDoubleBuffer() )
100 
101  VL_CHECK_OGL();
102 
103  // useful for debugging
104  // wglMakeCurrent(NULL,NULL);
105 }
106 //-----------------------------------------------------------------------------
107 void Applet::keyReleaseEvent(unsigned short, EKey key)
108 {
109  if (key == Key_Escape)
111  else
112  if (key == Key_T)
113  {
114  mFly->setEnabled(false);
115  mTrackball->setEnabled(true);
116  }
117  else
118  if (key == Key_F)
119  {
120  mTrackball->setEnabled(false);
121  mFly->setEnabled(true);
122  }
123  else
124  if (key == Key_F1)
125  openglContext()->setFullscreen( !openglContext()->fullscreen() );
126  else
127  if (key == Key_F5)
128  {
129  mReadPixels->setup( 0, 0, openglContext()->width(), openglContext()->height(), RDB_BACK_LEFT, false );
130  rendering()->onFinishedCallbacks()->push_back( mReadPixels.get() );
131  mReadPixels->setRemoveAfterCall(true);
132  Time time;
133  String filename = Say( appletName() + " - %n%02n%02n%02n%02n.png") << time.year() << time.month() << time.dayOfMonth() << time.hour() << time.second();
134  mReadPixels->setSavePath( filename );
135  Log::print( Say("Saved screenshot: '%s'\n") << filename );
136  openglContext()->update();
137  }
138  else
139  if (key == Key_U)
140  openglContext()->update();
141  else
142  if (key == Key_C)
143  openglContext()->setContinuousUpdate( !openglContext()->continuousUpdate() );
144 }
145 //-----------------------------------------------------------------------------
146 void Applet::resizeEvent(int w, int h)
147 {
148  // if a simple Rendering is attached as the rendering root than update viewport and projection matrix.
149  Rendering* rend = cast<Rendering>(rendering());
150  if (rend)
151  {
152  VL_CHECK( w == rend->renderer()->framebuffer()->width() );
153  VL_CHECK( h == rend->renderer()->framebuffer()->height() );
154  rend->camera()->viewport()->set( 0, 0, w, h );
155  rend->camera()->setProjectionPerspective();
156  }
157 }
158 //-----------------------------------------------------------------------------
160 {
161  mFly->setCamera( camera );
162  mTrackball->setCamera( camera );
163  mTrackball->setTransform( NULL );
164  mTrackball->setPivot( vec3(0,0,0) );
165 }
166 //-----------------------------------------------------------------------------
168 {
169  VL_CHECK(ogl_context)
170  VL_CHECK(mFly->openglContext() == NULL);
171  ogl_context->addEventListener( mFly.get() );
172 
173  VL_CHECK(mTrackball->openglContext() == NULL);
174  mTrackball->setPivot( vec3(0,0,0) );
175  ogl_context->addEventListener( mTrackball.get() );
176 }
177 //-----------------------------------------------------------------------------
179 {
180  if ( mTrackball ) {
181  ogl_context->removeEventListener( mTrackball.get() );
182  }
183 
184  if ( mFly ) {
185  ogl_context->removeEventListener( mFly.get() );
186  }
187 
188 }
189 //-----------------------------------------------------------------------------
191 {
192  mFly->setCamera(NULL);
193  mTrackball->setCamera(NULL);
194  mTrackball->setTransform(NULL);
195  mRendering = NULL;
199  mFly = NULL;
200  mTrackball = NULL;
201  mReadPixels = NULL;
202 }
203 //-----------------------------------------------------------------------------
virtual void destroyEvent()
Event generated right before the bound OpenGLContext is destroyed.
Definition: Applet.cpp:190
Collection< RenderEventCallback > * onFinishedCallbacks()
Returns the list of RenderEventCallback objects registered to the onRenderingFinished() event notific...
virtual void addedListenerEvent(OpenGLContext *openglContext)
Event generated whenever a listener is bound to an OpenGLContext context.
Definition: Applet.cpp:167
int second() const
Definition: Time.hpp:68
const T * get() const
Definition: Object.hpp:128
const Framebuffer * framebuffer() const
The Framebuffer on which the rendering is performed.
Definition: Renderer.hpp:99
A simple String formatting class.
Definition: Say.hpp:124
int year() const
Definition: Time.hpp:56
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
SceneManagerActorTree * sceneManager()
The scene manager used by the default rendering.
Definition: Applet.hpp:118
void set(int x, int y, int w, int h)
Definition: Viewport.hpp:63
double mFPS
Definition: Applet.hpp:165
int width() const
The width of a render target.
Definition: Framebuffer.hpp:72
This class lets you rotate a Camera or a Transform node using a virtual trackball.
void setProjectionPerspective()
Builds a perspective projection matrix for the Camera based on the Camera&#39;s and Viewport&#39;s settings...
Definition: Camera.cpp:198
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
void addEventListener(UIEventListener *el)
Adds an UIEventListener to be notified of OpenGLContext related events.
ref< SceneManagerActorTree > mSceneManagerActorTree
Definition: Applet.hpp:161
int dayOfMonth() const
Definition: Time.hpp:62
virtual void quitApplication()
Asks to the windowing system that is managing the OpenGLContext to quit the application.
const Camera * camera() const
The Camera that defines the point of view and viewport to be used when rendering the scene...
Definition: Rendering.hpp:131
void setFrameClock(real cur_time)
The update time of the current rendering frame.
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:141
Visualization Library main namespace.
virtual void updateEvent()
Event generated when the bound OpenGLContext does not have any other message to process and OpenGLCon...
Definition: Applet.cpp:75
void setRendering(RenderingAbstract *rendering)
Sets the rendering used by the Applet, by default a Rendering.
Definition: Applet.hpp:115
virtual void removedListenerEvent(OpenGLContext *)
Event generated whenever a listener is unbound from an OpenGLContext context.
Definition: Applet.cpp:178
virtual void keyReleaseEvent(unsigned short, EKey key)
Event generated when a key is released.
Definition: Applet.cpp:107
virtual void setContinuousUpdate(bool continuous)
If the OpenGL context is a widget this function sets whether its area is continuously updated at each...
Applet()
Constructor.
Definition: Applet.cpp:44
Simple class to be used as a timer and to retrieve the current time and date.
Definition: Time.hpp:49
String mAppletName
Definition: Applet.hpp:163
int height() const
The height of a render target.
Definition: Framebuffer.hpp:75
int mFrameCount
Definition: Applet.hpp:166
A RenderEventCallback that copyes a rectangular pixel area from a source buffer to an Image at the en...
Definition: ReadPixels.hpp:152
static void print(const String &message)
Application message for the user.
Definition: Log.cpp:136
RenderingAbstract * rendering()
The rendering used by the Applet, by default a Rendering.
Definition: Applet.hpp:109
virtual void render()=0
Executes the rendering.
T * as()
Casts an Object to the specified class.
Definition: Object.hpp:282
int month() const
Definition: Time.hpp:58
The GhostCameraManipulator class is an UIEventListener that controls the position and orientation of ...
#define NULL
Definition: OpenGLDefs.hpp:81
const Renderer * renderer() const
Utility function: returns the first renderer installed or NULL if none is found.
Definition: Rendering.hpp:110
The Rendering class collects all the information to perform the rendering of a scene.
Definition: Rendering.hpp:69
int hour() const
Definition: Time.hpp:64
void initialize()
Initializes the default rendering (with Rendering), the default scene manager (with SceneManagerActor...
Definition: Applet.cpp:54
const String & appletName() const
The applet name, used for the window title and for naming screenshots.
Definition: Applet.hpp:152
double mStartTime
Definition: Applet.hpp:164
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
ref< RenderingAbstract > mRendering
Definition: Applet.hpp:158
ref< ReadPixels > mReadPixels
Definition: Applet.hpp:162
static real currentTime()
Seconds passed from an arbitrary origin QueryPerformanceFrequency should be called only once in the a...
Definition: Time.cpp:114
ref< TrackballManipulator > mTrackball
Definition: Applet.hpp:160
virtual void swapBuffers()=0
Swaps the back and front buffers to present the last rendering.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual void updateScene()
Override this to update the content of your scene.
Definition: Applet.hpp:146
OpenGLContext * openglContext()
Returns the OpenGLContext to which this UIEventListener is bound or NULL if no context is bound...
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
void removeEventListener(UIEventListener *el)
Removes an UIEventListener.
void bindManipulators(Camera *camera)
Definition: Applet.cpp:159
ref< GhostCameraManipulator > mFly
Definition: Applet.hpp:159
#define VL_CHECK(expr)
Definition: checks.hpp:73
Collection< SceneManager > * sceneManagers()
Returns the list of SceneManager[s] containing the Actor[s] to be rendered.
Definition: Rendering.hpp:137
virtual void resizeEvent(int, int)
Event generated when the bound OpenGLContext is resized.
Definition: Applet.cpp:146
virtual void update()=0
If the OpenGLContext is a widget this function requests a redraw and generates an updateEvent()...
A SceneManagerBVH that implements its spatial partitioning strategy using an ActorTree.
virtual bool setFullscreen(bool)
If the OpenGL context is a widget this function requests a maximization to fullscreen.