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

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