Advertisement
cristiano002

Robocik_OpenGL_2

May 13th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.91 KB | None | 0 0
  1. // gl_teplate.c
  2. // A template program for most WIGGLE applications
  3.  
  4. #include <windows.h>            // Window defines
  5. #include <gl\gl.h>              // OpenGL
  6. #include <gl\glu.h>             // GLU library
  7. #include <math.h>                               // Define for sine and cosine
  8. #include "resource.h"           // About box resource identifiers.
  9.  
  10. // Define a constant for the value of PI
  11. #define GL_PI 3.1415f
  12.  
  13. double rot1,rot2,rot3;
  14. int licznik;
  15.  
  16.  
  17. // Color Palette handle
  18. HPALETTE hPalette = NULL;
  19.  
  20.  
  21. // Application name and instance storeage
  22. static LPCTSTR lpszAppName = "GL TEMPLATE";
  23. static HINSTANCE hInstance;
  24.  
  25. // Rotation amounts
  26. static GLfloat xRot = 0.0f;
  27. static GLfloat yRot = 0.0f;
  28.  
  29.  
  30. // Declaration for Window procedure
  31. LRESULT CALLBACK WndProc(   HWND    hWnd,
  32.                          UINT    message,
  33.                          WPARAM  wParam,
  34.                          LPARAM  lParam);
  35.  
  36. // Dialog procedure for about box
  37. BOOL APIENTRY AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam);
  38.  
  39. // Set Pixel Format function - forward declaration
  40. void SetDCPixelFormat(HDC hDC);
  41.  
  42.  
  43. // Change viewing volume and viewport.  Called when window is resized
  44. void ChangeSize(GLsizei w, GLsizei h)
  45. {
  46.     GLfloat nRange = 100.0f;
  47.  
  48.     // Prevent a divide by zero
  49.     if(h == 0)
  50.         h = 1;
  51.  
  52.     // Set Viewport to window dimensions
  53.     glViewport(0, 0, w, h);
  54.  
  55.     // Reset coordinate system
  56.     glMatrixMode(GL_PROJECTION);
  57.     glLoadIdentity();
  58.  
  59.     // Establish clipping volume (left, right, bottom, top, near, far)
  60.     if (w <= h)
  61.         glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
  62.     else
  63.         glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
  64.  
  65.     glMatrixMode(GL_MODELVIEW);
  66.     glLoadIdentity();
  67. }
  68.  
  69.  
  70.  
  71. // This function does any needed initialization on the rendering
  72. // context.
  73. void SetupRC()
  74. {
  75.     // STARY KOD
  76.     // Light values and coordinates
  77.     GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  78.     GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  79.     GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
  80.     GLfloat  lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
  81.     GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
  82.  
  83.  
  84.     glEnable(GL_DEPTH_TEST);    // Hidden surface removal
  85.     glFrontFace(GL_CCW);        // Counter clock-wise polygons face out
  86.     glEnable(GL_CULL_FACE);     // Do not calculate inside of jet
  87.  
  88.     // Enable lighting
  89.     glEnable(GL_LIGHTING);
  90.  
  91.     // Setup and enable light 0
  92.     glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  93.     glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  94.     glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  95.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  96.     glEnable(GL_LIGHT0);
  97.  
  98.     // Enable color tracking
  99.     glEnable(GL_COLOR_MATERIAL);
  100.    
  101.     // Set Material properties to follow glColor values
  102.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  103.  
  104.     // All materials hereafter have full specular reflectivity
  105.     // with a high shine
  106.     glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
  107.     glMateriali(GL_FRONT,GL_SHININESS,128);
  108.  
  109.  
  110.     // White background
  111.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
  112.     // Black brush
  113.     glColor3f(0.0,0.0,0.0);
  114.     //STARY KOD - KONIEC
  115.    
  116.  
  117.    
  118.  
  119.  
  120. }
  121.  
  122. void walec(double h, double r)
  123. {
  124.     double angle,x,y;
  125.     glBegin(GL_TRIANGLE_FAN);
  126.     glNormal3d(0,0,-1);
  127.     glVertex3d(0.0f, 0.0f, 0.0f);
  128.     for(angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI/8.0f))
  129.     {
  130.         x = r*sin(angle);
  131.         y = r*cos(angle);
  132.         glVertex3d(x, y, 0.0);  
  133.     }
  134.     glEnd();
  135.     glBegin(GL_QUAD_STRIP);
  136.    
  137.     for(angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI/8.0f))
  138.     {
  139.         x = r*sin(angle);
  140.         y = r*cos(angle);
  141.         glNormal3d(sin(angle),cos(angle),0.0);
  142.         glVertex3d(x, y, h);
  143.         glVertex3d(x, y, 0);
  144.     }
  145.     glEnd();
  146.  
  147.     glBegin(GL_TRIANGLE_FAN);
  148.     glNormal3d(0.0,0.0,1.0);
  149.     glVertex3d(0.0f, 0.0f, 0.0f + 1);
  150.     for(angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI/8.0f))
  151.     {
  152.         x = r*sin(angle);
  153.         y = r*cos(angle);
  154.         glVertex3d(x, y, 0.0+1);  
  155.     }
  156.     glEnd();
  157.  
  158.  
  159. }
  160.  
  161.  
  162. void szescian(void)
  163. {
  164.     glBegin(GL_QUADS);
  165.  
  166.  
  167.     glColor3d(1,0.5,0);
  168.     //pomarancz
  169.     glNormal3d(0,0,1);
  170.     glVertex3d( 20, 20, 20);
  171.     glVertex3d(-20, 20, 20);
  172.     glVertex3d(-20,-20, 20);
  173.     glVertex3d( 20,-20, 20);
  174.  
  175.     glColor3d(0,1,0);
  176.     glNormal3d(0,-1,0);
  177.     //zielony
  178.     glVertex3d(-20,-20, 20);
  179.     glVertex3d(-20,-20,-20);
  180.     glVertex3d( 20,-20,-20);
  181.     glVertex3d( 20,-20, 20);
  182.  
  183.     glColor3d(0,0.5,1);
  184.     glNormal3d(1,0,0);
  185.     //niebieski
  186.     glVertex3d( 20, 20, 20);
  187.     glVertex3d( 20,-20, 20);
  188.     glVertex3d( 20,-20,-20);
  189.     glVertex3d( 20, 20,-20);
  190.  
  191.     glColor3d(0,1,0);
  192.     glNormal3d(0,1,0);
  193.     //zielony
  194.     glVertex3d(-20, 20,-20);
  195.     glVertex3d(-20, 20, 20);
  196.     glVertex3d( 20, 20, 20);
  197.     glVertex3d( 20, 20,-20);
  198.  
  199.     glColor3d(1,0.5,0);
  200.     glNormal3d(0,0,-1);
  201.     //pomarancz
  202.     glVertex3d( 20, 20,-20);
  203.     glVertex3d( 20,-20,-20);
  204.     glVertex3d(-20,-20,-20);
  205.     glVertex3d(-20, 20,-20);
  206.  
  207.     glColor3d(0,0.5,1);
  208.     glNormal3d(-1,0,0);
  209.     //niebieski
  210.     glVertex3d(-20,-20,-20);
  211.     glVertex3d(-20,-20, 20);
  212.     glVertex3d(-20, 20, 20);
  213.     glVertex3d(-20, 20,-20);
  214.  
  215.     glEnd();
  216. }
  217.  
  218. void ramie(double r1, double r2, double h, double d)
  219. {
  220.     double angle,x,y;
  221.     glBegin(GL_TRIANGLE_FAN);
  222.     glNormal3d(0,0,1);
  223.     glVertex3d(0.0f, 0.0f, 0.0f);
  224.     for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  225.     {
  226.         x = r1*sin(angle);
  227.         y = r1*cos(angle);
  228.         glVertex3d(x, y, 0.0);  
  229.     }
  230.     glEnd();
  231.  
  232.     glBegin(GL_QUAD_STRIP);
  233.     for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  234.     {
  235.         x = r1*sin(angle);
  236.         y = r1*cos(angle);
  237.         glNormal3d(sin(angle),cos(angle),0.0);
  238.         glVertex3d(-x, -y, h);
  239.         glVertex3d(-x, -y, 0);
  240.     }
  241.     glEnd();
  242.  
  243.     glBegin(GL_TRIANGLE_FAN);
  244.     glNormal3d(0.0,0.0,1.0);
  245.     glVertex3d(0.0f, 0.0f, 0.0f +h);
  246.     for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  247.     {
  248.         x = r1*sin(angle);
  249.         y = r1*cos(angle);
  250.         glVertex3d(x, y, 0.0 +h);  
  251.     }
  252.     glEnd();
  253.  
  254.     ////////////////////////////////////////////////////////////////////
  255.  
  256.     glBegin(GL_TRIANGLE_FAN);
  257.     glNormal3d(0,0,1);
  258.     glVertex3d(0.0f - d, 0.0f, 0.0f);
  259.     for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  260.     {
  261.         x = -r2*sin(angle);
  262.         y = -r2*cos(angle);
  263.         glVertex3d(x - d, y, 0.0);  
  264.     }
  265.     glEnd();
  266.  
  267.     glBegin(GL_QUAD_STRIP);
  268.     for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  269.     {
  270.         x = r2*sin(angle);
  271.         y = r2*cos(angle);
  272.         glNormal3d(sin(angle),cos(angle),0.0);
  273.         glVertex3d(x-d, y, h);
  274.         glVertex3d(x-d, y, 0);
  275.     }
  276.     glEnd();
  277.  
  278.     glBegin(GL_TRIANGLE_FAN);
  279.     glVertex3d(0.0f -d , 0.0f, 0.0f +h);
  280.     glNormal3d(0.0,0.0,1.0);
  281.     for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  282.     {
  283.         x = -r2*sin(angle);
  284.         y = -r2*cos(angle);
  285.         glVertex3d(x -d , y, 0.0 +h);  
  286.     }
  287.     glEnd();
  288.  
  289.     ////////////////////////////////////////////////////////////////////
  290.     glBegin(GL_LINES);
  291.  
  292.     glVertex3d(0.0f, 0.0f + r1, 0.0f);
  293.     glVertex3d(0.0f -d, 0.0f + r2, 0.0f);
  294.  
  295.     glVertex3d(0.0f, 0.0f + r1, 0.0f + h);
  296.     glVertex3d(0.0f -d, 0.0f + r2, 0.0f + h);
  297.  
  298.     glVertex3d(0.0f, 0.0f - r1, 0.0f);
  299.     glVertex3d(0.0f -d, 0.0f - r2, 0.0f);
  300.  
  301.     glVertex3d(0.0f, 0.0f - r1, 0.0f + h);
  302.     glVertex3d(0.0f -d, 0.0f - r2, 0.0f + h);
  303.  
  304.     glEnd();
  305.  
  306. }
  307.  
  308. void robot(double d1, double d2, double d3)
  309. {
  310.     glColor3d(1,0,0);
  311.     glPushMatrix();
  312.    
  313.     glRotated(-90,1.0,0.0,0.0);
  314.     glTranslated(0.0,0.0,-50.0);
  315.     walec(5,30);
  316.  
  317.     glTranslated(0.0,0.0,5.0);
  318.     walec(40,10);
  319.  
  320.     glTranslated(0,0,40);
  321.     glRotated(d1,0,0,1);
  322.     walec(40,10);
  323.  
  324.     glTranslated(0,0,40);
  325.     glRotated(90,0,1,0);
  326.     glTranslated(0,0,-20);
  327.     walec(40,10);
  328.  
  329.     glTranslated(0,0,+40);
  330.     glRotated(90+d2,0,0,1);
  331.     ramie(15,10,5,30);
  332.  
  333.     glTranslated(-30,0,-5);
  334.     glRotated(d3,0,0,1);
  335.     ramie(15,10,5,30);
  336.     glPopMatrix();
  337.  
  338.  
  339. }
  340.  
  341. void dwa_roboty(void)
  342. {
  343.     glPushMatrix();
  344.     glTranslated(-50,0,0);
  345.     robot(rot1,rot2,rot3);
  346.     glPopMatrix();
  347.     /*
  348.     glPushMatrix();
  349.     glTranslated(50,0,0);
  350.     //glRotated(180,0,1,0);
  351.     robot(rot1,rot2,rot3);
  352.     glPopMatrix();
  353.     */
  354.  
  355.  
  356.  
  357.  
  358.  
  359. }
  360. // Called to draw scene
  361. void RenderScene(void)
  362. {
  363.     // Clear the window and the depth buffer
  364.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  365.     glEnable(GL_CULL_FACE);
  366.     // Save matrix state and do the rotation
  367.     glPushMatrix();
  368.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  369.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  370.  
  371.  
  372.     ////////////////////////////////////////////////////
  373.     ////////////////////////////////////////////////////
  374.     // WYWOLANIE FUNKCJI ROBOTA
  375.      robot(rot1,rot2,rot3);
  376.  
  377.     //szescian();
  378.  
  379.     // All obiect are shown as nets
  380.     glPolygonMode(GL_BACK,GL_LINE);
  381.  
  382.  
  383.  
  384.     /////////////////////////////////////////////////////
  385.     /////////////////////////////////////////////////////
  386.     // Restore transformations
  387.     glPopMatrix();
  388.  
  389.     // Flush drawing commands
  390.     glFlush();
  391. }
  392.  
  393.  
  394. // Select the pixel format for a given device context
  395. void SetDCPixelFormat(HDC hDC)
  396. {
  397.     int nPixelFormat;
  398.  
  399.     static PIXELFORMATDESCRIPTOR pfd = {
  400.         sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  401.         1,                                                              // Version of this structure    
  402.         PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  403.         PFD_SUPPORT_OPENGL |                                    // Support OpenGL calls in window
  404.         PFD_DOUBLEBUFFER,                       // Double buffered
  405.         PFD_TYPE_RGBA,                          // RGBA Color mode
  406.         24,                                     // Want 24bit color
  407.         0,0,0,0,0,0,                            // Not used to select mode
  408.         0,0,                                    // Not used to select mode
  409.         0,0,0,0,0,                              // Not used to select mode
  410.         32,                                     // Size of depth buffer
  411.         0,                                      // Not used to select mode
  412.         0,                                      // Not used to select mode
  413.         PFD_MAIN_PLANE,                         // Draw in main plane
  414.         0,                                      // Not used to select mode
  415.         0,0,0 };                                // Not used to select mode
  416.  
  417.         // Choose a pixel format that best matches that described in pfd
  418.         nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  419.  
  420.         // Set the pixel format for the device context
  421.         SetPixelFormat(hDC, nPixelFormat, &pfd);
  422. }
  423.  
  424.  
  425.  
  426. // If necessary, creates a 3-3-2 palette for the device context listed.
  427. HPALETTE GetOpenGLPalette(HDC hDC)
  428. {
  429.     HPALETTE hRetPal = NULL;        // Handle to palette to be created
  430.     PIXELFORMATDESCRIPTOR pfd;      // Pixel Format Descriptor
  431.     LOGPALETTE *pPal;                       // Pointer to memory for logical palette
  432.     int nPixelFormat;                       // Pixel format index
  433.     int nColors;                            // Number of entries in palette
  434.     int i;                                          // Counting variable
  435.     BYTE RedRange,GreenRange,BlueRange;
  436.     // Range for each color entry (7,7,and 3)
  437.  
  438.  
  439.     // Get the pixel format index and retrieve the pixel format description
  440.     nPixelFormat = GetPixelFormat(hDC);
  441.     DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  442.  
  443.     // Does this pixel format require a palette?  If not, do not create a
  444.     // palette and just return NULL
  445.     if(!(pfd.dwFlags & PFD_NEED_PALETTE))
  446.         return NULL;
  447.  
  448.     // Number of entries in palette.  8 bits yeilds 256 entries
  449.     nColors = 1 << pfd.cColorBits;
  450.  
  451.     // Allocate space for a logical palette structure plus all the palette entries
  452.     pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
  453.  
  454.     // Fill in palette header
  455.     pPal->palVersion = 0x300;               // Windows 3.0
  456.     pPal->palNumEntries = nColors; // table size
  457.  
  458.     // Build mask of all 1's.  This creates a number represented by having
  459.     // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  460.     // pfd.cBlueBits.  
  461.     RedRange = (1 << pfd.cRedBits) -1;
  462.     GreenRange = (1 << pfd.cGreenBits) - 1;
  463.     BlueRange = (1 << pfd.cBlueBits) -1;
  464.  
  465.     // Loop through all the palette entries
  466.     for(i = 0; i < nColors; i++)
  467.     {
  468.         // Fill in the 8-bit equivalents for each component
  469.         pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  470.         pPal->palPalEntry[i].peRed = (unsigned char)(
  471.             (double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  472.  
  473.         pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  474.         pPal->palPalEntry[i].peGreen = (unsigned char)(
  475.             (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  476.  
  477.         pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  478.         pPal->palPalEntry[i].peBlue = (unsigned char)(
  479.             (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  480.  
  481.         pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
  482.     }
  483.  
  484.  
  485.     // Create the palette
  486.     hRetPal = CreatePalette(pPal);
  487.  
  488.     // Go ahead and select and realize the palette for this device context
  489.     SelectPalette(hDC,hRetPal,FALSE);
  490.     RealizePalette(hDC);
  491.  
  492.     // Free the memory used for the logical palette structure
  493.     free(pPal);
  494.  
  495.     // Return the handle to the new palette
  496.     return hRetPal;
  497. }
  498.  
  499.  
  500. // Entry point of all Windows programs
  501. int APIENTRY WinMain(   HINSTANCE       hInst,
  502.                      HINSTANCE       hPrevInstance,
  503.                      LPSTR           lpCmdLine,
  504.                      int                     nCmdShow)
  505. {
  506.     MSG             msg;            // Windows message structure
  507.     WNDCLASS        wc;             // Windows class structure
  508.     HWND            hWnd;           // Storeage for window handle
  509.  
  510.     hInstance = hInst;
  511.  
  512.     // Register Window style
  513.     wc.style                = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  514.     wc.lpfnWndProc          = (WNDPROC) WndProc;
  515.     wc.cbClsExtra           = 0;
  516.     wc.cbWndExtra           = 0;
  517.     wc.hInstance            = hInstance;
  518.     wc.hIcon                = NULL;
  519.     wc.hCursor              = LoadCursor(NULL, IDC_ARROW);
  520.  
  521.     // No need for background brush for OpenGL window
  522.     wc.hbrBackground        = NULL;        
  523.  
  524.     wc.lpszMenuName         = MAKEINTRESOURCE(IDR_MENU);
  525.     wc.lpszClassName        = lpszAppName;
  526.  
  527.     // Register the window class
  528.     if(RegisterClass(&wc) == 0)
  529.         return FALSE;
  530.  
  531.  
  532.     // Create the main application window
  533.     hWnd = CreateWindow(
  534.         lpszAppName,
  535.         lpszAppName,
  536.  
  537.         // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  538.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  539.  
  540.         // Window position and size
  541.         50, 50,
  542.         400, 400,
  543.         NULL,
  544.         NULL,
  545.         hInstance,
  546.         NULL);
  547.  
  548.     // If window was not created, quit
  549.     if(hWnd == NULL)
  550.         return FALSE;
  551.  
  552.  
  553.     // Display the window
  554.     ShowWindow(hWnd,SW_SHOW);
  555.     UpdateWindow(hWnd);
  556.  
  557.     // Process application messages until the application closes
  558.     while( GetMessage(&msg, NULL, 0, 0))
  559.     {
  560.         TranslateMessage(&msg);
  561.         DispatchMessage(&msg);
  562.     }
  563.  
  564.     return msg.wParam;
  565. }
  566.  
  567.  
  568.  
  569.  
  570. // Window procedure, handles all messages for this program
  571. LRESULT CALLBACK WndProc(       HWND    hWnd,
  572.                          UINT    message,
  573.                          WPARAM  wParam,
  574.                          LPARAM  lParam)
  575. {
  576.     static HGLRC hRC;               // Permenant Rendering context
  577.     static HDC hDC;                 // Private GDI Device context
  578.  
  579.     switch (message)
  580.     {
  581.         // Window creation, setup for OpenGL
  582.     case WM_TIMER:
  583.         if(wParam==101)
  584.         {
  585.             licznik++;
  586.             if(licznik<15)
  587.             {
  588.                 rot2+=4.0;
  589.             }
  590.  
  591.             if(licznik>15 && licznik < 30)
  592.             {
  593.                 rot2-=4.0;
  594.             }
  595.  
  596.             if(licznik>30)
  597.             {licznik=0;}
  598.             InvalidateRect(hWnd,NULL,FALSE);
  599.         }
  600.         break;
  601.  
  602.     case WM_CREATE:
  603.  
  604.         SetTimer(hWnd,101,30,NULL);
  605.         // Store the device context
  606.         hDC = GetDC(hWnd);              
  607.  
  608.         // Select the pixel format
  609.         SetDCPixelFormat(hDC);          
  610.  
  611.         // Create palette if needed
  612.         hPalette = GetOpenGLPalette(hDC);
  613.  
  614.         // Create the rendering context and make it current
  615.         hRC = wglCreateContext(hDC);
  616.         wglMakeCurrent(hDC, hRC);
  617.  
  618.         // Call OpenGL setup code
  619.         SetupRC();
  620.  
  621.         break;
  622.  
  623.         // Window is being destroyed, cleanup
  624.     case WM_DESTROY:
  625.  
  626.         KillTimer(hWnd,101);
  627.         // Deselect the current rendering context and delete it
  628.         wglMakeCurrent(hDC,NULL);
  629.         wglDeleteContext(hRC);
  630.  
  631.         ReleaseDC(hWnd,hDC);
  632.  
  633.         // Delete the palette if it was created
  634.         if(hPalette != NULL)
  635.             DeleteObject(hPalette);
  636.  
  637.  
  638.         // Tell the application to terminate after the window
  639.         // is gone.
  640.         PostQuitMessage(0);
  641.         break;
  642.  
  643.         // Window is resized.
  644.     case WM_SIZE:
  645.         // Call our function which modifies the clipping
  646.         // volume and viewport
  647.         ChangeSize(LOWORD(lParam), HIWORD(lParam));
  648.         break;
  649.  
  650.  
  651.         // The painting function.  This message sent by Windows
  652.         // whenever the screen needs updating.
  653.     case WM_PAINT:
  654.         {
  655.             // Call OpenGL drawing code
  656.             RenderScene();
  657.  
  658.             SwapBuffers(hDC);
  659.  
  660.             // Validate the newly painted client area
  661.             ValidateRect(hWnd,NULL);
  662.         }
  663.         break;
  664.  
  665.  
  666.         // Windows is telling the application that it may modify
  667.         // the system palette.  This message in essance asks the
  668.         // application for a new palette.
  669.     case WM_QUERYNEWPALETTE:
  670.         // If the palette was created.
  671.         if(hPalette)
  672.         {
  673.             int nRet;
  674.  
  675.             // Selects the palette into the current device context
  676.             SelectPalette(hDC, hPalette, FALSE);
  677.  
  678.             // Map entries from the currently selected palette to
  679.             // the system palette.  The return value is the number
  680.             // of palette entries modified.
  681.             nRet = RealizePalette(hDC);
  682.  
  683.             // Repaint, forces remap of palette in current window
  684.             InvalidateRect(hWnd,NULL,FALSE);
  685.  
  686.             return nRet;
  687.         }
  688.         break;
  689.  
  690.  
  691.         // This window may set the palette, even though it is not the
  692.         // currently active window.
  693.     case WM_PALETTECHANGED:
  694.         // Don't do anything if the palette does not exist, or if
  695.         // this is the window that changed the palette.
  696.         if((hPalette != NULL) && ((HWND)wParam != hWnd))
  697.         {
  698.             // Select the palette into the device context
  699.             SelectPalette(hDC,hPalette,FALSE);
  700.  
  701.             // Map entries to system palette
  702.             RealizePalette(hDC);
  703.  
  704.             // Remap the current colors to the newly realized palette
  705.             UpdateColors(hDC);
  706.             return 0;
  707.         }
  708.         break;
  709.  
  710.  
  711.         // Key press, check for arrow keys to do cube rotation.
  712.     case WM_KEYDOWN:
  713.         {
  714.             if(wParam == VK_UP)
  715.                 xRot-= 5.0f;
  716.  
  717.             if(wParam == VK_DOWN)
  718.                 xRot += 5.0f;
  719.  
  720.             if(wParam == VK_LEFT)
  721.                 yRot -= 5.0f;
  722.  
  723.             if(wParam == VK_RIGHT)
  724.                 yRot += 5.0f;
  725.  
  726.             if(xRot > 356.0f)
  727.                 xRot = 0.0f;
  728.  
  729.             if(xRot < -1.0f)
  730.                 xRot = 355.0f;
  731.  
  732.             if(yRot > 356.0f)
  733.                 yRot = 0.0f;
  734.  
  735.             if(yRot < -1.0f)
  736.                 yRot = 355.0f;
  737.  
  738.             if(wParam == '1')
  739.                 rot1 -= 5.0f;
  740.  
  741.             if(wParam == '2')
  742.                 rot1 += 5.0f;
  743.  
  744.             if(wParam == '3')
  745.                 rot2 -= 5.0f;
  746.  
  747.             if(wParam == '4')
  748.                 rot2 += 5.0f;
  749.  
  750.             if(wParam == '5')
  751.                 rot3 -= 5.0f;
  752.  
  753.             if(wParam == '6')
  754.                 rot3 += 5.0f;
  755.  
  756.             InvalidateRect(hWnd,NULL,FALSE);
  757.         }
  758.         break;
  759.  
  760.         // A menu command
  761.     case WM_COMMAND:
  762.         {
  763.             HANDLE hMenu = GetMenu(hWnd);
  764.  
  765.             switch(LOWORD(wParam))
  766.             {
  767.                 // Exit the program
  768.             case ID_FILE_EXIT:
  769.                 DestroyWindow(hWnd);
  770.                 break;
  771.  
  772.                 // Display the about box
  773.             case ID_HELP_ABOUT:
  774.                 DialogBox (hInstance,
  775.                     MAKEINTRESOURCE(IDD_DIALOG_ABOUT),
  776.                     hWnd,
  777.                     AboutDlgProc);
  778.                 break;
  779.  
  780.             }
  781.         }
  782.         break;
  783.  
  784.  
  785.     default:   // Passes it on if unproccessed
  786.         return (DefWindowProc(hWnd, message, wParam, lParam));
  787.  
  788.     }
  789.  
  790.     return (0L);
  791. }
  792.  
  793.  
  794.  
  795.  
  796. // Dialog procedure.
  797. BOOL APIENTRY AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam)
  798. {
  799.  
  800.     switch (message)
  801.     {
  802.         // Initialize the dialog box
  803.     case WM_INITDIALOG:
  804.         {
  805.             int i;
  806.             GLenum glError;
  807.  
  808.             // glGetString demo
  809.             SetDlgItemText(hDlg,IDC_OPENGL_VENDOR,(LPCSTR)glGetString(GL_VENDOR));
  810.             SetDlgItemText(hDlg,IDC_OPENGL_RENDERER,(LPCSTR)glGetString(GL_RENDERER));
  811.             SetDlgItemText(hDlg,IDC_OPENGL_VERSION,(LPCSTR)glGetString(GL_VERSION));
  812.             SetDlgItemText(hDlg,IDC_OPENGL_EXTENSIONS,(LPCSTR)glGetString(GL_EXTENSIONS));
  813.  
  814.             // gluGetString demo
  815.             SetDlgItemText(hDlg,IDC_GLU_VERSION,(LPCSTR)gluGetString(GLU_VERSION));
  816.             SetDlgItemText(hDlg,IDC_GLU_EXTENSIONS,(LPCSTR)gluGetString(GLU_EXTENSIONS));
  817.  
  818.  
  819.             // Display any recent error messages
  820.             i = 0;
  821.             do {
  822.                 glError = glGetError();
  823.                 SetDlgItemText(hDlg,IDC_ERROR1+i,(LPCSTR)gluErrorString(glError));
  824.                 i++;
  825.             }
  826.             while(i < 6 && glError != GL_NO_ERROR);
  827.  
  828.  
  829.             return (TRUE);
  830.         }
  831.         break;
  832.  
  833.         // Process command messages
  834.     case WM_COMMAND:      
  835.         {
  836.             // Validate and Make the changes
  837.             if(LOWORD(wParam) == IDOK)
  838.                 EndDialog(hDlg,TRUE);
  839.         }
  840.         break;
  841.  
  842.         // Closed from sysbox
  843.     case WM_CLOSE:
  844.         EndDialog(hDlg,TRUE);
  845.         break;
  846.     }
  847.  
  848.     return FALSE;
  849. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement