Advertisement
Guest User

Untitled

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