| Support Forum Library Source SourceForge Page G3D Web Page |
Optional base class for quickly creating 3D applications. More...
Classes | |
| class | DebugShape |
| class | Settings |
Public Types | |
| enum | Action { ACTION_NONE, ACTION_QUIT, ACTION_SHOW_CONSOLE } |
Public Member Functions | |
| GApp (const Settings &options=Settings(), OSWindow *window=NULL) | |
| virtual | ~GApp () |
| virtual shared_ptr< Camera > | activeCamera () |
| The currently active camera for the primary view. | |
| virtual const shared_ptr< Camera > | activeCamera () const |
| virtual void | addWidget (const Widget::Ref &module, bool setFocus=true) |
| Installs a module. | |
| Manipulator::Ref | cameraManipulator () const |
| virtual shared_ptr< Camera > | debugCamera () |
| Exposes the debugging camera. | |
| RealTime | desiredFrameDuration () const |
| float | desiredFrameRate () const |
| void | drawDebugShapes () |
| Draw everything in debugShapeArray. | |
| void | drawMessage (const std::string &message) |
| Draw a simple, short message in the center of the screen and swap the buffers. | |
| const Stopwatch & | graphicsWatch () const |
| virtual void | loadScene (const std::string &sceneName) |
| Load a new scene. | |
| const Stopwatch & | logicWatch () const |
| bool | lowerFrameRateInBackground () const |
| const Stopwatch & | networkWatch () const |
| RealTime | realTime () const |
| Accumulated wall-clock time since init was called on this applet. | |
| void | removeAllDebugShapes () |
| Clears all debug shapes, regardless of their pending display time. | |
| void | removeDebugShape (DebugID id) |
| Clears just this debug shape (if it exists), regardless of its pending display time. | |
| virtual void | removeWidget (const Widget::Ref &module) |
| The actual removal of the module may be delayed until the next frame. | |
| virtual void | renderDebugInfo () |
| Called from GApplet::run immediately after doGraphics to render the debugging text. | |
| int | run () |
| Call this to run the app. | |
| virtual void | saveScene () |
| Save the current scene over the one on disk. | |
| virtual void | setActiveCamera (const shared_ptr< Camera > &camera) |
| void | setCameraManipulator (const Manipulator::Ref &man) |
| The manipulator that positions the debugCamera every frame. | |
| virtual void | setDesiredFrameRate (float fps) |
| Change to invoke frame limiting via onWait. | |
| virtual void | setExitCode (int code=0) |
| Invoke to true to end the program at the end of the next event loop. | |
| virtual void | setLowerFramerateInBackground (bool s) |
| If true, the desired frame rate is ignored when the OSWindow does not have focus and the program switches to running 4fps. | |
| virtual void | setRealTime (RealTime r) |
| virtual void | setSimTime (SimTime s) |
| virtual void | setSimTimeStep (float s) |
| shared_ptr< GuiWindow > | show (const shared_ptr< Texture > &t, const std::string &windowCaption="") |
| Displays the texture in a new GuiWindow. | |
| shared_ptr< GuiWindow > | show (const shared_ptr< PixelTransferBuffer > &t, const std::string &windowCaption="") |
| shared_ptr< GuiWindow > | show (const shared_ptr< Image > &t, const std::string &windowCaption="") |
| SimTime | simTime () const |
| In-simulation time since init was called on this applet. | |
| float | simTimeStep () const |
| Elapsed time per RENDERED frame for ideal simulation. | |
| const Stopwatch & | simulationWatch () const |
| const Stopwatch & | userInputWatch () const |
| void | vscreenPrintf (const char *fmt, va_list argPtr) G3D_CHECK_VPRINTF_METHOD_ARGS |
| const Stopwatch & | waitWatch () const |
| OSWindow * | window () const |
Static Public Member Functions | |
| static shared_ptr< class Lighting > | defaultLighting () |
Creates a default lighting environment for demos, which uses the file | |
Public Attributes | |
| bool | catchCommonExceptions |
| When true, there is an assertion failure if an exception is thrown. | |
| GConsoleRef | console |
| Command console. | |
| std::string | dataDir |
| Initialized to GApp::Settings::dataDir, or if that is "<AUTO>", to System::demoFindData(). | |
| shared_ptr< GFont > | debugFont |
| NULL if not loaded. | |
| GuiPane * | debugPane |
| debugWindow->pane() | |
| Array< DebugShape > | debugShapeArray |
| Shapes to be rendered each frame. | |
| GuiWindow::Ref | debugWindow |
| Add your own debugging controls to this window. | |
| DeveloperWindow::Ref | developerWindow |
| The window that displays buttons for debugging. | |
| Action | escapeKeyAction |
| When true an GKey::ESCAPE keydown event quits the program. | |
| bool | fastSwitchCamera |
| When true GKey::F2 keydown deactivates the camera and restores the mouse cursor. | |
| DebugID | m_lastDebugID |
| Last DebugShape::id issued. | |
| bool | manageUserInput |
| When true, the G3D::UserInput->beginEvents/endEvents processing is handled for you by calling processGEventQueue() before G3D::GApp::onUserInput is called. | |
| RenderDevice * | renderDevice |
| bool | showDebugText |
| When true, debugPrintf prints to the screen. | |
| bool | showRenderingStats |
| When true, renderDebugInfo prints the frame rate and other data to the screen. | |
| UserInput * | userInput |
Protected Member Functions | |
| CoordinateFrame | bookmark (const std::string &name, const CoordinateFrame &defaultValue=CoordinateFrame()) const |
| Shorthand for developerWindow->cameraControlWindow->bookmark(name) | |
| void | createDeveloperHUD (const shared_ptr< Scene > &scene=shared_ptr< Scene >()) |
| Call from onInit to create the developer HUD. | |
| virtual void | onAfterSimulation (RealTime rdt, SimTime sdt, SimTime idt) |
| Invoked after onSimulation is run on the installed GModules and GApp. | |
| virtual void | onAI () |
| Update any state you need to here. | |
| virtual void | onBeforeSimulation (RealTime &rdt, SimTime &sdt, SimTime &idt) |
| Invoked before onSimulation is run on the installed GModules and GApp. | |
| virtual void | onCleanup () |
| Unload/deallocate your data here. | |
| virtual void | onConsoleCommand (const std::string &cmd) |
| Invoked when a user presses enter in the in-game console. | |
| virtual bool | onEvent (const GEvent &event) |
| It is recommended to override onUserInput() instead of this method. | |
| virtual void | onGraphics (RenderDevice *rd, Array< shared_ptr< Surface > > &surface, Array< shared_ptr< Surface2D > > &surface2D) |
| Rendering callback used to paint the screen. | |
| virtual void | onGraphics2D (RenderDevice *rd, Array< shared_ptr< Surface2D > > &surface2D) |
| Called from the default onGraphics. | |
| virtual void | onGraphics3D (RenderDevice *rd, Array< shared_ptr< Surface > > &surface) |
| Called from the default onGraphics. | |
| virtual void | onInit () |
| Load your data here. | |
| virtual void | onNetwork () |
| For a networked app, override this to implement your network message polling. | |
| virtual void | onPose (Array< shared_ptr< Surface > > &posed3D, Array< shared_ptr< Surface2D > > &posed2D) |
| Called before onGraphics. | |
| virtual void | onSimulation (RealTime rdt, SimTime sdt, SimTime idt) |
| Override this with your simulation code. | |
| virtual void | onUserInput (class UserInput *userInput) |
| Routine for processing user input from the previous frame. | |
| virtual void | onWait (RealTime waitTime) |
| Task to be used for frame rate limiting. | |
| virtual void | processGEventQueue () |
| Processes all pending events on the OSWindow queue into the userInput. | |
| virtual void | renderCubeMap (RenderDevice *rd, Array< shared_ptr< Texture > > &output, const shared_ptr< Camera > &camera, shared_ptr< Texture > depthMap=shared_ptr< Texture >(), int resolution=1024) |
| Helper for generating cube maps. | |
| void | resize (int w, int h) |
| Change the size of the underlying Film. | |
Static Protected Member Functions | |
| static void | staticConsoleCallback (const std::string &command, void *me) |
Protected Attributes | |
| Array< std::string > | debugText |
| Strings that have been printed with screenPrintf. | |
| shared_ptr< Camera > | m_activeCamera |
| The currently selected camera. | |
| shared_ptr< class AmbientOcclusion > | m_ambientOcclusion |
| Default AO object for the primary view, allocated in GApp::GApp. | |
| shared_ptr< Manipulator > | m_cameraManipulator |
| Used to find the frame for defaultCamera. | |
| shared_ptr< Texture > | m_colorBuffer0 |
| Always bound to m_frameBuffer FrameBuffer::COLOR0. | |
| shared_ptr< Camera > | m_debugCamera |
| A camera that is driven by the debugController. | |
| shared_ptr < FirstPersonManipulator > | m_debugController |
| Allows first person (Quake game-style) control using the arrow keys or W,A,S,D and the mouse. | |
| Color4 | m_debugTextColor |
| GMutex | m_debugTextMutex |
| Color4 | m_debugTextOutlineColor |
| shared_ptr< Texture > | m_depthBuffer |
| Always bound to m_frameBuffer FrameBuffer::DEPTH. | |
| shared_ptr< DepthOfField > | m_depthOfField |
| shared_ptr< Renderbuffer > | m_depthRenderBuffer |
| Always bound to m_frameBuffer FrameBuffer::DEPTH. | |
| bool | m_endProgram |
| int | m_exitCode |
| shared_ptr< Film > | m_film |
| Allocated if GApp::Settings::FilmSettings::enabled was true when the constructor executed. | |
| shared_ptr< Framebuffer > | m_frameBuffer |
| Framebuffer used for rendering the 3D portion of the scene. | |
| shared_ptr< GBuffer > | m_gbuffer |
| NULL by default. | |
| Stopwatch | m_graphicsWatch |
| RealTime | m_lastFrameOverWait |
| Used to track how much onWait overshot its desired target during the previous frame. | |
| Stopwatch | m_logicWatch |
| shared_ptr< MotionBlur > | m_motionBlur |
| Stopwatch | m_networkWatch |
| Array< Surface2DRef > | m_posed2D |
| Array< shared_ptr< Surface > > | m_posed3D |
| Stopwatch | m_poseWatch |
| int | m_renderPeriod |
| onPose(), onGraphics(), and onWait() execute once ever m_renderPeriod simulation frames. | |
| Settings | m_settings |
| The original settings. | |
| Stopwatch | m_simulationWatch |
| Stopwatch | m_userInputWatch |
| Stopwatch | m_waitWatch |
| shared_ptr< WidgetManager > | m_widgetManager |
Friends | |
| class | DeveloperWindow |
| class | OSWindow |
| class | VideoRecordDialog |
Optional base class for quickly creating 3D applications.
GApp has several event handlers implemented as virtual methods. It invokes these in a cooperative, round-robin fashion. This avoids the need for threads in most applications. The methods are, in order of invocation from GApp::oneFrame:
The GApp::run method starts the main loop. It invokes GApp::onInit, runs the main loop until completion, and then invokes GApp::onCleanup.
onWait runs before onGraphics because the beginning of onGraphics causes the CPU to block, waiting for the GPU to complete the previous frame.
When you override a method, invoke the GApp version of that method to ensure that Widgets still work properly. This allows you to control whether your per-app operations occur before or after the Widget ones.
| enum G3D::GApp::Action |
| window | If null, a SDLWindow will be created for you. This argument is useful for substituting a different window system (e.g. GlutWindow) |
|
virtual |
|
inlinevirtual |
The currently active camera for the primary view.
|
inlinevirtual |
|
virtual |
Installs a module.
Actual insertion may be delayed until the next frame.
|
protected |
Shorthand for developerWindow->cameraControlWindow->bookmark(name)
|
inline |
|
protected |
Call from onInit to create the developer HUD.
| scene | May be NULL. |
|
inlinevirtual |
Exposes the debugging camera.
|
static |
Creates a default lighting environment for demos, which uses the file
on the noonclouds/noonclouds_*.jpg textures.
The code that it uses is below. Note that this loads a cube map every time that it is invoked, so this should not be used within the rendering loop.
|
inline |
|
inline |
| void G3D::GApp::drawDebugShapes | ( | ) |
Draw everything in debugShapeArray.
Subclasses should call from onGraphics3D() or onGraphics(). This will sort the debugShapeArray from back to front according to the current camera.
| void G3D::GApp::drawMessage | ( | const std::string & | message | ) |
Draw a simple, short message in the center of the screen and swap the buffers.
Useful for loading screens and other slow operations.
|
inline |
|
inlinevirtual |
Load a new scene.
A GApp may invoke this on itself, and the SceneEditorWindow will invoke this automatically when the user presses Reload or chooses a new scene from the GUI.
|
inline |
|
inline |
|
inline |
|
protectedvirtual |
Invoked after onSimulation is run on the installed GModules and GApp.
Not used by most programs.
|
protectedvirtual |
Update any state you need to here.
This is a good place for AI code, for example. Called after onNetwork and onUserInput, before onSimulation.
|
protectedvirtual |
Invoked before onSimulation is run on the installed GModules and GApp.
This is not used by most programs; it is primarily a hook for those performing extensive physical simulation on the GModules that need a setup and cleanup step.
If you mutate the timestep arguments then those mutated time steps are passed to the onSimulation method. However, the accumulated time will not be affected by the changed timestep.
|
protectedvirtual |
Unload/deallocate your data here.
Unlike the constructor, this catches common exceptions. It is called after the last frame is processed.
|
protectedvirtual |
Invoked when a user presses enter in the in-game console.
The default implementation ends the program if the command is "exit".
Sample implementation:
void App::onConsoleCommand(const std::string& str) {
Add console processing here
TextInput t(TextInput::FROM_STRING, str);
if (t.isValid() && (t.peek().type() == Token::SYMBOL)) {
std::string cmd = toLower(t.readSymbol());
if (cmd == "exit") {
setExitCode(0);
return;
} else if (cmd == "help") {
printConsoleHelp();
return;
}
Add commands here
} console->printf("Unknown command\n");
printConsoleHelp();
} void App::printConsoleHelp() {
console->printf("exit - Quit the program\n");
console->printf("help - Display this text\n\n");
console->printf("~/ESC - Open/Close console\n");
console->printf("F2 - Enable first-person camera control\n");
console->printf("F4 - Record video\n");
}
|
protectedvirtual |
It is recommended to override onUserInput() instead of this method.
Override if you need to explicitly handle events raw in the order they appear rather than once per frame by checking the current system state.
Note that the userInput contains a record of all keys pressed/held, mouse, and joystick state, so you do not have to override this method to handle basic input events.
Return true if the event has been consumed (i.e., no-one else including GApp should process it further).
The default implementation does nothing.
This runs after the m_widgetManager's onEvent, so a widget may consume events before the App sees them.
|
protectedvirtual |
Rendering callback used to paint the screen.
Called automatically. RenderDevice::beginFrame and endFrame are called for you before this is invoked.
The default implementation calls onGraphics2D and onGraphics3D.
|
protectedvirtual |
Called from the default onGraphics.
Override and implement.
|
protectedvirtual |
Called from the default onGraphics.
Override and implement. The RenderDevice will already be cleared and the default camera set when this method is invoked. By default, the Film's Framebuffer is bound and the output will be gamma corrected and bloomed. A common task is rendering UniversalSurface%s with your own shader, which can be done by:
|
protectedvirtual |
Load your data here.
Unlike the constructor, this catches common exceptions. It is called before the first frame is processed.
|
protectedvirtual |
For a networked app, override this to implement your network message polling.
|
protectedvirtual |
Called before onGraphics.
Append any models that you want rendered (you can also explicitly pose and render in your onGraphics method). The provided arrays will already contain posed models from any installed Widgets.
Override this with your simulation code.
Called from GApp::run.
Default implementation does nothing.
simTime(), idealSimTime() and realTime() are incremented after onSimulation is called, so at the beginning of call the current time is the end of the previous frame.
| rdt | Elapsed real-world time since the last call to onSimulation. |
| sdt | Elapsed sim-world time since the last call to onSimulation, computed by multiplying the wall-clock time by the simulation time rate. |
| idt | Elapsed ideal sim-world time. Use this for perfectly reproducible timing results. Ideal time always advances by the desiredFrameDuration * simTimeRate, no matter how much wall-clock time has elapsed. |
Routine for processing user input from the previous frame.
Default implementation does nothing.
Task to be used for frame rate limiting.
Overriding onWait is not recommended unless you have significant computation tasks that cannot be executed conveniently on a separate thread.
Frame rate limiting is useful to avoid overloading a maching that is running background tasks and for situations where fixed time steps are needed for simulation and there is no reason to render faster.
Default implementation System::sleep()s on waitTime (which is always non-negative)
|
protectedvirtual |
Processes all pending events on the OSWindow queue into the userInput.
This is automatically called once per frame. You can manually call it more frequently to get higher resolution mouse tracking or to prevent the OS from locking up (and potentially crashing) while in a lengthy onGraphics call.
|
inline |
Accumulated wall-clock time since init was called on this applet.
Since this time is accumulated, it may drift from the true wall-clock obtained by System::time().
| void G3D::GApp::removeAllDebugShapes | ( | ) |
Clears all debug shapes, regardless of their pending display time.
Clears just this debug shape (if it exists), regardless of its pending display time.
|
virtual |
The actual removal of the module may be delayed until the next frame.
|
protectedvirtual |
Helper for generating cube maps.
Invokes GApp::onGraphics3D six times, once for each face of a cube map. This is convenient both for microrendering and for generating cube maps to later use offline.
G3D::Film post-processing is not applied to the resulting images.
| output | If empty or the first element is NULL, this is set to a series of new 1024 x 1024 ImageFormat::RGB16F() textures. Otherwise, the provided elements are used. Textures are assumed to be square. The images are generated in G3D::CubeFace order. |
| camera | The field of view is changed to 90 degrees and the view is rotated in 90 degree increments, but other parameters from this camera are used unmodifed. The app->defaultCamera is temporarily set to each face's camera as well, and then restored. |
| depthMap | Optional pre-allocated depth texture to use as the depth map when rendering each face. Will be allocated to match the texture resolution if not provided. The default depth format is ImageFormat::DEPTH24(). |
Example:
|
virtual |
Called from GApplet::run immediately after doGraphics to render the debugging text.
Does nothing if debugMode is false. It is not usually necessary to override this method.
Change the size of the underlying Film.
Called by GApp::GApp() and GApp::onEvent(). This is not an event handler. If you want to be notified when your app is resized, override GApp::onEvent to handle the resize event (just don't forget to call GApp::onEvent as well)
| int G3D::GApp::run | ( | ) |
Call this to run the app.
|
inlinevirtual |
Save the current scene over the one on disk.
|
inline |
The manipulator that positions the debugCamera every frame.
By default, this is set to the debugController. This may be set to NULL to disable explicit camera positioning.
|
virtual |
Change to invoke frame limiting via onWait.
Defaults to finf()
Invoke to true to end the program at the end of the next event loop.
|
inlinevirtual |
If true, the desired frame rate is ignored when the OSWindow does not have focus and the program switches to running 4fps.
|
virtual |
| shared_ptr<GuiWindow> G3D::GApp::show | ( | const shared_ptr< Texture > & | t, |
| const std::string & | windowCaption = "" |
||
| ) |
Displays the texture in a new GuiWindow.
| shared_ptr<GuiWindow> G3D::GApp::show | ( | const shared_ptr< PixelTransferBuffer > & | t, |
| const std::string & | windowCaption = "" |
||
| ) |
| shared_ptr<GuiWindow> G3D::GApp::show | ( | const shared_ptr< Image > & | t, |
| const std::string & | windowCaption = "" |
||
| ) |
|
inline |
In-simulation time since init was called on this applet.
Takes into account simTimeSpeed. Automatically incremented after ooSimulation.
|
inline |
Elapsed time per RENDERED frame for ideal simulation.
Set to 0 to pause simulation, 1/fps to match real-time. The actual sdt argument to onSimulation is simTimStep / m_renderPeriod.
|
inline |
|
staticprotected |
|
inline |
| void G3D::GApp::vscreenPrintf | ( | const char * | fmt, |
| va_list | argPtr | ||
| ) |
|
inline |
|
inline |
|
friend |
|
friend |
|
friend |
| bool G3D::GApp::catchCommonExceptions |
When true, there is an assertion failure if an exception is thrown.
Default is true.
| GConsoleRef G3D::GApp::console |
Command console.
| std::string G3D::GApp::dataDir |
Initialized to GApp::Settings::dataDir, or if that is "<AUTO>", to System::demoFindData().
To make your program distributable, override the default and copy all data files you need to a local directory. Recommended setting is "data/" or "./", depending on where you put your data relative to the executable.
Your data directory must contain the default debugging font, "console-small.fnt", unless you change it.
| shared_ptr<GFont> G3D::GApp::debugFont |
NULL if not loaded.
| GuiPane* G3D::GApp::debugPane |
debugWindow->pane()
| Array<DebugShape> G3D::GApp::debugShapeArray |
Shapes to be rendered each frame.
Added to by G3D::debugDraw. Rendered by drawDebugShapes(); Automatically cleared once per frame.
|
protected |
Strings that have been printed with screenPrintf.
Protected by m_debugTextMutex.
| GuiWindow::Ref G3D::GApp::debugWindow |
Add your own debugging controls to this window.
| DeveloperWindow::Ref G3D::GApp::developerWindow |
The window that displays buttons for debugging.
If GApp::Settings::useDeveloperTools is true this will be created and added as a Widget on the GApp. Otherwise this will be NULL.
| Action G3D::GApp::escapeKeyAction |
When true an GKey::ESCAPE keydown event quits the program.
(default is true)
| bool G3D::GApp::fastSwitchCamera |
When true GKey::F2 keydown deactivates the camera and restores the mouse cursor.
(default is true). This works even if GApp::Settings::useDeveloperTools is false.
|
protected |
The currently selected camera.
Referenced by activeCamera().
|
protected |
Default AO object for the primary view, allocated in GApp::GApp.
|
protected |
Used to find the frame for defaultCamera.
Referenced by cameraManipulator(), and setCameraManipulator().
|
protected |
Always bound to m_frameBuffer FrameBuffer::COLOR0.
|
protected |
A camera that is driven by the debugController.
This is a copy of the default camera from the scene, but is not itself in the scene.
Do not reassign this–the CameraControlWindow is hardcoded to the original one.
Referenced by debugCamera().
|
protected |
Allows first person (Quake game-style) control using the arrow keys or W,A,S,D and the mouse.
To disable, use:
setCameraManipulator(shared_ptr<Manipulator>()); m_widgetManager->remove(m_debugController); m_debugController.reset();
|
protected |
|
protected |
|
protected |
|
protected |
Always bound to m_frameBuffer FrameBuffer::DEPTH.
If NULL, the preferred depth format may not be supported by a Texture. In that case, check m_depthRenderBuffer.
|
protected |
|
protected |
Always bound to m_frameBuffer FrameBuffer::DEPTH.
This is used if the preferred depth format is not supported for Textures.
|
protected |
|
protected |
|
protected |
Allocated if GApp::Settings::FilmSettings::enabled was true when the constructor executed.
Automatically resized by resize() when the screen size changes.
|
protected |
Framebuffer used for rendering the 3D portion of the scene.
|
protected |
NULL by default.
The DeveloperWindow assumes that this is the GBuffer to be visualized if it is non-NULL.
|
protected |
Referenced by graphicsWatch().
| DebugID G3D::GApp::m_lastDebugID |
Last DebugShape::id issued.
|
protected |
Used to track how much onWait overshot its desired target during the previous frame.
|
protected |
Referenced by logicWatch().
|
protected |
|
protected |
Referenced by networkWatch().
|
protected |
|
protected |
|
protected |
onPose(), onGraphics(), and onWait() execute once ever m_renderPeriod simulation frames.
This allows UI/network/simulation to be clocked much faster than rendering to increase responsiveness.
|
protected |
The original settings.
|
protected |
Referenced by simulationWatch().
|
protected |
Referenced by userInputWatch().
|
protected |
Referenced by waitWatch().
|
protected |
| bool G3D::GApp::manageUserInput |
When true, the G3D::UserInput->beginEvents/endEvents processing is handled for you by calling processGEventQueue() before G3D::GApp::onUserInput is called.
If you turn this off, you must call processGEventQueue() or provide your own event to userInput processing in onUserInput. (default is true)
| RenderDevice* G3D::GApp::renderDevice |
| bool G3D::GApp::showDebugText |
When true, debugPrintf prints to the screen.
(default is true)
| bool G3D::GApp::showRenderingStats |
When true, renderDebugInfo prints the frame rate and other data to the screen.
| UserInput* G3D::GApp::userInput |
1.8.2