plugin.cpp
上传用户:q202440
上传日期:2019-04-12
资源大小:95k
文件大小:7k
- /*OpenSceneGraph Firefox-plugin
- *License: GPL 2.0
- *Based on Work by Mozilla.org and OpenSceneGraph osgviewerGLUT-sample by Robert Osfield
- *(c) Andreas Goebel 2008
- */
- #include "plugin.h"
- #include <gl/gl.h>
- #include <stdlib.h>
- #include <Psapi.h>
- #include <osgDB/FileUtils>
- /*
- //Global Variables
- osg::ref_ptr<osgViewer::Viewer> viewer;
- osg::observer_ptr<osgViewer::GraphicsWindow> window;
- char* filename;
- */
- //Functions that translate the Windows Mouse-Events to osg-events
- void nsPluginInstance::mousebutton( int button, int state, int x, int y )
- {
- if (window.valid())
- {
- if (state==0) window->getEventQueue()->mouseButtonPress( x, y, button+1 );
- else window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
- }
- }
- void nsPluginInstance::mousemove( int x, int y )
- {
- if (window.valid())
- {
- window->getEventQueue()->mouseMotion( x, y );
- }
- }
- //////////////////////////////////////
- //
- // general initialization and shutdown
- //
- NPError NS_PluginInitialize()
- {
- return NPERR_NO_ERROR;
- }
- void NS_PluginShutdown()
- {
- }
- /////////////////////////////////////////////////////////////
- //
- // construction and destruction of our plugin instance object
- //
- nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
- {
- if(!aCreateDataStruct)
- return NULL;
- nsPluginInstance * plugin = new nsPluginInstance(aCreateDataStruct->instance);
- for (int i = 0; i<aCreateDataStruct->argc; ++i){
- if ( strcmp(aCreateDataStruct->argn[i] , "src") == 0)
- plugin->filename = aCreateDataStruct->argv[i]; //This structs holds tha value passed to the plugin with embed
- if ( strcmp(aCreateDataStruct->argn[i] , "width") == 0)
- plugin->width = atoi(aCreateDataStruct->argv[i]) ;
- if ( strcmp(aCreateDataStruct->argn[i] , "height") == 0)
- plugin->height= atoi(aCreateDataStruct->argv[i]) ;
- //MessageBox(NULL,aCreateDataStruct->argn[i],aCreateDataStruct->argv[i],MB_OK);
- }
- DWORD cchCurDir;
- LPTSTR lpszCurDir;
- TCHAR tchBuffer[MAX_PATH + 1];
- //DWORD nSize;
- lpszCurDir = tchBuffer;
- cchCurDir = MAX_PATH;
-
- HINSTANCE handle =LoadLibrary( "nposgviewer.dll" );
- //std::string dlldir;
- if (handle)
- {
- GetModuleFileName(handle, lpszCurDir, cchCurDir);
- //PathRemoveFileSpec(lpszCurDir);
- FreeLibrary(handle);
- //std::string dir=lpszCurDir;
- //int pos = dir.rfind("\");
- //dlldir = dir.substr(0,pos);*/
- char* lastSlash = strrchr(lpszCurDir, '\');
- if (lastSlash)
- *(lastSlash + 1) = ' ';
- }
- //std::string dlldir;
- //MessageBox(NULL, "Verzeichnis", lpszCurDir, MB_OK);
- //std::string bla = lpszCurDir; //Copy?
- //std::deque<std::string>& list2 = osgDB::getLibraryFilePathList();
- osgDB::getLibraryFilePathList().push_front(lpszCurDir);
- //list2.push_front(dlldir);
- //std::deque<std::string> list;
- //list.push_front(dlldir);
- //osgDB::appendPlatformSpecificLibraryFilePaths(list);
- //osgDB::
- //FreeLibrary(handle);
-
- return plugin;
-
- }
- void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
- {
- if(aPlugin)
- delete (nsPluginInstance *)aPlugin;
- }
- ////////////////////////////////////////
- //
- // nsPluginInstance class implementation
- //
- nsPluginInstance::nsPluginInstance(NPP aInstance) : nsPluginInstanceBase(),
- mInstance(aInstance),
- mInitialized(FALSE)
- {
- mhWnd = NULL; width = 400; height = 400; filename=NULL;
- }
- nsPluginInstance::~nsPluginInstance()
- {
- }
- static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM);
- static WNDPROC lpOldProc = NULL;
- void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
- void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
- /*
- HDC hDC;
- HGLRC hRC;
- */
- NPBool nsPluginInstance::init(NPWindow* aWindow)
- {
- if(aWindow == NULL)
- return FALSE;
- mhWnd = (HWND)aWindow->window;
- if(mhWnd == NULL)
- return FALSE;
- // subclass window so we can intercept window messages and
- // do our drawing to it
- lpOldProc = SubclassWindow(mhWnd, (WNDPROC)PluginWinProc);
- // associate window with our nsPluginInstance object so we can access
- // it in the window procedure
- SetWindowLong(mhWnd, GWL_USERDATA, (LONG)this);
- //we add EnableOpenGL and SetTimer
- EnableOpenGL( mhWnd, &hDC, &hRC );
- SetTimer(mhWnd, 0, 1, (TIMERPROC) NULL); // no timer callback
- //osg-related code:
- //Node-file is read via http by libcurl:
- std::string s = filename;
- osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(s);
- viewer = new osgViewer::Viewer;
- float w = (float)width; float h = (float)height; w*=1.1; h*=1.1; width=(int)w; height=(int)h;
- window = viewer->setUpViewerAsEmbeddedInWindow(0,0,width,height);
- viewer->setSceneData(loadedModel.get());
- osg::ref_ptr<osgGA::TrackballManipulator> tbm = new osgGA::TrackballManipulator();
- viewer->setCameraManipulator(tbm.get() );
- osg::ref_ptr<osgViewer::StatsHandler> stats = new osgViewer::StatsHandler();
- viewer->addEventHandler(stats);
- viewer->realize();
- mInitialized = TRUE;
- return TRUE;
- }
- void nsPluginInstance::shut()
- {
- // We add DisableOpenGL
- DisableOpenGL( mhWnd, hDC, hRC );
- // subclass it back
- SubclassWindow(mhWnd, lpOldProc);
- mhWnd = NULL;
- mInitialized = FALSE;
- }
- NPBool nsPluginInstance::isInitialized()
- {
- return mInitialized;
- }
- const char * nsPluginInstance::getVersion()
- {
- return NPN_UserAgent(mInstance);
- }
- static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- nsPluginInstance* inst = (nsPluginInstance*)GetWindowLong(hWnd, GWL_USERDATA);
- HDC hDC = inst->hDC;
- HGLRC hRC = inst->hRC;
- switch (msg) {
- case WM_PAINT:
- {
- wglMakeCurrent( hDC, hRC );
- if (inst->viewer.valid()) inst->viewer->frame();
- SwapBuffers( hDC );
- }
- break;
- case WM_TIMER:
- PostMessage( hWnd, WM_PAINT, NULL, NULL );
- break;
- case WM_LBUTTONDOWN:
- {
- short xp = LOWORD(lParam);
- short yp = HIWORD(lParam);
- inst->mousebutton(0,0,xp,yp);
- break;
- }
- case WM_LBUTTONUP:
- {
- short xp = LOWORD(lParam);
- short yp = HIWORD(lParam);
- inst->mousebutton(0,1,xp,yp);
- break;
- }
- case WM_RBUTTONDOWN:
- {
- short xp = LOWORD(lParam);
- short yp = HIWORD(lParam);
- inst->mousebutton(2,0,xp,yp);
- }
- break;
- case WM_RBUTTONUP:
- {
- short xp = LOWORD(lParam);
- short yp = HIWORD(lParam);
- inst->mousebutton(2,1,xp,yp);
- break;
- }
- case WM_MOUSEMOVE:
- {
- short xp = LOWORD(lParam);
- short yp = HIWORD(lParam);
- inst->mousemove(xp,yp);
- break;
- }
- default:
- break;
- }
- return DefWindowProc(hWnd, msg, wParam, lParam);
- }
- void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
- {
- PIXELFORMATDESCRIPTOR pfd;
- int format;
- // get the device context (DC)
- *hDC = GetDC( hWnd ); // set the pixel format for the DC
- ZeroMemory( &pfd, sizeof( pfd ) );
- pfd.nSize = sizeof( pfd );
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
- PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 16;
- pfd.iLayerType = PFD_MAIN_PLANE;
- format = ChoosePixelFormat( *hDC, &pfd );
- SetPixelFormat( *hDC, format, &pfd );
- // create and enable the render context (RC)
- *hRC = wglCreateContext( *hDC );
- wglMakeCurrent( *hDC, *hRC );
- }
- // Disable OpenGL
- void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
- {
- wglMakeCurrent( NULL, NULL );
- wglDeleteContext( hRC );
- ReleaseDC( hWnd, hDC );
- }