bartek27210

grafika_robocik

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