home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / srcos2 / vbglcnv.cpp < prev    next >
C/C++ Source or Header  |  1998-11-13  |  8KB  |  237 lines

  1. //===============================================================
  2. // vbaseGLCanvas - a basic GL window canvas for drawing
  3. //
  4. // Copyright (C) 1995-1998  Bruce E. Wampler
  5. //
  6. // This file is part of the V C++ GUI Framework, and is covered
  7. // under the terms of the GNU Library General Public License,
  8. // Version 2. This library has NO WARRANTY. See the source file
  9. // vapp.cxx for more complete information about license terms.
  10. //===============================================================
  11. #include <v/vbglcnv.h>          // our header
  12. #include <v/vapp.h>
  13. #include <v/vos2.h>
  14. #include <v/vwindow.h>
  15. #include <stdlib.h>
  16.  
  17. // OpenGL init
  18.   int vBaseGLCanvasPane::_pixelFormat = 0;
  19.   int vBaseGLCanvasPane::_doubleBuffer = 0;   // if we have double buffering
  20.   unsigned int vBaseGLCanvasPane::_vGLmode = 0;
  21.  
  22. //================>>> vBaseGLCanvasPane::vBaseGLCanvasPane <<<========================
  23.   vBaseGLCanvasPane::vBaseGLCanvasPane(unsigned int vGLmode,
  24.     PaneType pt) : vCanvasPane(pt)
  25.   {
  26.     SysDebug(Constructor,"vBaseGLCanvasPane::vBaseGLCanvasPane() constructor\n")
  27.     const unsigned int defGLmode = (vGL_RGB | vGL_DoubleBuffer | vGL_Depth);
  28.     _vGLmode = ((vGLmode == vGL_Default) ? defGLmode : vGLmode);
  29.     _hDC = 0;          // PS is not needed for Direct OpenGL
  30.     _hGLRC = 0;        // handle to OPENGL display context
  31.     _hPalette = 0;     // index mode (palette) not supported in OS/2
  32.     _hab = theApp->AppHab();
  33.   }
  34.  
  35. //================>>> vBaseGLCanvasPane::~vBaseGLCanvasPane <<<===================
  36.   vBaseGLCanvasPane::~vBaseGLCanvasPane()
  37.   {
  38.     SysDebug(Destructor,"vBaseGLCanvasPane::~vBaseGLCanvasPane() destructor\n")
  39.  
  40.     // OpenGL: finish OpenGL rendering
  41.     if (_hDC)
  42.       WinReleasePS(_hDC);
  43.  
  44.     if (_hGLRC)
  45.     {
  46.       pglMakeCurrent(_hab, 0, 0);
  47.       pglDestroyContext(_hab, _hGLRC);
  48.     }
  49.  
  50.     if (_hPalette)
  51.       GpiDeletePalette(_hPalette);
  52.   }
  53.  
  54. //==================>>> vBaseGLCanvasPane::initialize <<<==========================
  55.   void vBaseGLCanvasPane::initialize(vWindow* pWindow, HWND pWidget)
  56.   {
  57.     vCanvasPane::initialize(pWindow, pWidget);  // init std canvas
  58.     // the initialize will call CreateDC, which will normally create
  59.     // a PM presentation space.
  60.     //
  61.     // If we use the Direct OPENGL context we do not need a PS.
  62.     // The downside is no Gpi functions will be available to use in
  63.     // the OPENGL canvas.  I think this is also true of the X and windoze
  64.     // ports so will be okay.
  65.     // Will need to override CreateDC so that it does not create a PS.
  66.  
  67.     // first we need to check to see if OpenGL is installed on system
  68.     // If not, we punt with error message
  69.  
  70.     if (!pglQueryCapability(_hab))
  71.     {
  72.       UserDebug(WindowEvents,"No OpenGL support on this system! \n");
  73.       WinMessageBox(HWND_DESKTOP, _drawCanvas,
  74.     "No OpenGL support installed on this system. Use the OS/2 Selective Install utility to add OpenGL support.", "Error", 0,
  75.     MB_ERROR | MB_OK);
  76.       return;
  77.     }
  78.  
  79.     if (!_hGLRC)
  80.     {
  81.       // Initialize OpenGL rendering
  82.       // Set up the rendering pixel format
  83.       _setUpPixelFormat();
  84.  
  85.       // Set up Palette
  86.       _setUpPalette();
  87.  
  88.       // create a direct OpenGL context
  89.       _hGLRC = pglCreateContext( _hab, _pVisualConfig, 0, TRUE );
  90.  
  91.       // free the visual config
  92.       // EMX compiled apps bomb if you free the visual config!!
  93.       // IBM says EMX free() may not be compatible with VAC++
  94.       // calloc()/malloc() that OPENGL dll's are using
  95.       // Anyway, shouldn't matter since memory amount is small
  96. //      free( _pVisualConfig );
  97.     }
  98.  
  99.     // initialize graphic
  100.     graphicsInit();
  101.   }
  102.  
  103. #ifdef USECREATEDC
  104. //==================>>> vBaseGLCanvasPane::CreateDC <<<========================
  105.   void vBaseGLCanvasPane::CreateDC(void)
  106.   {
  107.     // This overrides the CreateDC of vCanvasPane and creates
  108.     // an OpenGL compatible DC, and then sets up the rendering stuff
  109.  
  110. //    vCanvasPane::CreateDC();   // we can have both regular DC
  111.                    // and a OpenGL DC.
  112.   }
  113. #endif
  114.  
  115. //==================>>> vBaseGLCanvasPane::_setUpPixelFormat <<<==========================
  116.   void vBaseGLCanvasPane::_setUpPixelFormat()
  117.   {
  118.     _doubleBuffer = 1;
  119.  
  120.     // prepare the mode
  121.     if (_vGLmode & vGL_Indexed)   // Indexed mode *********************
  122.     {
  123.       // for now we don't support indexed mode, since OS/2 can
  124.       // do a pretty good job of palette management itself on
  125.       // 256 color machines anyway
  126.       UserDebug(WindowEvents,"vBaseGLCanvasPane::_setUpPixelFormat: OpenGL starts (index mode not supported in OS/2)");
  127.  
  128.     }
  129.     else    // RGBA mode ********************
  130.     {
  131.       UserDebug(WindowEvents,"vBaseGLCanvasPane::_setUpPixelFormat: OpenGL starts in RGB, Double buffering and Depth mode");
  132.  
  133.       int attribList[] = { PGL_RGBA, PGL_RED_SIZE, 2, PGL_GREEN_SIZE, 2,
  134.     PGL_BLUE_SIZE, 2, PGL_ALPHA_SIZE, 1, PGL_DOUBLEBUFFER, PGL_None };
  135.  
  136.       // create a visual config
  137.       _pVisualConfig = pglChooseConfig( _hab, attribList );
  138.  
  139.       // if nothing suitable, downgrade to single buffer and retry
  140.       if ( _pVisualConfig == 0)
  141.       {
  142.     _doubleBuffer = 0;
  143.     int attribList[] = { PGL_RGBA, PGL_RED_SIZE, 2, PGL_GREEN_SIZE, 2,
  144.       PGL_BLUE_SIZE, 2, PGL_ALPHA_SIZE, 1, PGL_SINGLEBUFFER, 0 };
  145.  
  146.     // create a visual config
  147.     _pVisualConfig = pglChooseConfig( _hab, attribList );
  148.  
  149.     UserDebug(WindowEvents,"without Double buffering");
  150.       }
  151.       else
  152.     UserDebug(WindowEvents,"with Double buffering");
  153.     }
  154.  
  155.     if (_pVisualConfig == 0)
  156.       {
  157.     WinMessageBox(HWND_DESKTOP, _drawCanvas,
  158.       "pglChooseConfig failed.", "Error", 0,
  159.       MB_ERROR | MB_OK);
  160.     exit(1);
  161.       }
  162.  
  163.     UserDebug(WindowEvents,"\n");
  164.   }
  165.  
  166. //==================>>> vBaseGLCanvasPane::_setUpPalette <<<==========================
  167.   void vBaseGLCanvasPane::_setUpPalette()
  168.   {
  169.     // Index mode not supported in this release
  170. /*
  171.     PVOID pPal;
  172.  
  173.     int paletteSize;
  174.     DescribePixelFormat(_hDC, _pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  175.     if (pfd.dwFlags & PFD_NEED_PALETTE)
  176.     {
  177.       paletteSize = 1 << pfd.cColorBits;
  178.     }
  179.     else
  180.     {
  181.       return;
  182.     }
  183.     pPal =  malloc(paletteSize * sizeof(RGB2));
  184.  
  185.     // build a simple RGB color palette
  186.     {
  187.       int i;
  188.       for (i=0; i<paletteSize; ++i)
  189.       {
  190.     *ppal = SETRGB(255,255,255);
  191.     ppal ++;
  192.       }
  193.     }
  194.  
  195.     _hPalette = GpiCreatePalette(_hab, 0L, LCOLF_CONSECRGB, paletteSize, pPal);
  196.     free(pPal);
  197.  
  198.     if (_hPalette)
  199.     {
  200.       GpiSelectPalette(_hDC, _hPalette);
  201.       ULONG cclr;
  202.       WinRealizePalette ( WindowFromDC(_hDC), _hDC, &cclr)
  203.     }
  204. */
  205.   }
  206.  
  207. //==================>>> vBaseGLCanvasPane::graphicsInit <<<========================
  208.   void vBaseGLCanvasPane::graphicsInit(void)
  209.   {
  210.     // Perform only the most basic operations, including getting the context
  211.     pglMakeCurrent( _hab, _hGLRC, _drawCanvas );
  212.   }
  213.  
  214. //================>>> vBaseGLCanvasPane::vglMakeCurrent <<<===============
  215.   void vBaseGLCanvasPane::vglMakeCurrent(void)
  216.   {
  217.     pglMakeCurrent( _hab, _hGLRC, _drawCanvas );
  218.   }
  219.  
  220.   //==================>>> vBaseGLCanvasPane::vglFlush <<<========================
  221.   void vBaseGLCanvasPane::vglFlush(void)
  222.   {
  223.     if (_doubleBuffer)
  224.     pglSwapBuffers(_hab, _drawCanvas);
  225.     else
  226.     glFlush();
  227.   }
  228.  
  229.   //=====================>>> vBaseGLCanvasPane::Resize <<<============================
  230.   void vBaseGLCanvasPane::Resize(int newW, int newH)
  231.   {
  232.     // This is the routine the user will override to intercept size changes
  233.     vCanvasPane::Resize(newW,newH);
  234.     pglMakeCurrent( _hab, _hGLRC, _drawCanvas );
  235.     glViewport(0,0,newW,newH);
  236.   }
  237.