Advertisement
pabloducato

FINAL_PROJECT_OPENGL_WITH_TEXTURES

May 8th, 2018
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Gl_template.c
  2. //Wyłšczanie błędów przed "fopen"
  3. #define  _CRT_SECURE_NO_WARNINGS
  4.  
  5. // Ładowanie bibliotek:
  6. #ifdef _MSC_VER                         // Check if MS Visual C compiler
  7. #  pragma comment(lib, "opengl32.lib")  // Compiler-specific directive to avoid manually configuration
  8. #  pragma comment(lib, "glu32.lib")     // Link libraries
  9. #endif
  10.  
  11. // Ustalanie trybu tekstowego:
  12. #ifdef _MSC_VER        // Check if MS Visual C compiler
  13. #   ifndef _MBCS
  14. #      define _MBCS    // Uses Multi-byte character set
  15. #   endif
  16. #   ifdef _UNICODE     // Not using Unicode character set
  17. #      undef _UNICODE
  18. #   endif
  19. #   ifdef UNICODE
  20. #      undef UNICODE
  21. #   endif
  22. #endif
  23.  
  24.  
  25. #include <windows.h>            // Window defines
  26. #include <gl\gl.h>              // OpenGL
  27. #include <gl\glu.h>             // GLU library
  28. #include <math.h>               // Define for sqrt
  29. #include <stdio.h>
  30. #include "resource.h"           // About box resource identifiers.
  31. #include <GL/glut.h>
  32. #include <stdlib.h>
  33.  
  34. #define glRGB(x, y, z)  glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z)
  35. #define BITMAP_ID 0x4D42        // identyfikator formatu BMP
  36. #define GL_PI 3.14
  37.  
  38. // stałe do obsługi menu podręcznego
  39.  
  40. enum
  41. {
  42.     FULL_WINDOW, // aspekt obrazu - całe okno
  43.     ASPECT_1_1, // aspekt obrazu 1:1
  44.     EXIT // wyjście
  45. };
  46.  
  47. // aspekt obrazu
  48.  
  49. int Aspect = FULL_WINDOW;
  50.  
  51. // wpółrzędne położenia obserwatora
  52.  
  53. GLdouble eyex = 0;
  54. GLdouble eyey = 0;
  55. GLdouble eyez = 3;
  56.  
  57. // współrzędne punktu w którego kierunku jest zwrócony obserwator,
  58.  
  59. GLdouble centerx = 0;
  60. GLdouble centery = 0;
  61. GLdouble centerz = -100;
  62.  
  63. // funkcja generująca scenę 3D
  64. //
  65.  
  66.  
  67.  
  68. // obsługa klawiatury
  69.  
  70.  
  71. // Color Palette handle
  72. HPALETTE hPalette = NULL;
  73.  
  74. // Application name and instance storeage
  75. static LPCTSTR lpszAppName = "GL Template";
  76. static HINSTANCE hInstance;
  77.  
  78. // Rotation amounts
  79. static GLfloat xRot = 0.0f;
  80. static GLfloat yRot = 0.0f;
  81. static GLfloat rot1 = 0.0f;
  82. static GLfloat rot2 = 0.0f;
  83. static GLfloat rot3 = 0.0f;
  84.  
  85. static GLsizei lastHeight;
  86. static GLsizei lastWidth;
  87.  
  88. // Opis tekstury
  89. BITMAPINFOHEADER    bitmapInfoHeader;   // nagłówek obrazu
  90. unsigned char*      bitmapData;         // dane tekstury
  91. unsigned int        texture[1];         // obiekt tekstury
  92.  
  93.  
  94.                                         // Declaration for Window procedure
  95. LRESULT CALLBACK WndProc(HWND    hWnd,
  96.     UINT    message,
  97.     WPARAM  wParam,
  98.     LPARAM  lParam);
  99.  
  100. // Dialog procedure for about box
  101. BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
  102.  
  103. // Set Pixel Format function - forward declaration
  104. void SetDCPixelFormat(HDC hDC);
  105.  
  106.  
  107.  
  108.  
  109. // Reduces a normal vector specified as a set of three coordinates,
  110. // to a unit normal vector of length one.
  111. void ReduceToUnit(float vector[3])
  112. {
  113.     float length;
  114.  
  115.     // Calculate the length of the vector      
  116.     length = (float)sqrt((vector[0] * vector[0]) +
  117.         (vector[1] * vector[1]) +
  118.         (vector[2] * vector[2]));
  119.  
  120.     // Keep the program from blowing up by providing an exceptable
  121.     // value for vectors that may calculated too close to zero.
  122.     if (length == 0.0f)
  123.         length = 1.0f;
  124.  
  125.     // Dividing each element by the length will result in a
  126.     // unit normal vector.
  127.     vector[0] /= length;
  128.     vector[1] /= length;
  129.     vector[2] /= length;
  130. }
  131.  
  132.  
  133. // Points p1, p2, & p3 specified in counter clock-wise order
  134. void calcNormal(float v[3][3], float out[3])
  135. {
  136.     float v1[3], v2[3];
  137.     static const int x = 0;
  138.     static const int y = 1;
  139.     static const int z = 2;
  140.  
  141.     // Calculate two vectors from the three points
  142.     v1[x] = v[0][x] - v[1][x];
  143.     v1[y] = v[0][y] - v[1][y];
  144.     v1[z] = v[0][z] - v[1][z];
  145.  
  146.     v2[x] = v[1][x] - v[2][x];
  147.     v2[y] = v[1][y] - v[2][y];
  148.     v2[z] = v[1][z] - v[2][z];
  149.  
  150.     // Take the cross product of the two vectors to get
  151.     // the normal vector which will be stored in out
  152.     out[x] = v1[y] * v2[z] - v1[z] * v2[y];
  153.     out[y] = v1[z] * v2[x] - v1[x] * v2[z];
  154.     out[z] = v1[x] * v2[y] - v1[y] * v2[x];
  155.  
  156.     // Normalize the vector (shorten length to one)
  157.     ReduceToUnit(out);
  158. }
  159.  
  160. // Change viewing volume and viewport.  Called when window is resized
  161. void ChangeSize(GLsizei w, GLsizei h)
  162. {
  163.     GLfloat nRange = 400.0f;
  164.     GLfloat fAspect;
  165.     // Prevent a divide by zero
  166.     if (h == 0)
  167.         h = 1;
  168.  
  169.     lastWidth = w;
  170.     lastHeight = h;
  171.  
  172.     fAspect = (GLfloat)w / (GLfloat)h;
  173.     // Set Viewport to window dimensions
  174.     glViewport(0, 0, w, h);
  175.  
  176.     // Reset coordinate system
  177.     glMatrixMode(GL_PROJECTION);
  178.     glLoadIdentity();
  179.  
  180.     // Establish clipping volume (left, right, bottom, top, near, far)
  181.     if (w <= h)
  182.         glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
  183.     else
  184.         glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
  185.  
  186.     // Establish perspective:
  187.     /*
  188.     gluPerspective(60.0f,fAspect,1.0,400);
  189.     */
  190.  
  191.     glMatrixMode(GL_MODELVIEW);
  192.     glLoadIdentity();
  193. }
  194.  
  195.  
  196.  
  197. // This function does any needed initialization on the rendering
  198. // context.  Here it sets up and initializes the lighting for
  199. // the scene.
  200. void SetupRC()
  201. {
  202.     // Light values and coordinates
  203.     GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  204.     GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  205.     GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
  206.     GLfloat  lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
  207.     GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
  208.  
  209.  
  210.     glEnable(GL_DEPTH_TEST);    // Hidden surface removal
  211.     glFrontFace(GL_CCW);        // Counter clock-wise polygons face out
  212.                                 //glEnable(GL_CULL_FACE);       // Do not calculate inside of jet
  213.  
  214.                                 // Enable lighting
  215.     //GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  216.     glEnable(GL_LIGHTING);
  217.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
  218.     GLfloat gray[] = { 0.00f, 0.00f, 0.50f, 0.50f, };
  219.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
  220.     glBegin(GL_TRIANGLES);
  221.     glVertex3f(-15.0f, 0.0f, 30.0f);
  222.     glVertex3f(0.0f, 15.0f, 30.0f);
  223.     glVertex3f(0.0f, 0.0f, -56.0f);
  224.     glEnd();
  225.  
  226.                                 // Setup and enable light 0
  227.                                 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  228.                                 glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  229.                                 glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  230.                                 glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  231.                                 glEnable(GL_LIGHT0);
  232.                                 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f);
  233.                                 //glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 100.0f);
  234.  
  235.                                 // Enable color tracking
  236.         glEnable(GL_COLOR_MATERIAL);
  237.  
  238.                                 // Set Material properties to follow glColor values
  239.         glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  240.  
  241.                                 // All materials hereafter have full specular reflectivity
  242.                                 // with a high shine
  243.                                 //glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
  244.                                 //glMateriali(GL_FRONT,GL_SHININESS,128);
  245.  
  246.  
  247.                                 // White background
  248.     //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  249.     // Black brush
  250.     glColor3f(0.75f, 0.75f, 0.75f);
  251.     glBegin(GL_TRIANGLES);
  252.     glVertex3f(-15.0f, 0.0f, 30.0f);
  253.     glVertex3f(0.0f, 15.0f, 30.0f);
  254.     glVertex3f(0.0f, 0.0f, -56.0f);
  255.     glEnd();
  256. }
  257.  
  258. void skrzynka(void)
  259. {
  260.     glColor3d(0.6, 0.2, 0.1);
  261.  
  262.  
  263.     glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
  264.  
  265.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  266.     glBegin(GL_QUADS);
  267.     glNormal3d(0, 0, 1);
  268.     glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  269.     glTexCoord2d(0.0, 1.0); glVertex3d(-25, 25, 25);
  270.     glTexCoord2d(0.0, 0.0); glVertex3d(-25, -25, 25);
  271.     glTexCoord2d(1.0, 0.0); glVertex3d(25, -25, 25);
  272.     glEnd();
  273.     glBindTexture(GL_TEXTURE_2D, texture[1]);
  274.     glBegin(GL_QUADS);
  275.     glNormal3d(1, 0, 0);
  276.     glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
  277.     glTexCoord2d(0.0, 1.0); glVertex3d(25, -25, 25);
  278.     glTexCoord2d(0.0, 0.0); glVertex3d(25, -25, -25);
  279.     glTexCoord2d(1.0, 0.0); glVertex3d(25, 25, -25);
  280.     glEnd();
  281.  
  282.     glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
  283.  
  284.  
  285.  
  286.     glBegin(GL_QUADS);
  287.     glNormal3d(0, 0, -1);
  288.     glVertex3d(25, 25, -25);
  289.     glVertex3d(25, -25, -25);
  290.     glVertex3d(-25, -25, -25);
  291.     glVertex3d(-25, 25, -25);
  292.  
  293.     glNormal3d(-1, 0, 0);
  294.     glVertex3d(-25, 25, -25);
  295.     glVertex3d(-25, -25, -25);
  296.     glVertex3d(-25, -25, 25);
  297.     glVertex3d(-25, 25, 25);
  298.  
  299.     glNormal3d(0, 1, 0);
  300.     glVertex3d(25, 25, 25);
  301.     glVertex3d(25, 25, -25);
  302.     glVertex3d(-25, 25, -25);
  303.     glVertex3d(-25, 25, 25);
  304.  
  305.     glNormal3d(0, -1, 0);
  306.     glVertex3d(25, -25, 25);
  307.     glVertex3d(-25, -25, 25);
  308.     glVertex3d(-25, -25, -25);
  309.     glVertex3d(25, -25, -25);
  310.     glEnd();
  311. }
  312.  
  313. void walec01(void)
  314. {
  315.     GLUquadricObj*obj;
  316.     obj = gluNewQuadric();
  317.     gluQuadricNormals(obj, GLU_SMOOTH);
  318.     glColor3d(1, 0, 0);
  319.     glPushMatrix();
  320.     gluCylinder(obj, 20, 20, 30, 15, 7);
  321.     gluCylinder(obj, 0, 20, 0, 15, 7);
  322.     glTranslated(0, 0, 60);
  323.     glRotated(180.0, 0, 1, 0);
  324.     gluCylinder(obj, 0, 20, 30, 15, 7);
  325.     glPopMatrix();
  326. }
  327.  
  328. void kula(void)
  329. {
  330.     GLUquadricObj*obj;
  331.     obj = gluNewQuadric();
  332.     gluQuadricTexture(obj, GL_TRUE);
  333.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  334.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  335.     glColor3d(1.0, 0.8, 0.8);
  336.     glEnable(GL_TEXTURE_2D);
  337.     gluSphere(obj, 40, 15, 7);
  338.     glDisable(GL_TEXTURE_2D);
  339. }
  340. void szescian(void)
  341. {
  342.     glBegin(GL_QUADS); //POLYGON
  343.     //1
  344.     glColor3f(0.0, 0.7, 0.0);
  345.     glVertex3d(50, -50, 50);
  346.     glVertex3d(-50, -50, 50);
  347.     glVertex3d(-50, -60, 50);
  348.     glVertex3d(50, -60, 50);
  349.     //2
  350.     glColor3f(0.0, 0.7, 0.0);
  351.     glVertex3d(50, -50, 50);
  352.     glVertex3d(50, -60, 50);
  353.     glVertex3d(50, -60, -50);
  354.     glVertex3d(50, -50, -50);
  355.     //3
  356.     glColor3f(0.0, 0.7, 0.0);
  357.     glVertex3d(50, -50, 50);
  358.     glVertex3d(50, -50, -50);
  359.     glVertex3d(-50, -50, -50);
  360.     glVertex3d(-50, -50, 50);
  361.    
  362.     //4
  363.     glColor3f(0.0, 0.7, 0.0);
  364.     glVertex3d(-50, -50, 50);
  365.     glVertex3d(-50, -50, -50);
  366.     glVertex3d(-50, -60, -50);
  367.     glVertex3d(-50, -60, 50);
  368.     //5
  369.     glColor3f(0.0, 0.7, 0.0);
  370.     glVertex3d(50, -60, 50);
  371.     glVertex3d(-50, -60, 50);
  372.     glVertex3d(-50, -60, -50);
  373.     glVertex3d(50, -60, -50);
  374.     //6
  375.     glColor3f(0.0, 0.7, 0.0);
  376.     glVertex3d(50, -50, -50);
  377.     glVertex3d(50, -60, -50);
  378.     glVertex3d(-50, -60, -50);
  379.     glVertex3d(-50, -50, -50);
  380.     glEnd();
  381. }
  382.  
  383. void walec(double h, double r)
  384. {
  385.     double angle, x, y;
  386.     glBegin(GL_TRIANGLE_FAN);
  387.     glVertex3d(0.0f, 0.0f, 0.0f);
  388.     for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  389.     {
  390.         x = r*sin(angle);
  391.         y = r*cos(angle);
  392.         glVertex3d(x, y, 0.0);
  393.     }
  394.     glEnd();
  395.     glBegin(GL_TRIANGLE_FAN);
  396.     glVertex3d(0.0f, 0.0f, h);
  397.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  398.     {
  399.         x = r*sin(angle);
  400.         y = r*cos(angle);
  401.         glVertex3d(x, y, h);
  402.     }
  403.     glEnd();
  404.     glBegin(GL_QUAD_STRIP);
  405.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  406.     {
  407.         x = r*sin(angle);
  408.         y = r*cos(angle);
  409.         glVertex3d(x, y, h);
  410.         glVertex3d(x, y, 0);
  411.     }
  412.     glEnd();
  413. }
  414. void ramie(double r1, double r2, double h, double d)
  415. {
  416.     double angle, x, y;
  417.     glBegin(GL_TRIANGLE_FAN);
  418.     glColor3d(3, 12, 0);
  419.     glVertex3d(0.0f, 0.0f, 0.0f);
  420.     for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  421.     {
  422.         x = r1*sin(angle);
  423.         y = r1*cos(angle);
  424.         glVertex3d(x, y, 0.0);
  425.     }
  426.     glEnd();
  427.     glBegin(GL_QUAD_STRIP);
  428.     glColor3d(2, 0, 42);
  429.     for (angle = 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, 0);
  434.         glVertex3d(x, y, h);
  435.     }
  436.     glEnd();
  437.     glBegin(GL_TRIANGLE_FAN);
  438.     glColor3d(1, 0, 0);
  439.     glVertex3d(0.0f, 0.0f, h);
  440.     for (angle = 0.0f; angle >= -(GL_PI); angle -= (GL_PI / 8.0f))
  441.     {
  442.         x = r1*sin(angle);
  443.         y = r1*cos(angle);
  444.         glVertex3d(x, y, h);
  445.     }
  446.     glEnd();
  447.  
  448.     // 2 ************************************
  449.     glBegin(GL_QUADS);
  450.     glColor3d(0, 0.5, 1);
  451.     glVertex3d(0, r1, 0);
  452.     glVertex3d(d, r2, 0);
  453.     glVertex3d(d, -r2, 0);
  454.     glVertex3d(0, -r1, 0);
  455.     glEnd();
  456.     glBegin(GL_QUADS);
  457.     glColor3d(0, 0.5, 1);
  458.     glVertex3d(0, -r1, h);
  459.     glVertex3d(d, -r2, h);
  460.     glVertex3d(d, r2, h);
  461.     glVertex3d(0, r1, h);
  462.     glEnd();
  463.  
  464.     glBegin(GL_QUADS);
  465.     glColor3d(0, 0.5, 1);
  466.     glVertex3d(0, r1, h);
  467.     glVertex3d(d, r2, h);
  468.     glVertex3d(d, r2, 0);
  469.     glVertex3d(0, r1, 0);
  470.     glEnd();
  471.  
  472.     glBegin(GL_QUADS);
  473.     glColor3d(0, 0.5, 1);
  474.     glVertex3d(0, -r1, 0);
  475.     glVertex3d(d, -r2, 0);
  476.     glVertex3d(d, -r2, h);
  477.     glVertex3d(0, -r1, h);
  478.     glEnd();
  479.  
  480.     glBegin(GL_TRIANGLE_FAN);
  481.     glColor3d(3, 12, 0);
  482.     glVertex3d(d, 0.0f, 0.0f);
  483.     for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  484.     {
  485.         x = r2*sin(angle);
  486.         y = r2*cos(angle);
  487.         glVertex3d(x + d, y, 0.0);
  488.     }
  489.     glEnd();
  490.     glBegin(GL_QUAD_STRIP);
  491.     glColor3d(2, 0, 42);
  492.     for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  493.     {
  494.         x = r2*sin(angle);
  495.         y = r2*cos(angle);
  496.         glVertex3d(x + d, y, 0);
  497.         glVertex3d(x + d, y, h);
  498.     }
  499.     glEnd();
  500.     glBegin(GL_TRIANGLE_FAN);
  501.     glColor3d(1, 0, 0);
  502.     glVertex3d(d, 0.0f, h);
  503.     for (angle = -GL_PI; angle >= -(2 * GL_PI); angle -= (GL_PI / 8.0f))
  504.     {
  505.         x = r2*sin(angle);
  506.         y = r2*cos(angle);
  507.         glVertex3d(x + d, y, h);
  508.     }
  509.     glEnd();
  510. }
  511.  
  512. void home()
  513. {
  514.     //Roof
  515.     glClear(GL_COLOR_BUFFER_BIT);     // Clear display window
  516.                                       // Set line segment color as glColor3f(R,G,B)
  517.     glColor3f(0.3, 0.5, 0.8);
  518.     glBegin(GL_POLYGON);
  519.     glVertex2i(200, 500);
  520.     glVertex2i(600, 500);
  521.     glVertex2i(700, 350);
  522.     glVertex2i(300, 350);
  523.     glEnd();
  524.     // Top of Front Wall
  525.     glColor3f(0.1, 0.5, 0.0);
  526.     glBegin(GL_TRIANGLES);
  527.     glVertex2i(300, 350);
  528.     glVertex2i(100, 350);
  529.     glVertex2i(200, 500);  
  530.     glEnd();
  531.  
  532.     // Front Wall good no more pain
  533.     glColor3f(0.7, 0.2, 0.3);
  534.     glBegin(GL_POLYGON);
  535.     glVertex2i(100, 350);
  536.     glVertex2i(300, 350);
  537.     glVertex2i(300, 100);
  538.     glVertex2i(100, 100);
  539.     glEnd();
  540.     // Front Door
  541.     glColor3f(0.7, 0.2, 0.9);
  542.     glBegin(GL_POLYGON);
  543.     glVertex2i(150, 250);
  544.     glVertex2i(250, 250);
  545.     glVertex2i(250, 100);
  546.     glVertex2i(150, 100);
  547.     glEnd();
  548.  
  549.     // Front Door Lock good asa good
  550.     glColor3f(0.3, 0.7, 0.9);
  551.     glPointSize(15);
  552.     glBegin(GL_POINTS);
  553.     glVertex2i(170, 170);
  554.     glEnd();
  555.  
  556.     //side Wall good
  557.     glColor3f(0.1, 0.2, 0.3);
  558.     glBegin(GL_POLYGON);
  559.     glVertex2i(300, 350);
  560.     glVertex2i(700, 350);
  561.     glVertex2i(700, 100);
  562.     glVertex2i(300, 100);
  563.     glEnd();
  564.     // window one
  565.     glColor3f(0.2, 0.4, 0.3);
  566.     glBegin(GL_POLYGON);
  567.     glVertex2i(330, 320);
  568.     glVertex2i(450, 320);
  569.     glVertex2i(450, 230);
  570.     glVertex2i(330, 230);
  571.     glEnd();
  572.     // line of window one
  573.     glColor3f(0.1, 0.7, 0.5);
  574.     glLineWidth(5);
  575.     glBegin(GL_LINES);
  576.     glVertex2i(390, 320);
  577.     glVertex2i(390, 230);
  578.     glVertex2i(330, 273);
  579.     glVertex2i(450, 273);
  580.     glEnd();
  581.     // window two
  582.     glColor3f(0.2, 0.4, 0.3);
  583.     glBegin(GL_POLYGON);
  584.     glVertex2i(530, 320);
  585.     glVertex2i(650, 320);
  586.     glVertex2i(650, 230);
  587.     glVertex2i(530, 230);
  588.     glEnd();
  589.     // lines of window two
  590.     glColor3f(0.1, 0.7, 0.5);
  591.     glLineWidth(5);
  592.     glBegin(GL_LINES);
  593.     glVertex2i(590, 320);
  594.     glVertex2i(590, 230);
  595.     glVertex2i(530, 273);
  596.     glVertex2i(650, 273);
  597.     glEnd();
  598.     // Process all OpenGL routine s as quickly as possible
  599.     glFlush();
  600. }
  601.  
  602. void home2()
  603. {
  604.     //Roof
  605.     glClear(GL_COLOR_BUFFER_BIT);     // Clear display window
  606.                                       // Set line segment color as glColor3f(R,G,B)
  607.     glColor3f(0.3, 0.5, 0.8);
  608.     glBegin(GL_POLYGON);
  609.     glVertex2i(200, 500);
  610.     glVertex2i(600, 500);
  611.     glVertex2i(700, 350);
  612.     glVertex2i(300, 350);
  613.     glEnd();
  614.     // Top of Front Wall
  615.     glColor3f(0.1, 0.5, 0.0);
  616.     glBegin(GL_TRIANGLES);
  617.     glVertex2i(300, 350);
  618.     glVertex2i(100, 350);
  619.     glVertex2i(200, 500);
  620.     glEnd();
  621.     // Front Wall good no more pain
  622.     glColor3f(0.7, 0.2, 0.3);
  623.     glBegin(GL_POLYGON);
  624.     glVertex2i(100, 350);
  625.     glVertex2i(300, 350);
  626.     glVertex2i(300, 100);
  627.     glVertex2i(100, 100);
  628.     glEnd();
  629.     // Front Door
  630.     glColor3f(0.7, 0.2, 0.9);
  631.     glBegin(GL_POLYGON);
  632.     glVertex2i(150, 250);
  633.     glVertex2i(250, 250);
  634.     glVertex2i(250, 100);
  635.     glVertex2i(150, 100);
  636.     glEnd();
  637.  
  638.     // Front Door Lock good asa good
  639.     glColor3f(0.3, 0.7, 0.9);
  640.     glPointSize(15);
  641.     glBegin(GL_POINTS);
  642.     glVertex2i(170, 170);
  643.     glEnd();
  644.  
  645.     //side Wall good
  646.     glColor3f(0.1, 0.2, 0.3);
  647.     glBegin(GL_POLYGON);
  648.     glVertex2i(300, 350);
  649.     glVertex2i(700, 350);
  650.     glVertex2i(700, 100);
  651.     glVertex2i(300, 100);
  652.     glEnd();
  653.     // window one
  654.     glColor3f(0.2, 0.4, 0.3);
  655.     glBegin(GL_POLYGON);
  656.     glVertex2i(330, 320);
  657.     glVertex2i(450, 320);
  658.     glVertex2i(450, 230);
  659.     glVertex2i(330, 230);
  660.     glEnd();
  661.     // line of window one
  662.     glColor3f(0.1, 0.7, 0.5);
  663.     glLineWidth(5);
  664.     glBegin(GL_LINES);
  665.     glVertex2i(390, 320);
  666.     glVertex2i(390, 230);
  667.     glVertex2i(330, 273);
  668.     glVertex2i(450, 273);
  669.     glEnd();
  670.     // window two
  671.     glColor3f(0.2, 0.4, 0.3);
  672.     glBegin(GL_POLYGON);
  673.     glVertex2i(530, 320);
  674.     glVertex2i(650, 320);
  675.     glVertex2i(650, 230);
  676.     glVertex2i(530, 230);
  677.     glEnd();
  678.     // lines of window two
  679.     glColor3f(0.1, 0.7, 0.5);
  680.     glLineWidth(5);
  681.     glBegin(GL_LINES);
  682.     glVertex2i(590, 320);
  683.     glVertex2i(590, 230);
  684.     glVertex2i(530, 273);
  685.     glVertex2i(650, 273);
  686.     glEnd();
  687.     // Process all OpenGL routine s as quickly as possible
  688.     glFlush();
  689. }
  690.  
  691. void podloze()
  692. {
  693.     glPushMatrix();
  694.     glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
  695.  
  696.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  697.     glBegin(GL_QUADS);
  698.     glNormal3d(0, 0, 1);
  699.     //1
  700.     glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
  701.     glTexCoord2d(0.0, 1.0); glVertex3d(-152, -60, 152);
  702.     glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, 152);
  703.     glTexCoord2d(1.0, 0.0); glVertex3d(152, -70, 152);
  704.     //2
  705.     glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
  706.     glTexCoord2d(0.0, 1.0); glVertex3d(152, -70, 152);
  707.     glTexCoord2d(0.0, 0.0); glVertex3d(152, -70, -152);
  708.     glTexCoord2d(1.0, 0.0); glVertex3d(152, -60, -152);
  709.     //3
  710.     glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
  711.     glTexCoord2d(0.0, 1.0); glVertex3d(152, -60, -152);
  712.     glTexCoord2d(0.0, 0.0); glVertex3d(-152, -60, -152);
  713.     glTexCoord2d(1.0, 0.0); glVertex3d(-152, -60, 152);
  714.     //4
  715.     glTexCoord2d(1.0, 1.0); glVertex3d(-152, -60, 152);
  716.     glTexCoord2d(0.0, 1.0); glVertex3d(-152, -60, -152);
  717.     glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
  718.     glTexCoord2d(1.0, 0.0); glVertex3d(-152, -70, 152);
  719.     //5
  720.     glTexCoord2d(1.0, 1.0); glVertex3d(152, -70, 152);
  721.     glTexCoord2d(0.0, 1.0); glVertex3d(-152, -70, 152);
  722.     glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
  723.     glTexCoord2d(1.0, 0.0); glVertex3d(152, -70, -152);
  724.     //6
  725.     glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, -152);
  726.     glTexCoord2d(0.0, 1.0); glVertex3d(152, -70, -152);
  727.     glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
  728.     glTexCoord2d(1.0, 0.0); glVertex3d(-152, -60, -152);
  729.     glEnd();
  730.     //podstawa wiatraka z walca
  731.     glBindTexture(GL_TEXTURE_2D, texture[0]);
  732.     glEnable(GL_TEXTURE_2D); //wlaczenie teksturowania
  733.                              /*GLUquadricObj *obj;
  734.                              obj = gluNewQuadric();
  735.                              glColor3f(1.0, 1.0, 1.0);
  736.                              gluCylinder(obj,40, 40, 5, 15, 7); glRotated(-90, 1, 0, 0);*/
  737.     glDisable(GL_TEXTURE_2D);
  738.  
  739.  
  740.     glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
  741.     glBegin(GL_QUADS);
  742.     glColor3f(1.0, 1.0, 1.0);
  743.     glVertex3d(152,-60, 152);
  744.     glVertex3d(-152, -60, 152);
  745.     glVertex3d(-152, -70, 152);
  746.     glVertex3d(152, -70, 152);
  747.     //2
  748.     glColor3f(1.0, 1.0, 1.0);
  749.     glVertex3d(152, -60, 152);
  750.     glVertex3d(152, -70, 152);
  751.     glVertex3d(152, -70, -152);
  752.     glVertex3d(152, -60, -152);
  753.     //3
  754.     glColor3f(1.0, 1.0, 1.0);
  755.     glVertex3d(152, -60, 152);
  756.     glVertex3d(152, -60, -152);
  757.     glVertex3d(-152, -60, -152);
  758.     glVertex3d(-152, -60, 152);
  759.  
  760.     //4
  761.     glColor3f(1.0, 1.0, 1.0);
  762.     glVertex3d(-152, -60, 152);
  763.     glVertex3d(-152, -60, -152);
  764.     glVertex3d(-152, -70, -152);
  765.     glVertex3d(-152, -70, 152);
  766.     //5
  767.     glColor3f(1.0, 1.0, 1.0);
  768.     glVertex3d(152, -70, 152);
  769.     glVertex3d(-152, -70, 152);
  770.     glVertex3d(-152, -70, -152);
  771.     glVertex3d(152, -70, -152);
  772.     //6
  773.     glColor3f(1.0, 1.0, 1.0);
  774.     glVertex3d(152, -60, -152);
  775.     glVertex3d(152, -70, -152);
  776.     glVertex3d(-152, -70, -152);
  777.     glVertex3d(-152, -60, -152);
  778.     //glPopMatrix();
  779.     glEnd();
  780. }
  781.  
  782. void robot(double d1, double d2, double d3)
  783. {
  784.     glPushMatrix();
  785.     glBegin(GL_QUADS); //POLYGON
  786.                        //1
  787.                        //glColor3d(1, 0.5, 0);
  788.                        //glColor3f(0.0, 1.0, 0.0); green
  789.                        //glColor3f( 0.0, 0.0, 1.0 ); blue
  790.                        //glColor3f( 1.0, 0.0, 1.0 ); purple
  791.                        //glColor3f(1.0, 0.0, 0.0); //red
  792.     //1
  793.     glColor3f(1.0, 1.0, 1.0);
  794.     glVertex3d(80, -50, 80);
  795.     glVertex3d(-80, -50, 80);
  796.     glVertex3d(-80, -60, 80);
  797.     glVertex3d(80, -60, 80);
  798.     //2
  799.     glColor3f(1.0, 1.0, 1.0);
  800.     glVertex3d(80, -50, 80);
  801.     glVertex3d(80, -60, 80);
  802.     glVertex3d(80, -60, -80);
  803.     glVertex3d(80, -50, -80);
  804.     //3
  805.     glColor3f(1.0, 1.0, 1.0);
  806.     glVertex3d(80, -50, 80);
  807.     glVertex3d(80, -50, -80);
  808.     glVertex3d(-80, -50, -80);
  809.     glVertex3d(-80, -50, 80);
  810.  
  811.     //4
  812.     glColor3f(1.0, 1.0, 1.0);
  813.     glVertex3d(-80, -50, 80);
  814.     glVertex3d(-80, -50, -80);
  815.     glVertex3d(-80, -60, -80);
  816.     glVertex3d(-80, -60, 80);
  817.     //5
  818.     glColor3f(1.0, 1.0, 1.0);
  819.     glVertex3d(80, -60, 80);
  820.     glVertex3d(-80, -60, 80);
  821.     glVertex3d(-80, -60, -80);
  822.     glVertex3d(80, -60, -80);
  823.     //6
  824.     glColor3f(1.0, 1.0, 1.0);
  825.     glVertex3d(80, -50, -80);
  826.     glVertex3d(80, -60, -80);
  827.     glVertex3d(-80, -60, -80);
  828.     glVertex3d(-80, -50, -80);
  829.     glEnd();
  830.     //
  831.     //
  832.     glColor3f(0.1, 0.1, 0.1);
  833.     glRotated(-90, 1, 0, 0);
  834.     glTranslated(0, 0,-50);
  835.     walec(5,40);
  836.     glColor3f(0.75, 0.75, 0.75);
  837.     glTranslated(0, 0, 5);
  838.     glRotated(0, 0, 0, 1);
  839.     walec(40,10);
  840.     glTranslated(0, 0, 40);
  841.     walec(40,10);
  842.     glTranslated(0, 0, 40);
  843.     glRotated(d1, 0, 0, 1);
  844.     walec(40, 10);
  845.     glTranslated(0, 0, 40);
  846.     glRotated(90, 0, 1, 0);
  847.     glTranslated(0, 0,-20);
  848.     walec(40,10);
  849.     glTranslated(0, 0, +40);
  850.     glRotated(90, 0, 0, 1);
  851.     walec(40, 10);
  852.     glTranslated(0, 0, 40);
  853.     glRotated(90, 0, 1, 0);
  854.     glTranslated(0, 0, -20);
  855.     //tyl
  856.     glColor3f(1.0, 0.1, 0.1);
  857.     walec(40, 10);
  858.     glColor3f(0.75, 0.75, 0.75);
  859.     glTranslated(0, 0, -40);
  860.     glRotated(-90, 0, 1, 0);
  861.     glTranslated(0, 0, 20);
  862.     glTranslated(60, 0, -120);
  863.     glRotated(-90 + d2, 0, 0, 1);
  864.     glColor3f(0.5, 0.0, 0.0);
  865.     walec(20, 5);
  866.     glTranslated(0, 0, 0); //polozenie x,y,z zwiekszanie z>0 idzie do środka sceny, z<0 oddala od sceny
  867.     glRotated(-90, 0, 1, 0);
  868.     glTranslated(0, 0, -20);
  869.     walec(40, 5);
  870.     glTranslated(0, 0, -40);
  871.     glColor3f(1.0, 1.0, 1.0);
  872.     walec(40, 5);
  873.     glTranslated(0, 0, 80);
  874.     walec(40, 5);
  875.     glRotated(90, 0, 1, 0);
  876.     glTranslated(20, 0, -20);
  877.     glColor3f(1.0, 0.1, 0.1);
  878.     walec(20, 5);
  879.     glTranslated(0, 0, 30);
  880.     glRotated(90, 270, 1, 0);
  881.     glTranslated(0, -10, -20);
  882.     walec(40, 5);
  883.     glTranslated(0, 0, 40);
  884.     glColor3f(0.75, 0.75, 0.75);
  885.     walec(40, 5);
  886.     glTranslated(0, 0, -80);
  887.     walec(40, 5);
  888.     glPopMatrix();
  889.     glEnd();
  890. }
  891.  
  892. // LoadBitmapFile
  893. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
  894. //       Wypełnia strukturę nagłówka.
  895. //   Nie obsługuje map 8-bitowych.
  896. unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  897. {
  898.     FILE *filePtr;                          // wskaźnik pozycji pliku
  899.     BITMAPFILEHEADER    bitmapFileHeader;       // nagłówek pliku
  900.     unsigned char       *bitmapImage;           // dane obrazu
  901.     int                 imageIdx = 0;       // licznik pikseli
  902.     unsigned char       tempRGB;                // zmienna zamiany składowych
  903.  
  904.                                                 // otwiera plik w trybie "read binary"
  905.     filePtr = fopen(filename, "rb");
  906.     if (filePtr == NULL)
  907.         return NULL;
  908.  
  909.     // wczytuje nagłówek pliku
  910.     fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
  911.  
  912.     // sprawdza, czy jest to plik formatu BMP
  913.     if (bitmapFileHeader.bfType != BITMAP_ID)
  914.     {
  915.         fclose(filePtr);
  916.         return NULL;
  917.     }
  918.  
  919.     // wczytuje nagłówek obrazu
  920.     fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
  921.  
  922.     // ustawia wskaźnik pozycji pliku na początku danych obrazu
  923.     fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
  924.  
  925.     // przydziela pamięć buforowi obrazu
  926.     bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
  927.  
  928.     // sprawdza, czy udało się przydzielić pamięć
  929.     if (!bitmapImage)
  930.     {
  931.         free(bitmapImage);
  932.         fclose(filePtr);
  933.         return NULL;
  934.     }
  935.  
  936.     // wczytuje dane obrazu
  937.     fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
  938.  
  939.     // sprawdza, czy dane zostały wczytane
  940.     if (bitmapImage == NULL)
  941.     {
  942.         fclose(filePtr);
  943.         return NULL;
  944.     }
  945.  
  946.     // zamienia miejscami składowe R i B
  947.     for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
  948.     {
  949.         tempRGB = bitmapImage[imageIdx];
  950.         bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
  951.         bitmapImage[imageIdx + 2] = tempRGB;
  952.     }
  953.  
  954.     // zamyka plik i zwraca wskaźnik bufora zawierającego wczytany obraz
  955.     fclose(filePtr);
  956.     return bitmapImage;
  957. }
  958. void RenderScene(void)
  959. {
  960.  
  961.     //float normal[3];  // Storeage for calculated surface normal
  962.  
  963.     // Clear the window with current clearing color
  964.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  965.  
  966.     // Save the matrix state and do the rotations
  967.     glPushMatrix();
  968.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  969.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  970.  
  971.     /////////////////////////////////////////////////////////////////
  972.     // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN:           //
  973.     /////////////////////////////////////////////////////////////////
  974.  
  975.     //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
  976.     glPolygonMode(GL_BACK, GL_LINE);
  977.     //szescian();
  978.     //walec(20, 30);
  979.     //ramie(40, 25, 30, 60);
  980.     glTranslated(-400.0, -160.0, -310.0);
  981.     home();
  982.     glTranslated(400.0, 160.0, 310.0);
  983.     podloze();
  984.     robot(rot1, rot2, rot3);
  985.     glTranslated(140.0, 0.0, 0.0);
  986.     podloze();
  987.     robot(rot1, rot2, rot3);
  988.     glTranslated(-280.0, 0.0, 0.0);
  989.     podloze();
  990.     robot(rot1, rot2, rot3);
  991.     glTranslated(0.0, 0.0, -160.0);
  992.     podloze();
  993.     robot(rot1, rot2, rot3);
  994.     glTranslated(140.0, 0.0, 0.0);
  995.     podloze();
  996.     robot(rot1, rot2, rot3);
  997.     glTranslated(140.0, 0.0, 0.0);
  998.     podloze();
  999.     robot(rot1, rot2, rot3);
  1000.         glPopMatrix();
  1001.     glMatrixMode(GL_MODELVIEW);
  1002.  
  1003.     // Flush drawing commands
  1004.     glFlush();
  1005. }
  1006.  
  1007.  
  1008. // Select the pixel format for a given device context
  1009. void SetDCPixelFormat(HDC hDC)
  1010. {
  1011.     int nPixelFormat;
  1012.  
  1013.     static PIXELFORMATDESCRIPTOR pfd = {
  1014.         sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  1015.         1,                                                              // Version of this structure    
  1016.         PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  1017.         PFD_SUPPORT_OPENGL |                    // Support OpenGL calls in window
  1018.         PFD_DOUBLEBUFFER,                       // Double buffered
  1019.         PFD_TYPE_RGBA,                          // RGBA Color mode
  1020.         24,                                     // Want 24bit color
  1021.         0,0,0,0,0,0,                            // Not used to select mode
  1022.         0,0,                                    // Not used to select mode
  1023.         0,0,0,0,0,                              // Not used to select mode
  1024.         32,                                     // Size of depth buffer
  1025.         0,                                      // Not used to select mode
  1026.         0,                                      // Not used to select mode
  1027.         PFD_MAIN_PLANE,                         // Draw in main plane
  1028.         0,                                      // Not used to select mode
  1029.         0,0,0 };                                // Not used to select mode
  1030.  
  1031.                                                 // Choose a pixel format that best matches that described in pfd
  1032.     nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  1033.  
  1034.     // Set the pixel format for the device context
  1035.     SetPixelFormat(hDC, nPixelFormat, &pfd);
  1036. }
  1037.  
  1038.  
  1039.  
  1040. // If necessary, creates a 3-3-2 palette for the device context listed.
  1041. HPALETTE GetOpenGLPalette(HDC hDC)
  1042. {
  1043.     HPALETTE hRetPal = NULL;    // Handle to palette to be created
  1044.     PIXELFORMATDESCRIPTOR pfd;  // Pixel Format Descriptor
  1045.     LOGPALETTE *pPal;           // Pointer to memory for logical palette
  1046.     int nPixelFormat;           // Pixel format index
  1047.     int nColors;                // Number of entries in palette
  1048.     int i;                      // Counting variable
  1049.     BYTE RedRange, GreenRange, BlueRange;
  1050.     // Range for each color entry (7,7,and 3)
  1051.  
  1052.  
  1053.     // Get the pixel format index and retrieve the pixel format description
  1054.     nPixelFormat = GetPixelFormat(hDC);
  1055.     DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  1056.  
  1057.     // Does this pixel format require a palette?  If not, do not create a
  1058.     // palette and just return NULL
  1059.     if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  1060.         return NULL;
  1061.  
  1062.     // Number of entries in palette.  8 bits yeilds 256 entries
  1063.     nColors = 1 << pfd.cColorBits;
  1064.  
  1065.     // Allocate space for a logical palette structure plus all the palette entries
  1066.     pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  1067.  
  1068.     // Fill in palette header
  1069.     pPal->palVersion = 0x300;       // Windows 3.0
  1070.     pPal->palNumEntries = nColors; // table size
  1071.  
  1072.                                    // Build mask of all 1's.  This creates a number represented by having
  1073.                                    // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  1074.                                    // pfd.cBlueBits.  
  1075.     RedRange = (1 << pfd.cRedBits) - 1;
  1076.     GreenRange = (1 << pfd.cGreenBits) - 1;
  1077.     BlueRange = (1 << pfd.cBlueBits) - 1;
  1078.  
  1079.     // Loop through all the palette entries
  1080.     for (i = 0; i < nColors; i++)
  1081.     {
  1082.         // Fill in the 8-bit equivalents for each component
  1083.         pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  1084.         pPal->palPalEntry[i].peRed = (unsigned char)(
  1085.             (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  1086.  
  1087.         pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  1088.         pPal->palPalEntry[i].peGreen = (unsigned char)(
  1089.             (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  1090.  
  1091.         pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  1092.         pPal->palPalEntry[i].peBlue = (unsigned char)(
  1093.             (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  1094.  
  1095.         pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  1096.     }
  1097.  
  1098.  
  1099.     // Create the palette
  1100.     hRetPal = CreatePalette(pPal);
  1101.  
  1102.     // Go ahead and select and realize the palette for this device context
  1103.     SelectPalette(hDC, hRetPal, FALSE);
  1104.     RealizePalette(hDC);
  1105.  
  1106.     // Free the memory used for the logical palette structure
  1107.     free(pPal);
  1108.  
  1109.     // Return the handle to the new palette
  1110.     return hRetPal;
  1111. }
  1112.  
  1113.  
  1114. // Entry point of all Windows programs
  1115. int APIENTRY WinMain(HINSTANCE       hInst,
  1116.     HINSTANCE       hPrevInstance,
  1117.     LPSTR           lpCmdLine,
  1118.     int                     nCmdShow)
  1119. {
  1120.     MSG                     msg;            // Windows message structure
  1121.     WNDCLASS        wc;                     // Windows class structure
  1122.     HWND            hWnd;           // Storeage for window handle
  1123.  
  1124.     hInstance = hInst;
  1125.  
  1126.     // Register Window style
  1127.     wc.style = CS_HREDRAW | CS_VREDRAW;
  1128.     wc.lpfnWndProc = (WNDPROC)WndProc;
  1129.     wc.cbClsExtra = 0;
  1130.     wc.cbWndExtra = 0;
  1131.     wc.hInstance = hInstance;
  1132.     wc.hIcon = NULL;
  1133.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  1134.  
  1135.     // No need for background brush for OpenGL window
  1136.     wc.hbrBackground = NULL;
  1137.  
  1138.     wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  1139.     wc.lpszClassName = lpszAppName;
  1140.  
  1141.     // Register the window class
  1142.     if (RegisterClass(&wc) == 0)
  1143.         return FALSE;
  1144.  
  1145.  
  1146.     // Create the main application window
  1147.     hWnd = CreateWindow(
  1148.         lpszAppName,
  1149.         lpszAppName,
  1150.  
  1151.         // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  1152.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  1153.  
  1154.         // Window position and size
  1155.         50, 50,
  1156.         400, 400,
  1157.         NULL,
  1158.         NULL,
  1159.         hInstance,
  1160.         NULL);
  1161.  
  1162.     // If window was not created, quit
  1163.     if (hWnd == NULL)
  1164.         return FALSE;
  1165.  
  1166.  
  1167.     // Display the window
  1168.     ShowWindow(hWnd, SW_SHOW);
  1169.     UpdateWindow(hWnd);
  1170.  
  1171.     // Process application messages until the application closes
  1172.     while (GetMessage(&msg, NULL, 0, 0))
  1173.     {
  1174.         TranslateMessage(&msg);
  1175.         DispatchMessage(&msg);
  1176.     }
  1177.  
  1178.     return msg.wParam;
  1179. }
  1180. // Window procedure, handles all messages for this program
  1181. LRESULT CALLBACK WndProc(HWND    hWnd,
  1182.     UINT    message,
  1183.     WPARAM  wParam,
  1184.     LPARAM  lParam)
  1185. {
  1186.     static HGLRC hRC;               // Permenant Rendering context
  1187.     static HDC hDC;                 // Private GDI Device context
  1188.     int licznik = 0;
  1189.     switch (message)
  1190.     {
  1191.     case WM_TIMER:
  1192.     {
  1193.         if (wParam == 101)
  1194.         {
  1195.             licznik++;
  1196.             if (licznik < 15)
  1197.             {
  1198.                 rot1 += 1.0;
  1199.                 rot2 += 10.0;
  1200.             }
  1201.             if (licznik > 15 && licznik < 30)
  1202.             {
  1203.                 rot1 -= 3.0;
  1204.                 rot2 -= 10.0;
  1205.             }
  1206.             if (licznik>30)
  1207.             {
  1208.                 licznik = 0;
  1209.             }
  1210.             InvalidateRect(hWnd, NULL, FALSE);
  1211.         }
  1212.         break;
  1213.     }
  1214.         // Window creation, setup for OpenGL
  1215.     case WM_CREATE:
  1216.         // Store the device context
  1217.         hDC = GetDC(hWnd);
  1218.  
  1219.         // Select the pixel format
  1220.         SetDCPixelFormat(hDC);
  1221.         SetTimer(hWnd, 101, 1, NULL);
  1222.  
  1223.         // Create palette if needed
  1224.         hPalette = GetOpenGLPalette(hDC);
  1225.  
  1226.         // Create the rendering context and make it current
  1227.         hRC = wglCreateContext(hDC);
  1228.         wglMakeCurrent(hDC, hRC);
  1229.         SetupRC();
  1230.         glGenTextures(2, &texture[0]);                  // tworzy obiekt tekstury          
  1231.  
  1232.                                                         // ładuje pierwszy obraz tekstury:
  1233.         bitmapData = LoadBitmapFile("Bitmapy\\GRASS.BMP", &bitmapInfoHeader);
  1234.  
  1235.         glBindTexture(GL_TEXTURE_2D, texture[0]);       // aktywuje obiekt tekstury
  1236.  
  1237.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1238.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1239.  
  1240.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  1241.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  1242.  
  1243.         // tworzy obraz tekstury
  1244.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  1245.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  1246.  
  1247.         if (bitmapData)
  1248.             free(bitmapData);
  1249.  
  1250.         // ładuje drugi obraz tekstury:
  1251.         bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  1252.         glBindTexture(GL_TEXTURE_2D, texture[1]);       // aktywuje obiekt tekstury
  1253.  
  1254.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1255.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1256.  
  1257.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  1258.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  1259.  
  1260.         // tworzy obraz tekstury
  1261.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  1262.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  1263.  
  1264.         if (bitmapData)
  1265.             free(bitmapData);
  1266.  
  1267.         // ustalenie sposobu mieszania tekstury z tłem
  1268.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  1269.         break;
  1270.  
  1271.         // Window is being destroyed, cleanup
  1272.     case WM_DESTROY:
  1273.         // Deselect the current rendering context and delete it
  1274.         wglMakeCurrent(hDC, NULL);
  1275.         wglDeleteContext(hRC);
  1276.         KillTimer(hWnd, 101);
  1277.  
  1278.         // Delete the palette if it was created
  1279.         if (hPalette != NULL)
  1280.             DeleteObject(hPalette);
  1281.  
  1282.         // Tell the application to terminate after the window
  1283.         // is gone.
  1284.         PostQuitMessage(0);
  1285.         break;
  1286.  
  1287.         // Window is resized.
  1288.     case WM_SIZE:
  1289.         // Call our function which modifies the clipping
  1290.         // volume and viewport
  1291.         ChangeSize(LOWORD(lParam), HIWORD(lParam));
  1292.         break;
  1293.  
  1294.  
  1295.         // The painting function.  This message sent by Windows
  1296.         // whenever the screen needs updating.
  1297.     case WM_PAINT:
  1298.     {
  1299.         // Call OpenGL drawing code
  1300.         RenderScene();
  1301.  
  1302.         SwapBuffers(hDC);
  1303.  
  1304.         // Validate the newly painted client area
  1305.         ValidateRect(hWnd, NULL);
  1306.     }
  1307.     break;
  1308.  
  1309.     // Windows is telling the application that it may modify
  1310.     // the system palette.  This message in essance asks the
  1311.     // application for a new palette.
  1312.     case WM_QUERYNEWPALETTE:
  1313.         // If the palette was created.
  1314.         if (hPalette)
  1315.         {
  1316.             int nRet;
  1317.  
  1318.             // Selects the palette into the current device context
  1319.             SelectPalette(hDC, hPalette, FALSE);
  1320.  
  1321.             // Map entries from the currently selected palette to
  1322.             // the system palette.  The return value is the number
  1323.             // of palette entries modified.
  1324.             nRet = RealizePalette(hDC);
  1325.  
  1326.             // Repaint, forces remap of palette in current window
  1327.             InvalidateRect(hWnd, NULL, FALSE);
  1328.  
  1329.             return nRet;
  1330.         }
  1331.         break;
  1332.  
  1333.  
  1334.         // This window may set the palette, even though it is not the
  1335.         // currently active window.
  1336.     case WM_PALETTECHANGED:
  1337.         // Don't do anything if the palette does not exist, or if
  1338.         // this is the window that changed the palette.
  1339.         if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  1340.         {
  1341.             // Select the palette into the device context
  1342.             SelectPalette(hDC, hPalette, FALSE);
  1343.  
  1344.             // Map entries to system palette
  1345.             RealizePalette(hDC);
  1346.  
  1347.             // Remap the current colors to the newly realized palette
  1348.             UpdateColors(hDC);
  1349.             return 0;
  1350.         }
  1351.         break;
  1352.  
  1353.         // Key press, check for arrow keys to do cube rotation.
  1354.     case WM_KEYDOWN:
  1355.     {
  1356.         if (wParam == VK_UP)
  1357.             xRot -= 5.0f;
  1358.         if (wParam == VK_DOWN)
  1359.             xRot += 5.0f;
  1360.         if (wParam == VK_LEFT)
  1361.             yRot -= 5.0f;
  1362.         if (wParam == VK_RIGHT)
  1363.             yRot += 5.0f;
  1364.         if (wParam == '1')
  1365.             rot1 -=5.0f;
  1366.         if (wParam == '2')
  1367.             rot1 += 5.0f;
  1368.         if (wParam == '3')
  1369.             rot2 -= 5.0f;
  1370.         if (wParam == '4')
  1371.             rot2 += 5.0f;
  1372.         if (wParam == '5')
  1373.             rot3 -= 5.0f;
  1374.         if (wParam == '6')
  1375.             rot3 += 5.0f;
  1376.  
  1377.         xRot = (const int)xRot % 360;
  1378.         yRot = (const int)yRot % 360;
  1379.         rot1 = (const int)rot1 % 360;
  1380.         rot2 = (const int)rot2 % 360;
  1381.         rot3 = (const int)rot3 % 360;
  1382.        
  1383.  
  1384.         InvalidateRect(hWnd, NULL, FALSE);
  1385.     }
  1386.     break;
  1387.  
  1388.     // A menu command
  1389.     case WM_COMMAND:
  1390.     {
  1391.         switch (LOWORD(wParam))
  1392.         {
  1393.             // Exit the program
  1394.         case ID_FILE_EXIT:
  1395.             DestroyWindow(hWnd);
  1396.             break;
  1397.         }
  1398.     }
  1399.     break;
  1400.  
  1401.  
  1402.     default:   // Passes it on if unproccessed
  1403.         return (DefWindowProc(hWnd, message, wParam, lParam));
  1404.  
  1405.     }
  1406.  
  1407.     return (0L);
  1408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement