Facebook
From Anorexic Pintail, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 201
  1. // Gl_template.c
  2. //Wyłšczanie błędów przed "fopen"
  3. #define  _CRT_SECURE_NO_WARNINGS
  4.  
  5. // Ładowanie bibliotek:
  6. #ifdef _MSC_VER                         // Check if MS Visual C compiler
  7. #  pragma comment(lib, "opengl32.lib")  // Compiler-specific directive to avoid manually configuration
  8. #  pragma comment(lib, "glu32.lib")     // Link libraries
  9. #endif
  10.  
  11. // Ustalanie trybu tekstowego:
  12. #ifdef _MSC_VER        // Check if MS Visual C compiler
  13. #   ifndef _MBCS
  14. #      define _MBCS    // Uses Multi-byte character set
  15. #   endif
  16. #   ifdef _UNICODE     // Not using Unicode character set
  17. #      undef _UNICODE
  18. #   endif
  19. #   ifdef UNICODE
  20. #      undef UNICODE
  21. #   endif
  22. #endif
  23.  
  24.  
  25. #include <windows.h>            // Window defines
  26. #include <gl\gl.h>              // OpenGL
  27. #include <gl\glu.h>             // GLU library
  28. #include <math.h>                               // Define for sqrt
  29. #include <stdio.h>
  30. #include "resource.h"           // About box resource identifiers.
  31.  
  32. #define glRGB(x, y, z)  glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z)
  33. #define BITMAP_ID 0x4D42                // identyfikator formatu BMP
  34. #define GL_PI 3.14
  35.  
  36. // Color Palette handle
  37. HPALETTE hPalette = NULL;
  38.  
  39. // Application name and instance storeage
  40. static LPCTSTR lpszAppName = "GL Template";
  41. static HINSTANCE hInstance;
  42.  
  43. // Rotation amounts
  44. static GLfloat xRot = 0.0f;
  45. static GLfloat yRot = 0.0f;
  46.  
  47.  
  48. static GLsizei lastHeight;
  49. static GLsizei lastWidth;
  50.  
  51. // Opis tekstury
  52. BITMAPINFOHEADER        bitmapInfoHeader;       // nagłówek obrazu
  53. unsigned char*          bitmapData;                     // dane tekstury
  54. unsigned int            texture[2];                     // obiekt tekstury
  55.  
  56.  
  57.                                                                                 // Declaration for Window procedure
  58. LRESULT CALLBACK WndProc(HWND    hWnd,
  59.         UINT    message,
  60.         WPARAM  wParam,
  61.         LPARAM  lParam);
  62.  
  63. // Dialog procedure for about box
  64. BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
  65.  
  66. // Set Pixel Format function - forward declaration
  67. void SetDCPixelFormat(HDC hDC);
  68.  
  69.  
  70.  
  71. // Reduces a normal vector specified as a set of three coordinates,
  72. // to a unit normal vector of length one.
  73. void ReduceToUnit(float vector[3])
  74. {
  75.         float length;
  76.  
  77.         // Calculate the length of the vector          
  78.         length = (float)sqrt((vector[0] * vector[0]) +
  79.                 (vector[1] * vector[1]) +
  80.                 (vector[2] * vector[2]));
  81.  
  82.         // Keep the program from blowing up by providing an exceptable
  83.         // value for vectors that may calculated too close to zero.
  84.         if (length == 0.0f)
  85.                 length = 1.0f;
  86.  
  87.         // Dividing each element by the length will result in a
  88.         // unit normal vector.
  89.         vector[0] /= length;
  90.         vector[1] /= length;
  91.         vector[2] /= length;
  92. }
  93.  
  94.  
  95. // Points p1, p2, & p3 specified in counter clock-wise order
  96. void calcNormal(float v[3][3], float out[3])
  97. {
  98.         float v1[3], v2[3];
  99.         static const int x = 0;
  100.         static const int y = 1;
  101.         static const int z = 2;
  102.  
  103.         // Calculate two vectors from the three points
  104.         v1[x] = v[0][x] - v[1][x];
  105.         v1[y] = v[0][y] - v[1][y];
  106.         v1[z] = v[0][z] - v[1][z];
  107.  
  108.         v2[x] = v[1][x] - v[2][x];
  109.         v2[y] = v[1][y] - v[2][y];
  110.         v2[z] = v[1][z] - v[2][z];
  111.  
  112.         // Take the cross product of the two vectors to get
  113.         // the normal vector which will be stored in out
  114.         out[x] = v1[y] * v2[z] - v1[z] * v2[y];
  115.         out[y] = v1[z] * v2[x] - v1[x] * v2[z];
  116.         out[z] = v1[x] * v2[y] - v1[y] * v2[x];
  117.  
  118.         // Normalize the vector (shorten length to one)
  119.         ReduceToUnit(out);
  120. }
  121.  
  122.  
  123.  
  124. // Change viewing volume and viewport.  Called when window is resized
  125. void ChangeSize(GLsizei w, GLsizei h)
  126. {
  127.         GLfloat nRange = 100.0f;
  128.         GLfloat fAspect;
  129.         // Prevent a divide by zero
  130.         if (h == 0)
  131.                 h = 1;
  132.  
  133.         lastWidth = w;
  134.         lastHeight = h;
  135.  
  136.         fAspect = (GLfloat)w / (GLfloat)h;
  137.         // Set Viewport to window dimensions
  138.         glViewport(0, 0, w, h);
  139.  
  140.         // Reset coordinate system
  141.         glMatrixMode(GL_PROJECTION);
  142.         glLoadIdentity();
  143.  
  144.         // Establish clipping volume (left, right, bottom, top, near, far)
  145.         if (w <= h)
  146.                 glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
  147.         else
  148.                 glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
  149.  
  150.         // Establish perspective:
  151.         /*
  152.         gluPerspective(60.0f,fAspect,1.0,400);
  153.         */
  154.  
  155.         glMatrixMode(GL_MODELVIEW);
  156.         glLoadIdentity();
  157. }
  158.  
  159.  
  160.  
  161. // This function does any needed initialization on the rendering
  162. // context.  Here it sets up and initializes the lighting for
  163. // the scene.
  164. void SetupRC()
  165. {
  166.         // Light values and coordinates
  167.         //GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  168.         //GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  169.         //GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
  170.         //GLfloat        lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
  171.         //GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
  172.  
  173.  
  174.         glEnable(GL_DEPTH_TEST);        // Hidden surface removal
  175.         glFrontFace(GL_CCW);            // Counter clock-wise polygons face out
  176.                                                                 //glEnable(GL_CULL_FACE);               // Do not calculate inside of jet
  177.  
  178.                                                                 // Enable lighting
  179.                                                                 //glEnable(GL_LIGHTING);
  180.  
  181.                                                                 // Setup and enable light 0
  182.                                                                 //glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  183.                                                                 //glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  184.                                                                 //glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  185.                                                                 //glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  186.                                                                 //glEnable(GL_LIGHT0);
  187.  
  188.                                                                 // Enable color tracking
  189.                                                                 //glEnable(GL_COLOR_MATERIAL);
  190.  
  191.                                                                 // Set Material properties to follow glColor values
  192.                                                                 //glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  193.  
  194.                                                                 // All materials hereafter have full specular reflectivity
  195.                                                                 // with a high shine
  196.                                                                 //glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
  197.                                                                 //glMateriali(GL_FRONT,GL_SHININESS,128);
  198.  
  199.  
  200.                                                                 // White background
  201.         glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  202.         // Black brush
  203.         glColor3f(0.0, 0.0, 0.0);
  204. }
  205.  
  206. void skrzynka(void)
  207. {
  208.         glColor3d(0.8, 0.7, 0.3);
  209.  
  210.  
  211.         glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
  212.  
  213.         glBindTexture(GL_TEXTURE_2D, texture[0]);
  214.         glBegin(GL_QUADS);
  215.         glNormal3d(0, 0, 1);
  216.         glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  217.         glTexCoord2d(0.0, 1.0); glVertex3d(-25, 25, 25);
  218.         glTexCoord2d(0.0, 0.0); glVertex3d(-25, -25, 25);
  219.         glTexCoord2d(1.0, 0.0); glVertex3d(25, -25, 25);
  220.         glEnd();
  221.         glBindTexture(GL_TEXTURE_2D, texture[1]);
  222.         glBegin(GL_QUADS);
  223.         glNormal3d(1, 0, 0);
  224.         glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  225.         glTexCoord2d(0.0, 1.0); glVertex3d(25, -25, 25);
  226.         glTexCoord2d(0.0, 0.0); glVertex3d(25, -25, -25);
  227.         glTexCoord2d(1.0, 0.0); glVertex3d(25, 25, -25);
  228.         glEnd();
  229.  
  230.         glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
  231.  
  232.  
  233.  
  234.         glBegin(GL_QUADS);
  235.         glNormal3d(0, 0, -1);
  236.         glVertex3d(25, 25, -25);
  237.         glVertex3d(25, -25, -25);
  238.         glVertex3d(-25, -25, -25);
  239.         glVertex3d(-25, 25, -25);
  240.  
  241.         glNormal3d(-1, 0, 0);
  242.         glVertex3d(-25, 25, -25);
  243.         glVertex3d(-25, -25, -25);
  244.         glVertex3d(-25, -25, 25);
  245.         glVertex3d(-25, 25, 25);
  246.  
  247.         glNormal3d(0, 1, 0);
  248.         glVertex3d(25, 25, 25);
  249.         glVertex3d(25, 25, -25);
  250.         glVertex3d(-25, 25, -25);
  251.         glVertex3d(-25, 25, 25);
  252.  
  253.         glNormal3d(0, -1, 0);
  254.         glVertex3d(25, -25, 25);
  255.         glVertex3d(-25, -25, 25);
  256.         glVertex3d(-25, -25, -25);
  257.         glVertex3d(25, -25, -25);
  258.         glEnd();
  259. }
  260.  
  261. void walec01(void)
  262. {
  263.         GLUquadricObj*obj;
  264.         obj = gluNewQuadric();
  265.         gluQuadricNormals(obj, GLU_SMOOTH);
  266.         glColor3d(1, 0, 0);
  267.         glPushMatrix();
  268.         gluCylinder(obj, 20, 20, 30, 15, 7);
  269.         gluCylinder(obj, 0, 20, 0, 15, 7);
  270.         glTranslated(0, 0, 60);
  271.         glRotated(180.0, 0, 1, 0);
  272.         gluCylinder(obj, 0, 20, 30, 15, 7);
  273.         glPopMatrix();
  274. }
  275.  
  276. void kula(void)
  277. {
  278.         GLUquadricObj*obj;
  279.         obj = gluNewQuadric();
  280.         gluQuadricTexture(obj, GL_TRUE);
  281.         glBindTexture(GL_TEXTURE_2D, texture[0]);
  282.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  283.         glColor3d(1.0, 0.8, 0.8);
  284.         glEnable(GL_TEXTURE_2D);
  285.         gluSphere(obj, 40, 15, 7);
  286.         glDisable(GL_TEXTURE_2D);
  287. }
  288.  
  289.  
  290.  
  291.  
  292. // LoadBitmapFile
  293. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
  294. //       Wypełnia strukturę nagłówka.
  295. //       Nie obsługuje map 8-bitowych.
  296. unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  297. {
  298.         FILE *filePtr;                                                  // wskaźnik pozycji pliku
  299.         BITMAPFILEHEADER        bitmapFileHeader;               // nagłówek pliku
  300.         unsigned char           *bitmapImage;                   // dane obrazu
  301.         int                                     imageIdx = 0;           // licznik pikseli
  302.         unsigned char           tempRGB;                                // zmienna zamiany składowych
  303.  
  304.                                                                                                 // otwiera plik w trybie "read binary"
  305.         filePtr = fopen(filename, "rb");
  306.         if (filePtr == NULL)
  307.                 return NULL;
  308.  
  309.         // wczytuje nagłówek pliku
  310.         fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
  311.  
  312.         // sprawdza, czy jest to plik formatu BMP
  313.         if (bitmapFileHeader.bfType != BITMAP_ID)
  314.         {
  315.                 fclose(filePtr);
  316.                 return NULL;
  317.         }
  318.  
  319.         // wczytuje nagłówek obrazu
  320.         fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
  321.  
  322.         // ustawia wskaźnik pozycji pliku na początku danych obrazu
  323.         fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
  324.  
  325.         // przydziela pamięć buforowi obrazu
  326.         bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
  327.  
  328.         // sprawdza, czy udało się przydzielić pamięć
  329.         if (!bitmapImage)
  330.         {
  331.                 free(bitmapImage);
  332.                 fclose(filePtr);
  333.                 return NULL;
  334.         }
  335.  
  336.         // wczytuje dane obrazu
  337.         fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
  338.  
  339.         // sprawdza, czy dane zostały wczytane
  340.         if (bitmapImage == NULL)
  341.         {
  342.                 fclose(filePtr);
  343.                 return NULL;
  344.         }
  345.  
  346.         // zamienia miejscami składowe R i B
  347.         for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
  348.         {
  349.                 tempRGB = bitmapImage[imageIdx];
  350.                 bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
  351.                 bitmapImage[imageIdx + 2] = tempRGB;
  352.         }
  353.  
  354.         // zamyka plik i zwraca wskaźnik bufora zawierającego wczytany obraz
  355.         fclose(filePtr);
  356.         return bitmapImage;
  357. }
  358. void szescian(void)
  359.         {
  360.                 glBegin(GL_QUADS);
  361.                         glColor3d(1, 0.5, 0);
  362.                         glVertex3d(20, 20, 20);
  363.                         glVertex3d(-20, 20, 20);
  364.                         glVertex3d(-20, -20, 20);
  365.                         glVertex3d(20, -20, 20);
  366.                         glColor3d(0, 0.5, 1);
  367.                         glVertex3d(20, 20, 20);
  368.                         glVertex3d(20, -20, 20);
  369.                         glVertex3d(20, -20, -20);
  370.                         glVertex3d(20, 20, -20);
  371.                 glEnd();
  372.         }
  373.  
  374. void walec(double r, double h)
  375. {
  376.         double angle, x, y;
  377.         glBegin(GL_TRIANGLE_FAN);
  378.         glColor3d(1, 0.5, 0);
  379.         glVertex3d(0.0f, 0.0f, 0.0f);
  380.         for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  381.         {
  382.                 x = r*sin(angle);
  383.                 y = r*cos(angle);
  384.                 glVertex3d(x, y, 0.0);
  385.         }
  386.         glEnd();
  387.         glBegin(GL_QUAD_STRIP);
  388.         for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  389.         {
  390.                 x = r*sin(angle);
  391.                 y = r*cos(angle);
  392.                 glVertex3d(x, y, 0);
  393.                 glVertex3d(x, y, h);
  394.         }
  395.         glEnd();
  396.         glBegin(GL_TRIANGLE_FAN);
  397.         glColor3d(1, 0.5, 0);
  398.         glVertex3d(0.0f, 0.0f, h);
  399.         for (angle = 0.0f; angle >= (-2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  400.         {
  401.                 x = r*sin(angle);
  402.                 y = r*cos(angle);
  403.                 glVertex3d(x, y, h);
  404.         }
  405.         glEnd();
  406. }
  407.  
  408. void ramie(double r1, double r2, double d, double h)
  409. {
  410.         double angle, x, y;
  411.         glBegin(GL_TRIANGLE_FAN);
  412.         glColor3d(1, 0.5, 0);
  413.         glVertex3d(0.0f, 0.0f, 0.0f);
  414.         for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  415.         {
  416.                 x = r1*sin(angle);
  417.                 y = r1*cos(angle);
  418.                 glVertex3d(x, y, 0.0);
  419.         }
  420.         glEnd();
  421.         glBegin(GL_QUAD_STRIP);
  422.         for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  423.         {
  424.                 x = r1*sin(angle);
  425.                 y = r1*cos(angle);
  426.                 glVertex3d(x, y, 0);
  427.                 glVertex3d(x, y, h);
  428.         }
  429.         glEnd();
  430.         glBegin(GL_TRIANGLE_FAN);
  431.         glColor3d(1, 0.5, 0);
  432.         glVertex3d(0.0f, 0.0f, h);
  433.         for (angle = 0.0f; angle >= (-1.0f*GL_PI); angle -= (GL_PI / 8.0f))
  434.         {
  435.                 x = r1*sin(angle);
  436.                 y = r1*cos(angle);
  437.                 glVertex3d(x, y, h);
  438.         }
  439.         glEnd();
  440.         //drugi polwalec
  441.         glBegin(GL_TRIANGLE_FAN);
  442.         glColor3d(0, 0, 1);
  443.         glVertex3d(0.0f + d, 0.0f, 0.0f);
  444.         for (angle = 0.0f; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  445.         {
  446.                 x = r2*sin(angle);
  447.                 y = r2*cos(angle);
  448.                 glVertex3d(x + d, y, 0.0);
  449.         }
  450.         glEnd();
  451.         glBegin(GL_QUAD_STRIP);
  452.         for (angle = 0.0f; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  453.         {
  454.                 x = r2*sin(angle);
  455.                 y = r2*cos(angle);
  456.                 glVertex3d(x + d, y, 0);
  457.                 glVertex3d(x + d, y, h);
  458.         }
  459.         glEnd();
  460.         glBegin(GL_TRIANGLE_FAN);
  461.         glColor3d(0, 0, 1);
  462.         glVertex3d(0.0f + d, 0.0f, h);
  463.         for (angle = (-1.0f * GL_PI); angle >= (-2.0f * GL_PI); angle -= (GL_PI / 8.0f))
  464.         {
  465.                 x = r2*sin(angle);
  466.                 y = r2*cos(angle);
  467.                 glVertex3d(x + d, y, h);
  468.         }
  469.         glEnd();
  470. //szescian
  471.         glBegin(GL_QUADS);
  472.  
  473.         //podstawa
  474.                 /*glColor3d(1, 0.5, 0);
  475.                 glVertex3d(0, -r1, h);
  476.                 glVertex3d(d, -r2, h);
  477.                 glVertex3d(d, -r2, 0);
  478.                 glVertex3d(0, -r1, 0);*/
  479. //podstawa
  480.                 glColor3d(1, 0.5, 0);
  481.                 glVertex3d(0, -r1, 0);
  482.                 glVertex3d(d, -r2, 0);
  483.                 glVertex3d(d, -r2, h);
  484.                 glVertex3d(0, -r1, h);
  485.         //pokrywka
  486.                 glColor3d(1, 0.5, 0);
  487.                 glVertex3d(0, r1, h);
  488.                 glVertex3d(d, r2, h);
  489.                 glVertex3d(d, r2, 0);
  490.                 glVertex3d(0, r1, 0);
  491.  
  492. //sciana 1
  493.                 glColor3d(0, 0.5, 1);
  494.                 glVertex3d(0, -r1, h);
  495.                 glVertex3d(d, -r2, h);
  496.                 glVertex3d(d, r2, h);
  497.                 glVertex3d(0, r1, h);
  498.  
  499. //sciana 2
  500.                 glColor3d(0, 0.5, 1);
  501.                 glVertex3d(0, r1, 0);
  502.                 glVertex3d(d, r2, 0);
  503.                 glVertex3d(d, -r2, 0);
  504.                 glVertex3d(0, -r1, 0);
  505.  
  506.                
  507.         glEnd();
  508.  
  509. }
  510. // Called to draw scene
  511. void RenderScene(void)
  512. {
  513.         //float normal[3];      // Storeage for calculated surface normal
  514.  
  515.         // Clear the window with current clearing color
  516.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  517.  
  518.         // Save the matrix state and do the rotations
  519.         glPushMatrix();
  520.         glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  521.         glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  522.  
  523.         /////////////////////////////////////////////////////////////////
  524.         // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN:               //
  525.         /////////////////////////////////////////////////////////////////
  526.  
  527.         //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
  528.         glPolygonMode(GL_BACK, GL_LINE);
  529.  
  530.         //Uzyskanie siatki:
  531.         //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  532.  
  533.         //Wyrysowanie prostokata:
  534.         //glRectd(-10.0, -10.0, 20.0, 20.0);
  535.         //szescian();
  536.         //walec(30, 20);
  537.         ramie(30, 20, 30, 50);
  538.  
  539.  
  540.  
  541.         /////////////////////////////////////////////////////////////////
  542.         /////////////////////////////////////////////////////////////////
  543.         /////////////////////////////////////////////////////////////////
  544.         glPopMatrix();
  545.         glMatrixMode(GL_MODELVIEW);
  546.  
  547.         // Flush drawing commands
  548.         glFlush();
  549. }
  550.  
  551.  
  552. // Select the pixel format for a given device context
  553. void SetDCPixelFormat(HDC hDC)
  554. {
  555.         int nPixelFormat;
  556.  
  557.         static PIXELFORMATDESCRIPTOR pfd = {
  558.                 sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  559.                 1,                                                              // Version of this structure    
  560.                 PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  561.                 PFD_SUPPORT_OPENGL |                                    // Support OpenGL calls in window
  562.                 PFD_DOUBLEBUFFER,                       // Double buffered
  563.                 PFD_TYPE_RGBA,                          // RGBA Color mode
  564.                 24,                                     // Want 24bit color
  565.                 0,0,0,0,0,0,                            // Not used to select mode
  566.                 0,0,                                    // Not used to select mode
  567.                 0,0,0,0,0,                              // Not used to select mode
  568.                 32,                                     // Size of depth buffer
  569.                 0,                                      // Not used to select mode
  570.                 0,                                      // Not used to select mode
  571.                 PFD_MAIN_PLANE,                         // Draw in main plane
  572.                 0,                                      // Not used to select mode
  573.                 0,0,0 };                                // Not used to select mode
  574.  
  575.                                                                                                 // Choose a pixel format that best matches that described in pfd
  576.         nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  577.  
  578.         // Set the pixel format for the device context
  579.         SetPixelFormat(hDC, nPixelFormat, &pfd);
  580. }
  581.  
  582.  
  583.  
  584. // If necessary, creates a 3-3-2 palette for the device context listed.
  585. HPALETTE GetOpenGLPalette(HDC hDC)
  586. {
  587.         HPALETTE hRetPal = NULL;        // Handle to palette to be created
  588.         PIXELFORMATDESCRIPTOR pfd;      // Pixel Format Descriptor
  589.         LOGPALETTE *pPal;                       // Pointer to memory for logical palette
  590.         int nPixelFormat;                       // Pixel format index
  591.         int nColors;                            // Number of entries in palette
  592.         int i;                                          // Counting variable
  593.         BYTE RedRange, GreenRange, BlueRange;
  594.         // Range for each color entry (7,7,and 3)
  595.  
  596.  
  597.         // Get the pixel format index and retrieve the pixel format description
  598.         nPixelFormat = GetPixelFormat(hDC);
  599.         DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  600.  
  601.         // Does this pixel format require a palette?  If not, do not create a
  602.         // palette and just return NULL
  603.         if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  604.                 return NULL;
  605.  
  606.         // Number of entries in palette.  8 bits yeilds 256 entries
  607.         nColors = 1 << pfd.cColorBits;
  608.  
  609.         // Allocate space for a logical palette structure plus all the palette entries
  610.         pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  611.  
  612.         // Fill in palette header
  613.         pPal->palVersion = 0x300;               // Windows 3.0
  614.         pPal->palNumEntries = nColors; // table size
  615.  
  616.                                                                    // Build mask of all 1's.  This creates a number represented by having
  617.                                                                    // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  618.                                                                    // pfd.cBlueBits.  
  619.         RedRange = (1 << pfd.cRedBits) - 1;
  620.         GreenRange = (1 << pfd.cGreenBits) - 1;
  621.         BlueRange = (1 << pfd.cBlueBits) - 1;
  622.  
  623.         // Loop through all the palette entries
  624.         for (i = 0; i < nColors; i++)
  625.         {
  626.                 // Fill in the 8-bit equivalents for each component
  627.                 pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  628.                 pPal->palPalEntry[i].peRed = (unsigned char)(
  629.                         (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  630.  
  631.                 pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  632.                 pPal->palPalEntry[i].peGreen = (unsigned char)(
  633.                         (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  634.  
  635.                 pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  636.                 pPal->palPalEntry[i].peBlue = (unsigned char)(
  637.                         (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  638.  
  639.                 pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  640.         }
  641.  
  642.  
  643.         // Create the palette
  644.         hRetPal = CreatePalette(pPal);
  645.  
  646.         // Go ahead and select and realize the palette for this device context
  647.         SelectPalette(hDC, hRetPal, FALSE);
  648.         RealizePalette(hDC);
  649.  
  650.         // Free the memory used for the logical palette structure
  651.         free(pPal);
  652.  
  653.         // Return the handle to the new palette
  654.         return hRetPal;
  655. }
  656.  
  657.  
  658. // Entry point of all Windows programs
  659. int APIENTRY WinMain(HINSTANCE       hInst,
  660.         HINSTANCE       hPrevInstance,
  661.         LPSTR           lpCmdLine,
  662.         int                     nCmdShow)
  663. {
  664.         MSG                     msg;            // Windows message structure
  665.         WNDCLASS        wc;                     // Windows class structure
  666.         HWND            hWnd;           // Storeage for window handle
  667.  
  668.         hInstance = hInst;
  669.  
  670.         // Register Window style
  671.         wc.style = CS_HREDRAW | CS_VREDRAW;
  672.         wc.lpfnWndProc = (WNDPROC)WndProc;
  673.         wc.cbClsExtra = 0;
  674.         wc.cbWndExtra = 0;
  675.         wc.hInstance = hInstance;
  676.         wc.hIcon = NULL;
  677.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  678.  
  679.         // No need for background brush for OpenGL window
  680.         wc.hbrBackground = NULL;
  681.  
  682.         wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  683.         wc.lpszClassName = lpszAppName;
  684.  
  685.         // Register the window class
  686.         if (RegisterClass(&wc) == 0)
  687.                 return FALSE;
  688.  
  689.  
  690.         // Create the main application window
  691.         hWnd = CreateWindow(
  692.                 lpszAppName,
  693.                 lpszAppName,
  694.  
  695.                 // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  696.                 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  697.  
  698.                 // Window position and size
  699.                 50, 50,
  700.                 400, 400,
  701.                 NULL,
  702.                 NULL,
  703.                 hInstance,
  704.                 NULL);
  705.  
  706.         // If window was not created, quit
  707.         if (hWnd == NULL)
  708.                 return FALSE;
  709.  
  710.  
  711.         // Display the window
  712.         ShowWindow(hWnd, SW_SHOW);
  713.         UpdateWindow(hWnd);
  714.  
  715.         // Process application messages until the application closes
  716.         while (GetMessage(&msg, NULL, 0, 0))
  717.         {
  718.                 TranslateMessage(&msg);
  719.                 DispatchMessage(&msg);
  720.         }
  721.  
  722.         return msg.wParam;
  723. }
  724.  
  725.  
  726.  
  727.  
  728. // Window procedure, handles all messages for this program
  729. LRESULT CALLBACK WndProc(HWND    hWnd,
  730.         UINT    message,
  731.         WPARAM  wParam,
  732.         LPARAM  lParam)
  733. {
  734.         static HGLRC hRC;               // Permenant Rendering context
  735.         static HDC hDC;                 // Private GDI Device context
  736.  
  737.         switch (message)
  738.         {
  739.                 // Window creation, setup for OpenGL
  740.         case WM_CREATE:
  741.                 // Store the device context
  742.                 hDC = GetDC(hWnd);
  743.  
  744.                 // Select the pixel format
  745.                 SetDCPixelFormat(hDC);
  746.  
  747.                 // Create palette if needed
  748.                 hPalette = GetOpenGLPalette(hDC);
  749.  
  750.                 // Create the rendering context and make it current
  751.                 hRC = wglCreateContext(hDC);
  752.                 wglMakeCurrent(hDC, hRC);
  753.                 SetupRC();
  754.                 glGenTextures(2, &texture[0]);                  // tworzy obiekt tekstury                      
  755.  
  756.                                                                                                                 // ładuje pierwszy obraz tekstury:
  757.                 bitmapData = LoadBitmapFile("Bitmapy\\checker.bmp", &bitmapInfoHeader);
  758.  
  759.                 glBindTexture(GL_TEXTURE_2D, texture[0]);       // aktywuje obiekt tekstury
  760.  
  761.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  762.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  763.  
  764.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  765.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  766.  
  767.                 // tworzy obraz tekstury
  768.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  769.                         bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  770.  
  771.                 if (bitmapData)
  772.                         free(bitmapData);
  773.  
  774.                 // ładuje drugi obraz tekstury:
  775.                 bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  776.                 glBindTexture(GL_TEXTURE_2D, texture[1]);       // aktywuje obiekt tekstury
  777.  
  778.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  779.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  780.  
  781.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  782.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  783.  
  784.                 // tworzy obraz tekstury
  785.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  786.                         bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  787.  
  788.                 if (bitmapData)
  789.                         free(bitmapData);
  790.  
  791.                 // ustalenie sposobu mieszania tekstury z tłem
  792.                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  793.                 break;
  794.  
  795.                 // Window is being destroyed, cleanup
  796.         case WM_DESTROY:
  797.                 // Deselect the current rendering context and delete it
  798.                 wglMakeCurrent(hDC, NULL);
  799.                 wglDeleteContext(hRC);
  800.  
  801.                 // Delete the palette if it was created
  802.                 if (hPalette != NULL)
  803.                         DeleteObject(hPalette);
  804.  
  805.                 // Tell the application to terminate after the window
  806.                 // is gone.
  807.                 PostQuitMessage(0);
  808.                 break;
  809.  
  810.                 // Window is resized.
  811.         case WM_SIZE:
  812.                 // Call our function which modifies the clipping
  813.                 // volume and viewport
  814.                 ChangeSize(LOWORD(lParam), HIWORD(lParam));
  815.                 break;
  816.  
  817.  
  818.                 // The painting function.  This message sent by Windows
  819.                 // whenever the screen needs updating.
  820.         case WM_PAINT:
  821.         {
  822.                 // Call OpenGL drawing code
  823.                 RenderScene();
  824.  
  825.                 SwapBuffers(hDC);
  826.  
  827.                 // Validate the newly painted client area
  828.                 ValidateRect(hWnd, NULL);
  829.         }
  830.         break;
  831.  
  832.         // Windows is telling the application that it may modify
  833.         // the system palette.  This message in essance asks the
  834.         // application for a new palette.
  835.         case WM_QUERYNEWPALETTE:
  836.                 // If the palette was created.
  837.                 if (hPalette)
  838.                 {
  839.                         int nRet;
  840.  
  841.                         // Selects the palette into the current device context
  842.                         SelectPalette(hDC, hPalette, FALSE);
  843.  
  844.                         // Map entries from the currently selected palette to
  845.                         // the system palette.  The return value is the number
  846.                         // of palette entries modified.
  847.                         nRet = RealizePalette(hDC);
  848.  
  849.                         // Repaint, forces remap of palette in current window
  850.                         InvalidateRect(hWnd, NULL, FALSE);
  851.  
  852.                         return nRet;
  853.                 }
  854.                 break;
  855.  
  856.  
  857.                 // This window may set the palette, even though it is not the
  858.                 // currently active window.
  859.         case WM_PALETTECHANGED:
  860.                 // Don't do anything if the palette does not exist, or if
  861.                 // this is the window that changed the palette.
  862.                 if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  863.                 {
  864.                         // Select the palette into the device context
  865.                         SelectPalette(hDC, hPalette, FALSE);
  866.  
  867.                         // Map entries to system palette
  868.                         RealizePalette(hDC);
  869.  
  870.                         // Remap the current colors to the newly realized palette
  871.                         UpdateColors(hDC);
  872.                         return 0;
  873.                 }
  874.                 break;
  875.  
  876.                 // Key press, check for arrow keys to do cube rotation.
  877.         case WM_KEYDOWN:
  878.         {
  879.                 if (wParam == VK_UP)
  880.                         xRot -= 5.0f;
  881.  
  882.                 if (wParam == VK_DOWN)
  883.                         xRot += 5.0f;
  884.  
  885.                 if (wParam == VK_LEFT)
  886.                         yRot -= 5.0f;
  887.  
  888.                 if (wParam == VK_RIGHT)
  889.                         yRot += 5.0f;
  890.  
  891.                 xRot = (const int)xRot % 360;
  892.                 yRot = (const int)yRot % 360;
  893.  
  894.                 InvalidateRect(hWnd, NULL, FALSE);
  895.         }
  896.         break;
  897.  
  898.         // A menu command
  899.         case WM_COMMAND:
  900.         {
  901.                 switch (LOWORD(wParam))
  902.                 {
  903.                         // Exit the program
  904.                 case ID_FILE_EXIT:
  905.                         DestroyWindow(hWnd);
  906.                         break;
  907.                 }
  908.         }
  909.         break;
  910.  
  911.  
  912.         default:   // Passes it on if unproccessed
  913.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  914.  
  915.         }
  916.  
  917.         return (0L);
  918. }
  919.