Facebook
From dcx, 4 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 209
  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.  
  359.  
  360.  
  361. void szescian(void)
  362. {
  363.         glBegin(GL_QUADS);  
  364.        
  365.         glColor3d(1, 0.5, 0);
  366.         glVertex3d(20, 20, 20);  
  367.         glVertex3d(-20, 20, 20);  
  368.         glVertex3d(-20, -20, 20);  
  369.         glVertex3d(20, -20, 20);
  370.  
  371.         glColor3d(0, 0.5, 1);
  372.         glVertex3d(20, 20, 20);  
  373.         glVertex3d(20, -20, 20);
  374.         glVertex3d(20, -20, -20);  
  375.         glVertex3d(20, 20, -20);  
  376.  
  377.  
  378.         glEnd();
  379. }
  380.  
  381. void walec(double h, double r)
  382. {
  383.         double angle, x, y;
  384.  
  385.         glColor3d(0, 0, 0);
  386.         glBegin(GL_TRIANGLE_FAN);
  387.         glVertex3d(0.0f, 0.0f, 0.0f);
  388.         for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  389.         {
  390.                 x = r*sin(angle);  y = r*cos(angle);  glVertex3d(x, y, 0.0);
  391.         }
  392.         glEnd();
  393.  
  394.         glBegin(GL_QUAD_STRIP);
  395.         for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  396.         {
  397.                 x = r*sin(angle);
  398.                 y = r*cos(angle);  
  399.                 glVertex3d(x, y, 0);
  400.                 glVertex3d(x, y, h);
  401.         }
  402.         glEnd();
  403.  
  404.        
  405.         glBegin(GL_TRIANGLE_FAN);
  406.         glColor3d(1, 0.5, 0);
  407.         glVertex3d(0.0f, 0.0f, h);
  408.         for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  409.         {
  410.                 x = r*sin(angle);  y = r*cos(angle);
  411.                 glVertex3d(x, y, h);
  412.         }
  413.         glEnd();
  414.  
  415.  
  416. }
  417.  
  418.  
  419.  
  420.  
  421. void ramie(double r1, double r2, double h, double d)
  422. {
  423.  
  424.         //pol walca
  425.         double r = r1;
  426.         {
  427.         double angle, x, y;
  428.  
  429.  
  430.                 glBegin(GL_TRIANGLE_FAN);
  431.                 glVertex3d(0.0f, 0.0f, 0.0f);
  432.                 for (angle = (GL_PI); angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  433.                 {
  434.                         x = r*sin(angle);  y = r*cos(angle);  glVertex3d(x, y, 0.0);
  435.                 }
  436.                 glEnd();
  437.  
  438.                 glBegin(GL_QUAD_STRIP);
  439.                 for (angle = (GL_PI); angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  440.                 {
  441.                         x = r*sin(angle);
  442.                         y = r*cos(angle);
  443.                         glVertex3d(x, y, 0);
  444.                         glVertex3d(x, y, h);
  445.                 }
  446.                 glEnd();
  447.  
  448.  
  449.                 glBegin(GL_TRIANGLE_FAN);
  450.                 glVertex3d(0.0f, 0.0f, h);
  451.                 for (angle = 0.0f; angle >= -(GL_PI); angle -= (GL_PI / 8.0f))
  452.                 {
  453.                         x = r*sin(angle);  y = r*cos(angle);
  454.                         glVertex3d(x, y, h);
  455.                 }
  456.                 glEnd();
  457.  
  458.  
  459.         }
  460.  
  461.  
  462.         //pol walca #2
  463.         r = r2;
  464.         {
  465.                 double angle, x, y;
  466.  
  467.                 glBegin(GL_TRIANGLE_FAN);
  468.                 glVertex3d(0.0f+d, 0.0f, 0.0f);
  469.                 for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  470.                 {
  471.                         x = r*sin(angle);  y = r*cos(angle);  
  472.                         glVertex3d(x+d, y, 0.0);
  473.                 }
  474.                 glEnd();
  475.  
  476.                 glBegin(GL_QUAD_STRIP);
  477.                 for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  478.                 {
  479.                         x = r*sin(angle);
  480.                         y = r*cos(angle);
  481.                         glVertex3d(x+d, y, 0);
  482.                         glVertex3d(x+d, y, h);
  483.                 }
  484.                 glEnd();
  485.  
  486.  
  487.                 glBegin(GL_TRIANGLE_FAN);
  488.                 glVertex3d(0.0f+d, 0.0f, h);
  489.                 for (angle =-GL_PI; angle >= -(2.0*GL_PI); angle -= (GL_PI / 8.0f))
  490.                 {
  491.                         x = r*sin(angle);
  492.                         y = r*cos(angle);
  493.                         glVertex3d(x+d, y, h);
  494.                 }
  495.                 glEnd();
  496.         }
  497.  
  498.         // srodek
  499.         {
  500.                 glBegin(GL_QUADS);
  501.                 glVertex3d(0, r1, h);
  502.                 glVertex3d(0, -r1, h);
  503.                 glVertex3d(d, -r2, h);
  504.                 glVertex3d(d, r2, h);
  505.                 glEnd();
  506.                
  507.                 glBegin(GL_QUADS);
  508.                 glVertex3d(0, r1, 0);
  509.                 glVertex3d(d, r2, 0);
  510.                 glVertex3d(d, -r2, 0);
  511.                 glVertex3d(0, -r1, 0);
  512.                 glEnd();
  513.  
  514.                 glBegin(GL_QUADS);
  515.                 glVertex3d(0, r1, 0);
  516.                 glVertex3d(0, r1, h);
  517.                 glVertex3d(d, r2, h);
  518.                 glVertex3d(d, r2, 0);
  519.                 glEnd();
  520.  
  521.                 glBegin(GL_QUADS);
  522.                 glVertex3d(0, -r1, h);
  523.                 glVertex3d(0, -r1, 0);
  524.                 glVertex3d(d, -r2, 0);
  525.                 glVertex3d(d, -r2, h);
  526.                 glEnd();
  527.  
  528.  
  529.  
  530.         }
  531.  
  532. }
  533.  
  534.  
  535.  
  536.  
  537. // Called to draw scene
  538. void RenderScene(void)
  539. {
  540.         //float normal[3];      // Storeage for calculated surface normal
  541.  
  542.         // Clear the window with current clearing color
  543.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  544.  
  545.         // Save the matrix state and do the rotations
  546.         glPushMatrix();
  547.         glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  548.         glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  549.  
  550.         /////////////////////////////////////////////////////////////////
  551.         // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN:               //
  552.         /////////////////////////////////////////////////////////////////
  553.  
  554.         //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
  555.         glPolygonMode(GL_BACK, GL_LINE);
  556.  
  557.         //Uzyskanie siatki:
  558.         //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  559.  
  560.         //Wyrysowanie prostokata:
  561.         //glRectd(-10.0, -10.0, 20.0, 20.0);
  562.         //szescian();
  563.  
  564.         //walec(30, 20);
  565.  
  566.         ramie(15, 10, 5, 30);
  567.  
  568.         /////////////////////////////////////////////////////////////////
  569.         /////////////////////////////////////////////////////////////////
  570.         /////////////////////////////////////////////////////////////////
  571.         glPopMatrix();
  572.         glMatrixMode(GL_MODELVIEW);
  573.  
  574.         // Flush drawing commands
  575.         glFlush();
  576. }
  577.  
  578.  
  579. // Select the pixel format for a given device context
  580. void SetDCPixelFormat(HDC hDC)
  581. {
  582.         int nPixelFormat;
  583.  
  584.         static PIXELFORMATDESCRIPTOR pfd = {
  585.                 sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  586.                 1,                                                              // Version of this structure    
  587.                 PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  588.                 PFD_SUPPORT_OPENGL |                                    // Support OpenGL calls in window
  589.                 PFD_DOUBLEBUFFER,                       // Double buffered
  590.                 PFD_TYPE_RGBA,                          // RGBA Color mode
  591.                 24,                                     // Want 24bit color
  592.                 0,0,0,0,0,0,                            // Not used to select mode
  593.                 0,0,                                    // Not used to select mode
  594.                 0,0,0,0,0,                              // Not used to select mode
  595.                 32,                                     // Size of depth buffer
  596.                 0,                                      // Not used to select mode
  597.                 0,                                      // Not used to select mode
  598.                 PFD_MAIN_PLANE,                         // Draw in main plane
  599.                 0,                                      // Not used to select mode
  600.                 0,0,0 };                                // Not used to select mode
  601.  
  602.                                                                                                 // Choose a pixel format that best matches that described in pfd
  603.         nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  604.  
  605.         // Set the pixel format for the device context
  606.         SetPixelFormat(hDC, nPixelFormat, &pfd);
  607. }
  608.  
  609.  
  610.  
  611. // If necessary, creates a 3-3-2 palette for the device context listed.
  612. HPALETTE GetOpenGLPalette(HDC hDC)
  613. {
  614.         HPALETTE hRetPal = NULL;        // Handle to palette to be created
  615.         PIXELFORMATDESCRIPTOR pfd;      // Pixel Format Descriptor
  616.         LOGPALETTE *pPal;                       // Pointer to memory for logical palette
  617.         int nPixelFormat;                       // Pixel format index
  618.         int nColors;                            // Number of entries in palette
  619.         int i;                                          // Counting variable
  620.         BYTE RedRange, GreenRange, BlueRange;
  621.         // Range for each color entry (7,7,and 3)
  622.  
  623.  
  624.         // Get the pixel format index and retrieve the pixel format description
  625.         nPixelFormat = GetPixelFormat(hDC);
  626.         DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  627.  
  628.         // Does this pixel format require a palette?  If not, do not create a
  629.         // palette and just return NULL
  630.         if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  631.                 return NULL;
  632.  
  633.         // Number of entries in palette.  8 bits yeilds 256 entries
  634.         nColors = 1 << pfd.cColorBits;
  635.  
  636.         // Allocate space for a logical palette structure plus all the palette entries
  637.         pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  638.  
  639.         // Fill in palette header
  640.         pPal->palVersion = 0x300;               // Windows 3.0
  641.         pPal->palNumEntries = nColors; // table size
  642.  
  643.                                                                    // Build mask of all 1's.  This creates a number represented by having
  644.                                                                    // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  645.                                                                    // pfd.cBlueBits.  
  646.         RedRange = (1 << pfd.cRedBits) - 1;
  647.         GreenRange = (1 << pfd.cGreenBits) - 1;
  648.         BlueRange = (1 << pfd.cBlueBits) - 1;
  649.  
  650.         // Loop through all the palette entries
  651.         for (i = 0; i < nColors; i++)
  652.         {
  653.                 // Fill in the 8-bit equivalents for each component
  654.                 pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  655.                 pPal->palPalEntry[i].peRed = (unsigned char)(
  656.                         (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  657.  
  658.                 pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  659.                 pPal->palPalEntry[i].peGreen = (unsigned char)(
  660.                         (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  661.  
  662.                 pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  663.                 pPal->palPalEntry[i].peBlue = (unsigned char)(
  664.                         (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  665.  
  666.                 pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  667.         }
  668.  
  669.  
  670.         // Create the palette
  671.         hRetPal = CreatePalette(pPal);
  672.  
  673.         // Go ahead and select and realize the palette for this device context
  674.         SelectPalette(hDC, hRetPal, FALSE);
  675.         RealizePalette(hDC);
  676.  
  677.         // Free the memory used for the logical palette structure
  678.         free(pPal);
  679.  
  680.         // Return the handle to the new palette
  681.         return hRetPal;
  682. }
  683.  
  684.  
  685. // Entry point of all Windows programs
  686. int APIENTRY WinMain(HINSTANCE       hInst,
  687.         HINSTANCE       hPrevInstance,
  688.         LPSTR           lpCmdLine,
  689.         int                     nCmdShow)
  690. {
  691.         MSG                     msg;            // Windows message structure
  692.         WNDCLASS        wc;                     // Windows class structure
  693.         HWND            hWnd;           // Storeage for window handle
  694.  
  695.         hInstance = hInst;
  696.  
  697.         // Register Window style
  698.         wc.style = CS_HREDRAW | CS_VREDRAW;
  699.         wc.lpfnWndProc = (WNDPROC)WndProc;
  700.         wc.cbClsExtra = 0;
  701.         wc.cbWndExtra = 0;
  702.         wc.hInstance = hInstance;
  703.         wc.hIcon = NULL;
  704.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  705.  
  706.         // No need for background brush for OpenGL window
  707.         wc.hbrBackground = NULL;
  708.  
  709.         wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  710.         wc.lpszClassName = lpszAppName;
  711.  
  712.         // Register the window class
  713.         if (RegisterClass(&wc) == 0)
  714.                 return FALSE;
  715.  
  716.  
  717.         // Create the main application window
  718.         hWnd = CreateWindow(
  719.                 lpszAppName,
  720.                 lpszAppName,
  721.  
  722.                 // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  723.                 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  724.  
  725.                 // Window position and size
  726.                 50, 50,
  727.                 400, 400,
  728.                 NULL,
  729.                 NULL,
  730.                 hInstance,
  731.                 NULL);
  732.  
  733.         // If window was not created, quit
  734.         if (hWnd == NULL)
  735.                 return FALSE;
  736.  
  737.  
  738.         // Display the window
  739.         ShowWindow(hWnd, SW_SHOW);
  740.         UpdateWindow(hWnd);
  741.  
  742.         // Process application messages until the application closes
  743.         while (GetMessage(&msg, NULL, 0, 0))
  744.         {
  745.                 TranslateMessage(&msg);
  746.                 DispatchMessage(&msg);
  747.         }
  748.  
  749.         return msg.wParam;
  750. }
  751.  
  752.  
  753.  
  754.  
  755. // Window procedure, handles all messages for this program
  756. LRESULT CALLBACK WndProc(HWND    hWnd,
  757.         UINT    message,
  758.         WPARAM  wParam,
  759.         LPARAM  lParam)
  760. {
  761.         static HGLRC hRC;               // Permenant Rendering context
  762.         static HDC hDC;                 // Private GDI Device context
  763.  
  764.         switch (message)
  765.         {
  766.                 // Window creation, setup for OpenGL
  767.         case WM_CREATE:
  768.                 // Store the device context
  769.                 hDC = GetDC(hWnd);
  770.  
  771.                 // Select the pixel format
  772.                 SetDCPixelFormat(hDC);
  773.  
  774.                 // Create palette if needed
  775.                 hPalette = GetOpenGLPalette(hDC);
  776.  
  777.                 // Create the rendering context and make it current
  778.                 hRC = wglCreateContext(hDC);
  779.                 wglMakeCurrent(hDC, hRC);
  780.                 SetupRC();
  781.                 glGenTextures(2, &texture[0]);                  // tworzy obiekt tekstury                      
  782.  
  783.                                                                                                                 // ładuje pierwszy obraz tekstury:
  784.                 bitmapData = LoadBitmapFile("Bitmapy\\checker.bmp", &bitmapInfoHeader);
  785.  
  786.                 glBindTexture(GL_TEXTURE_2D, texture[0]);       // aktywuje obiekt tekstury
  787.  
  788.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  789.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  790.  
  791.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  792.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  793.  
  794.                 // tworzy obraz tekstury
  795.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  796.                         bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  797.  
  798.                 if (bitmapData)
  799.                         free(bitmapData);
  800.  
  801.                 // ładuje drugi obraz tekstury:
  802.                 bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  803.                 glBindTexture(GL_TEXTURE_2D, texture[1]);       // aktywuje obiekt tekstury
  804.  
  805.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  806.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  807.  
  808.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  809.                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  810.  
  811.                 // tworzy obraz tekstury
  812.                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  813.                         bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  814.  
  815.                 if (bitmapData)
  816.                         free(bitmapData);
  817.  
  818.                 // ustalenie sposobu mieszania tekstury z tłem
  819.                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  820.                 break;
  821.  
  822.                 // Window is being destroyed, cleanup
  823.         case WM_DESTROY:
  824.                 // Deselect the current rendering context and delete it
  825.                 wglMakeCurrent(hDC, NULL);
  826.                 wglDeleteContext(hRC);
  827.  
  828.                 // Delete the palette if it was created
  829.                 if (hPalette != NULL)
  830.                         DeleteObject(hPalette);
  831.  
  832.                 // Tell the application to terminate after the window
  833.                 // is gone.
  834.                 PostQuitMessage(0);
  835.                 break;
  836.  
  837.                 // Window is resized.
  838.         case WM_SIZE:
  839.                 // Call our function which modifies the clipping
  840.                 // volume and viewport
  841.                 ChangeSize(LOWORD(lParam), HIWORD(lParam));
  842.                 break;
  843.  
  844.  
  845.                 // The painting function.  This message sent by Windows
  846.                 // whenever the screen needs updating.
  847.         case WM_PAINT:
  848.         {
  849.                 // Call OpenGL drawing code
  850.                 RenderScene();
  851.  
  852.                 SwapBuffers(hDC);
  853.  
  854.                 // Validate the newly painted client area
  855.                 ValidateRect(hWnd, NULL);
  856.         }
  857.         break;
  858.  
  859.         // Windows is telling the application that it may modify
  860.         // the system palette.  This message in essance asks the
  861.         // application for a new palette.
  862.         case WM_QUERYNEWPALETTE:
  863.                 // If the palette was created.
  864.                 if (hPalette)
  865.                 {
  866.                         int nRet;
  867.  
  868.                         // Selects the palette into the current device context
  869.                         SelectPalette(hDC, hPalette, FALSE);
  870.  
  871.                         // Map entries from the currently selected palette to
  872.                         // the system palette.  The return value is the number
  873.                         // of palette entries modified.
  874.                         nRet = RealizePalette(hDC);
  875.  
  876.                         // Repaint, forces remap of palette in current window
  877.                         InvalidateRect(hWnd, NULL, FALSE);
  878.  
  879.                         return nRet;
  880.                 }
  881.                 break;
  882.  
  883.  
  884.                 // This window may set the palette, even though it is not the
  885.                 // currently active window.
  886.         case WM_PALETTECHANGED:
  887.                 // Don't do anything if the palette does not exist, or if
  888.                 // this is the window that changed the palette.
  889.                 if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  890.                 {
  891.                         // Select the palette into the device context
  892.                         SelectPalette(hDC, hPalette, FALSE);
  893.  
  894.                         // Map entries to system palette
  895.                         RealizePalette(hDC);
  896.  
  897.                         // Remap the current colors to the newly realized palette
  898.                         UpdateColors(hDC);
  899.                         return 0;
  900.                 }
  901.                 break;
  902.  
  903.                 // Key press, check for arrow keys to do cube rotation.
  904.         case WM_KEYDOWN:
  905.         {
  906.                 if (wParam == VK_UP)
  907.                         xRot -= 5.0f;
  908.  
  909.                 if (wParam == VK_DOWN)
  910.                         xRot += 5.0f;
  911.  
  912.                 if (wParam == VK_LEFT)
  913.                         yRot -= 5.0f;
  914.  
  915.                 if (wParam == VK_RIGHT)
  916.                         yRot += 5.0f;
  917.  
  918.                 xRot = (const int)xRot % 360;
  919.                 yRot = (const int)yRot % 360;
  920.  
  921.                 InvalidateRect(hWnd, NULL, FALSE);
  922.         }
  923.         break;
  924.  
  925.         // A menu command
  926.         case WM_COMMAND:
  927.         {
  928.                 switch (LOWORD(wParam))
  929.                 {
  930.                         // Exit the program
  931.                 case ID_FILE_EXIT:
  932.                         DestroyWindow(hWnd);
  933.                         break;
  934.                 }
  935.         }
  936.         break;
  937.  
  938.  
  939.         default:   // Passes it on if unproccessed
  940.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  941.  
  942.         }
  943.  
  944.         return (0L);
  945. }
  946.