Advertisement
Guest User

Untitled

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