Overview
This example shows how to embed a VR-Vantage window in an MFC application.
Expected Result
example MFC Result
Example details
All MFC applications create a class that derives from CWinApp, and overrides its InitInstance() and Run() methods as well as its virtual destructor to initialize, run, and tear down the application.
Declare ExampleMFCApp, which derives from CWinApp:
class ExampleMFCApp : public CWinApp
The first CWinApp method overridden is InitInstance():
virtual BOOL InitInstance()
It creates tbe VR-Vantage display engine:
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
It then calls a helper function, initMfc(), which registers the window creators:
InitInstance() then registers the OSG plugin:
DT_DLL_VRVOSG void init(DtDe &anIG, bool initOpenGL=true)
It creates the main window by calling the helper function createMainWindow():
It initializes the display engine with a display engine initializer created by makeDeInitializer() (see below):
myDe->setDeInitializer(initializer);
myDe->initialize();
InitInstance() the creates a DtTimedSection to track frame time during the Tick() function (see below):
myTimer = new DtTimedSection;
And finally, calls and returns the result of the base class InitInstance() method:
return CWinApp::InitInstance();
The other CWinApp method overridden is Run():
Run() needs to manage Windows messages. If a message exists, it calls PumpMessage(); and if PumpMessage() fails, the instance is terminated:
_AFX_THREAD_STATE* pState = AfxGetThreadState();
for(;;)
{
if (PeekMessage(&(pState->m_msgCur) , NULL , 0, 0, PM_NOREMOVE))
{
if (!PumpMessage())
{
return ExitInstance();
It calls the Tick() method when there are no Windows messages to process:
else
{
ExampleMFCApp::Tick();
}
ExampleMFCApp also overrides CWinApp's virtual destructor so that it can delete the objects created at initialization:
virtual ~ExampleMFCApp()
{
delete myDe;
delete myTimer;
}
The following helper methods are defined (and called from InitInstance() or Run()):
The initialization configuration for the makVrv::DtDe.
Definition DtDeInitializer.h:40
makeDeInitializer sets up all the configuration information needed for the VR-Vantage display engine. It first creates an observer configuration specifying a name and using the defaults for all other observer parameters:
std::string observerName = "Observer 1";
TODO: This class is used to encapsulate a set of default observer settings and parameters.
Definition DtObserverConfiguration.h:23
It then creates a channel configuration, and tells it to use the observer configuration created above:
Configuration for a single channel Serialized and sent to remote displays as necessary to configure r...
Definition DtChannelConfiguration.h:27
virtual void setObserverName(const std::string &observerName)
Set the observer name.
Next, it creates a window configuration, and tells it to be an embedded window and to use the channel configuration created above:
This owns a list of channel configurations.
Definition DtWindowConfiguration.h:27
virtual ConfigurationType & add(const ConfigurationType &orig)
Add a Channel configuration.
Definition DtConfigurationCollection.h:68
DtChannelConfigurations & channelConfigurations()
Get the channel configurations for this window.
@ EmbeddedWindow
Definition DtWindowConfiguration.h:38
void setWindowType(WindowType wt)
WindowType setter.
Then a display configuration is created, using the window configuration created above:
std::string displayName = "Example VR-Vantage Embedded in MFC App";
displayConfig.windowConfigurations().add(windowConfig);
This owns a list of window configurations.
Definition DtDisplayConfiguration.h:44
An input driver configuration is created next:
virtual void setOptionalParameter(const std::string &name, const std::string &value)
Set an optional parameter.
Now that a display configuration and input driver configuration exist, they are used to create a display engine initializer, which is then returned:
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
init.deConfiguration().displayConfigurations().add(displayConfig);
init.setDisplayEngineName(displayName);
init.setInputDriverConfiguration( inputConfig );
void initMfc()
initMfc() registers creators for the main window (DtMfcMainWindow, derived from CFrameWnd and DtDeMainWindow):
static void registerInstance(DtDe &de, DtDeMainWindowProvider *instance)
Register the provider with a DtDe.
Provides a MFC Main Window.
Definition MFCClasses.h:187
and the embedded OSG window (DtEmbeddedMfcOsgWindow, derived from CWnd and DtOsgWindow):
void setCreator(int index, DtWindowCreator *creator)
Set the function to create a window.
static DtWindowFactory & instance(DtDe &de)
Get the instance for this factory. Creates this factory if one doesn't exist (lazy creation).
Creator class for making the Embedded Mfc window.
Definition MFCClasses.h:151
void createMainWindow()
createMainWindow() gets the window provider:
The abstract main window provider.
Definition DtDeMainWindow.h:85
static DtDeMainWindowProvider & instance(DtDe &de)
Get the main window provider instance for a DtDe.
and then creates the new DtMfcMainWindow by calling the window provider's igMainWindow() accessor (which creates the main window since it doesn't yet exist):
Tick() calls DtTimedSection's stop() method to recalculate the time the application has run
and then calls the Display Engine's tick method with the DtTimedSection's duration() as its argument:
Finally, an instance of the derived application class must be created with global scope:
Building the Example
VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
Running the Example
This example is an application. You can run it by running ./bin64/exampleMFC.exe (on Windows) or ./bin64/exampleMFC (on Linux). For more information about running examples, please see Running Applications and Examples.
Example Source Files
exampleMFC.cxx
#include <afxwin.h>
#include <matrix/vlMath.h>
#include <vlutil/vlLogFileInfo.h>
class ExampleMFCApp : public CWinApp
{
public:
virtual BOOL InitInstance()
{
initMfc();
createMainWindow();
myDe->setDeInitializer(initializer);
myDe->initialize();
myDe->sharedState().setImguiOverlayOn( false );
myTimer = new DtTimedSection;
myTimer->start();
return CWinApp::InitInstance();
}
virtual int Run()
{
_AFX_THREAD_STATE* pState = AfxGetThreadState();
for(;;)
{
if (PeekMessage(&(pState->m_msgCur) , NULL , 0, 0, PM_NOREMOVE))
{
if (!PumpMessage())
{
return ExitInstance();
}
}
else
{
ExampleMFCApp::Tick();
}
}
}
virtual ~ExampleMFCApp()
{
delete myDe;
delete myTimer;
}
protected:
makVrv::DtDeInitializer makeDeInitializer()
{
std::string observerName = "Observer 1";
makVrv::DtObserverConfiguration observerConfig(observerName);
makVrv::DtChannelConfiguration channel;
makVrv::DtWindowConfiguration windowConfig;
std::string displayName = "Example VR-Vantage Embedded in MFC App";
makVrv::DtDisplayConfiguration displayConfig(displayName);
displayConfig.windowConfigurations().add(windowConfig);
makVrv::DtInputDriverConfiguration inputConfig;
makVrv::DtDeInitializer
init;
init.deConfiguration().displayConfigurations().add(displayConfig);
init.setDisplayEngineName(displayName);
init.setInputDriverConfiguration( inputConfig );
DtLogFileInfo::instance().setAppName( "exampleMFC" );
DtLogFileInfo::instance().setProtocol( DT_PROTOCOL_SHORT_NAME );
DtLogFileInfo::instance().setSvnRev(
SVN_REV );
init.setLogFileName( DtLogFileInfo::instance().getLogFileName() );
}
void initMfc()
{
new DtMfcMainWindowProvider(*myDe));
new DtEmbeddedMfcOsgWindowCreator);
}
void createMainWindow()
{
makVrv::DtDeMainWindowProvider& windowProvider =
}
void Tick()
{
static bool firstT = true;
myTimer->stop();
myDe->tick(myTimer->duration());
myTimer->start();
if ( firstT )
{
firstT = false;
makVrv::DtScene& sceneDriver = myDe->scene();
std::string groundDbPath = myDe->dePathConfiguration().sharedDataPath();
groundDbPath += "/TerrainData/TerrainConfiguration/Ground_DB II.mtf";
{
}
myDe->driverManager().inputDriver().currentObserver()->setLocation( 0., 0., 500. );
}
}
protected:
makVrv::DtDe* myDe;
DtTimedSection* myTimer;
};
ExampleMFCApp theApp;
Contains makVrv::DtDeSharedState class.
Contains makVrv::DtIGLogger class.
The declaration of the makVrv::DtDouglasSeaState and makVrv::DtDouglasSwellState classes.
Contains makVrv:DtMasterScene class.
The declaration of the makVrv::DtEnvironment class.
Contains DtMfcModelSetStringProvider class.
Contains makVrv::DtObserver class.
Contains makVrv::DtOceanLayer class.
Contains makVrv::DtOsgEmptySceneLogo class.
Contains makVrv::DtScene class.
Contains makVrv::DtTerrain class.
For the purpose of the example, it doesn't really matter what this class does; but an instance of it ...
Definition DtMfcModelSetStringProvider.h:25
const std::string & logFileName() const
Get the name of the output log.
DtDebugLog::PrintLevel notifyLevel() const
Get the initial notify level of print output.
virtual void showBasedOnSettings()=0
Show the main window based on application settings. This also handles the creation of the startup dia...
DtDeMainWindow & igMainWindow()
Use this accessor to lazily create the singleton instance of the DtDeMainWindow;.
@ Moderate
Definition DtDouglasSeaScale.h:39
virtual void setSeaChoppiness(float chop)
Set / get the amount of chop on the sea.
virtual void setSeaStateDirection(float dir)
Set / get the sea state direction, in radians from True North.
virtual void setWindDrivenSeaState(bool enabled)
Set / get if sea state is wind driven or manually driven.
virtual void setSeaState(DtDouglasSeaState::Scale state)
static void registerInstance(DtDe &de, DtModelSetStringProvider *instance)
Register the provider with a DtDe.
virtual void setSeaLevel(double seaLevel)
static void setEnabled(bool enabled)
This global method will enable or disable the use of an empty scene overlay for all uses of this clas...
DtTerrain & terrain()
accessor for the DtTerrain object
Definition DtScene.h:56
const std::string & terrainFileName() const
Get the loaded terrain filename.
bool loadTerrain(const DtUnicode &filePath)
Load the terrain from a file.
DtEnvironment & environment()
accessor for the DtEnvironment object
Definition DtScene.h:87
virtual DtOceanLayer * findOceanLayer(const std::string &layerName)
Return the DtOceanLayer with the name layerName.
virtual DtOceanLayer & addOceanLayer(const makArchives::DtOceanLayerRecord &rec)
Contains necessary ig version information.
#define VANTAGE_VERSION_STRING
Definition igVersion.h:12
DT_DLL_VRVGRAPHICS void init(DtDe &anIG)
DT_DLL_VRVOSGEARTH void init(DtDe &de)
DT_DLL_VRVPOSTPROCESSOR void init(DtDe &anIG)
Initialize the VRV Post Processor library.
DT_DLL_VRVTERRAIN void init(DtDe &anIG)
DT_DLL_VRVTRITON void init(DtDe &anIG)
Initialize the VRV Triton library.
#define SVN_REV
Definition svnRev.h:1
Contains DLL export macros.
Contains DLL export macros.
Contains DLL export macros.
Contains DLL export macros.
MFCClasses.h
#ifndef MFCClasses_H
#define MFCClasses_H
#include <afxwin.h>
#include <GL/GL.h>
#include <osg/GraphicsContext>
#include <osgViewer/GraphicsWindow>
#include <vlutil/vlPerformanceStats.h>
{
class DtDeSplashScreen;
}
{
public:
protected:
};
{
public:
void oglInitialize(
bool doubleBuffer,
bool stereo,
unsigned int colorBits,
unsigned int depthBits, unsigned int stencilBits, bool vsync);
afx_msg
void OnSize(UINT nType,
int cx,
int cy);
afx_msg
void OnDraw(CDC *pDC);
afx_msg
int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
private:
};
{
public:
};
{
public:
virtual bool valid() const;
virtual bool realizeImplementation();
virtual bool isRealizedImplementation() const;
virtual void closeImplementation();
virtual void grabFocus();
virtual void grabFocusIfPointerInWindow();
virtual bool makeCurrentImplementation();
virtual bool releaseContextImplementation();
virtual void swapBuffersImplementation();
virtual void requestWarpPointer( float x, float y );
virtual void useCursor( bool onOff );
protected:
};
class DtMfcMainWindowProvider : public makVrv::DtDeMainWindowProvider
{
public:
DtMfcMainWindowProvider(makVrv::DtDe& de);
protected:
virtual makVrv::DtDeMainWindow* createMainWindow(makVrv::DtDe& de);
};
#endif
Contains makVrv::DtDe class.
Contains makVrv::DtDeMainWindow class.
Contains makVrv::DtOsgView class.
Contains makVrv::DtIgView class.
Contains makVrv::DtViewFactory class.
A simple OpenGL OSG control window.
Definition MFCClasses.h:78
afx_msg void OnRButtonUp(UINT nFlags, CPoint point)
afx_msg void OnRButtonDown(UINT nFlags, CPoint point)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point)
afx_msg void OnMButtonDown(UINT nFlags, CPoint point)
bool isRealizedImplementation() const
HDC myHDC
Definition MFCClasses.h:143
bool makeCurrent()
Make this window's context current.
void oglCreate(CRect rect, CWnd *parent)
virtual void sizeChanged(int width, int height)
Called when the size of the window has changed. This is necessary since often the window system will ...
bool release()
Release this window's context.
HGLRC myHGLRC
Definition MFCClasses.h:144
DtEmbeddedMfcOsgWindow(makVrv::DtDe &de, makVrv::DtWindowConfiguration &wc)
CTOR.
DtMfcControlAsOsgContext * myControl
Definition MFCClasses.h:141
afx_msg void OnSize(UINT nType, int cx, int cy)
virtual ~DtEmbeddedMfcOsgWindow()
DTOR.
afx_msg void OnLButtonUp(UINT nFlags, CPoint point)
void oglClose()
Deinitialize OpenGL.
virtual bool isValid() const
Check to see if the window is still valid.
afx_msg void OnMButtonUp(UINT nFlags, CPoint point)
afx_msg void OnDraw(CDC *pDC)
virtual void resize(int width, int height)
Resize the window.
virtual void setPosition(int x, int y)
Set the position of the window in screen coordinates.
CWnd * myHWnd
Definition MFCClasses.h:142
int myPixelFormat
Definition MFCClasses.h:145
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
void oglInitialize(bool doubleBuffer, bool stereo, unsigned int colorBits, unsigned int depthBits, unsigned int stencilBits, bool vsync)
Initialize the OpenGL context.
void setVSync(bool enabled)
Control this window's vsync.
afx_msg void OnMouseMove(UINT nFlags, CPoint point)
void swapBuffers()
Swap this window's OpenGL buffers.
Internal class for embedding The OpenGL Qt code into OSG.
Definition MFCClasses.h:162
Simple MFC based Main Window.
Definition MFCClasses.h:27
virtual void setApplicationTitle(const std::string &appName)
Set the application's name, typically for the Title Bar.
virtual std::string applicationTitle() const
Get the application's name.
virtual void quit()
Quit the application.
virtual bool isStatusBarVisible() const
Returns whether the background status bar dock widget is currently visible.
virtual void setShowQuitDialogOnClose(bool value)
Set whether or not to show quit dialog on close if this is the last window.
virtual void setStatusBarVisible(bool visible)
Shows or hides the background status bar dock widget.
virtual void setApplicationIcon(const std::string &iconName)
Set the application's icon, typically use a resource name.
DtMfcMainWindow(makVrv::DtDe &de)
virtual void showBasedOnSettings()
Show the main window based on application settings. This also handles the creation of the startup dia...
virtual void setSplashScreen(makVrv::DtDeSplashScreen *splashScreen)
Not supported in this example.
virtual void destroyWindow()
Destroy the window.
bool myShowQuitFlag
Definition MFCClasses.h:70
The abstract main window class.
Definition DtDeMainWindow.h:25
DtDe & de()
Definition DtDeMainWindow.h:68
The VR-Vantage Splash Screen Interface.
Definition DtDeSplashScreen.h:35
A basic OSG display window, used to get the osg context for a display window.
Definition DtOsgWindow.h:37
Abstract Base class.
Definition DtWindow.h:26
virtual int height() const
Get the window height.
DtDe & de()
Accessor to get the de;.
Definition DtWindow.h:95
virtual int width() const
Get the window width.
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
Contains DLL export macros.
MFCClasses.cxx
: DtDeMainWindow(de)
, myShowQuitFlag(false)
{
Create(NULL, "VR-Vantage MFC Example");
}
{
this->SetWindowText(appName.c_str());
}
{
CString cstr;
this->GetWindowText(cstr);
return std::string(cstr);
}
{
HANDLE image = LoadImage(AfxGetInstanceHandle(),iconName.c_str(),
IMAGE_ICON,16,16,LR_LOADFROMFILE | LR_DEFAULTSIZE);
if(image != NULL)
{
this->SetIcon((HICON)image,FALSE);
}
}
{
}
{
this->CloseWindow();
}
{
}
{
}
{
return false;
}
{
ShowWindow(1);
}
{
this->SendMessage(WM_QUIT);
}
osg::GraphicsContext::Traits* traits)
, myControl(wnd)
, myDe( de )
{
}
{
return true;
}
{
myControl->oglInitialize(_traits->doubleBuffer,
_traits->quadBufferStereo,
_traits->red + _traits->green + _traits->blue + _traits->alpha,
_traits->depth,
_traits->stencil,
_traits->vsync);
return true;
}
{
return myControl->isRealizedImplementation();
}
{
}
{
}
{
}
{
{
}
}
{
}
{
}
{
}
{
}
: DtOsgWindow(de,wc)
, myControl(0)
, myHWnd(0)
, myHDC(0)
, myHGLRC(0)
, myPixelFormat(0)
{
osg::ref_ptr<osg::GraphicsContext::Traits> traits = DtOsgWindow::createDefaultTraits();
traits->x = 0;
traits->y = 0;
traits->doubleBuffer = myDe.deInitializer().doubleBuffer();
traits->vsync = myDe.deInitializer().vsync();
traits->width = 0;
traits->height = 0;
traits->windowName = myWindowConfiguration.name();
myContext = myControl;
if(mainWindow)
{
RECT bounds;
mainWindow->GetClientRect(&bounds);
oglCreate(bounds, mainWindow);
}
}
{
}
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
END_MESSAGE_MAP()
{
return true;
}
{
}
{
}
{
}
{
}
{
CString className = AfxRegisterWndClass(
CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
NULL,
(HBRUSH)GetStockObject(BLACK_BRUSH), NULL);
CreateEx(0, className, "OpenGL", WS_CHILD | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0);
}
{
ValidateRect(NULL);
}
{
myControl->getEventQueue()->mouseMotion(point.x,point.y);
}
{
myControl->getEventQueue()->mouseButtonPress(point.x,point.y,1);
}
{
myControl->getEventQueue()->mouseButtonRelease(point.x,point.y,1);
}
{
myControl->getEventQueue()->mouseButtonPress(point.x,point.y,3);
}
{
myControl->getEventQueue()->mouseButtonRelease(point.x,point.y,3);
}
{
myControl->getEventQueue()->mouseButtonPress(point.x,point.y,2);
}
{
myControl->getEventQueue()->mouseButtonRelease(point.x,point.y,2);
}
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
unsigned int color,
unsigned int depth,
unsigned int stencil,
bool vsync)
{
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)) ;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1 ;
pfd.dwFlags = PFD_DOUBLEBUFFER |
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW ;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = color;
pfd.cDepthBits = depth;
pfd.cStencilBits = stencil;
pfd.iLayerType = PFD_MAIN_PLANE ;
}
{
typedef BOOL (APIENTRY *PFNWGLSWAPINTERVALFARPROC)( int );
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = 0;
const char *extensions = (const char*)glGetString( GL_EXTENSIONS );
if( strstr( extensions, "WGL_EXT_swap_control" ) == 0 )
return;
else
{
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress( "wglSwapIntervalEXT" );
if( wglSwapIntervalEXT )
{
wglSwapIntervalEXT(enabled ? 1 : 0);
}
}
}
{
}
{
CWnd::OnSize(nType, cx, cy);
}
{
}
{
return wglMakeCurrent(
myHDC,0) == TRUE;
}
{
}
: DtDeMainWindowProvider(de)
{
myDestroyMainWindowFlag = false;
}
{
DtMfcMainWindow* window = new DtMfcMainWindow(de);
AfxGetApp()->m_pMainWnd = window;
return window;
}
{
return new DtEmbeddedMfcOsgWindow(de,config);
}
virtual makVrv::DtWindow * create(makVrv::DtDe &de, makVrv::DtWindowConfiguration &config)
Create an window instance.
DtMfcControlAsOsgContext(makVrv::DtDe &de, DtEmbeddedMfcOsgWindow *wnd, osg::GraphicsContext::Traits *traits)
virtual void useCursor(bool onOff)
virtual void closeImplementation()
virtual bool valid() const
virtual bool releaseContextImplementation()
virtual bool isRealizedImplementation() const
virtual void swapBuffersImplementation()
virtual bool realizeImplementation()
virtual bool makeCurrentImplementation()
virtual void requestWarpPointer(float x, float y)
virtual void grabFocusIfPointerInWindow()
DtEmbeddedMfcOsgWindow * myControl
Definition MFCClasses.h:181
virtual makVrv::DtDeMainWindow * createMainWindow(makVrv::DtDe &de)
Create a main window.
DtMfcMainWindowProvider(makVrv::DtDe &de)
Constructor.
DtWindowConfiguration & myWindowConfiguration
Definition DtWindow.h:121
T value(details::expression_node< T > *n)
Definition exprtk.hpp:12342
Definition DtGlStudioDrawable.h:29