Advertisement
pabloducato

projekt_openGL_z_teksturami

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