Advertisement
Guest User

Untitled

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