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