Create an MFC single document project,
OSGObject.h
#pragma once #include <osgViewer\Viewer> #include <osgDB\ReadFile> #include <osgViewer\api\Win32\GraphicsWindowWin32> #include <osgGA\TrackballManipulator> #include <osg\Group> class COSGObject { public: COSGObject(HWND hWnd); ~COSGObject(); void InitOSG(); void InitSceneGraph(); void InitCameraConfig(); void PreFrameUpdate(); void PostFrameUpdate(); static void Render(void* ptr); osgViewer::Viewer* getOsgViewer(); private: HWND m_hwnd; osgViewer::Viewer* mViewer; osg::ref_ptr<osg::Group> mRoot; };
OSGObject.cpp
#pragma once #include <osgViewer\Viewer> #include <osgDB\ReadFile> #include <osgViewer\api\Win32\GraphicsWindowWin32> #include <osgGA\TrackballManipulator> #include <osg\Group> class COSGObject { public: COSGObject(HWND hWnd); ~COSGObject(); void InitOSG(); void InitSceneGraph(); void InitCameraConfig(); void PreFrameUpdate(); void PostFrameUpdate(); static void Render(void* ptr); osgViewer::Viewer* getOsgViewer(); private: HWND m_hwnd; osgViewer::Viewer* mViewer; osg::ref_ptr<osg::Group> mRoot; };
View.h
// This MFC sample source code demonstrates how to use the MFC Microsoft Office Fluent user interface // ( "the Fluent the UI"). This example is only for reference // to supplement "Microsoft Foundation Class Reference" and // MFC C ++ library software related electronic documentation that came. // copy, use or distribute Fluent UI licensing terms are provided separately. // To learn more about the Fluent UI licensing program, please visit // http://go.microsoft.com/fwlink/?LinkId=238214 . // // Copyright (C) in the Microsoft Corporation // All rights reserved. // View.h: View class interface // #pragma Once #include " Doc.h " #include " OSGObject.h " class View:public CView { protected : // only create a sequence of CMFC_OsgEarth20190901View (); the DECLARE_DYNCREATE (View) // Characteristics public : Doc * the GetDocument () const ; // operations public : COSGObject * mOsgObj; HANDLE mThreadHandle; // rewrite public : Virtual void the OnDraw (the CDC * the pDC); // rewritten to draw the view Virtual BOOL the PreCreateWindow (the CREATESTRUCT & CS); protected : // implemented public: virtual ~View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: afx_msg void OnFilePrintPreview(); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnContextMenu(CWnd* pWnd, CPoint point); DECLARE_MESSAGE_MAP() public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnDestroy(); virtual void OnInitialUpdate(); }; #ifndef _DEBUG // View.cpp 中的调试版本 inline Doc* View::GetDocument() const { return reinterpret_cast<Doc*>(m_pDocument); } #endif
View.cpp
// This MFC sample source code demonstrates how to use the MFC Microsoft Office Fluent user interface // ( "the Fluent the UI"). This example is only for reference // to supplement "Microsoft Foundation Class Reference" and // MFC C ++ library software related electronic documentation that came. // copy, use or distribute Fluent UI licensing terms are provided separately. // To learn more about the Fluent UI licensing program, please visit // http://go.microsoft.com/fwlink/?LinkId=238214 . // // Copyright (C) in the Microsoft Corporation // All rights reserved. // View.cpp: to achieve the View class // #include " stdafx.h " // SHARED_HANDLERS can preview thumbnails and search filters handle // define ATL project, and share documents and allow the project code. #ifndef SHARED_HANDLERS #endif #include "Doc.h" #include "View.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // View IMPLEMENT_DYNCREATE(View, CView) BEGIN_MESSAGE_MAP(View, CView) ON_WM_CONTEXTMENU() ON_WM_RBUTTONUP() ON_WM_CREATE() ON_WM_ERASEBKGND() ON_WM_DESTROY() END_MESSAGE_MAP() // View 构造/析构 View::View() { // TODO: 在此处添加构造代码 mOsgObj = 0; mThreadHandle = 0; } :: View ~ View () { } BOOL :: View the PreCreateWindow (the CREATESTRUCT & CS) { // the TODO: here by modifying // modify the CS window class or style the CREATESTRUCT return CView :: the PreCreateWindow (CS); } / / View rendering void View :: the OnDraw (the CDC * / * the pDC * / ) { Doc * = pDoc the GetDocument (); the ASSERT_VALID (pDoc); IF (! pDoc) return ; // the TODO: native data added here drawing code } void View :: OnRButtonUp (UINT / * nFlags */, CPoint point) { ClientToScreen(&point); OnContextMenu(this, point); } void View::OnContextMenu(CWnd* /* pWnd */, CPoint point) { #ifndef SHARED_HANDLERS //theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE); #endif } // View 诊断 #ifdef _DEBUG void View::AssertValid() const { CView::AssertValid(); } void View::Dump(CDumpContext& dc) const { CView :: the Dump (DC); } Doc * :: View the GetDocument () const // non-debug version inlined { the ASSERT (m_pDocument -> IsKindOf (the RUNTIME_CLASS (Doc))); return (Doc * ) m_pDocument; } #endif // _DEBUG // View message handler int View :: the OnCreate (lPCREATESTRUCT lpCreateStruct) { IF (CView :: the OnCreate (lpCreateStruct) == - . 1 ) return - . 1 ; // the TODO: Add your this specific create code mOsgObj = new new COSGObject (m_hWnd); return 0 ; } BOOL :: View the OnEraseBkgnd (the CDC * the pDC) { // the TODO: this addition message handler code, and / or invoke the default value IF ( 0 == mOsgObj) { return CView :: the OnEraseBkgnd (the pDC); } the else { return FALSE; } return CView :: the OnEraseBkgnd (the pDC); } void View :: the OnDestroy () { CView :: the OnDestroy (); // the TODO: here add a message handler code IF (! mOsgObj = 0 ) { Delete mOsgObj; } The WaitForSingleObject (mThreadHandle, 1000 ); } void View :: the OnInitialUpdate () { CView :: the OnInitialUpdate (); // the TODO: add this special code and / or call the base class mOsgObj-> InitOSG (); mThreadHandle = (HANDLE) the _beginthread (:: & COSGObject the Render, 0 , mOsgObj); }