home *** CD-ROM | disk | FTP | other *** search
/ 3DS Max Model 2 / 3dsModelCD2.iso / ALTEROS / PLUGINS / GLC.X3D / CPP / APP_END < prev    next >
Encoding:
Text File  |  2001-02-02  |  7.8 KB  |  279 lines

  1. float sizex,sizey;
  2. LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM);
  3. GLsizei glnWidth, glnHeight;
  4. GLdouble gldAspect;
  5. void SetDCPixelFormat (HWND,HDC);
  6. void InitializeRC (void);
  7. void DrawScene (HDC, UINT,UINT);
  8.  
  9. HPALETTE hPalette = NULL;
  10. GLfloat nSize = 0.0f;
  11. GLfloat nCol = 0.0f;
  12. GLfloat nTop = 1.0f;
  13. GLfloat nBottom = 0.0f;
  14. int bFlag = 1;
  15. char wndname[256]="";
  16.  
  17. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  18.                     LPSTR lpszCmdLine, int nCmdShow)
  19. {
  20.     static char szAppName[] = "3D Exploration Exported APP";
  21.     WNDCLASS wc;
  22.     HWND hwnd;
  23.     MSG msg;
  24.  
  25.  
  26.     wc.style = CS_HREDRAW | CS_VREDRAW;
  27.     wc.lpfnWndProc = (WNDPROC) WndProc;
  28.     wc.cbClsExtra = 0;
  29.     wc.cbWndExtra = 0;
  30.     wc.hInstance = hInstance;
  31.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  33.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  34.     wc.lpszMenuName = NULL;
  35.     wc.lpszClassName = szAppName;
  36.  
  37.     RegisterClass (&wc);
  38.  
  39.     hwnd = CreateWindow (szAppName, szAppName,
  40.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  41.         CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
  42.         HWND_DESKTOP, NULL, hInstance, NULL);
  43.  
  44.     ShowWindow (hwnd, nCmdShow);
  45.     UpdateWindow (hwnd);
  46.  
  47.     while (GetMessage (&msg, NULL, 0, 0)) {
  48.         TranslateMessage (&msg);
  49.         DispatchMessage (&msg);
  50.     }
  51.     return msg.wParam;
  52. }
  53.  
  54. /*
  55.  *  WndProc processes messages to the main window.
  56.  */
  57.  
  58. LONG WINAPI WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  59. {
  60.     static HDC hdc;
  61.     static HGLRC hrc;
  62.     PAINTSTRUCT ps;
  63.     static UINT nAngle = 0;
  64.     static UINT nAngle2 = 0;
  65.     static UINT nTimer;
  66.     int n;
  67.                 
  68.     switch (msg) {
  69.     
  70.     case WM_CREATE:
  71.         //
  72.         // Create a rendering context and set a timer.
  73.         //
  74.         hdc = GetDC (hwnd);
  75.         SetDCPixelFormat (hwnd,hdc);
  76.         hrc = wglCreateContext (hdc);
  77.         wglMakeCurrent (hdc, hrc);
  78.         InitializeRC ();
  79.         nTimer = SetTimer (hwnd, 1, 1, NULL);
  80.         return 0;
  81.  
  82.     case WM_SIZE:
  83.         //
  84.         // Redefine the viewing volume and viewport when the window size
  85.         // changes.
  86.         //
  87.         glnWidth = (GLsizei) LOWORD (lParam);
  88.         glnHeight = (GLsizei) HIWORD (lParam);
  89.  
  90.         return 0;
  91.  
  92.     case WM_PAINT:{
  93.         //
  94.         // Draw the scene.
  95.         //
  96.         BeginPaint (hwnd, &ps);
  97.         DrawScene (hdc, nAngle,nAngle2);
  98.         EndPaint (hwnd, &ps);
  99.  
  100.         }return 0;
  101.  
  102.     case WM_TIMER:
  103.         //
  104.         // Update the rotation angle and force a repaint.
  105.         //
  106.         nAngle += 2;
  107.         if (nAngle >= 360)
  108.             nAngle -= 360;
  109.         nAngle2 += 1;
  110.         if (nAngle2 >= 360)
  111.             nAngle2 -= 360;
  112.  
  113.         if (bFlag == 1)
  114.             nSize += 0.05f;
  115.             nCol += 0.01f;
  116.             if (nSize >= nTop)
  117.                 bFlag = 0;
  118.  
  119.         if (bFlag == 0)
  120.             nSize -= 0.05f;
  121.             nCol -= 0.01f;
  122.             if (nSize <= nBottom)
  123.                 bFlag = 1;
  124.  
  125.         InvalidateRect (hwnd, NULL, FALSE);
  126.         return 0;
  127.  
  128.     case WM_QUERYNEWPALETTE:
  129.         //
  130.         // If the program is using a color palette, realize the palette
  131.         // and update the client area when the window receives the input
  132.         // focus.
  133.         //
  134.         if (hPalette != NULL) {
  135.             if (n = RealizePalette (hdc))
  136.                 InvalidateRect (hwnd, NULL, FALSE);
  137.             return n;
  138.         }
  139.         break;
  140.  
  141.     case WM_PALETTECHANGED:
  142.         //
  143.         // If the program is using a color palette, realize the palette
  144.         // and update the colors in the client area when another program
  145.         // realizes its palette.
  146.         //
  147.         if ((hPalette != NULL) && ((HWND) wParam != hwnd)) {
  148.             if (RealizePalette (hdc))
  149.                 UpdateColors (hdc);
  150.             return 0;
  151.         }
  152.         break;
  153.  
  154.     case WM_DESTROY:
  155.         //
  156.         // Clean up and terminate.
  157.         //
  158.         wglMakeCurrent (NULL, NULL);
  159.         wglDeleteContext (hrc);
  160.         ReleaseDC (hwnd, hdc);
  161.         if (hPalette != NULL)
  162.             DeleteObject (hPalette);
  163.         KillTimer (hwnd, nTimer);
  164.         PostQuitMessage (0);
  165.         return 0;
  166.     }
  167.     return DefWindowProc (hwnd, msg, wParam, lParam);
  168. }
  169.  
  170. /*
  171.  *  SetDCPixelFormat sets the pixel format for a device context in
  172.  *  preparation for creating a rendering context.
  173.  *
  174.  *  Input parameters:
  175.  *    hdc = Device context handle
  176.  *
  177.  *  Returns:
  178.  *    Nothing
  179.  */
  180.  
  181. void SetDCPixelFormat (HWND hwnd,HDC hdc)
  182. {
  183.     HANDLE hHeap;
  184.     int nColors, i;
  185.     LPLOGPALETTE lpPalette;
  186.     BYTE byRedMask, byGreenMask, byBlueMask;
  187.  
  188.     static PIXELFORMATDESCRIPTOR pfd = {
  189.         sizeof (PIXELFORMATDESCRIPTOR),             // Size of this structure
  190.         1,                                          // Version number
  191.         PFD_DRAW_TO_WINDOW |                        // Flags
  192.         PFD_SUPPORT_OPENGL |
  193.         PFD_GENERIC_ACCELERATED|
  194.         PFD_DOUBLEBUFFER,
  195.         PFD_TYPE_RGBA,                              // RGBA pixel values
  196.         24,                                         // 24-bit color
  197.         0, 0, 0, 0, 0, 0,                           // Don't care about these
  198.         0, 0,                                       // No alpha buffer
  199.         0, 0, 0, 0, 0,                              // No accumulation buffer
  200.         32,                                         // 32-bit depth buffer
  201.         0,                                          // No stencil buffer
  202.         0,                                          // No auxiliary buffers
  203.         PFD_MAIN_PLANE,                             // Layer type
  204.         0,                                          // Reserved (must be 0)
  205.         0, 0, 0                                     // No layer masks
  206.     };
  207.  
  208.     int nPixelFormat;
  209.  
  210.     nPixelFormat = ChoosePixelFormat (hdc, &pfd);
  211.     SetPixelFormat (hdc, nPixelFormat, &pfd);
  212.  
  213.     if (pfd.dwFlags & PFD_NEED_PALETTE) {
  214.         nColors = 1 << pfd.cColorBits;
  215.         hHeap = GetProcessHeap ();
  216.  
  217.         (LPLOGPALETTE) lpPalette = (LPLOGPALETTE)HeapAlloc (hHeap, 0,
  218.             sizeof (LOGPALETTE) + (nColors * sizeof (PALETTEENTRY)));
  219.  
  220.         lpPalette->palVersion = 0x300;
  221.         lpPalette->palNumEntries = nColors;
  222.  
  223.         byRedMask = (1 << pfd.cRedBits) - 1;
  224.         byGreenMask = (1 << pfd.cGreenBits) - 1;
  225.         byBlueMask = (1 << pfd.cBlueBits) - 1;
  226.  
  227.         for (i=0; i<nColors; i++) {
  228.             lpPalette->palPalEntry[i].peRed =
  229.                 (((i >> pfd.cRedShift) & byRedMask) * 255) / byRedMask;
  230.             lpPalette->palPalEntry[i].peGreen =
  231.                 (((i >> pfd.cGreenShift) & byGreenMask) * 255) / byGreenMask;
  232.             lpPalette->palPalEntry[i].peBlue =
  233.                 (((i >> pfd.cBlueShift) & byBlueMask) * 255) / byBlueMask;
  234.             lpPalette->palPalEntry[i].peFlags = 0;
  235.         }
  236.  
  237.         hPalette = CreatePalette (lpPalette);
  238.         HeapFree (hHeap, 0, lpPalette);
  239.  
  240.         if (hPalette != NULL) {
  241.             SelectPalette (hdc, hPalette, FALSE);
  242.             RealizePalette (hdc);
  243.         }
  244.     }
  245. }
  246.  
  247. /*
  248.  *  InitializeRC initializes the current rendering context.
  249.  *
  250.  *  Input parameters:
  251.  *    None
  252.  *
  253.  *  Returns:
  254.  *    Nothing
  255.  */
  256.  
  257.  
  258. void InitializeRC (void)
  259. {
  260.     GLfloat glfLightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  261.     GLfloat glfLightDiffuse[] = { 1.2f, 1.2f, 1.2f, 1.0f };
  262.     GLfloat glfLightSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
  263.  
  264.     //
  265.     // Add a light to the scene.
  266.     //
  267.  
  268.     glLightfv (GL_LIGHT0, GL_AMBIENT, glfLightAmbient);
  269.     glLightfv (GL_LIGHT0, GL_DIFFUSE, glfLightDiffuse);
  270.     glLightfv (GL_LIGHT0, GL_SPECULAR, glfLightSpecular);
  271.     glEnable (GL_LIGHTING);
  272.     glEnable (GL_LIGHT0);
  273.  
  274.     //
  275.     // Enable depth testing and backface culling.
  276.     //
  277.  
  278.     glEnable (GL_DEPTH_TEST);    
  279.