home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / opengl / openglobj.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  10KB  |  392 lines

  1. // OpenGLObj.cpp : Implementation of COpenGLApp and DLL registration.
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "OpenGL.h"
  15. #include "OpenGLObj.h"
  16. #include "glaux.h"
  17.  
  18. #pragma comment(lib, "winmm.lib")
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. //
  22.  
  23. unsigned char threeto8[8] =
  24. {
  25.     0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
  26. };
  27.  
  28. unsigned char twoto8[4] =
  29. {
  30.     0, 0x55, 0xaa, 0xff
  31. };
  32.  
  33. unsigned char oneto8[2] =
  34. {
  35.     0, 255
  36. };
  37.  
  38. static int defaultOverride[13] =
  39. {
  40.     0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
  41. };
  42.  
  43. static PALETTEENTRY defaultPalEntry[20] =
  44. {
  45.     { 0,   0,   0,    0 },
  46.     { 0x80,0,   0,    0 },
  47.     { 0,   0x80,0,    0 },
  48.     { 0x80,0x80,0,    0 },
  49.     { 0,   0,   0x80, 0 },
  50.     { 0x80,0,   0x80, 0 },
  51.     { 0,   0x80,0x80, 0 },
  52.     { 0xC0,0xC0,0xC0, 0 },
  53.  
  54.     { 192, 220, 192,  0 },
  55.     { 166, 202, 240,  0 },
  56.     { 255, 251, 240,  0 },
  57.     { 160, 160, 164,  0 },
  58.  
  59.     { 0x80,0x80,0x80, 0 },
  60.     { 0xFF,0,   0,    0 },
  61.     { 0,   0xFF,0,    0 },
  62.     { 0xFF,0xFF,0,    0 },
  63.     { 0,   0,   0xFF, 0 },
  64.     { 0xFF,0,   0xFF, 0 },
  65.     { 0,   0xFF,0xFF, 0 },
  66.     { 0xFF,0xFF,0xFF, 0 }
  67. };
  68.  
  69. void COpenGLObj::CreateContext(HDC hdc, RECT& rc)
  70. {
  71.     PIXELFORMATDESCRIPTOR pfd;
  72.     GLfloat     fMaxObjSize, fAspect;
  73.     GLfloat     fNearPlane, fFarPlane;
  74.  
  75.     if (!bSetupPixelFormat(hdc))
  76.         return;
  77.  
  78.     CreateRGBPalette(hdc);
  79.  
  80.     ::SelectPalette(hdc, m_hPal, FALSE);
  81.     ::RealizePalette(hdc);
  82.  
  83.     int n = ::GetPixelFormat(hdc);
  84.     ::DescribePixelFormat(hdc, n, sizeof(pfd), &pfd);
  85.  
  86.  
  87.     m_hrc = wglCreateContext(hdc);
  88.     wglMakeCurrent(hdc, m_hrc);
  89.  
  90.     glClearDepth(10.0f);
  91.     glEnable(GL_DEPTH_TEST);
  92.  
  93.     if (rc.bottom)
  94.         fAspect = (GLfloat)rc.right/rc.bottom;
  95.     else    // don't divide by zero, not that we should ever run into that...
  96.         fAspect = 1.0f;
  97.     fNearPlane = 3.0f;
  98.     fFarPlane = 20.0f;
  99.     fMaxObjSize = 3.0f;
  100.     m_fRadius = fNearPlane + fMaxObjSize / 2.0f;
  101.  
  102.     glMatrixMode(GL_PROJECTION);
  103.     glLoadIdentity();
  104.     gluPerspective(30.0f, fAspect, fNearPlane, fFarPlane);
  105.     glMatrixMode(GL_MODELVIEW);
  106. }
  107.  
  108. LRESULT COpenGLObj::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  109. {
  110.     // Won't be here unless we just got activated and created a window
  111.     HDC hdc = GetDC();
  112.     RECT rc;
  113.     GetClientRect(&rc);
  114.     CreateContext(hdc, rc);
  115.     // SetTimer(1, 33, NULL);
  116.     SetTimer(1, 33);
  117.     joySetThreshold(JOYSTICKID1, 10);
  118.     if (joySetCapture(m_hWnd, JOYSTICKID1, 30, TRUE) == JOYERR_NOERROR)
  119.         m_bstrCaption = _T("Joystick Mode");
  120.     m_bActive = TRUE;
  121.     return 0;
  122. }
  123.  
  124. LRESULT COpenGLObj::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  125. {
  126.     m_bActive = FALSE;
  127.     KillTimer(1);
  128.  
  129.     joyReleaseCapture(JOYSTICKID1);
  130.  
  131.     ::wglMakeCurrent(NULL,  NULL);
  132.  
  133.     if (m_hrc)
  134.     {
  135.         ::wglDeleteContext(m_hrc);
  136.         m_hrc = NULL;
  137.     }
  138.     return 0;
  139. }
  140.  
  141. LRESULT COpenGLObj::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  142. {
  143.     SetCapture();
  144.     m_bMouseCaptured = TRUE;
  145.     m_xPos = (short)LOWORD(lParam);  // horizontal position of cursor
  146.     m_yPos = (short)HIWORD(lParam);  // vertical position of cursor
  147.     return 0;
  148. }
  149.  
  150. LRESULT COpenGLObj::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  151. {
  152.     m_bMouseCaptured = FALSE;
  153.     ReleaseCapture();
  154.     return 0;
  155. }
  156.  
  157. LRESULT COpenGLObj::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  158. {
  159.     if (m_bMouseCaptured)
  160.     {
  161.         int xPos = (short)LOWORD(lParam);  // horizontal position of cursor
  162.         int yPos = (short)HIWORD(lParam);  // vertical position of cursor
  163.         GLfloat fNearPlane = 3.0f;
  164.         GLfloat fFarPlane = 7.0f;
  165.         GLfloat fMaxObjSize = 3.0f;
  166.         m_fRadius += (float)(m_xPos - xPos)/100.0f;
  167.         m_xPos = xPos;
  168.         m_yPos = yPos;
  169.     }
  170.     return 0;
  171. }
  172.  
  173. HRESULT COpenGLObj::OnDraw(ATL_DRAWINFO& di)
  174. {
  175.     HDC hdc = di.hdcDraw;
  176.     RECT& rc = *(RECT*)di.prcBounds;
  177.  
  178.     ::SelectPalette(hdc, m_hPal, FALSE);
  179.     ::RealizePalette(hdc);
  180.  
  181.     wglMakeCurrent(hdc, m_hrc);
  182.  
  183.     glClearColor(0.0f, 0.0f, 0.0f, 10.0f);
  184.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  185.  
  186.  
  187.     glPushMatrix();
  188.  
  189.         glTranslatef(0.0f, 0.0f, -m_fRadius);
  190.         glRotatef(m_wAngleX, 1.0f, 0.0f, 0.0f);
  191.         glRotatef(m_wAngleY, 0.0f, 1.0f, 0.0f);
  192.         glRotatef(m_wAngleZ, 0.0f, 0.0f, 1.0f);
  193.  
  194.         m_wAngleX -= (float(joyposY - 32768) / 32768.0f) * 20.0f;
  195.         m_wAngleY -= (float(joyposX - 32768) / 32768.0f) * 20.0f;
  196.         m_wAngleZ += 1.0f;
  197.  
  198.         glBegin(GL_QUAD_STRIP);
  199.             glColor3f(1.0f, 0.0f, 1.0f);
  200.             glVertex3f(-0.5f, 0.5f, 0.5f);
  201.  
  202.             glColor3f(1.0f, 0.0f, 0.0f);
  203.             glVertex3f(-0.5f, -0.5f, 0.5f);
  204.  
  205.             glColor3f(1.0f, 1.0f, 1.0f);
  206.             glVertex3f(0.5f, 0.5f, 0.5f);
  207.  
  208.             glColor3f(1.0f, 1.0f, 0.0f);
  209.             glVertex3f(0.5f, -0.5f, 0.5f);
  210.  
  211.             glColor3f(0.0f, 1.0f, 1.0f);
  212.             glVertex3f(0.5f, 0.5f, -0.5f);
  213.  
  214.             glColor3f(0.0f, 1.0f, 0.0f);
  215.             glVertex3f(0.5f, -0.5f, -0.5f);
  216.  
  217.             glColor3f(0.0f, 0.0f, 1.0f);
  218.             glVertex3f(-0.5f, 0.5f, -0.5f);
  219.  
  220.             glColor3f(0.0f, 0.0f, 0.0f);
  221.             glVertex3f(-0.5f, -0.5f,  -0.5f);
  222.  
  223.             glColor3f(1.0f, 0.0f, 1.0f);
  224.             glVertex3f(-0.5f, 0.5f, 0.5f);
  225.  
  226.             glColor3f(1.0f, 0.0f, 0.0f);
  227.             glVertex3f(-0.5f, -0.5f, 0.5f);
  228.  
  229.         glEnd();
  230.  
  231.         glBegin(GL_QUADS);
  232.             glColor3f(1.0f, 0.0f, 1.0f);
  233.             glVertex3f(-0.5f, 0.5f, 0.5f);
  234.  
  235.             glColor3f(1.0f, 1.0f, 1.0f);
  236.             glVertex3f(0.5f, 0.5f, 0.5f);
  237.  
  238.             glColor3f(0.0f, 1.0f, 1.0f);
  239.             glVertex3f(0.5f, 0.5f, -0.5f);
  240.  
  241.             glColor3f(0.0f, 0.0f, 1.0f);
  242.             glVertex3f(-0.5f, 0.5f, -0.5f);
  243.  
  244.         glEnd();
  245.  
  246.         glBegin(GL_QUADS);
  247.             glColor3f(1.0f, 0.0f, 0.0f);
  248.             glVertex3f(-0.5f, -0.5f, 0.5f);
  249.  
  250.             glColor3f(1.0f, 1.0f, 0.0f);
  251.             glVertex3f(0.5f, -0.5f, 0.5f);
  252.  
  253.             glColor3f(0.0f, 1.0f, 0.0f);
  254.             glVertex3f(0.5f, -0.5f, -0.5f);
  255.  
  256.             glColor3f(0.0f, 0.0f, 0.0f);
  257.             glVertex3f(-0.5f, -0.5f,  -0.5f);
  258.         glEnd();
  259.  
  260.         glColor3f(1.0f, 0.0f, 0.0f);
  261.         auxWireSphere(2.5);
  262.  
  263.     glPopMatrix();
  264.  
  265.     glFinish();
  266.     SwapBuffers(wglGetCurrentDC());
  267.  
  268.     SetTextColor(hdc, RGB(128,128,255));
  269.     SetBkMode(hdc, TRANSPARENT);
  270.     USES_CONVERSION;
  271.     LPCTSTR lpsz = OLE2T(m_bstrCaption);
  272.     DrawText(hdc, lpsz, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  273.  
  274.     return 1;
  275. }
  276.  
  277.  
  278. BOOL COpenGLObj::bSetupPixelFormat(HDC hdc)
  279. {
  280.     static PIXELFORMATDESCRIPTOR pfd =
  281.     {
  282.         sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
  283.         1,                              // version number
  284.         PFD_DRAW_TO_WINDOW |            // support window
  285.           PFD_SUPPORT_OPENGL |          // support OpenGL
  286.           PFD_DOUBLEBUFFER,             // double buffered
  287.         PFD_TYPE_RGBA,                  // RGBA type
  288.         24,                             // 24-bit color depth
  289.         0, 0, 0, 0, 0, 0,               // color bits ignored
  290.         0,                              // no alpha buffer
  291.         0,                              // shift bit ignored
  292.         0,                              // no accumulation buffer
  293.         0, 0, 0, 0,                     // accum bits ignored
  294.         32,                             // 32-bit z-buffer
  295.         0,                              // no stencil buffer
  296.         0,                              // no auxiliary buffer
  297.         PFD_MAIN_PLANE,                 // main layer
  298.         0,                              // reserved
  299.         0, 0, 0                         // layer masks ignored
  300.     };
  301.     int pixelformat;
  302.  
  303.     if ( (pixelformat = ChoosePixelFormat(hdc, &pfd)) == 0 )
  304.     {
  305.         ATLASSERT(FALSE);
  306.         return FALSE;
  307.     }
  308.  
  309.     if (SetPixelFormat(hdc, pixelformat, &pfd) == FALSE)
  310.     {
  311.         ATLASSERT(FALSE);
  312.         return FALSE;
  313.     }
  314.  
  315.     return TRUE;
  316. }
  317.  
  318.  
  319. unsigned char COpenGLObj::ComponentFromIndex(int i, UINT nbits, UINT shift)
  320. {
  321.     unsigned char val;
  322.  
  323.     val = (unsigned char) (i >> shift);
  324.     switch (nbits)
  325.     {
  326.  
  327.     case 1:
  328.         val &= 0x1;
  329.         return oneto8[val];
  330.     case 2:
  331.         val &= 0x3;
  332.         return twoto8[val];
  333.     case 3:
  334.         val &= 0x7;
  335.         return threeto8[val];
  336.  
  337.     default:
  338.         return 0;
  339.     }
  340. }
  341.  
  342.  
  343. void COpenGLObj::CreateRGBPalette(HDC hdc)
  344. {
  345.     PIXELFORMATDESCRIPTOR pfd;
  346.     int n, i;
  347.  
  348.     if (m_pPal)
  349.         return;
  350.  
  351.     n = ::GetPixelFormat(hdc);
  352.     ::DescribePixelFormat(hdc, n, sizeof(pfd), &pfd);
  353.  
  354.     if (pfd.dwFlags & PFD_NEED_PALETTE)
  355.     {
  356.         n = 1 << pfd.cColorBits;
  357.         m_pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)];
  358.  
  359.         ATLASSERT(m_pPal != NULL);
  360.  
  361.         m_pPal->palVersion = 0x300;
  362.         m_pPal->palNumEntries = n;
  363.         for (i=0; i<n; i++)
  364.         {
  365.             m_pPal->palPalEntry[i].peRed =
  366.                     ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
  367.             m_pPal->palPalEntry[i].peGreen =
  368.                     ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
  369.             m_pPal->palPalEntry[i].peBlue =
  370.                     ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
  371.             m_pPal->palPalEntry[i].peFlags = 0;
  372.         }
  373.  
  374.         /* fix up the palette to include the default GDI palette */
  375.         if ((pfd.cColorBits == 8)                           &&
  376.             (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) &&
  377.             (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
  378.             (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6)
  379.            )
  380.         {
  381.             for (i = 1 ; i <= 12 ; i++)
  382.                 m_pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
  383.         }
  384.  
  385.         m_hPal = ::CreatePalette((LPLOGPALETTE)m_pPal);
  386. //        delete pPal;
  387.  
  388.         ::SelectPalette(hdc, m_hPal, FALSE);
  389.         ::RealizePalette(hdc);
  390.     }
  391. }
  392.