home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible / OpenGL_Superbible_Waite_Group_Press_1996.iso / book / chapt10 / tank / borland / viewwnd.c < prev   
C/C++ Source or Header  |  1996-07-07  |  6KB  |  247 lines

  1. // ViewWnd.c
  2. // This file contains the window procedure and code related to the
  3. // view window. This window does the OpenGL rendering of the terrain.
  4.  
  5. #include <windows.h>    // Normal Windows stuff
  6. #include <math.h>
  7. #include <gl/gl.h>        // Core OpenGL functions
  8. #include <gl/glu.h>        // OpenGL Utility functions
  9. #include <gl/glaux.h>
  10. #include "externs.h"    // Data shared between files
  11. #include "glutils.h"    // Utilities for this application
  12.  
  13.  
  14. // Lighting values
  15. GLfloat  ambientLight[] = { 0.4f, 0.4f, 0.4f, 1.0f };
  16. GLfloat  diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  17. GLfloat     lightPos[] = { 1.0f, 2.0f, 0.0f, 0.0f };
  18.  
  19.  
  20.  
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Setup the main view windows Rendering Context
  23. void SetupViewRC(void)
  24.     {
  25.     // Black background
  26.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  27.  
  28.     // Only draw the outside of CW wound objects
  29.     glCullFace(GL_BACK);    // Cull the back
  30.     glFrontFace(GL_CCW);    // Counter Clock wise wound is front
  31.     glEnable(GL_CULL_FACE); // Enable the culling
  32.  
  33.     // Do depth testing
  34.     glEnable(GL_DEPTH_TEST);
  35.  
  36.     // Setup lighting parameters
  37.     // Enable lighting
  38.     glEnable(GL_LIGHTING);
  39.  
  40.     // Setup and enable light 0
  41.     glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  42.     glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  43.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  44.     glEnable(GL_LIGHT0);
  45.  
  46.     // Enable color tracking
  47.     glEnable(GL_COLOR_MATERIAL);
  48.     
  49.     // Set Material properties to follow glColor values
  50.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  51.  
  52.     // Corrects aux lib object normals
  53.     glEnable(GL_NORMALIZE);
  54.  
  55.     // Build the scene, everything but the robot and tank
  56.     // Create the display lists
  57.     glNewList(nWorldList,GL_COMPILE);
  58.         // Draw the ground grid
  59.         DrawGround();    
  60.  
  61.         // Place the objects about the scene
  62.         PlaceSlabs();
  63.         PlacePyramids();
  64.     glEndList();
  65.     }
  66.  
  67.  
  68.  
  69. ////////////////////////////////////////////////////////////
  70. ////////////////////////////////////////////////////////////
  71. // Window procedure, handles all messages for this window
  72. LRESULT CALLBACK WndProcView(HWND    hWnd,
  73.                             UINT    message,
  74.                             WPARAM  wParam,
  75.                             LPARAM  lParam)
  76.     {
  77.     static HDC      hDC;    // Keep the Device Context
  78.     static HGLRC  hRC;    // Keep the Rendering Context
  79.     
  80.     switch (message)
  81.         {
  82.         // Window creation, setup here
  83.         case WM_CREATE:
  84.             // Save the device context
  85.             hDC = GetDC(hWnd);
  86.  
  87.             // Set the pixel format
  88.             SetDCDepthPixelFormat(hDC);
  89.  
  90.             // Create palette if needed, this will return NULL if 
  91.             // no palette is required
  92.             hPalette = GetOpenGLPalette(hDC);
  93.     
  94.             // Create the rendering context and make it current
  95.             hRC = wglCreateContext(hDC);
  96.             wglMakeCurrent(hDC, hRC);
  97.  
  98.             // Do some setup here
  99.             SetupViewRC();
  100.  
  101.             wglMakeCurrent(hDC,NULL);
  102.  
  103.             break;
  104.  
  105.         // Window is being destroyed, cleanup
  106.         case WM_DESTROY:
  107.             // Cleanup...
  108.             // Deselect the current rendering context and delete it
  109.             wglMakeCurrent(hDC,NULL);
  110.             wglDeleteContext(hRC);
  111.  
  112.             // Destroy the palette if it was created 
  113.             if(hPalette != NULL)
  114.                 DeleteObject(hPalette);
  115.  
  116.             // Release the device context
  117.             ReleaseDC(hWnd,hDC);
  118.             break;
  119.  
  120.         // Window is resized. Setup the viewing transformation
  121.         case WM_SIZE:
  122.             {
  123.             int nWidth,nHeight;
  124.             double dAspect;
  125.  
  126.             nWidth = LOWORD(lParam);  // width of client area 
  127.             nHeight = HIWORD(lParam); // height of client area 
  128.     
  129.             if(nHeight == 0)          // Don't allow divide by zero
  130.                 nHeight = 1;
  131.  
  132.             dAspect = (double)nWidth/(double)nHeight;
  133.  
  134.             // Make this rendering context current
  135.             wglMakeCurrent(hDC, hRC);
  136.  
  137.             // Set the viewport to be the entire window
  138.             glViewport(0, 0, nWidth, nHeight);
  139.     
  140.             // Setup Perspective
  141.             glMatrixMode(GL_PROJECTION);
  142.             glLoadIdentity();
  143.  
  144.             // Establish viewing volume
  145.             gluPerspective(35.0, dAspect,1, 2000);
  146.  
  147.             glMatrixMode(GL_MODELVIEW);
  148.             glLoadIdentity();
  149.  
  150.             // Reset of light position necessary
  151.             glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  152.  
  153.             wglMakeCurrent(hDC,NULL);
  154.             }
  155.             break;
  156.  
  157.         case WM_PAINT:
  158.             {
  159.             float fRadius = 50.0f;
  160.  
  161.             wglMakeCurrent(hDC, hRC);
  162.  
  163.             // Clear the window with current clearing color
  164.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  165.  
  166.             glPushMatrix();
  167.  
  168.             // Handy dandy little function
  169.             gluLookAt(pObject->xPos, 5.0f, pObject->zPos,    // Eye Position (x,y,z)
  170.                 pObject->xPos + (fRadius*(float)cos(pObject->radsFromEast)), // Center X
  171.                 5.0f,                                                            // Center Y
  172.                 pObject->zPos - (fRadius*(float)sin(pObject->radsFromEast)), // Center Z
  173.     
  174.                 0.0f, 1.0f, 0.0f);                           // Up is straight up Y axis
  175.  
  176.             // Reset of light position necessary
  177.             glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  178.  
  179.             RenderWorld();
  180.  
  181.             glPopMatrix();
  182.  
  183.             SwapBuffers(hDC);
  184.  
  185.             wglMakeCurrent(hDC,NULL);
  186.  
  187.             // Validate the newly painted client area
  188.             ValidateRect(hWnd,NULL);
  189.             }
  190.             break;
  191.  
  192.  
  193.         // Windows is telling the application that it may modify
  194.         // the system palette.  This message in essance asks the 
  195.         // application for a new palette.
  196.         case WM_QUERYNEWPALETTE:
  197.             // If the palette was created.
  198.             if(hPalette)
  199.                 {
  200.                 int nRet;
  201.  
  202.                 // Selects the palette into the current device context
  203.                 SelectPalette(hDC, hPalette, FALSE);
  204.  
  205.                 // Map entries from the currently selected palette to
  206.                 // the system palette.  The return value is the number 
  207.                 // of palette entries modified.
  208.                 nRet = RealizePalette(hDC);
  209.  
  210.                 // Repaint, forces remap of palette in current window
  211.                 InvalidateRect(hWnd,NULL,FALSE);
  212.  
  213.                 return nRet;
  214.                 }
  215.             break;
  216.  
  217.     
  218.         // This window may set the palette, even though it is not the 
  219.         // currently active window.
  220.         case WM_PALETTECHANGED:
  221.             // Don't do anything if the palette does not exist, or if
  222.             // this is the window that changed the palette.
  223.             if((hPalette != NULL) && ((HWND)wParam != hWnd))
  224.                 {
  225.                 // Select the palette into the device context
  226.                 SelectPalette(hDC,hPalette,FALSE);
  227.  
  228.                 // Map entries to system palette
  229.                 RealizePalette(hDC);
  230.                 
  231.                 // Remap the current colors to the newly realized palette
  232.                 UpdateColors(hDC);
  233.                 return 0;
  234.                 }
  235.             break;
  236.  
  237.     default:   // Passes it on if unproccessed
  238.         return (DefWindowProc(hWnd, message, wParam, lParam));
  239.  
  240.     }
  241.  
  242.     return (0L);
  243.     }
  244.  
  245.  
  246.  
  247.