bartek27210

grafika osw

May 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.32 KB | None | 0 0
  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. double rot1, rot2, rot3;
  37. int licznik, sw = 0;
  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.     float a, b, c, d;
  170.     // Do not calculate inside of jet
  171.     GLfloat  ambientLight[] = { 0.3f,0.3f, 0.3f, 1.0f };
  172.     GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  173.     GLfloat  specular[] = { a, b, c,d };
  174.     GLfloat   lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
  175.     GLfloat  specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  176.     glEnable(GL_DEPTH_TEST);
  177.     // usuwanie niewidocznych powierzchni
  178.     glFrontFace(GL_CCW);
  179.     // ustalenie ”zewnętrznych” ścian
  180.     glEnable(GL_CULL_FACE); // wyłączenie obliczania wewnętrznych ścian
  181.                             // Uaktywnienie oświetlenia
  182.     glEnable(GL_LIGHTING);
  183.     switch (sw)
  184.     {
  185.     case 0:
  186.         a = 0.0f;
  187.         b = 150.0f;
  188.         c = 150.0f;
  189.         d = 1.0f;
  190.         break;
  191.     case 1:
  192.         a = 1100.0f;
  193.         b = 1000.0f;
  194.         c = 15000.0f;
  195.         d = 100.0f;
  196.         break;
  197.     case 2:
  198.         a = 50.0f;
  199.         b = 0.0f;
  200.         c = 0.0f;
  201.         d = 150.0f;
  202.         break;
  203.     case 3:
  204.         a = 20.0f;
  205.         b = 100.0f;
  206.         c = 50.0f;
  207.         d = 10.0f;
  208.         break;
  209.     case 4:
  210.         a = 150.0f;
  211.         b = 150.0f;
  212.         c = 150.0f;
  213.         d = 150.0f;
  214.         break;
  215.     }
  216.     // Ustawienie parametrów źródła światła 0
  217.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
  218.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  219.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  220.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  221.     glEnable(GL_LIGHT0);
  222.     // Uaktywnienie śledzenia kolorów
  223.         glEnable(GL_COLOR_MATERIAL);
  224.     // Materiały mają śledzić kolory ustalone poleceniem glColor
  225.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  226.     // Od tej chwili wszystkie materiały będą miały pełną odblaskowość
  227.     // z dużą jasnością
  228.     glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
  229.     glMateriali(GL_FRONT, GL_SHININESS, 128);
  230.     // Normalne zostaną automatycznie normalizowane
  231.     glEnable(GL_NORMALIZE);
  232.                                
  233.  
  234.                                 // White background
  235.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  236.     // Black brush
  237.     glColor3f(0.0, 0.0, 0.0);
  238. }
  239.  
  240. void skrzynka(void)
  241. {
  242.     glColor3d(0.4, 0.8, 0.1);
  243.    
  244.  
  245.     //glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
  246.  
  247.     //glBindTexture(GL_TEXTURE_2D, texture[0]);
  248.     glBegin(GL_QUADS);
  249.     glNormal3d(0, 0, 1);
  250.     glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  251.     glTexCoord2d(0.0, 1.0); glVertex3d(-25, 25, 25);
  252.     glTexCoord2d(0.0, 0.0); glVertex3d(-25, -25, 25);
  253.     glTexCoord2d(1.0, 0.0); glVertex3d(25, -25, 25);
  254.     glEnd();
  255.     //glBindTexture(GL_TEXTURE_2D, texture[1]);
  256.     glBegin(GL_QUADS);
  257.     glNormal3d(1, 0, 0);
  258.     glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  259.     glTexCoord2d(0.0, 1.0); glVertex3d(25, -25, 25);
  260.     glTexCoord2d(0.0, 0.0); glVertex3d(25, -25, -25);
  261.     glTexCoord2d(1.0, 0.0); glVertex3d(25, 25, -25);
  262.     glEnd();
  263.  
  264.     //glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
  265.  
  266.  
  267.     glBegin(GL_QUADS);
  268.  
  269.     glNormal3d(0, 0, -1);
  270.     glVertex3d(25, 25, -25);
  271.     glVertex3d(25, -25, -25);
  272.     glVertex3d(-25, -25, -25);
  273.     glVertex3d(-25, 25, -25);
  274.  
  275.     glNormal3d(-1, 0, 0);
  276.     glVertex3d(-25, 25, -25);
  277.     glVertex3d(-25, -25, -25);
  278.     glVertex3d(-25, -25, 25);
  279.     glVertex3d(-25, 25, 25);
  280.  
  281.     glNormal3d(0, 0, 1);
  282.     glVertex3d(25, 25, 25);
  283.     glVertex3d(25, 25, -25);
  284.     glVertex3d(-25, 25, -25);
  285.     glVertex3d(-25, 25, 25);
  286.  
  287.     glNormal3d(1, 0, 0);
  288.     glVertex3d(25, -25, 25);
  289.     glVertex3d(-25, -25, 25);
  290.     glVertex3d(-25, -25, -25);
  291.     glVertex3d(25, -25, -25);
  292.     glEnd();
  293. }
  294.  
  295. void walec01(void)
  296. {
  297.     GLUquadricObj*obj;
  298.     obj = gluNewQuadric();
  299.     gluQuadricNormals(obj, GLU_SMOOTH);
  300.     glColor3d(1, 0, 0);
  301.     glPushMatrix();
  302.     gluCylinder(obj, 20, 20, 30, 15, 7);
  303.     gluCylinder(obj, 0, 20, 0, 15, 7);
  304.     glTranslated(0, 0, 60);
  305.     glRotated(180.0, 0, 1, 0);
  306.     gluCylinder(obj, 0, 20, 30, 15, 7);
  307.     glPopMatrix();
  308. }
  309.  
  310. void kula(void)
  311. {
  312.     GLUquadricObj*obj;
  313.     obj = gluNewQuadric();
  314.     gluQuadricTexture(obj, GL_TRUE);
  315.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  316.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  317.     glColor3d(1.0, 0.8, 0.8);
  318.     glEnable(GL_TEXTURE_2D);
  319.     gluSphere(obj, 40, 15, 7);
  320.     glDisable(GL_TEXTURE_2D);
  321. }
  322.  
  323. void walec(double h, double r)
  324. {
  325.     double angle, x, y;
  326.     glColor3d(0.9, 0.6, 0.2);
  327.     glBegin(GL_TRIANGLE_FAN);
  328.     glNormal3d(0.0, 0.0,-1.0);
  329.     glVertex3d(0.0f, 0.0f, 0.0f);
  330.     for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  331.     {
  332.         x = r*sin(angle);
  333.         y = r*cos(angle);
  334.         glVertex3d(x, y, 0.0);
  335.     }
  336.     glEnd();
  337.  
  338.     glBegin(GL_TRIANGLE_FAN);
  339.     glNormal3d(0.0, 0.0, 1.0);
  340.     glVertex3d(0.0f, 0.0f, h);
  341.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  342.     {
  343.         x = r*sin(angle);
  344.         y = r*cos(angle);
  345.         glVertex3d(x, y, h);
  346.     }
  347.     glEnd();
  348.  
  349.     glBegin(GL_QUAD_STRIP);
  350.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  351.     {
  352.         x = r*sin(angle);
  353.         y = r*cos(angle);
  354.         glNormal3d(sin(angle), cos(angle), 0.0);
  355.         glVertex3d(x, y, h);
  356.         glVertex3d(x, y, 0);
  357.     }
  358.     glEnd();
  359. }
  360.  
  361. void ramie(double r1, double r2, double h, double d)
  362. {
  363.     double angle, x, y;
  364.     glBegin(GL_TRIANGLE_FAN);
  365.     glNormal3d(0.0, 0.0, -1.0);
  366.     glVertex3d(0.0f, 0.0f, 0.0f);
  367.     for (angle = 0.0f; angle <= (2.0f*GL_PI) / 2; angle += (GL_PI / 8.0f))
  368.     {
  369.         x = r1*sin(angle);
  370.         y = r1*cos(angle);
  371.         glVertex3d(-x, -y, 0.0);
  372.     }
  373.     glEnd();
  374.  
  375.     glBegin(GL_TRIANGLE_FAN);
  376.     glNormal3d(0.0, 0.0, 1.0);
  377.     glVertex3d(0.0f, 0.0f, h);
  378.     for (angle = 0.0f; angle >= -(2.0f*GL_PI) / 2; angle -= (GL_PI / 8.0f))
  379.     {
  380.         x = r1*sin(angle);
  381.         y = r1*cos(angle);
  382.         glVertex3d(x, y, h);
  383.     }
  384.     glEnd();
  385.  
  386.     glBegin(GL_QUAD_STRIP);
  387.     for (angle = 0.0f; angle >= -(2.0f*GL_PI) / 2; angle -= (GL_PI / 8.0f))
  388.     {
  389.         x = r1*sin(angle);
  390.         y = r1*cos(angle);
  391.         glNormal3d(sin(angle), cos(angle), 0.0);
  392.         glVertex3d(x, y, h);
  393.         glVertex3d(x, y, 0);
  394.     }
  395.     glEnd();
  396.  
  397.     //drugi
  398.    
  399.     glBegin(GL_TRIANGLE_FAN);
  400.     glNormal3d(0.0, 0.0, -1.0);
  401.     glVertex3d(0.0f + d, 0.0f, 0.0f);
  402.     for (angle = 0.0f; angle <= (2.0f*GL_PI) / 2; angle += (GL_PI / 8.0f))
  403.     {
  404.         x = r2*sin(angle);
  405.         y = r2*cos(angle);
  406.         glVertex3d(x + d, y, 0.0);
  407.     }
  408.     glEnd();
  409.  
  410.     glBegin(GL_TRIANGLE_FAN);
  411.     glNormal3d(0.0, 0.0, -1.0);
  412.     glVertex3d(0.0f + d, 0.0f, h);
  413.     for (angle = 0.0f; angle >= -(2.0f*GL_PI) / 2; angle -= (GL_PI / 8.0f))
  414.     {
  415.         x = r2*sin(angle);
  416.         y = r2*cos(angle);
  417.         glVertex3d(-x + d, -y, h);
  418.     }
  419.     glEnd();
  420.     glBegin(GL_QUAD_STRIP);
  421.     for (angle = 0.0f; angle >= -(2.0f*GL_PI) / 2; angle -= (GL_PI / 8.0f))
  422.     {
  423.         x = r2*sin(angle);
  424.         y = r2*cos(angle);
  425.         glNormal3d(sin(angle), cos(angle), 0.0);
  426.         glVertex3d(-x + d, -y, h);
  427.         glVertex3d(-x + d, -y, 0);
  428.     }
  429.     glEnd();
  430.  
  431.     glBegin(GL_QUADS);
  432.     glNormal3d(1, 0, 0);
  433.     glVertex3d(0.0, r1, 0.0);
  434.     glVertex3d(d, r2, 0.0);
  435.     glVertex3d(d, -r2, 0.0);
  436.     glVertex3d(0.0, -r1, 0.0);
  437.     glEnd();
  438.  
  439.     glBegin(GL_QUADS);
  440.     glNormal3d(1, 0, 0);
  441.     glVertex3d(0.0, -r1, h);
  442.     glVertex3d(d, -r2, h);
  443.     glVertex3d(d, r2, h);
  444.     glVertex3d(0.0, r1, h);
  445.     glEnd();
  446.  
  447.     glBegin(GL_QUADS);
  448.     //glNormal3d(0, 0, -1);
  449.     glVertex3d(0.0, r1, 0.0);
  450.     glVertex3d(0.0, r1, h);
  451.     glVertex3d(d, r2, h);
  452.     glVertex3d(d, r2, 0.0);
  453.     glEnd();
  454.  
  455.     glBegin(GL_QUADS);
  456.     //glNormal3d(0, 0, -1);
  457.     glVertex3d(d, -r2, 0.0);
  458.     glVertex3d(d, -r2, h);
  459.     glVertex3d(0.0, -r1, h);
  460.     glVertex3d(0.0, -r1, 0.0);
  461.     glEnd();
  462.  
  463. }
  464.  
  465. void robot(double d1, double d2, double d3)
  466. {
  467.     glPushMatrix();
  468.     glRotated(-90, 1, 0, 0);
  469.     glTranslated(0, 0, -50);
  470.     walec(5, 30);
  471.     glTranslated(0, 0, 5);
  472.     walec(40, 10);
  473.     glTranslated(0, 0, 40);
  474.     glRotated(d1, 0, 0, 1);
  475.     walec(40, 10);
  476.     glTranslated(0, 0, 40);
  477.     glRotated(90, 0, 1, 0);
  478.     glTranslated(0, 0, -20);
  479.     walec(40, 10);
  480.     glTranslated(0, 0, +40);
  481.     glRotated(90 + d2, 0, 0, 1);
  482.     ramie(15, 10, 5, 30);
  483.     glTranslated(30, 0, -5);
  484.     glRotated(d3, 0, 0, 1);
  485.     ramie(15, 10, 5, 30);
  486.     glPopMatrix();
  487. }
  488. void robot1(double d1, double d2, double d3)
  489. {
  490.     //glPushMatrix();
  491.     glRotated(-180, 2, 0, 0);
  492.     glTranslated(0, 0, -50);
  493.    
  494.     glTranslated(-25, -30, 50);
  495.     glRotated(90, 1, 10, 1);
  496.     ramie(15, 10, 5, 30);
  497.     glTranslated(30, 0, -5);
  498.     glRotated(d3, 0, 0, 1);
  499.     ramie(15, 10, 5, 30);
  500.     //glPopMatrix();
  501. }
  502. // LoadBitmapFile
  503. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
  504. //       Wypełnia strukturę nagłówka.
  505. //   Nie obsługuje map 8-bitowych.
  506. unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  507. {
  508.     FILE *filePtr;                          // wskaźnik pozycji pliku
  509.     BITMAPFILEHEADER    bitmapFileHeader;       // nagłówek pliku
  510.     unsigned char       *bitmapImage;           // dane obrazu
  511.     int                 imageIdx = 0;       // licznik pikseli
  512.     unsigned char       tempRGB;                // zmienna zamiany składowych
  513.  
  514.                                                 // otwiera plik w trybie "read binary"
  515.     filePtr = fopen(filename, "rb");
  516.     if (filePtr == NULL)
  517.         return NULL;
  518.  
  519.     // wczytuje nagłówek pliku
  520.     fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
  521.  
  522.     // sprawdza, czy jest to plik formatu BMP
  523.     if (bitmapFileHeader.bfType != BITMAP_ID)
  524.     {
  525.         fclose(filePtr);
  526.         return NULL;
  527.     }
  528.  
  529.     // wczytuje nagłówek obrazu
  530.     fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
  531.  
  532.     // ustawia wskaźnik pozycji pliku na początku danych obrazu
  533.     fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
  534.  
  535.     // przydziela pamięć buforowi obrazu
  536.     bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
  537.  
  538.     // sprawdza, czy udało się przydzielić pamięć
  539.     if (!bitmapImage)
  540.     {
  541.         free(bitmapImage);
  542.         fclose(filePtr);
  543.         return NULL;
  544.     }
  545.    
  546.     // wczytuje dane obrazu
  547.     fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
  548.  
  549.     // sprawdza, czy dane zostały wczytane
  550.     if (bitmapImage == NULL)
  551.     {
  552.         fclose(filePtr);
  553.         return NULL;
  554.     }
  555.  
  556.     // zamienia miejscami składowe R i B
  557.     for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
  558.     {
  559.         tempRGB = bitmapImage[imageIdx];
  560.         bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
  561.         bitmapImage[imageIdx + 2] = tempRGB;
  562.     }
  563.  
  564.     // zamyka plik i zwraca wskaźnik bufora zawierającego wczytany obraz
  565.     fclose(filePtr);
  566.     return bitmapImage;
  567. }
  568.  
  569.  
  570. // Called to draw scene
  571. void RenderScene(void)
  572. {
  573.     //float normal[3];  // Storeage for calculated surface normal
  574.  
  575.     // Clear the window with current clearing color
  576.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  577.  
  578.     // Save the matrix state and do the rotations
  579.     glPushMatrix();
  580.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  581.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  582.  
  583.     /////////////////////////////////////////////////////////////////
  584.     // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN:           //
  585.     /////////////////////////////////////////////////////////////////
  586.  
  587.     //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
  588.     glPolygonMode(GL_BACK, GL_LINE);
  589.     //robot(rot1, rot2, rot3);
  590.     //Uzyskanie siatki:
  591.     //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  592.  
  593.     //Wyrysowanie prostokata:
  594.     //skrzynka();
  595.     //glRectd(-10.0, -10.0, 20.0, 20.0);
  596.     robot(rot1, rot2, rot3);
  597.     //robot1(rot1, rot2, rot3);
  598.     //ramie(30, 15, 40, 50);
  599.     //walec(20, 50);
  600.     /////////////////////////////////////////////////////////////////
  601.     /////////////////////////////////////////////////////////////////
  602.     /////////////////////////////////////////////////////////////////
  603.     glPopMatrix();
  604.     glMatrixMode(GL_MODELVIEW);
  605.  
  606.     // Flush drawing commands
  607.     glFlush();
  608. }
  609.  
  610.  
  611. // Select the pixel format for a given device context
  612. void SetDCPixelFormat(HDC hDC)
  613. {
  614.     int nPixelFormat;
  615.  
  616.     static PIXELFORMATDESCRIPTOR pfd = {
  617.         sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  618.         1,                                                              // Version of this structure    
  619.         PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  620.         PFD_SUPPORT_OPENGL |                    // Support OpenGL calls in window
  621.         PFD_DOUBLEBUFFER,                       // Double buffered
  622.         PFD_TYPE_RGBA,                          // RGBA Color mode
  623.         24,                                     // Want 24bit color
  624.         0,0,0,0,0,0,                            // Not used to select mode
  625.         0,0,                                    // Not used to select mode
  626.         0,0,0,0,0,                              // Not used to select mode
  627.         32,                                     // Size of depth buffer
  628.         0,                                      // Not used to select mode
  629.         0,                                      // Not used to select mode
  630.         PFD_MAIN_PLANE,                         // Draw in main plane
  631.         0,                                      // Not used to select mode
  632.         0,0,0 };                                // Not used to select mode
  633.  
  634.                                                 // Choose a pixel format that best matches that described in pfd
  635.     nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  636.  
  637.     // Set the pixel format for the device context
  638.     SetPixelFormat(hDC, nPixelFormat, &pfd);
  639. }
  640.  
  641.  
  642.  
  643. // If necessary, creates a 3-3-2 palette for the device context listed.
  644. HPALETTE GetOpenGLPalette(HDC hDC)
  645. {
  646.     HPALETTE hRetPal = NULL;    // Handle to palette to be created
  647.     PIXELFORMATDESCRIPTOR pfd;  // Pixel Format Descriptor
  648.     LOGPALETTE *pPal;           // Pointer to memory for logical palette
  649.     int nPixelFormat;           // Pixel format index
  650.     int nColors;                // Number of entries in palette
  651.     int i;                      // Counting variable
  652.     BYTE RedRange, GreenRange, BlueRange;
  653.     // Range for each color entry (7,7,and 3)
  654.  
  655.  
  656.     // Get the pixel format index and retrieve the pixel format description
  657.     nPixelFormat = GetPixelFormat(hDC);
  658.     DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  659.  
  660.     // Does this pixel format require a palette?  If not, do not create a
  661.     // palette and just return NULL
  662.     if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  663.         return NULL;
  664.  
  665.     // Number of entries in palette.  8 bits yeilds 256 entries
  666.     nColors = 1 << pfd.cColorBits;
  667.  
  668.     // Allocate space for a logical palette structure plus all the palette entries
  669.     pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  670.  
  671.     // Fill in palette header
  672.     pPal->palVersion = 0x300;       // Windows 3.0
  673.     pPal->palNumEntries = nColors; // table size
  674.  
  675.                                    // Build mask of all 1's.  This creates a number represented by having
  676.                                    // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  677.                                    // pfd.cBlueBits.  
  678.     RedRange = (1 << pfd.cRedBits) - 1;
  679.     GreenRange = (1 << pfd.cGreenBits) - 1;
  680.     BlueRange = (1 << pfd.cBlueBits) - 1;
  681.  
  682.     // Loop through all the palette entries
  683.     for (i = 0; i < nColors; i++)
  684.     {
  685.         // Fill in the 8-bit equivalents for each component
  686.         pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  687.         pPal->palPalEntry[i].peRed = (unsigned char)(
  688.             (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  689.  
  690.         pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  691.         pPal->palPalEntry[i].peGreen = (unsigned char)(
  692.             (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  693.  
  694.         pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  695.         pPal->palPalEntry[i].peBlue = (unsigned char)(
  696.             (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  697.  
  698.         pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  699.     }
  700.  
  701.  
  702.     // Create the palette
  703.     hRetPal = CreatePalette(pPal);
  704.  
  705.     // Go ahead and select and realize the palette for this device context
  706.     SelectPalette(hDC, hRetPal, FALSE);
  707.     RealizePalette(hDC);
  708.  
  709.     // Free the memory used for the logical palette structure
  710.     free(pPal);
  711.  
  712.     // Return the handle to the new palette
  713.     return hRetPal;
  714. }
  715.  
  716.  
  717. // Entry point of all Windows programs
  718. int APIENTRY WinMain(HINSTANCE       hInst,
  719.     HINSTANCE       hPrevInstance,
  720.     LPSTR           lpCmdLine,
  721.     int                     nCmdShow)
  722. {
  723.     MSG                     msg;            // Windows message structure
  724.     WNDCLASS        wc;                     // Windows class structure
  725.     HWND            hWnd;           // Storeage for window handle
  726.  
  727.     hInstance = hInst;
  728.  
  729.     // Register Window style
  730.     wc.style = CS_HREDRAW | CS_VREDRAW;
  731.     wc.lpfnWndProc = (WNDPROC)WndProc;
  732.     wc.cbClsExtra = 0;
  733.     wc.cbWndExtra = 0;
  734.     wc.hInstance = hInstance;
  735.     wc.hIcon = NULL;
  736.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  737.  
  738.     // No need for background brush for OpenGL window
  739.     wc.hbrBackground = NULL;
  740.  
  741.     wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  742.     wc.lpszClassName = lpszAppName;
  743.  
  744.     // Register the window class
  745.     if (RegisterClass(&wc) == 0)
  746.         return FALSE;
  747.  
  748.  
  749.     // Create the main application window
  750.     hWnd = CreateWindow(
  751.         lpszAppName,
  752.         lpszAppName,
  753.  
  754.         // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  755.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  756.  
  757.         // Window position and size
  758.         50, 50,
  759.         400, 400,
  760.         NULL,
  761.         NULL,
  762.         hInstance,
  763.         NULL);
  764.  
  765.     // If window was not created, quit
  766.     if (hWnd == NULL)
  767.         return FALSE;
  768.  
  769.  
  770.     // Display the window
  771.     ShowWindow(hWnd, SW_SHOW);
  772.     UpdateWindow(hWnd);
  773.  
  774.     // Process application messages until the application closes
  775.     while (GetMessage(&msg, NULL, 0, 0))
  776.     {
  777.         TranslateMessage(&msg);
  778.         DispatchMessage(&msg);
  779.     }
  780.  
  781.     return msg.wParam;
  782. }
  783.  
  784.  
  785.  
  786.  
  787. // Window procedure, handles all messages for this program
  788. LRESULT CALLBACK WndProc(HWND    hWnd,
  789.     UINT    message,
  790.     WPARAM  wParam,
  791.     LPARAM  lParam)
  792. {
  793.     static HGLRC hRC;               // Permenant Rendering context
  794.     static HDC hDC;                 // Private GDI Device context
  795.  
  796.     switch (message)
  797.     {
  798.        
  799.         // Window creation, setup for OpenGL
  800.     case WM_CREATE:
  801.         SetTimer(hWnd, 101, 200, NULL);
  802.         // Store the device context
  803.         hDC = GetDC(hWnd);
  804.  
  805.         // Select the pixel format
  806.         SetDCPixelFormat(hDC);
  807.  
  808.         // Create palette if needed
  809.         hPalette = GetOpenGLPalette(hDC);
  810.  
  811.         // Create the rendering context and make it current
  812.         hRC = wglCreateContext(hDC);
  813.         wglMakeCurrent(hDC, hRC);
  814.         SetupRC();
  815.         glGenTextures(2, &texture[0]);                  // tworzy obiekt tekstury          
  816.  
  817.                                                         // ładuje pierwszy obraz tekstury:
  818.         bitmapData = LoadBitmapFile("Bitmapy\\checker.bmp", &bitmapInfoHeader);
  819.  
  820.         glBindTexture(GL_TEXTURE_2D, texture[0]);       // aktywuje obiekt tekstury
  821.  
  822.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  823.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  824.  
  825.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  826.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  827.  
  828.         // tworzy obraz tekstury
  829.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  830.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  831.  
  832.         if (bitmapData)
  833.             free(bitmapData);
  834.  
  835.         // ładuje drugi obraz tekstury:
  836.         bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  837.         glBindTexture(GL_TEXTURE_2D, texture[1]);       // aktywuje obiekt tekstury
  838.  
  839.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  840.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  841.  
  842.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  843.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  844.  
  845.         // tworzy obraz tekstury
  846.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  847.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  848.  
  849.         if (bitmapData)
  850.             free(bitmapData);
  851.  
  852.         // ustalenie sposobu mieszania tekstury z tłem
  853.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  854.         break;
  855.  
  856.  
  857.     case WM_TIMER:
  858.         if (wParam == 101)
  859.         {
  860.             licznik++;
  861.             if (licznik < 15)
  862.             {
  863.                 rot1 += 15.0f;
  864.                 rot3 += 10.0f;
  865.  
  866.             }
  867.             if (licznik > 15 && licznik < 30)
  868.             {
  869.                 rot1 -= 15.0f;
  870.                 rot3 -= 10.0f;
  871.             }
  872.             if (licznik>30)
  873.             {
  874.                 licznik = 0;
  875.             }
  876.             InvalidateRect(hWnd, NULL, FALSE);
  877.         }
  878.         break;
  879.         // Window is being destroyed, cleanup
  880.     case WM_DESTROY:
  881.         KillTimer(hWnd, 101);
  882.         // Deselect the current rendering context and delete it
  883.         wglMakeCurrent(hDC, NULL);
  884.         wglDeleteContext(hRC);
  885.  
  886.         // Delete the palette if it was created
  887.         if (hPalette != NULL)
  888.             DeleteObject(hPalette);
  889.  
  890.         // Tell the application to terminate after the window
  891.         // is gone.
  892.         PostQuitMessage(0);
  893.         break;
  894.  
  895.         // Window is resized.
  896.     case WM_SIZE:
  897.         // Call our function which modifies the clipping
  898.         // volume and viewport
  899.         ChangeSize(LOWORD(lParam), HIWORD(lParam));
  900.         break;
  901.  
  902.  
  903.         // The painting function.  This message sent by Windows
  904.         // whenever the screen needs updating.
  905.     case WM_PAINT:
  906.     {
  907.         // Call OpenGL drawing code
  908.         RenderScene();
  909.  
  910.         SwapBuffers(hDC);
  911.  
  912.         // Validate the newly painted client area
  913.         ValidateRect(hWnd, NULL);
  914.     }
  915.     break;
  916.  
  917.     // Windows is telling the application that it may modify
  918.     // the system palette.  This message in essance asks the
  919.     // application for a new palette.
  920.     case WM_QUERYNEWPALETTE:
  921.         // If the palette was created.
  922.         if (hPalette)
  923.         {
  924.             int nRet;
  925.  
  926.             // Selects the palette into the current device context
  927.             SelectPalette(hDC, hPalette, FALSE);
  928.  
  929.             // Map entries from the currently selected palette to
  930.             // the system palette.  The return value is the number
  931.             // of palette entries modified.
  932.             nRet = RealizePalette(hDC);
  933.  
  934.             // Repaint, forces remap of palette in current window
  935.             InvalidateRect(hWnd, NULL, FALSE);
  936.  
  937.             return nRet;
  938.         }
  939.         break;
  940.  
  941.  
  942.         // This window may set the palette, even though it is not the
  943.         // currently active window.
  944.     case WM_PALETTECHANGED:
  945.         // Don't do anything if the palette does not exist, or if
  946.         // this is the window that changed the palette.
  947.         if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  948.         {
  949.             // Select the palette into the device context
  950.             SelectPalette(hDC, hPalette, FALSE);
  951.  
  952.             // Map entries to system palette
  953.             RealizePalette(hDC);
  954.  
  955.             // Remap the current colors to the newly realized palette
  956.             UpdateColors(hDC);
  957.             return 0;
  958.         }
  959.         break;
  960.  
  961.         // Key press, check for arrow keys to do cube rotation.
  962.     case WM_KEYDOWN:
  963.     {
  964.         if (wParam == VK_UP)
  965.             xRot -= 5.0f;
  966.  
  967.         if (wParam == VK_DOWN)
  968.             xRot += 5.0f;
  969.  
  970.         if (wParam == VK_LEFT)
  971.             yRot -= 5.0f;
  972.  
  973.         if (wParam == VK_RIGHT)
  974.             yRot += 5.0f;
  975.  
  976.         if (wParam == '1')
  977.             rot1 -= 5.0f;
  978.         if (wParam == '2')
  979.             rot1 += 5.0f;
  980.         if (wParam == '3')
  981.             rot2 -= 5.0f;
  982.         if (wParam == '4')
  983.             rot2 += 5.0f;
  984.         if (wParam == '5')
  985.             rot3 -= 5.0f;
  986.         if (wParam == '6')
  987.             rot3 += 5.0f;
  988.         if (wParam == '7')
  989.             sw += 1;
  990.         if (wParam == '8')
  991.             sw -= 1;
  992.         if (wParam == '9')
  993.             sw = 0;
  994.        
  995.         xRot = (const int)xRot % 360;
  996.         yRot = (const int)yRot % 360;
  997.  
  998.         InvalidateRect(hWnd, NULL, FALSE);
  999.     }
  1000.     break;
  1001.  
  1002.     // A menu command
  1003.     case WM_COMMAND:
  1004.     {
  1005.         switch (LOWORD(wParam))
  1006.         {
  1007.             // Exit the program
  1008.         case ID_FILE_EXIT:
  1009.             DestroyWindow(hWnd);
  1010.             break;
  1011.         }
  1012.     }
  1013.     break;
  1014.  
  1015.  
  1016.     default:   // Passes it on if unproccessed
  1017.         return (DefWindowProc(hWnd, message, wParam, lParam));
  1018.  
  1019.     }
  1020.  
  1021.     return (0L);
  1022. }
Advertisement
Add Comment
Please, Sign In to add comment