Advertisement
pabloducato

10_05_2018

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