Advertisement
elektryk798

grafika_chyba_c++

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