Robert Osfield schrieb:
Hi Andreas,

I had a quick review of the plugin.cpp you posted and it's tiny, which
is definitely a good thing ;-)

I see that code you are using is being passed in a native window
handle so there is probably a reasonable chance that we could just use
this with and have the osgViewer create the graphics context using the
window inheritance feature.  This might be a way of make the code more
portable across platforms as well as it would reduce the amount of
platform specific code.  This is a bridge that could be crossed later
though.

In the review of the code I spotted that the plugin.h was referenced,
but you didn't include the plugin.h.  Could you post this.  Without
the header I can't say for sure, but my guess is that the OSG related
global variables could be moved into the nsPluginInstance class that
you've created.

Do you have particular web documentation you've been using for inspiration?

Robert.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Find the attached files. I have already refactored this not to use global variables. The sources I used are here:


http://www.codeproject.com/KB/openGL/FirefoxOpenGL.aspx


and the osgglut-example.

Regards,

Andreas
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#ifndef __PLUGIN_H__
#define __PLUGIN_H__

#include "pluginbase.h"
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <windows.h>
#include <windowsx.h>

class nsPluginInstance : public nsPluginInstanceBase
{
public:
        nsPluginInstance(NPP aInstance);
        ~nsPluginInstance();

        NPBool init(NPWindow* aWindow);
        void shut();
        NPBool isInitialized();

        // locals
        const char * getVersion();
        void mousebutton( int button, int state, int x, int y );
        void mousemove( int x, int y );
        char* filename;
        int width;
        int height;
        HDC hDC;
        HGLRC hRC;
        osg::ref_ptr<osgViewer::Viewer> viewer;
private:
        NPP mInstance;
        NPBool mInitialized;
        HWND mhWnd;
        
        osg::observer_ptr<osgViewer::GraphicsWindow> window;
        
        

};

#endif // __PLUGIN_H__
/*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>



/*
//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]) ; 
		
	}
	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:
	osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(filename);
	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());
	viewer->setCameraManipulator(new osgGA::TrackballManipulator);
	viewer->addEventHandler(new osgViewer::StatsHandler);
	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 );
}    
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to