Advertisement
pabloducato

projekt_openGL_bez_tekstur

May 1st, 2018
366
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[2];         // 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.     //glColor3d(1, 0.5, 0);
  345.     //glColor3f(0.0, 1.0, 0.0); green
  346.     //glColor3f( 0.0, 0.0, 1.0 ); blue
  347.     //glColor3f( 1.0, 0.0, 1.0 ); purple
  348.     //glColor3f(1.0, 0.0, 0.0); //red
  349.     glColor3f(0.0, 0.7, 0.0);
  350.     glVertex3d(50, -50, 50);
  351.     glVertex3d(-50, -50, 50);
  352.     glVertex3d(-50, -60, 50);
  353.     glVertex3d(50, -60, 50);
  354.     //2
  355.     glColor3f(0.0, 0.7, 0.0);
  356.     glVertex3d(50, -50, 50);
  357.     glVertex3d(50, -60, 50);
  358.     glVertex3d(50, -60, -50);
  359.     glVertex3d(50, -50, -50);
  360.     //3
  361.     glColor3f(0.0, 0.7, 0.0);
  362.     glVertex3d(50, -50, 50);
  363.     glVertex3d(50, -50, -50);
  364.     glVertex3d(-50, -50, -50);
  365.     glVertex3d(-50, -50, 50);
  366.    
  367.     //4
  368.     glColor3f(0.0, 0.7, 0.0);
  369.     glVertex3d(-50, -50, 50);
  370.     glVertex3d(-50, -50, -50);
  371.     glVertex3d(-50, -60, -50);
  372.     glVertex3d(-50, -60, 50);
  373.     //5
  374.     glColor3f(0.0, 0.7, 0.0);
  375.     glVertex3d(50, -60, 50);
  376.     glVertex3d(-50, -60, 50);
  377.     glVertex3d(-50, -60, -50);
  378.     glVertex3d(50, -60, -50);
  379.     //6
  380.     glColor3f(0.0, 0.7, 0.0);
  381.     glVertex3d(50, -50, -50);
  382.     glVertex3d(50, -60, -50);
  383.     glVertex3d(-50, -60, -50);
  384.     glVertex3d(-50, -50, -50);
  385.     glEnd();
  386. }
  387.  
  388. void walec(double h, double r)
  389. {
  390.     double angle, x, y;
  391.     glBegin(GL_TRIANGLE_FAN);
  392.     glVertex3d(0.0f, 0.0f, 0.0f);
  393.     for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  394.     {
  395.         x = r*sin(angle);
  396.         y = r*cos(angle);
  397.         glVertex3d(x, y, 0.0);
  398.     }
  399.     glEnd();
  400.     glBegin(GL_TRIANGLE_FAN);
  401.     glVertex3d(0.0f, 0.0f, h);
  402.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  403.     {
  404.         x = r*sin(angle);
  405.         y = r*cos(angle);
  406.         glVertex3d(x, y, h);
  407.     }
  408.     glEnd();
  409.     glBegin(GL_QUAD_STRIP);
  410.     for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  411.     {
  412.         x = r*sin(angle);
  413.         y = r*cos(angle);
  414.         glVertex3d(x, y, h);
  415.         glVertex3d(x, y, 0);
  416.     }
  417.     glEnd();
  418. }
  419. void ramie(double r1, double r2, double h, double d)
  420. {
  421.     double angle, x, y;
  422.     glBegin(GL_TRIANGLE_FAN);
  423.     glColor3d(3, 12, 0);
  424.     glVertex3d(0.0f, 0.0f, 0.0f);
  425.     for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  426.     {
  427.         x = r1*sin(angle);
  428.         y = r1*cos(angle);
  429.         glVertex3d(x, y, 0.0);
  430.     }
  431.     glEnd();
  432.     glBegin(GL_QUAD_STRIP);
  433.     glColor3d(2, 0, 42);
  434.     for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  435.     {
  436.         x = r1*sin(angle);
  437.         y = r1*cos(angle);
  438.         glVertex3d(x, y, 0);
  439.         glVertex3d(x, y, h);
  440.     }
  441.     glEnd();
  442.     glBegin(GL_TRIANGLE_FAN);
  443.     glColor3d(1, 0, 0);
  444.     glVertex3d(0.0f, 0.0f, h);
  445.     for (angle = 0.0f; angle >= -(GL_PI); angle -= (GL_PI / 8.0f))
  446.     {
  447.         x = r1*sin(angle);
  448.         y = r1*cos(angle);
  449.         glVertex3d(x, y, h);
  450.     }
  451.     glEnd();
  452.  
  453.     // 2 ************************************
  454.     glBegin(GL_QUADS);
  455.     glColor3d(0, 0.5, 1);
  456.     glVertex3d(0, r1, 0);
  457.     glVertex3d(d, r2, 0);
  458.     glVertex3d(d, -r2, 0);
  459.     glVertex3d(0, -r1, 0);
  460.     glEnd();
  461.     glBegin(GL_QUADS);
  462.     glColor3d(0, 0.5, 1);
  463.     glVertex3d(0, -r1, h);
  464.     glVertex3d(d, -r2, h);
  465.     glVertex3d(d, r2, h);
  466.     glVertex3d(0, r1, h);
  467.     glEnd();
  468.  
  469.     glBegin(GL_QUADS);
  470.     glColor3d(0, 0.5, 1);
  471.     glVertex3d(0, r1, h);
  472.     glVertex3d(d, r2, h);
  473.     glVertex3d(d, r2, 0);
  474.     glVertex3d(0, r1, 0);
  475.     glEnd();
  476.  
  477.     glBegin(GL_QUADS);
  478.     glColor3d(0, 0.5, 1);
  479.     glVertex3d(0, -r1, 0);
  480.     glVertex3d(d, -r2, 0);
  481.     glVertex3d(d, -r2, h);
  482.     glVertex3d(0, -r1, h);
  483.     glEnd();
  484.  
  485.     glBegin(GL_TRIANGLE_FAN);
  486.     glColor3d(3, 12, 0);
  487.     glVertex3d(d, 0.0f, 0.0f);
  488.     for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  489.     {
  490.         x = r2*sin(angle);
  491.         y = r2*cos(angle);
  492.         glVertex3d(x + d, y, 0.0);
  493.     }
  494.     glEnd();
  495.     glBegin(GL_QUAD_STRIP);
  496.     glColor3d(2, 0, 42);
  497.     for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
  498.     {
  499.         x = r2*sin(angle);
  500.         y = r2*cos(angle);
  501.         glVertex3d(x + d, y, 0);
  502.         glVertex3d(x + d, y, h);
  503.     }
  504.     glEnd();
  505.     glBegin(GL_TRIANGLE_FAN);
  506.     glColor3d(1, 0, 0);
  507.     glVertex3d(d, 0.0f, h);
  508.     for (angle = -GL_PI; angle >= -(2 * GL_PI); angle -= (GL_PI / 8.0f))
  509.     {
  510.         x = r2*sin(angle);
  511.         y = r2*cos(angle);
  512.         glVertex3d(x + d, y, h);
  513.     }
  514.     glEnd();
  515. }
  516.  
  517. void home()
  518. {
  519.     //Roof
  520.     glClear(GL_COLOR_BUFFER_BIT);     // Clear display window
  521.                                       // Set line segment color as glColor3f(R,G,B)
  522.     glColor3f(0.3, 0.5, 0.8);
  523.     glBegin(GL_POLYGON);
  524.     glVertex2i(200, 500);
  525.     glVertex2i(600, 500);
  526.     glVertex2i(700, 350);
  527.     glVertex2i(300, 350);
  528.     glEnd();
  529.     // Top of Front Wall
  530.     glColor3f(0.1, 0.5, 0.0);
  531.     glBegin(GL_TRIANGLES);
  532.     glVertex2i(300, 350);
  533.     glVertex2i(100, 350);
  534.     glVertex2i(200, 500);  
  535.     glEnd();
  536.     /*glColor3f(0.1, 0.5, 0.0);
  537.     glBegin(GL_TRIANGLES);
  538.     glVertex2i(200, 500);
  539.     glVertex2i(100, 350);
  540.     glVertex2i(300, 350);
  541.     glEnd();*/
  542.     // Front Wall good no more pain
  543.     glColor3f(0.7, 0.2, 0.3);
  544.     glBegin(GL_POLYGON);
  545.     glVertex2i(100, 350);
  546.     glVertex2i(300, 350);
  547.     glVertex2i(300, 100);
  548.     glVertex2i(100, 100);
  549.     glEnd();
  550.     // Front Door
  551.     glColor3f(0.7, 0.2, 0.9);
  552.     glBegin(GL_POLYGON);
  553.     glVertex2i(150, 250);
  554.     glVertex2i(250, 250);
  555.     glVertex2i(250, 100);
  556.     glVertex2i(150, 100);
  557.     glEnd();
  558.  
  559.     // Front Door Lock good asa good
  560.     glColor3f(0.3, 0.7, 0.9);
  561.     glPointSize(15);
  562.     glBegin(GL_POINTS);
  563.     glVertex2i(170, 170);
  564.     glEnd();
  565.  
  566.     //side Wall good
  567.     glColor3f(0.1, 0.2, 0.3);
  568.     glBegin(GL_POLYGON);
  569.     glVertex2i(300, 350);
  570.     glVertex2i(700, 350);
  571.     glVertex2i(700, 100);
  572.     glVertex2i(300, 100);
  573.     glEnd();
  574.     // window one
  575.     glColor3f(0.2, 0.4, 0.3);
  576.     glBegin(GL_POLYGON);
  577.     glVertex2i(330, 320);
  578.     glVertex2i(450, 320);
  579.     glVertex2i(450, 230);
  580.     glVertex2i(330, 230);
  581.     glEnd();
  582.     // line of window one
  583.     glColor3f(0.1, 0.7, 0.5);
  584.     glLineWidth(5);
  585.     glBegin(GL_LINES);
  586.     glVertex2i(390, 320);
  587.     glVertex2i(390, 230);
  588.     glVertex2i(330, 273);
  589.     glVertex2i(450, 273);
  590.     glEnd();
  591.     // window two
  592.     glColor3f(0.2, 0.4, 0.3);
  593.     glBegin(GL_POLYGON);
  594.     glVertex2i(530, 320);
  595.     glVertex2i(650, 320);
  596.     glVertex2i(650, 230);
  597.     glVertex2i(530, 230);
  598.     glEnd();
  599.     // lines of window two
  600.     glColor3f(0.1, 0.7, 0.5);
  601.     glLineWidth(5);
  602.     glBegin(GL_LINES);
  603.     glVertex2i(590, 320);
  604.     glVertex2i(590, 230);
  605.     glVertex2i(530, 273);
  606.     glVertex2i(650, 273);
  607.     glEnd();
  608.  
  609.     // Entrance Path don on always off!!!
  610.     /*glColor3f(0.3, 0.5, 0.7);
  611.     glLineWidth(3);
  612.     glBegin(GL_POLYGON);
  613.     glVertex2i(150, 100);
  614.     glVertex2i(250, 100);
  615.     glVertex2i(210, 0);
  616.     glVertex2i(40, 0);
  617.     glEnd();*/
  618.     // Process all OpenGL routine s as quickly as possible
  619.     glFlush();
  620. }
  621.  
  622. void home2()
  623. {
  624.     //Roof
  625.     glClear(GL_COLOR_BUFFER_BIT);     // Clear display window
  626.                                       // Set line segment color as glColor3f(R,G,B)
  627.     glColor3f(0.3, 0.5, 0.8);
  628.     glBegin(GL_POLYGON);
  629.     glVertex2i(200, 500);
  630.     glVertex2i(600, 500);
  631.     glVertex2i(700, 350);
  632.     glVertex2i(300, 350);
  633.     glEnd();
  634.     // Top of Front Wall
  635.     glColor3f(0.1, 0.5, 0.0);
  636.     glBegin(GL_TRIANGLES);
  637.     glVertex2i(300, 350);
  638.     glVertex2i(100, 350);
  639.     glVertex2i(200, 500);
  640.     glEnd();
  641.     /*glColor3f(0.1, 0.5, 0.0);
  642.     glBegin(GL_TRIANGLES);
  643.     glVertex2i(200, 500);
  644.     glVertex2i(100, 350);
  645.     glVertex2i(300, 350);
  646.     glEnd();*/
  647.     // Front Wall good no more pain
  648.     glColor3f(0.7, 0.2, 0.3);
  649.     glBegin(GL_POLYGON);
  650.     glVertex2i(100, 350);
  651.     glVertex2i(300, 350);
  652.     glVertex2i(300, 100);
  653.     glVertex2i(100, 100);
  654.     glEnd();
  655.     // Front Door
  656.     glColor3f(0.7, 0.2, 0.9);
  657.     glBegin(GL_POLYGON);
  658.     glVertex2i(150, 250);
  659.     glVertex2i(250, 250);
  660.     glVertex2i(250, 100);
  661.     glVertex2i(150, 100);
  662.     glEnd();
  663.  
  664.     // Front Door Lock good asa good
  665.     glColor3f(0.3, 0.7, 0.9);
  666.     glPointSize(15);
  667.     glBegin(GL_POINTS);
  668.     glVertex2i(170, 170);
  669.     glEnd();
  670.  
  671.     //side Wall good
  672.     glColor3f(0.1, 0.2, 0.3);
  673.     glBegin(GL_POLYGON);
  674.     glVertex2i(300, 350);
  675.     glVertex2i(700, 350);
  676.     glVertex2i(700, 100);
  677.     glVertex2i(300, 100);
  678.     glEnd();
  679.     // window one
  680.     glColor3f(0.2, 0.4, 0.3);
  681.     glBegin(GL_POLYGON);
  682.     glVertex2i(330, 320);
  683.     glVertex2i(450, 320);
  684.     glVertex2i(450, 230);
  685.     glVertex2i(330, 230);
  686.     glEnd();
  687.     // line of window one
  688.     glColor3f(0.1, 0.7, 0.5);
  689.     glLineWidth(5);
  690.     glBegin(GL_LINES);
  691.     glVertex2i(390, 320);
  692.     glVertex2i(390, 230);
  693.     glVertex2i(330, 273);
  694.     glVertex2i(450, 273);
  695.     glEnd();
  696.     // window two
  697.     glColor3f(0.2, 0.4, 0.3);
  698.     glBegin(GL_POLYGON);
  699.     glVertex2i(530, 320);
  700.     glVertex2i(650, 320);
  701.     glVertex2i(650, 230);
  702.     glVertex2i(530, 230);
  703.     glEnd();
  704.     // lines of window two
  705.     glColor3f(0.1, 0.7, 0.5);
  706.     glLineWidth(5);
  707.     glBegin(GL_LINES);
  708.     glVertex2i(590, 320);
  709.     glVertex2i(590, 230);
  710.     glVertex2i(530, 273);
  711.     glVertex2i(650, 273);
  712.     glEnd();
  713.  
  714.     // Entrance Path don on always off!!!
  715.     /*glColor3f(0.3, 0.5, 0.7);
  716.     glLineWidth(3);
  717.     glBegin(GL_POLYGON);
  718.     glVertex2i(150, 100);
  719.     glVertex2i(250, 100);
  720.     glVertex2i(210, 0);
  721.     glVertex2i(40, 0);
  722.     glEnd();*/
  723.     // Process all OpenGL routine s as quickly as possible
  724.     glFlush();
  725. }
  726.  
  727. void podloze()
  728. {
  729.     glPushMatrix();
  730.     glBegin(GL_QUADS);
  731.     glColor3f(0.3, 0.75, 0.8);
  732.     glVertex3d(152,-60, 152);
  733.     glVertex3d(-152, -60, 152);
  734.     glVertex3d(-152, -70, 152);
  735.     glVertex3d(152, -70, 152);
  736.     //2
  737.     glColor3f(0.3, 0.75, 0.8);
  738.     glVertex3d(152, -60, 152);
  739.     glVertex3d(152, -70, 152);
  740.     glVertex3d(152, -70, -152);
  741.     glVertex3d(152, -60, -152);
  742.     //3
  743.     glColor3f(0.3, 0.75, 0.8);
  744.     glVertex3d(152, -60, 152);
  745.     glVertex3d(152, -60, -152);
  746.     glVertex3d(-152, -60, -152);
  747.     glVertex3d(-152, -60, 152);
  748.  
  749.     //4
  750.     glColor3f(0.3, 0.75, 0.8);
  751.     glVertex3d(-152, -60, 152);
  752.     glVertex3d(-152, -60, -152);
  753.     glVertex3d(-152, -70, -152);
  754.     glVertex3d(-152, -70, 152);
  755.     //5
  756.     glColor3f(0.3, 0.75, 0.8);
  757.     glVertex3d(152, -70, 152);
  758.     glVertex3d(-152, -70, 152);
  759.     glVertex3d(-152, -70, -152);
  760.     glVertex3d(152, -70, -152);
  761.     //6
  762.     glColor3f(0.3, 0.75, 0.8);
  763.     glVertex3d(152, -60, -152);
  764.     glVertex3d(152, -70, -152);
  765.     glVertex3d(-152, -70, -152);
  766.     glVertex3d(-152, -60, -152);
  767.     //glPopMatrix();
  768.     glEnd();
  769. }
  770.  
  771. void robot(double d1, double d2, double d3)
  772. {
  773.     glPushMatrix();
  774.     glBegin(GL_QUADS); //POLYGON
  775.                        //1
  776.                        //glColor3d(1, 0.5, 0);
  777.                        //glColor3f(0.0, 1.0, 0.0); green
  778.                        //glColor3f( 0.0, 0.0, 1.0 ); blue
  779.                        //glColor3f( 1.0, 0.0, 1.0 ); purple
  780.                        //glColor3f(1.0, 0.0, 0.0); //red
  781.     //1
  782.     glColor3f(1.0, 1.0, 1.0);
  783.     glVertex3d(80, -50, 80);
  784.     glVertex3d(-80, -50, 80);
  785.     glVertex3d(-80, -60, 80);
  786.     glVertex3d(80, -60, 80);
  787.     //2
  788.     glColor3f(1.0, 1.0, 1.0);
  789.     glVertex3d(80, -50, 80);
  790.     glVertex3d(80, -60, 80);
  791.     glVertex3d(80, -60, -80);
  792.     glVertex3d(80, -50, -80);
  793.     //3
  794.     glColor3f(1.0, 1.0, 1.0);
  795.     glVertex3d(80, -50, 80);
  796.     glVertex3d(80, -50, -80);
  797.     glVertex3d(-80, -50, -80);
  798.     glVertex3d(-80, -50, 80);
  799.  
  800.     //4
  801.     glColor3f(1.0, 1.0, 1.0);
  802.     glVertex3d(-80, -50, 80);
  803.     glVertex3d(-80, -50, -80);
  804.     glVertex3d(-80, -60, -80);
  805.     glVertex3d(-80, -60, 80);
  806.     //5
  807.     glColor3f(1.0, 1.0, 1.0);
  808.     glVertex3d(80, -60, 80);
  809.     glVertex3d(-80, -60, 80);
  810.     glVertex3d(-80, -60, -80);
  811.     glVertex3d(80, -60, -80);
  812.     //6
  813.     glColor3f(1.0, 1.0, 1.0);
  814.     glVertex3d(80, -50, -80);
  815.     glVertex3d(80, -60, -80);
  816.     glVertex3d(-80, -60, -80);
  817.     glVertex3d(-80, -50, -80);
  818.     glEnd();
  819.     //
  820.     //
  821.     glColor3f(0.1, 0.1, 0.1);
  822.     glRotated(-90, 1, 0, 0);
  823.     glTranslated(0, 0,-50);
  824.     walec(5,40);
  825.     glColor3f(0.75, 0.75, 0.75);
  826.     glTranslated(0, 0, 5);
  827.     glRotated(0, 0, 0, 1);
  828.     walec(40,10);
  829.     glTranslated(0, 0, 40);
  830.     walec(40,10);
  831.     glTranslated(0, 0, 40);
  832.     glRotated(d1, 0, 0, 1);
  833.     walec(40, 10);
  834.     glTranslated(0, 0, 40);
  835.     glRotated(90, 0, 1, 0);
  836.     glTranslated(0, 0,-20);
  837.     walec(40,10);
  838.     glTranslated(0, 0, +40);
  839.     glRotated(90, 0, 0, 1);
  840.     walec(40, 10);
  841.     glTranslated(0, 0, 40);
  842.     glRotated(90, 0, 1, 0);
  843.     glTranslated(0, 0, -20);
  844.     //tyl
  845.     glColor3f(1.0, 0.1, 0.1);
  846.     walec(40, 10);
  847.     glColor3f(0.75, 0.75, 0.75);
  848.     glTranslated(0, 0, -40);
  849.     glRotated(-90, 0, 1, 0);
  850.     glTranslated(0, 0, 20);
  851.     glTranslated(60, 0, -120);
  852.     glRotated(-90 + d2, 0, 0, 1);
  853.     glColor3f(0.5, 0.0, 0.0);
  854.     walec(20, 5);
  855.     glTranslated(0, 0, 0); //polozenie x,y,z zwiekszanie z>0 idzie do środka sceny, z<0 oddala od sceny
  856.     glRotated(-90, 0, 1, 0);
  857.     glTranslated(0, 0, -20);
  858.     walec(40, 5);
  859.     glTranslated(0, 0, -40);
  860.     glColor3f(1.0, 1.0, 1.0);
  861.     walec(40, 5);
  862.     glTranslated(0, 0, 80);
  863.     walec(40, 5);
  864.     glRotated(90, 0, 1, 0);
  865.     glTranslated(20, 0, -20);
  866.     glColor3f(1.0, 0.1, 0.1);
  867.     walec(20, 5);
  868.     glTranslated(0, 0, 30);
  869.     glRotated(90, 270, 1, 0);
  870.     glTranslated(0, -10, -20);
  871.     walec(40, 5);
  872.     glTranslated(0, 0, 40);
  873.     glColor3f(0.75, 0.75, 0.75);
  874.     walec(40, 5);
  875.     glTranslated(0, 0, -80);
  876.     walec(40, 5);
  877.     glPopMatrix();
  878.     glEnd();
  879. }
  880.  
  881. // LoadBitmapFile
  882. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
  883. //       Wypełnia strukturę nagłówka.
  884. //   Nie obsługuje map 8-bitowych.
  885. unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
  886. {
  887.     FILE *filePtr;                          // wskaźnik pozycji pliku
  888.     BITMAPFILEHEADER    bitmapFileHeader;       // nagłówek pliku
  889.     unsigned char       *bitmapImage;           // dane obrazu
  890.     int                 imageIdx = 0;       // licznik pikseli
  891.     unsigned char       tempRGB;                // zmienna zamiany składowych
  892.  
  893.                                                 // otwiera plik w trybie "read binary"
  894.     filePtr = fopen(filename, "rb");
  895.     if (filePtr == NULL)
  896.         return NULL;
  897.  
  898.     // wczytuje nagłówek pliku
  899.     fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
  900.  
  901.     // sprawdza, czy jest to plik formatu BMP
  902.     if (bitmapFileHeader.bfType != BITMAP_ID)
  903.     {
  904.         fclose(filePtr);
  905.         return NULL;
  906.     }
  907.  
  908.     // wczytuje nagłówek obrazu
  909.     fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
  910.  
  911.     // ustawia wskaźnik pozycji pliku na początku danych obrazu
  912.     fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
  913.  
  914.     // przydziela pamięć buforowi obrazu
  915.     bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
  916.  
  917.     // sprawdza, czy udało się przydzielić pamięć
  918.     if (!bitmapImage)
  919.     {
  920.         free(bitmapImage);
  921.         fclose(filePtr);
  922.         return NULL;
  923.     }
  924.  
  925.     // wczytuje dane obrazu
  926.     fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
  927.  
  928.     // sprawdza, czy dane zostały wczytane
  929.     if (bitmapImage == NULL)
  930.     {
  931.         fclose(filePtr);
  932.         return NULL;
  933.     }
  934.  
  935.     // zamienia miejscami składowe R i B
  936.     for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
  937.     {
  938.         tempRGB = bitmapImage[imageIdx];
  939.         bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
  940.         bitmapImage[imageIdx + 2] = tempRGB;
  941.     }
  942.  
  943.     // zamyka plik i zwraca wskaźnik bufora zawierającego wczytany obraz
  944.     fclose(filePtr);
  945.     return bitmapImage;
  946. }
  947. //void gluLookAt(GLdouble eyeX,
  948. //  GLdouble eyeY,
  949. //  GLdouble eyeZ,
  950. //  GLdouble centerX,
  951. //  GLdouble centerY,
  952. //  GLdouble centerZ,
  953. //  GLdouble upX,
  954. //  GLdouble upY,
  955. //  GLdouble upZ);
  956.  
  957. // Called to draw scene
  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.     //Uzyskanie siatki:
  1001.     //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  1002.  
  1003.     //Wyrysowanie prostokata:
  1004.     //glRectd(-10.0, -10.0, 20.0, 20.0);
  1005.  
  1006.     /////////////////////////////////////////////////////////////////
  1007.     /////////////////////////////////////////////////////////////////
  1008.     /////////////////////////////////////////////////////////////////
  1009.     glPopMatrix();
  1010.     glMatrixMode(GL_MODELVIEW);
  1011.  
  1012.     // Flush drawing commands
  1013.     glFlush();
  1014. }
  1015.  
  1016.  
  1017. // Select the pixel format for a given device context
  1018. void SetDCPixelFormat(HDC hDC)
  1019. {
  1020.     int nPixelFormat;
  1021.  
  1022.     static PIXELFORMATDESCRIPTOR pfd = {
  1023.         sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  1024.         1,                                                              // Version of this structure    
  1025.         PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  1026.         PFD_SUPPORT_OPENGL |                    // Support OpenGL calls in window
  1027.         PFD_DOUBLEBUFFER,                       // Double buffered
  1028.         PFD_TYPE_RGBA,                          // RGBA Color mode
  1029.         24,                                     // Want 24bit color
  1030.         0,0,0,0,0,0,                            // Not used to select mode
  1031.         0,0,                                    // Not used to select mode
  1032.         0,0,0,0,0,                              // Not used to select mode
  1033.         32,                                     // Size of depth buffer
  1034.         0,                                      // Not used to select mode
  1035.         0,                                      // Not used to select mode
  1036.         PFD_MAIN_PLANE,                         // Draw in main plane
  1037.         0,                                      // Not used to select mode
  1038.         0,0,0 };                                // Not used to select mode
  1039.  
  1040.                                                 // Choose a pixel format that best matches that described in pfd
  1041.     nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  1042.  
  1043.     // Set the pixel format for the device context
  1044.     SetPixelFormat(hDC, nPixelFormat, &pfd);
  1045. }
  1046.  
  1047.  
  1048.  
  1049. // If necessary, creates a 3-3-2 palette for the device context listed.
  1050. HPALETTE GetOpenGLPalette(HDC hDC)
  1051. {
  1052.     HPALETTE hRetPal = NULL;    // Handle to palette to be created
  1053.     PIXELFORMATDESCRIPTOR pfd;  // Pixel Format Descriptor
  1054.     LOGPALETTE *pPal;           // Pointer to memory for logical palette
  1055.     int nPixelFormat;           // Pixel format index
  1056.     int nColors;                // Number of entries in palette
  1057.     int i;                      // Counting variable
  1058.     BYTE RedRange, GreenRange, BlueRange;
  1059.     // Range for each color entry (7,7,and 3)
  1060.  
  1061.  
  1062.     // Get the pixel format index and retrieve the pixel format description
  1063.     nPixelFormat = GetPixelFormat(hDC);
  1064.     DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  1065.  
  1066.     // Does this pixel format require a palette?  If not, do not create a
  1067.     // palette and just return NULL
  1068.     if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  1069.         return NULL;
  1070.  
  1071.     // Number of entries in palette.  8 bits yeilds 256 entries
  1072.     nColors = 1 << pfd.cColorBits;
  1073.  
  1074.     // Allocate space for a logical palette structure plus all the palette entries
  1075.     pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  1076.  
  1077.     // Fill in palette header
  1078.     pPal->palVersion = 0x300;       // Windows 3.0
  1079.     pPal->palNumEntries = nColors; // table size
  1080.  
  1081.                                    // Build mask of all 1's.  This creates a number represented by having
  1082.                                    // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  1083.                                    // pfd.cBlueBits.  
  1084.     RedRange = (1 << pfd.cRedBits) - 1;
  1085.     GreenRange = (1 << pfd.cGreenBits) - 1;
  1086.     BlueRange = (1 << pfd.cBlueBits) - 1;
  1087.  
  1088.     // Loop through all the palette entries
  1089.     for (i = 0; i < nColors; i++)
  1090.     {
  1091.         // Fill in the 8-bit equivalents for each component
  1092.         pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  1093.         pPal->palPalEntry[i].peRed = (unsigned char)(
  1094.             (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  1095.  
  1096.         pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  1097.         pPal->palPalEntry[i].peGreen = (unsigned char)(
  1098.             (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  1099.  
  1100.         pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  1101.         pPal->palPalEntry[i].peBlue = (unsigned char)(
  1102.             (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  1103.  
  1104.         pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  1105.     }
  1106.  
  1107.  
  1108.     // Create the palette
  1109.     hRetPal = CreatePalette(pPal);
  1110.  
  1111.     // Go ahead and select and realize the palette for this device context
  1112.     SelectPalette(hDC, hRetPal, FALSE);
  1113.     RealizePalette(hDC);
  1114.  
  1115.     // Free the memory used for the logical palette structure
  1116.     free(pPal);
  1117.  
  1118.     // Return the handle to the new palette
  1119.     return hRetPal;
  1120. }
  1121.  
  1122.  
  1123. // Entry point of all Windows programs
  1124. int APIENTRY WinMain(HINSTANCE       hInst,
  1125.     HINSTANCE       hPrevInstance,
  1126.     LPSTR           lpCmdLine,
  1127.     int                     nCmdShow)
  1128. {
  1129.     MSG                     msg;            // Windows message structure
  1130.     WNDCLASS        wc;                     // Windows class structure
  1131.     HWND            hWnd;           // Storeage for window handle
  1132.  
  1133.     hInstance = hInst;
  1134.  
  1135.     // Register Window style
  1136.     wc.style = CS_HREDRAW | CS_VREDRAW;
  1137.     wc.lpfnWndProc = (WNDPROC)WndProc;
  1138.     wc.cbClsExtra = 0;
  1139.     wc.cbWndExtra = 0;
  1140.     wc.hInstance = hInstance;
  1141.     wc.hIcon = NULL;
  1142.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  1143.  
  1144.     // No need for background brush for OpenGL window
  1145.     wc.hbrBackground = NULL;
  1146.  
  1147.     wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  1148.     wc.lpszClassName = lpszAppName;
  1149.  
  1150.     // Register the window class
  1151.     if (RegisterClass(&wc) == 0)
  1152.         return FALSE;
  1153.  
  1154.  
  1155.     // Create the main application window
  1156.     hWnd = CreateWindow(
  1157.         lpszAppName,
  1158.         lpszAppName,
  1159.  
  1160.         // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  1161.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  1162.  
  1163.         // Window position and size
  1164.         50, 50,
  1165.         400, 400,
  1166.         NULL,
  1167.         NULL,
  1168.         hInstance,
  1169.         NULL);
  1170.  
  1171.     // If window was not created, quit
  1172.     if (hWnd == NULL)
  1173.         return FALSE;
  1174.  
  1175.  
  1176.     // Display the window
  1177.     ShowWindow(hWnd, SW_SHOW);
  1178.     UpdateWindow(hWnd);
  1179.  
  1180.     // Process application messages until the application closes
  1181.     while (GetMessage(&msg, NULL, 0, 0))
  1182.     {
  1183.         TranslateMessage(&msg);
  1184.         DispatchMessage(&msg);
  1185.     }
  1186.  
  1187.     return msg.wParam;
  1188. }
  1189.  
  1190.  
  1191.  
  1192.  
  1193. // Window procedure, handles all messages for this program
  1194. LRESULT CALLBACK WndProc(HWND    hWnd,
  1195.     UINT    message,
  1196.     WPARAM  wParam,
  1197.     LPARAM  lParam)
  1198. {
  1199.     static HGLRC hRC;               // Permenant Rendering context
  1200.     static HDC hDC;                 // Private GDI Device context
  1201.     int licznik = 0;
  1202.     switch (message)
  1203.     {
  1204.     case WM_TIMER:
  1205.     {
  1206.         if (wParam == 101)
  1207.         {
  1208.             licznik++;
  1209.             if (licznik < 15)
  1210.             {
  1211.                 rot1 += 1.0;
  1212.                 rot2 += 10.0;
  1213.             }
  1214.             if (licznik > 15 && licznik < 30)
  1215.             {
  1216.                 rot1 -= 3.0;
  1217.                 rot2 -= 10.0;
  1218.             }
  1219.             if (licznik>30)
  1220.             {
  1221.                 licznik = 0;
  1222.             }
  1223.             InvalidateRect(hWnd, NULL, FALSE);
  1224.         }
  1225.         break;
  1226.             /*rot1 += 4.0;
  1227.             rot2 += 4.0;
  1228.             rot3 += 8;
  1229.             if (licznik>10.0 && licznik < 100.0)
  1230.                 rot1 -= 6.0;
  1231.             rot2 -= 6.0;
  1232.             rot3 -= 6.0;*/
  1233.             /*licznik++;
  1234.             if (licznik<2.0)
  1235.             rot1 += 1.0;
  1236.             rot2 += 0.25;
  1237.             rot3 += 1.0;
  1238.             if (licznik>2.0 && licznik < 60.0)
  1239.             rot1 -= 3.0;
  1240.             rot2 -= 5.0;
  1241.             rot3 -= 3.0;
  1242.             if (licznik>60.0)
  1243.             {
  1244.                 licznik = 0.0;
  1245.             }
  1246.             InvalidateRect(hWnd, NULL, FALSE);
  1247.         }
  1248.         break;*/
  1249.     }
  1250.         // Window creation, setup for OpenGL
  1251.     case WM_CREATE:
  1252.         // Store the device context
  1253.         hDC = GetDC(hWnd);
  1254.  
  1255.         // Select the pixel format
  1256.         SetDCPixelFormat(hDC);
  1257.         SetTimer(hWnd, 101, 1, NULL);
  1258.  
  1259.         // Create palette if needed
  1260.         hPalette = GetOpenGLPalette(hDC);
  1261.  
  1262.         // Create the rendering context and make it current
  1263.         hRC = wglCreateContext(hDC);
  1264.         wglMakeCurrent(hDC, hRC);
  1265.         SetupRC();
  1266.         glGenTextures(2, &texture[0]);                  // tworzy obiekt tekstury          
  1267.  
  1268.                                                         // ładuje pierwszy obraz tekstury:
  1269.         bitmapData = LoadBitmapFile("Bitmapy\\checker.bmp", &bitmapInfoHeader);
  1270.  
  1271.         glBindTexture(GL_TEXTURE_2D, texture[0]);       // aktywuje obiekt tekstury
  1272.  
  1273.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1274.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1275.  
  1276.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  1277.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  1278.  
  1279.         // tworzy obraz tekstury
  1280.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  1281.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  1282.  
  1283.         if (bitmapData)
  1284.             free(bitmapData);
  1285.  
  1286.         // ładuje drugi obraz tekstury:
  1287.         bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  1288.         glBindTexture(GL_TEXTURE_2D, texture[1]);       // aktywuje obiekt tekstury
  1289.  
  1290.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1291.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1292.  
  1293.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  1294.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  1295.  
  1296.         // tworzy obraz tekstury
  1297.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  1298.             bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  1299.  
  1300.         if (bitmapData)
  1301.             free(bitmapData);
  1302.  
  1303.         // ustalenie sposobu mieszania tekstury z tłem
  1304.         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  1305.         break;
  1306.  
  1307.         // Window is being destroyed, cleanup
  1308.     case WM_DESTROY:
  1309.         // Deselect the current rendering context and delete it
  1310.         wglMakeCurrent(hDC, NULL);
  1311.         wglDeleteContext(hRC);
  1312.         KillTimer(hWnd, 101);
  1313.  
  1314.         // Delete the palette if it was created
  1315.         if (hPalette != NULL)
  1316.             DeleteObject(hPalette);
  1317.  
  1318.         // Tell the application to terminate after the window
  1319.         // is gone.
  1320.         PostQuitMessage(0);
  1321.         break;
  1322.  
  1323.         // Window is resized.
  1324.     case WM_SIZE:
  1325.         // Call our function which modifies the clipping
  1326.         // volume and viewport
  1327.         ChangeSize(LOWORD(lParam), HIWORD(lParam));
  1328.         break;
  1329.  
  1330.  
  1331.         // The painting function.  This message sent by Windows
  1332.         // whenever the screen needs updating.
  1333.     case WM_PAINT:
  1334.     {
  1335.         // Call OpenGL drawing code
  1336.         RenderScene();
  1337.  
  1338.         SwapBuffers(hDC);
  1339.  
  1340.         // Validate the newly painted client area
  1341.         ValidateRect(hWnd, NULL);
  1342.     }
  1343.     break;
  1344.  
  1345.     // Windows is telling the application that it may modify
  1346.     // the system palette.  This message in essance asks the
  1347.     // application for a new palette.
  1348.     case WM_QUERYNEWPALETTE:
  1349.         // If the palette was created.
  1350.         if (hPalette)
  1351.         {
  1352.             int nRet;
  1353.  
  1354.             // Selects the palette into the current device context
  1355.             SelectPalette(hDC, hPalette, FALSE);
  1356.  
  1357.             // Map entries from the currently selected palette to
  1358.             // the system palette.  The return value is the number
  1359.             // of palette entries modified.
  1360.             nRet = RealizePalette(hDC);
  1361.  
  1362.             // Repaint, forces remap of palette in current window
  1363.             InvalidateRect(hWnd, NULL, FALSE);
  1364.  
  1365.             return nRet;
  1366.         }
  1367.         break;
  1368.  
  1369.  
  1370.         // This window may set the palette, even though it is not the
  1371.         // currently active window.
  1372.     case WM_PALETTECHANGED:
  1373.         // Don't do anything if the palette does not exist, or if
  1374.         // this is the window that changed the palette.
  1375.         if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  1376.         {
  1377.             // Select the palette into the device context
  1378.             SelectPalette(hDC, hPalette, FALSE);
  1379.  
  1380.             // Map entries to system palette
  1381.             RealizePalette(hDC);
  1382.  
  1383.             // Remap the current colors to the newly realized palette
  1384.             UpdateColors(hDC);
  1385.             return 0;
  1386.         }
  1387.         break;
  1388.  
  1389.         // Key press, check for arrow keys to do cube rotation.
  1390.     case WM_KEYDOWN:
  1391.     {
  1392.         if (wParam == VK_UP)
  1393.             xRot -= 5.0f;
  1394.         if (wParam == VK_DOWN)
  1395.             xRot += 5.0f;
  1396.         if (wParam == VK_LEFT)
  1397.             yRot -= 5.0f;
  1398.         if (wParam == VK_RIGHT)
  1399.             yRot += 5.0f;
  1400.         if (wParam == '1')
  1401.             rot1 -=5.0f;
  1402.         if (wParam == '2')
  1403.             rot1 += 5.0f;
  1404.         if (wParam == '3')
  1405.             rot2 -= 5.0f;
  1406.         if (wParam == '4')
  1407.             rot2 += 5.0f;
  1408.         if (wParam == '5')
  1409.             rot3 -= 5.0f;
  1410.         if (wParam == '6')
  1411.             rot3 += 5.0f;
  1412.  
  1413.         xRot = (const int)xRot % 360;
  1414.         yRot = (const int)yRot % 360;
  1415.         rot1 = (const int)rot1 % 360;
  1416.         rot2 = (const int)rot2 % 360;
  1417.         rot3 = (const int)rot3 % 360;
  1418.        
  1419.  
  1420.         InvalidateRect(hWnd, NULL, FALSE);
  1421.     }
  1422.     break;
  1423.  
  1424.     // A menu command
  1425.     case WM_COMMAND:
  1426.     {
  1427.         switch (LOWORD(wParam))
  1428.         {
  1429.             // Exit the program
  1430.         case ID_FILE_EXIT:
  1431.             DestroyWindow(hWnd);
  1432.             break;
  1433.         }
  1434.     }
  1435.     break;
  1436.  
  1437.  
  1438.     default:   // Passes it on if unproccessed
  1439.         return (DefWindowProc(hWnd, message, wParam, lParam));
  1440.  
  1441.     }
  1442.  
  1443.     return (0L);
  1444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement