Advertisement
bartek27210

grafikaq labki

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