I just did the same Win32 example and it works fine. Please, remove the comment from the RenderingThread since it is doing the rendering, otherwise you will not see the blue OSG window. Bellow is the code. As for the exceptions, I think you are mixing debug and release libs of OSG with your program. If you are running debug, then link against osgd.lib, osgViewerd.lib and OpenThreadsd.lib
// TestWin32.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "TestWin32.h" #include <osgViewer/Viewer> #include <osgViewer/api/Win32/GraphicsWindowWin32> #include <OpenThreads/Thread> #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); class RenderingThread : public OpenThreads::Thread, public osg::Referenced { public: RenderingThread(osgViewer::Viewer* viewer) : m_Viewer(viewer) , m_Running(false) { } virtual void run() { m_Running = true; while (m_Running) { m_Viewer->frame(); } cancel(); } void setIsRunning( bool running ) { m_Running = running; } protected: osg::ref_ptr<osgViewer::Viewer> m_Viewer; bool m_Running; }; void EmbedOSG(HWND handle) { // Local Variable to hold window size data RECT rect; // Create the viewer for this window osgViewer::Viewer* v = new osgViewer::Viewer(); // Get the current window size ::GetWindowRect(handle, &rect); // Init the GraphicsContext Traits osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; // Init the Windata Variable that holds the handle for the Window to display OSG in. osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(handle); // Setup the traits parameters traits->x = 0; traits->y = 0; traits->width = rect.right - rect.left; traits->height = rect.bottom - rect.top; traits->windowDecoration = false; traits->doubleBuffer = true; traits->sharedContext = 0; traits->setInheritedWindowPixelFormat = true; traits->inheritedWindowData = windata; // Create the Graphics Context osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get()); // Init a new Camera (Master for this View) osg::ref_ptr<osg::Camera> camera = new osg::Camera; // Assign Graphics Context to the Camera camera->setGraphicsContext(gc); // Set the viewport for the Camera camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height)); // Set projection matrix and camera attribtues camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f)); camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0, 1000.0); // Add the Camera to the Viewer v->setCamera(camera.get()); // Setup the geometry for this control //TODO seems to need example osghud //v->setSceneData( createHUD(osg::Vec2(0,traits->height/2.f), osg::Vec2(traits->width,traits->height), handle) ); // Realize the Viewer v->realize(); // TODO skip for now // Start the rendring thread and save it as user data for later clearance osg::ref_ptr< RenderingThread > renderer = new RenderingThread(v); renderer->startThread(); v->getCamera()->setUserData( renderer.get() ); } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN32)); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // // COMMENTS: // // This function and its usage are only necessary if you want this code // to be compatible with Win32 systems prior to the 'RegisterClassEx' // function that was added to Windows 95. It is important to call this function // so that the application will get 'well formed' small icons associated // with it. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN32)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTWIN32); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); EmbedOSG(hWnd); return TRUE; } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } On Mon, Jul 7, 2014 at 3:45 PM, Bluish Joe <mirco.andr...@tesigroup.it> wrote: > Thanks Trajce, now I'm understanding something more. > I tried using your code. I finally wrote a Win32 application (without > CLR). This is the code: > > > Code: > #include "stdafx.h" > #include "TestOsgVSWin32.h" > > #include <windows.h> > > #include <osgViewer/Viewer> > #include <osgViewer/api/Win32/GraphicsWindowWin32> > #include <osg/GraphicsContext> > > const LPCWSTR g_szClassName = L"myWindowClass"; > > // Step 4: the Window Procedure > LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) > { > switch(msg) > { > case WM_CLOSE: > DestroyWindow(hwnd); > break; > case WM_DESTROY: > PostQuitMessage(0); > break; > default: > return DefWindowProc(hwnd, msg, wParam, lParam); > } > return 0; > } > > int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, > LPSTR lpCmdLine, int nCmdShow) > { > WNDCLASSEX wc; > HWND hwnd; > MSG Msg; > > //Step 1: Registering the Window Class > wc.cbSize = sizeof(WNDCLASSEX); > wc.style = 0; > wc.lpfnWndProc = WndProc; > wc.cbClsExtra = 0; > wc.cbWndExtra = 0; > wc.hInstance = hInstance; > wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); > wc.hCursor = LoadCursor(NULL, IDC_ARROW); > wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); > wc.lpszMenuName = NULL; > wc.lpszClassName = g_szClassName; > wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); > > if(!RegisterClassEx(&wc)) > { > MessageBox(NULL, L"Window Registration Failed!", L"Error!", > MB_ICONEXCLAMATION | MB_OK); > return 0; > } > > // Step 2: Creating the Window > hwnd = CreateWindowEx( > WS_EX_CLIENTEDGE, > g_szClassName, > L"The title of my window", > WS_OVERLAPPEDWINDOW, > CW_USEDEFAULT, CW_USEDEFAULT, 600, 300, > NULL, NULL, hInstance, NULL); > > if(hwnd == NULL) > { > MessageBox(NULL, L"Window Creation Failed!", L"Error!", > MB_ICONEXCLAMATION | MB_OK); > return 0; > } > > ShowWindow(hwnd, nCmdShow); > UpdateWindow(hwnd); > EmbedOSG(hwnd); > > // Step 3: The Message Loop > while(GetMessage(&Msg, NULL, 0, 0) > 0) > { > TranslateMessage(&Msg); > DispatchMessage(&Msg); > } > return Msg.wParam; > } > > > void EmbedOSG(HWND handle) > { > // Local Variable to hold window size data > RECT rect; > > > // Create the viewer for this window > osgViewer::Viewer* v = new osgViewer::Viewer(); > > > // Get the current window size > ::GetWindowRect(handle, &rect); > > > // Init the GraphicsContext Traits > osg::ref_ptr<osg::GraphicsContext::Traits> traits = new > osg::GraphicsContext::Traits; > > > // Init the Windata Variable that holds the handle for the Window to > display OSG in. > osg::ref_ptr<osg::Referenced> windata = new > osgViewer::GraphicsWindowWin32::WindowData(handle); > > > // Setup the traits parameters > traits->x = 0; > traits->y = 0; > traits->width = rect.right - rect.left; > traits->height = rect.bottom - rect.top; > traits->windowDecoration = false; > traits->doubleBuffer = true; > traits->sharedContext = 0; > traits->setInheritedWindowPixelFormat = true; > traits->inheritedWindowData = windata; > > > // Create the Graphics Context > osg::GraphicsContext* gc = > osg::GraphicsContext::createGraphicsContext(traits.get()); > > > // Init a new Camera (Master for this View) > osg::ref_ptr<osg::Camera> camera = new osg::Camera; > > > // Assign Graphics Context to the Camera > camera->setGraphicsContext(gc); > > > // Set the viewport for the Camera > camera->setViewport(new osg::Viewport(traits->x, traits->y, > traits->width, traits->height)); > > > // Set projection matrix and camera attribtues > camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); > camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f)); > camera->setProjectionMatrixAsPerspective(30.0f, > static_cast<double>(traits->width)/static_cast<double>(traits->height), > 1.0, 1000.0); > > > // Add the Camera to the Viewer > v->setCamera(camera.get()); > > > // Setup the geometry for this control > //TODO seems to need example osghud > //v->setSceneData( createHUD(osg::Vec2(0,traits->height/2.f), > osg::Vec2(traits->width,traits->height), handle) ); > > > // Realize the Viewer > v->realize(); > > /* TODO skip for now > // Start the rendring thread and save it as user data for later > clearance > osg::ref_ptr< RenderingThread > renderer = new RenderingThread(v); > renderer->startThread(); > v->getCamera()->setUserData( renderer.get() ); > */ > } > > > > But I encounter the same issue: > System.AccessViolationException when doing > Code: > traits->inheritedWindowData = windata; > > > If I investigate more I see that this exception occures in this location > in ref_ptr file: > > Code: > inline ref_ptr& operator = (T* ptr) > { > if (_ptr==ptr) return *this; > T* tmp_ptr = _ptr; > _ptr = ptr; > if (_ptr) _ptr->ref(); > // unref second to prevent any deletion of any object which > might > // be referenced by the other object. i.e rp is child of the > // original _ptr. > if (tmp_ptr) tmp_ptr->unref(); > <--System.AccessViolationException! > return *this; > } > > > > > ------------------ > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=60195#60195 > > > > > > _______________________________________________ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > -- trajce nikolov nick
_______________________________________________ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org