Advertisement
sildren12

Untitled

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