Advertisement

Ported Nehe tutorials 2-10 to Firefox [multiple plugins per page issue]

Started by March 13, 2005 12:20 AM
41 comments, last by shadowwz 19 years, 8 months ago
I'd like to be able to render with OpenGL to a RECT as part of a plugin. But it needs to be offscreen so it doesn't affect the main application. But I'm running into an issue where I can't create a rendering context, maybe you can find my error. Here is the original sample code:

uint16 nsPluginInstance::HandleEvent(void* aEvent)
{
  NPEvent * event = (NPEvent *)aEvent;
  switch (event->event) {
    case WM_PAINT: 
    {
      if(!mWindow)
        break;

      // get the dirty rectangle to update or repaint the whole window
      RECT * drc = (RECT *)event->lParam;
      if(drc)
        FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));
      else {
        RECT rc;
        rc.bottom = mWindow->y + mWindow->height;
        rc.left   = mWindow->x;
        rc.right  = mWindow->x + mWindow->width;
        rc.top    = mWindow->y;
        FillRect((HDC)event->wParam, &rc, (HBRUSH)(COLOR_ACTIVECAPTION+1));
      }
      break;
    }
    case WM_KEYDOWN:
    {
      Beep(1000,100);
      break;
    }
    default:
      return 0;
  }
  return 1;
}
And here, I'm trying to create a rendering context without creating a window. But the message box comes up with "Can't Create A GL Rendering Context."...

uint16 nsPluginInstance::HandleEvent(void* aEvent)
{
  NPEvent * event = (NPEvent *)aEvent;
  switch (event->event) {
    case WM_PAINT: 
    {
      if(!mWindow)
        break;

      // get the dirty rectangle to update or repaint the whole window
      RECT * drc = (RECT *)event->lParam;
      if(drc)
        FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));
      else {
        RECT rc;
        rc.bottom = mWindow->y + mWindow->height;
        rc.left   = mWindow->x;
        rc.right  = mWindow->x + mWindow->width;
        rc.top    = mWindow->y;

        //FillRect((HDC)event->wParam, &rc, (HBRUSH)(COLOR_ACTIVECAPTION+1));

	  HWND hWND=(HWND)mWindow->window;
	  if (!hWND)
		  MessageBox(NULL,"Can't get the window handle.","ERROR",MB_OK|MB_ICONEXCLAMATION);
	  else
	  {
		  char message[50] = {0};
		  wsprintf(message, "Found Window Handle: %hu", hWND);
		  MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONEXCLAMATION);
	  }

	  HDC hDC = GetDC(hWND);
	  if (!hDC)
		  MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
	  else
	  {
		  char message[50] = {0};
		  wsprintf(message, "Found GL Device Context: %hu", hDC);
		  MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONEXCLAMATION);
	  }

        HGLRC hRC = wglCreateContext(hDC);
        if(!hRC)
          MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);

        if(!wglMakeCurrent(hDC,hRC))
          MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
      }
      break;
    }
    case WM_KEYDOWN:
    {
      Beep(1000,100);
      break;
    }
    default:
      return 0;
  }
  return 1;
}
[Edited by - tgraupmann on March 19, 2005 7:35:10 PM]
*News tagenigma.com is my new domain.
I'm tempted to just create a hidden window.
*News tagenigma.com is my new domain.
Advertisement
why not to create child window with same size as rect and then create your contex
This isn't the best solution, but I have Firefox creating a visible window.

I just had to add the following lines to [plugin.cpp].

#include "lesson2.h" // Nehe example 2

extern bool	fullscreen;NPBool nsPluginInstance::init(NPWindow* aWindow){	// I think we'll need a hidden window to render to	fullscreen=FALSE; // will always be windowed in browser		// Create Our OpenGL Window	if (!CreateGLWindow("NeHe's First Polygon Tutorial",640,480,16,fullscreen))	{		return FALSE; // Quit If Window Was Not Created	}		// no GUI to init in windowless case	mInitialized = TRUE;	return TRUE;}


void nsPluginInstance::shut(){	// Shutdown	KillGLWindow(); // Kill the OpenGL Window		// no GUI to shut in windowless case	mInitialized = FALSE;}


extern HDC hDC;uint16 nsPluginInstance::HandleEvent(void* aEvent){  NPEvent * event = (NPEvent *)aEvent;  switch (event->event) {    case WM_PAINT:     {      if(!mWindow)        break;	  // get the dirty rectangle to update or repaint the whole window      RECT * drc = (RECT *)event->lParam;      if(drc)	  {		  FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));		  DrawGLScene();		  SwapBuffers(hDC);	  }      else {        RECT rc;        rc.bottom = mWindow->y + mWindow->height;        rc.left   = mWindow->x;        rc.right  = mWindow->x + mWindow->width;        rc.top    = mWindow->y;        FillRect((HDC)event->wParam, &rc, (HBRUSH)(COLOR_ACTIVECAPTION+1));		DrawGLScene();		SwapBuffers(hDC);      }      break;    }    case WM_KEYDOWN:    {      Beep(1000,100);      break;    }    default:      return 0;  }  return 1;}
*News tagenigma.com is my new domain.
Quote: Original post by shadowwz
why not to create child window with same size as rect and then create your contex


Good suggestion. I'll give that a shot.
*News tagenigma.com is my new domain.
also the problem was in the pixelformat,you used default wich maybe can be used for opengl contex
Advertisement
What's the command you use to draw a BITMAP on a RECT?

	  // get the dirty rectangle to update or repaint the whole window      RECT * drc = (RECT *)event->lParam;      if(drc)	  {		  DrawGLScene();		  SwapBuffers(hDC);		  //FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));		  HDC hdcDest = CreateCompatibleDC(hDC);		  HBITMAP hBitmap = CreateCompatibleBitmap(hDC,childRect.right,childRect.bottom);		  HGDIOBJ oldObj = SelectObject(hdcDest,hBitmap);		  BitBlt(hdcDest,0,0,childRect.right,childRect.bottom,				hDC,0,0,SRCCOPY);		  // Draw the bitmap on the RECT called drc		  SelectObject(hDC,oldObj);		  DeleteDC(hdcDest);		  DeleteObject(hBitmap);	  }
*News tagenigma.com is my new domain.
if your opengl window is hidden,it may not work with device contex.
- you can draw scene to texture, wich can be saved to bitmap - or using glReadPixel to save scene to bitmap.
Quote: Original post by tgraupmann
This isn't the best solution, but I have Firefox creating a visible window.

I just had to add the following lines to [plugin.cpp].

#include "lesson2.h" // Nehe example 2

extern bool	fullscreen;NPBool nsPluginInstance::init(NPWindow* aWindow){	// I think we'll need a hidden window to render to	fullscreen=FALSE; // will always be windowed in browser		// Create Our OpenGL Window	if (!CreateGLWindow("NeHe's First Polygon Tutorial",640,480,16,fullscreen))	{		return FALSE; // Quit If Window Was Not Created	}		// no GUI to init in windowless case	mInitialized = TRUE;	return TRUE;}



don't you need to update window handle of aWindow,with new created one ?

Quote:

extern HDC hDC;uint16 nsPluginInstance::HandleEvent(void* aEvent){  NPEvent * event = (NPEvent *)aEvent;  switch (event->event) {    case WM_PAINT:     {      if(!mWindow)        break;	  // get the dirty rectangle to update or repaint the whole window      RECT * drc = (RECT *)event->lParam;      if(drc)	  {		  FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));		  DrawGLScene();		  SwapBuffers(hDC);	  }      else {        RECT rc;        rc.bottom = mWindow->y + mWindow->height;        rc.left   = mWindow->x;        rc.right  = mWindow->x + mWindow->width;        rc.top    = mWindow->y;        FillRect((HDC)event->wParam, &rc, (HBRUSH)(COLOR_ACTIVECAPTION+1));		DrawGLScene();		SwapBuffers(hDC);      }      break;    }


why you using "FillRect((HDC)event->wParam,&rc,(HBRUSH)(COLOR_ACTIVECAPTION+1));"
if you using "DrawGLScene(); SwapBuffers(hDC);" ?

also maybe you should change the name of "extern HDC hDC;" to something else,you may be not initialized it.

You could take a look at pBuffers or RENDER_TO_TEXTURE extensions. They are quite easy to create, and designed for offscreen rendering.

If you need some help i could explain some more, but i think you can find all you need using google. If not, let me know.

[edit]Don't know if firefox supports these features though[/edit]

This topic is closed to new replies.

Advertisement