home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mesa5.zip / mesa5src.zip / MesaDLL / os2wgl.cpp < prev    next >
Text File  |  2002-12-23  |  12KB  |  466 lines

  1. /* $Id: os2wgl.c,v 1.3 1998/09/16 02:46:56 brianp Exp $ */
  2.  
  3. /*
  4. * File name    : os2wgl.c
  5. * WGL stuff. Added by Oleg Letsinsky, ajl@ultersys.ru
  6. * Some things originated from the 3Dfx WGL functions
  7. * changes EK
  8. */
  9. /* changes (c) EK */
  10. #define POKA 0
  11.  
  12.  
  13. #define USE_MESA 1
  14. #define WARPMESA_USEPMGPI 1
  15. #define WARPMESA_USEDIVE  0
  16.  
  17. #define GL_GLEXT_PROTOTYPES
  18.  
  19. #include "GL/gl.h"
  20. #include "GL/glext.h"
  21. //#include <GL/glu.h>
  22.  
  23. #include <stdio.h>
  24. #include "WarpGL.h"
  25.  
  26.  
  27. #include "os2mesadef.h"
  28. #include "GL/os2mesa.h"
  29. #include "mtypes.h"
  30. #include "glapi.h"
  31.  
  32. #define MAX_MESA_ATTRS 20
  33.  
  34. struct __pixelformat__
  35. {
  36.     PIXELFORMATDESCRIPTOR      pfd;
  37.     GLboolean doubleBuffered;
  38. };
  39.  
  40. struct __pixelformat__ pix[] =
  41. {
  42.     /* Double Buffer, alpha */
  43.     {  {       sizeof(PIXELFORMATDESCRIPTOR),  1,
  44.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
  45.         PFD_TYPE_RGBA,
  46.         24,    8,      0,      8,      8,      8,      16,     8,      24,
  47.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  48.         GL_TRUE
  49.     },
  50.     /* Single Buffer, alpha */
  51.     {  {       sizeof(PIXELFORMATDESCRIPTOR),  1,
  52.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT,
  53.         PFD_TYPE_RGBA,
  54.         24,    8,      0,      8,      8,      8,      16,     8,      24,
  55.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  56.         GL_FALSE
  57.     },
  58.     /* Double Buffer, no alpha */
  59.     {  {       sizeof(PIXELFORMATDESCRIPTOR),  1,
  60.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
  61.         PFD_TYPE_RGBA,
  62.         24,    8,      0,      8,      8,      8,      16,     0,      0,
  63.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  64.         GL_TRUE
  65.     },
  66.     /* Single Buffer, no alpha */
  67.     {  {       sizeof(PIXELFORMATDESCRIPTOR),  1,
  68.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT,
  69.         PFD_TYPE_RGBA,
  70.         24,    8,      0,      8,      8,      8,      16,     0,      0,
  71.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  72.         GL_FALSE
  73.     },
  74. };
  75.  
  76. int                            qt_ext = sizeof(pix) / sizeof(pix[0]);
  77.  
  78. #if POKA
  79. struct __pixelformat__ pix[] =
  80. {
  81.     {  {       sizeof(XVisualInfo),  1,
  82.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT|PFD_DOUBLEBUFFER|PFD_SWAP_COPY,
  83.         PFD_TYPE_RGBA,
  84.         24,    8,      0,      8,      8,      8,      16,     8,      24,
  85.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  86.         GL_TRUE
  87.     },
  88.     {  {       sizeof(XVisualInfo),  1,
  89.         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_GENERIC_FORMAT,
  90.         PFD_TYPE_RGBA,
  91.         24,    8,      0,      8,      8,      8,      16,     8,      24,
  92.         0,     0,      0,      0,      0,      16,     8,      0,      0,      0,      0,      0,      0 },
  93.         GL_FALSE
  94.     },
  95. };
  96. #endif
  97.  
  98. int  qt_pix = sizeof(pix) / sizeof(pix[0]);
  99.  
  100.  
  101. typedef struct {
  102.     WMesaContext ctx;
  103.     HDC hdc;
  104. } MesaWglCtx;
  105.  
  106. #define MESAWGL_CTX_MAX_COUNT 20
  107.  
  108. static MesaWglCtx wgl_ctx[MESAWGL_CTX_MAX_COUNT];
  109.  
  110. static unsigned ctx_count = 0;
  111. static unsigned ctx_current = -1;
  112. static unsigned curPFD = 0;
  113.  
  114. HAB   hab;      /* PM anchor block handle         */
  115. HPS   hpsCurrent;
  116. int   PMuseDive=0;
  117.  
  118. /* Must be called at initialization */
  119. /* possibly at any thread but yet it is not multy-PM-thread  */
  120. void APIENTRY  OS2_WMesaInitHab(HAB  proghab,int useDive)
  121. { hab = proghab;
  122.   PMuseDive = useDive;
  123.     /* mask Underflow exeptions for each thread */
  124.     _control87(EM_UNDERFLOW,EM_UNDERFLOW);
  125. //printf("1 useDive=%i\n",useDive);
  126.  
  127. }
  128.  
  129.  
  130.  
  131. WGLAPI BOOL GLAPIENTRY wglCopyContext(HGLRC hglrcSrc,HGLRC hglrcDst,UINT mask)
  132. {
  133.     return(FALSE);
  134. }
  135.  
  136.  
  137. WGLAPI HGLRC GLAPIENTRY wglCreateContext(HDC hdc,HPS hpsCurrent, HAB hab)
  138. {
  139.     HWND               hWnd;
  140.     int i = 0;
  141.     if(!(hWnd = WinWindowFromDC(hdc)))
  142.     {
  143.         SetLastError(0);
  144.         return(NULL);
  145.     }
  146.     if (!ctx_count)
  147.     {
  148.        for(i=0;i<MESAWGL_CTX_MAX_COUNT;i++)
  149.        {
  150.                wgl_ctx[i].ctx = NULL;
  151.                wgl_ctx[i].hdc = NULLHANDLE;
  152.        }
  153.     }
  154.     for( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  155.     {
  156.         if ( wgl_ctx[i].ctx == NULL )
  157.         {
  158.             wgl_ctx[i].ctx = WMesaCreateContext( hWnd, NULL,
  159.                                        hpsCurrent, hab, GL_TRUE,
  160.                                        pix[curPFD-1].doubleBuffered,PMuseDive );
  161.  
  162.             if (wgl_ctx[i].ctx == NULL)
  163.                 break;
  164.             wgl_ctx[i].hdc = hdc;
  165.             ctx_count++;
  166.             return ((HGLRC)wgl_ctx[i].ctx);
  167.         }
  168.     }
  169.     SetLastError(0);
  170.     return(NULL);
  171. }
  172.  
  173.  
  174. WGLAPI BOOL wglDeleteContext(HGLRC hglrc)
  175. {
  176.     int i;
  177.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  178.     {
  179.        if ( wgl_ctx[i].ctx == (PWMC) hglrc )
  180.        {
  181.             WMesaMakeCurrent((PWMC)hglrc);
  182.             WMesaDestroyContext();
  183.             wgl_ctx[i].ctx = NULL;
  184.             wgl_ctx[i].hdc = NULLHANDLE;
  185.             ctx_count--;
  186.             return(TRUE);
  187.        }
  188.     }
  189.     SetLastError(0);
  190.     return(FALSE);
  191. }
  192.  
  193. HGLRC wglCreateLayerContext(HDC hdc,int iLayerPlane)
  194. {
  195.     SetLastError(0);
  196.     return(NULL);
  197. }
  198.  
  199. HGLRC wglGetCurrentContext(VOID)
  200. {
  201.    if (ctx_current < 0)
  202.       return 0;
  203.    else
  204.       return wgl_ctx[ctx_current].ctx;
  205. }
  206.  
  207. HDC wglGetCurrentDC(VOID)
  208. {
  209.    if (ctx_current < 0)
  210.       return 0;
  211.    else
  212.       return wgl_ctx[ctx_current].hdc;
  213. }
  214.  
  215. BOOL wglMakeCurrent(HDC hdc,HGLRC hglrc)
  216. {
  217.     int i;
  218.  
  219.     /* new code suggested by Andy Sy */
  220.     if (!hdc || !hglrc) {
  221.        WMesaMakeCurrent(NULL);
  222.        ctx_current = -1;
  223.        return TRUE;
  224.     }
  225.  
  226.     for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ )
  227.     {
  228.         if ( wgl_ctx[i].ctx == hglrc )
  229.         {
  230.             wgl_ctx[i].hdc = hdc;
  231.             WMesaMakeCurrent( (WMesaContext) hglrc );
  232.             ctx_current = i;
  233.             return TRUE;
  234.         }
  235.     }
  236.     return FALSE;
  237. }
  238.  
  239.  
  240. BOOL  wglShareLists(HGLRC hglrc1,HGLRC hglrc2)
  241. {
  242.     return(TRUE);
  243. }
  244.  
  245. BOOL  wglUseFontBitmapsA(HDC hdc,int first,int count,int listBase)
  246. {
  247.     return(FALSE);
  248. }
  249.  
  250. BOOL  wglUseFontBitmapsW(HDC hdc,int first,int count,int listBase)
  251. {
  252.     return(FALSE);
  253. }
  254.  
  255. #if POKA
  256. BOOL  wglUseFontOutlinesA(HDC hdc,int first,int count,
  257.                                   int listBase,float deviation,
  258.                                   float extrusion,int format,
  259.                                   LPGLYPHMETRICSFLOAT lpgmf)
  260. {
  261.     SetLastError(0);
  262.     return(FALSE);
  263. }
  264.  
  265.  BOOL  wglUseFontOutlinesW(HDC hdc,int first,int count,
  266.                                   int listBase,float deviation,
  267.                                   float extrusion,int format,
  268.                                   LPGLYPHMETRICSFLOAT lpgmf)
  269. {
  270.     SetLastError(0);
  271.     return(FALSE);
  272. }
  273.  
  274.  BOOL  wglDescribeLayerPlane(HDC hdc,int iPixelFormat,
  275.                                     int iLayerPlane,UINT nBytes,
  276.                                     LPLAYERPLANEDESCRIPTOR plpd)
  277. {
  278.     SetLastError(0);
  279.     return(FALSE);
  280. }
  281.  
  282.  int  wglSetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  283.                                        int iStart,int cEntries,
  284.                                        CONST COLORREF *pcr)
  285. {
  286.     SetLastError(0);
  287.     return(0);
  288. }
  289.  
  290.  int  wglGetLayerPaletteEntries(HDC hdc,int iLayerPlane,
  291.                                        int iStart,int cEntries,
  292.                                        COLORREF *pcr)
  293. {
  294.     SetLastError(0);
  295.     return(0);
  296. }
  297.  
  298. #endif /* POKA */
  299.  
  300.  BOOL  wglRealizeLayerPalette(HDC hdc,int iLayerPlane,BOOL bRealize)
  301. {
  302.     SetLastError(0);
  303.     return(FALSE);
  304. }
  305.  
  306.  BOOL  wglSwapLayerBuffers(HDC hdc,UINT fuPlanes)
  307. {
  308.     if( !hdc )
  309.     {
  310.         WMesaSwapBuffers();
  311.         return(TRUE);
  312.     }
  313.     SetLastError(0);
  314.     return(FALSE);
  315. }
  316.  
  317.  
  318.  int  wglChoosePixelFormat(HDC hdc,
  319.                                    PIXELFORMATDESCRIPTOR *ppfd)
  320. {
  321.     int                i,best = -1,bestdelta = 0x7FFFFFFF,delta,qt_valid_pix;
  322.  
  323.     qt_valid_pix = qt_pix;
  324.     if(ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR) || ppfd->nVersion != 1)
  325.     {
  326.         SetLastError(0);
  327.         return(0);
  328.     }
  329.     for(i = 0;i < qt_valid_pix;i++)
  330.     {
  331.         delta = 0;
  332.         if(
  333.             (ppfd->dwFlags & PFD_DRAW_TO_WINDOW) &&
  334.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_WINDOW))
  335.             continue;
  336.         if(
  337.             (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) &&
  338.             !(pix[i].pfd.dwFlags & PFD_DRAW_TO_BITMAP))
  339.             continue;
  340.         if(
  341.             (ppfd->dwFlags & PFD_SUPPORT_GDI) &&
  342.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_GDI))
  343.             continue;
  344.         if(
  345.             (ppfd->dwFlags & PFD_SUPPORT_OPENGL) &&
  346.             !(pix[i].pfd.dwFlags & PFD_SUPPORT_OPENGL))
  347.             continue;
  348.         if(
  349.             !(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
  350.             ((ppfd->dwFlags & PFD_DOUBLEBUFFER) != (pix[i].pfd.dwFlags & PFD_DOUBLEBUFFER)))
  351.             continue;
  352.         if(
  353.             !(ppfd->dwFlags & PFD_STEREO_DONTCARE) &&
  354.             ((ppfd->dwFlags & PFD_STEREO) != (pix[i].pfd.dwFlags & PFD_STEREO)))
  355.             continue;
  356.         if(ppfd->iPixelType != pix[i].pfd.iPixelType)
  357.             delta++;
  358.         if(delta < bestdelta)
  359.         {
  360.             best = i + 1;
  361.             bestdelta = delta;
  362.             if(bestdelta == 0)
  363.                 break;
  364.         }
  365.     }
  366.     if(best == -1)
  367.     {
  368.         SetLastError(0);
  369.         return(0);
  370.     }
  371.     return(best);
  372. }
  373.  
  374.  
  375. XVisualInfo *wglDescribePixelFormat(int iPixelFormat)
  376. {
  377.     if(iPixelFormat < 1 || iPixelFormat > qt_pix)
  378.     {
  379.         SetLastError(0);
  380.         return(0);
  381.     }
  382.     return  &(pix[iPixelFormat - 1].pfd);
  383. }
  384.  
  385. #if POKA
  386.  
  387. /*
  388. * GetProcAddress - return the address of an appropriate extension
  389. */
  390.  PROC  wglGetProcAddress(LPCSTR lpszProc)
  391. {
  392.     int                i;
  393.     for(i = 0;i < qt_ext;i++)
  394.         if(!strcmp(lpszProc,ext[i].name))
  395.             return(ext[i].proc);
  396.  
  397.         SetLastError(0);
  398.         return(NULL);
  399. }
  400.  
  401. #endif /* POKA */
  402.  int  wglGetPixelFormat(HDC hdc)
  403. {
  404.     if(curPFD == 0)
  405.     {
  406.         SetLastError(0);
  407.         return(0);
  408.     }
  409.     return(curPFD);
  410. }
  411.  
  412. /* I don't understand all this suxx PixelFormat manipulation */
  413. int evglSetPixelFormat(int iPixelFormat)
  414. {
  415.    if(iPixelFormat == 1  || iPixelFormat == 2)
  416.          curPFD = iPixelFormat;
  417.    else
  418.          curPFD = 1;
  419.    return 0;
  420. }
  421.  
  422.  BOOL  wglSetPixelFormat(HDC hdc,int iPixelFormat,
  423.                                 PIXELFORMATDESCRIPTOR *ppfd)
  424. {
  425.     int                qt_valid_pix;
  426.  
  427.     qt_valid_pix = qt_pix;
  428. #if POKA == 200
  429.     if(iPixelFormat < 1 || iPixelFormat > qt_valid_pix || ppfd->nSize != sizeof(PIXELFORMATDESCRIPTOR))
  430.     {
  431.         SetLastError(0);
  432.         return(FALSE);
  433.     }
  434. #endif /*  POKA == 200 */
  435.     curPFD = iPixelFormat;
  436.     return(TRUE);
  437. }
  438.  
  439.  
  440.  
  441. BOOL  wglSwapBuffers(void)
  442. {
  443.    if (ctx_current < 0)
  444.       return FALSE;
  445.  
  446.    if(wgl_ctx[ctx_current].ctx == NULL) {
  447.       SetLastError(0);
  448.       return(FALSE);
  449.    }
  450.    WMesaSwapBuffers();
  451.    return(TRUE);
  452. }
  453.  
  454.  
  455. /*************************************/
  456. static int LastError=0;
  457.  
  458. int SetLastError(int nerr)
  459. {  LastError = nerr;
  460.    return 0;
  461. }
  462. int GetLastError(void)
  463. {  return LastError;
  464. }
  465.  
  466.