Advertisement
Guest User

Untitled

a guest
May 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.26 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.  
  360. void szescian(void) {
  361. glBegin(GL_QUADS);
  362. glColor3d(1, 0.5, 0);
  363. glVertex3d(20, 20, 20);
  364. glVertex3d(-20, 20, 20);
  365. glVertex3d(-20, -20, 20);
  366. glVertex3d(20, -20, 20);
  367. glColor3d(0, 0.5, 1);
  368. glVertex3d(20, 20, 20);
  369. glVertex3d(20, -20, 20);
  370. glVertex3d(20, -20, -20);
  371. glVertex3d(20, 20, -20);
  372. glEnd();
  373. }
  374.  
  375. void walec(double r, double h)
  376. {
  377. glColor3d(0, 0.5, 1);
  378. double angle, x, y;
  379. glBegin(GL_TRIANGLE_FAN);
  380. glVertex3d(0.0f, 0.0f, 0.0f);
  381. for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  382. {
  383. x = r*sin(angle);
  384. y = r*cos(angle);
  385. glVertex3d(x, y, 0.0);
  386. }
  387. glEnd();
  388. glBegin(GL_QUAD_STRIP);
  389. for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  390. {
  391. x = r*sin(angle);
  392. y = r*cos(angle);
  393. glVertex3d(x, y, 0);
  394. glVertex3d(x, y, h);
  395. }
  396. glEnd();
  397.  
  398. glColor3d(1, 0.5, 0);
  399.  
  400. glBegin(GL_TRIANGLE_FAN);
  401. glVertex3d(0.0f, 0.0f, h);
  402. for (angle = 0.0f; angle >= (-2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  403. {
  404. x = r*sin(angle);
  405. y = r*cos(angle);
  406. glVertex3d(x, y, h);
  407. }
  408. glEnd();
  409. }
  410.  
  411. void ramie(double r1, double r2, double h, double d)
  412. {
  413.  
  414. glColor3d(0, 0.5, 1);
  415. double angle, x, y;
  416.  
  417. glBegin(GL_TRIANGLE_FAN);
  418. glVertex3d(0.0f, 0.0f, 0.0f);
  419. for (angle = 1.0f*GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  420. {
  421. x = r1*sin(angle);
  422. y = r1*cos(angle);
  423. glVertex3d(x, y, 0.0);
  424. }
  425. glEnd();
  426. glColor3d(0, 0.5, 1);
  427. glBegin(GL_QUAD_STRIP);
  428. for (angle = 1.0f*GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
  429. {
  430. x = r1*sin(angle);
  431. y = r1*cos(angle);
  432. glVertex3d(x, y, 0);
  433. glVertex3d(x, y, h);
  434. }
  435. glEnd();
  436.  
  437. glColor3d(0, 0.5, 1);
  438.  
  439. glBegin(GL_TRIANGLE_FAN);
  440. glVertex3d(0.0f, 0.0f, h);
  441. for (angle = 0.0f; angle >= (-1.0f*GL_PI); angle -= (GL_PI / 8.0f))
  442. {
  443. x = r1*sin(angle);
  444. y = r1*cos(angle);
  445. glVertex3d(x, y, h);
  446. }
  447. glEnd();
  448.  
  449.  
  450.  
  451.  
  452. glColor3d(0, 0.5, 1);
  453.  
  454. glBegin(GL_TRIANGLE_FAN);
  455. glVertex3d(0.0f + d, 0.0f, 0.0f);
  456. for (angle = 0.0f*GL_PI; angle <= (1.0f*GL_PI); angle += (GL_PI / 8.0f))
  457. {
  458. x = r2*sin(angle);
  459. y = r2*cos(angle);
  460. glVertex3d(x + d, y, 0.0);
  461. }
  462. glEnd();
  463. glBegin(GL_QUAD_STRIP);
  464. for (angle = 0.0f*GL_PI; angle <= (1.0f*GL_PI); angle += (GL_PI / 8.0f))
  465. {
  466. x = r2*sin(angle);
  467. y = r2*cos(angle);
  468. glVertex3d(x + d, y, 0);
  469. glVertex3d(x + d, y, h);
  470. }
  471. glEnd();
  472.  
  473. glColor3d(0, 0.5, 1);
  474.  
  475. glBegin(GL_TRIANGLE_FAN);
  476. glVertex3d(0.0f + d, 0.0f, h);
  477. for (angle = (-1.0f*GL_PI); angle >= (-2.0f*GL_PI); angle -= (GL_PI / 8.0f))
  478. {
  479. x = r2*sin(angle);
  480. y = r2*cos(angle);
  481. glVertex3d(x + d, y, h);
  482. }
  483. glEnd();
  484.  
  485. ////////////////////////////
  486. glBegin(GL_QUADS);
  487. glColor3d(4, 0.5, 0);
  488. glVertex3d(0, -r1, h);
  489. glVertex3d(d, -r2, h);
  490. glVertex3d(d, r2, h);
  491. glVertex3d(0, r1, h);
  492.  
  493.  
  494. glColor3d(4, 0.5, 0);
  495. glVertex3d(0, r1, 0);
  496. glVertex3d(d, r2, 0);
  497. glVertex3d(d, -r2, 0);
  498. glVertex3d(0, -r1, 0);
  499.  
  500. glColor3d(5, 2, 0);
  501. glVertex3d(0, r1, 0);
  502. glVertex3d(0, r1, h);
  503. glVertex3d(d, r2, h);
  504. glVertex3d(d, r2, 0);
  505.  
  506. glColor3d(5, 2, 0);
  507. glVertex3d(0, -r1, 0);
  508. glVertex3d(d, -r2, 0);
  509. glVertex3d(d, -r2, h);
  510. glVertex3d(0, -r1, h);
  511. glEnd();
  512. }
  513.  
  514. // Called to draw scene
  515. void RenderScene(void)
  516. {
  517.  
  518.  
  519.  
  520. //float normal[3]; // Storeage for calculated surface normal
  521.  
  522. // Clear the window with current clearing color
  523. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  524.  
  525. // Save the matrix state and do the rotations
  526. glPushMatrix();
  527. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  528. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  529.  
  530. /////////////////////////////////////////////////////////////////
  531. // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN: //
  532. /////////////////////////////////////////////////////////////////
  533.  
  534. //szescian();
  535. //walec(20, 30);
  536. ramie(15, 10, 30, 52);
  537.  
  538.  
  539. //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
  540. glPolygonMode(GL_BACK, GL_LINE);
  541.  
  542. //Uzyskanie siatki:
  543. //glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  544.  
  545. //Wyrysowanie prostokata:
  546. //glRectd(-10.0, -10.0, 20.0, 20.0);
  547.  
  548. /////////////////////////////////////////////////////////////////
  549. /////////////////////////////////////////////////////////////////
  550. /////////////////////////////////////////////////////////////////
  551. glPopMatrix();
  552. glMatrixMode(GL_MODELVIEW);
  553.  
  554. // Flush drawing commands
  555. glFlush();
  556. }
  557.  
  558.  
  559. // Select the pixel format for a given device context
  560. void SetDCPixelFormat(HDC hDC)
  561. {
  562. int nPixelFormat;
  563.  
  564. static PIXELFORMATDESCRIPTOR pfd = {
  565. sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
  566. 1, // Version of this structure
  567. PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
  568. PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
  569. PFD_DOUBLEBUFFER, // Double buffered
  570. PFD_TYPE_RGBA, // RGBA Color mode
  571. 24, // Want 24bit color
  572. 0,0,0,0,0,0, // Not used to select mode
  573. 0,0, // Not used to select mode
  574. 0,0,0,0,0, // Not used to select mode
  575. 32, // Size of depth buffer
  576. 0, // Not used to select mode
  577. 0, // Not used to select mode
  578. PFD_MAIN_PLANE, // Draw in main plane
  579. 0, // Not used to select mode
  580. 0,0,0 }; // Not used to select mode
  581.  
  582. // Choose a pixel format that best matches that described in pfd
  583. nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  584.  
  585. // Set the pixel format for the device context
  586. SetPixelFormat(hDC, nPixelFormat, &pfd);
  587. }
  588.  
  589.  
  590.  
  591. // If necessary, creates a 3-3-2 palette for the device context listed.
  592. HPALETTE GetOpenGLPalette(HDC hDC)
  593. {
  594. HPALETTE hRetPal = NULL; // Handle to palette to be created
  595. PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
  596. LOGPALETTE *pPal; // Pointer to memory for logical palette
  597. int nPixelFormat; // Pixel format index
  598. int nColors; // Number of entries in palette
  599. int i; // Counting variable
  600. BYTE RedRange, GreenRange, BlueRange;
  601. // Range for each color entry (7,7,and 3)
  602.  
  603.  
  604. // Get the pixel format index and retrieve the pixel format description
  605. nPixelFormat = GetPixelFormat(hDC);
  606. DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  607.  
  608. // Does this pixel format require a palette? If not, do not create a
  609. // palette and just return NULL
  610. if (!(pfd.dwFlags & PFD_NEED_PALETTE))
  611. return NULL;
  612.  
  613. // Number of entries in palette. 8 bits yeilds 256 entries
  614. nColors = 1 << pfd.cColorBits;
  615.  
  616. // Allocate space for a logical palette structure plus all the palette entries
  617. pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
  618.  
  619. // Fill in palette header
  620. pPal->palVersion = 0x300; // Windows 3.0
  621. pPal->palNumEntries = nColors; // table size
  622.  
  623. // Build mask of all 1's. This creates a number represented by having
  624. // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  625. // pfd.cBlueBits.
  626. RedRange = (1 << pfd.cRedBits) - 1;
  627. GreenRange = (1 << pfd.cGreenBits) - 1;
  628. BlueRange = (1 << pfd.cBlueBits) - 1;
  629.  
  630. // Loop through all the palette entries
  631. for (i = 0; i < nColors; i++)
  632. {
  633. // Fill in the 8-bit equivalents for each component
  634. pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  635. pPal->palPalEntry[i].peRed = (unsigned char)(
  636. (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  637.  
  638. pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  639. pPal->palPalEntry[i].peGreen = (unsigned char)(
  640. (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  641.  
  642. pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  643. pPal->palPalEntry[i].peBlue = (unsigned char)(
  644. (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  645.  
  646. pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
  647. }
  648.  
  649.  
  650. // Create the palette
  651. hRetPal = CreatePalette(pPal);
  652.  
  653. // Go ahead and select and realize the palette for this device context
  654. SelectPalette(hDC, hRetPal, FALSE);
  655. RealizePalette(hDC);
  656.  
  657. // Free the memory used for the logical palette structure
  658. free(pPal);
  659.  
  660. // Return the handle to the new palette
  661. return hRetPal;
  662. }
  663.  
  664.  
  665. // Entry point of all Windows programs
  666. int APIENTRY WinMain(HINSTANCE hInst,
  667. HINSTANCE hPrevInstance,
  668. LPSTR lpCmdLine,
  669. int nCmdShow)
  670. {
  671. MSG msg; // Windows message structure
  672. WNDCLASS wc; // Windows class structure
  673. HWND hWnd; // Storeage for window handle
  674.  
  675. hInstance = hInst;
  676.  
  677. // Register Window style
  678. wc.style = CS_HREDRAW | CS_VREDRAW;
  679. wc.lpfnWndProc = (WNDPROC)WndProc;
  680. wc.cbClsExtra = 0;
  681. wc.cbWndExtra = 0;
  682. wc.hInstance = hInstance;
  683. wc.hIcon = NULL;
  684. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  685.  
  686. // No need for background brush for OpenGL window
  687. wc.hbrBackground = NULL;
  688.  
  689. wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  690. wc.lpszClassName = lpszAppName;
  691.  
  692. // Register the window class
  693. if (RegisterClass(&wc) == 0)
  694. return FALSE;
  695.  
  696.  
  697. // Create the main application window
  698. hWnd = CreateWindow(
  699. lpszAppName,
  700. lpszAppName,
  701.  
  702. // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  703. WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  704.  
  705. // Window position and size
  706. 50, 50,
  707. 400, 400,
  708. NULL,
  709. NULL,
  710. hInstance,
  711. NULL);
  712.  
  713. // If window was not created, quit
  714. if (hWnd == NULL)
  715. return FALSE;
  716.  
  717.  
  718. // Display the window
  719. ShowWindow(hWnd, SW_SHOW);
  720. UpdateWindow(hWnd);
  721.  
  722. // Process application messages until the application closes
  723. while (GetMessage(&msg, NULL, 0, 0))
  724. {
  725. TranslateMessage(&msg);
  726. DispatchMessage(&msg);
  727. }
  728.  
  729. return msg.wParam;
  730. }
  731.  
  732.  
  733.  
  734.  
  735. // Window procedure, handles all messages for this program
  736. LRESULT CALLBACK WndProc(HWND hWnd,
  737. UINT message,
  738. WPARAM wParam,
  739. LPARAM lParam)
  740. {
  741. static HGLRC hRC; // Permenant Rendering context
  742. static HDC hDC; // Private GDI Device context
  743.  
  744. switch (message)
  745. {
  746. // Window creation, setup for OpenGL
  747. case WM_CREATE:
  748. // Store the device context
  749. hDC = GetDC(hWnd);
  750.  
  751. // Select the pixel format
  752. SetDCPixelFormat(hDC);
  753.  
  754. // Create palette if needed
  755. hPalette = GetOpenGLPalette(hDC);
  756.  
  757. // Create the rendering context and make it current
  758. hRC = wglCreateContext(hDC);
  759. wglMakeCurrent(hDC, hRC);
  760. SetupRC();
  761. glGenTextures(2, &texture[0]); // tworzy obiekt tekstury
  762.  
  763. // ładuje pierwszy obraz tekstury:
  764. bitmapData = LoadBitmapFile("Bitmapy\\checker.bmp", &bitmapInfoHeader);
  765.  
  766. glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury
  767.  
  768. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  769. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  770.  
  771. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  772. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  773.  
  774. // tworzy obraz tekstury
  775. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  776. bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  777.  
  778. if (bitmapData)
  779. free(bitmapData);
  780.  
  781. // ładuje drugi obraz tekstury:
  782. bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
  783. glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury
  784.  
  785. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  786. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  787.  
  788. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  789. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  790.  
  791. // tworzy obraz tekstury
  792. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
  793. bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
  794.  
  795. if (bitmapData)
  796. free(bitmapData);
  797.  
  798. // ustalenie sposobu mieszania tekstury z tłem
  799. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  800. break;
  801.  
  802. // Window is being destroyed, cleanup
  803. case WM_DESTROY:
  804. // Deselect the current rendering context and delete it
  805. wglMakeCurrent(hDC, NULL);
  806. wglDeleteContext(hRC);
  807.  
  808. // Delete the palette if it was created
  809. if (hPalette != NULL)
  810. DeleteObject(hPalette);
  811.  
  812. // Tell the application to terminate after the window
  813. // is gone.
  814. PostQuitMessage(0);
  815. break;
  816.  
  817. // Window is resized.
  818. case WM_SIZE:
  819. // Call our function which modifies the clipping
  820. // volume and viewport
  821. ChangeSize(LOWORD(lParam), HIWORD(lParam));
  822. break;
  823.  
  824.  
  825. // The painting function. This message sent by Windows
  826. // whenever the screen needs updating.
  827. case WM_PAINT:
  828. {
  829. // Call OpenGL drawing code
  830. RenderScene();
  831.  
  832. SwapBuffers(hDC);
  833.  
  834. // Validate the newly painted client area
  835. ValidateRect(hWnd, NULL);
  836. }
  837. break;
  838.  
  839. // Windows is telling the application that it may modify
  840. // the system palette. This message in essance asks the
  841. // application for a new palette.
  842. case WM_QUERYNEWPALETTE:
  843. // If the palette was created.
  844. if (hPalette)
  845. {
  846. int nRet;
  847.  
  848. // Selects the palette into the current device context
  849. SelectPalette(hDC, hPalette, FALSE);
  850.  
  851. // Map entries from the currently selected palette to
  852. // the system palette. The return value is the number
  853. // of palette entries modified.
  854. nRet = RealizePalette(hDC);
  855.  
  856. // Repaint, forces remap of palette in current window
  857. InvalidateRect(hWnd, NULL, FALSE);
  858.  
  859. return nRet;
  860. }
  861. break;
  862.  
  863.  
  864. // This window may set the palette, even though it is not the
  865. // currently active window.
  866. case WM_PALETTECHANGED:
  867. // Don't do anything if the palette does not exist, or if
  868. // this is the window that changed the palette.
  869. if ((hPalette != NULL) && ((HWND)wParam != hWnd))
  870. {
  871. // Select the palette into the device context
  872. SelectPalette(hDC, hPalette, FALSE);
  873.  
  874. // Map entries to system palette
  875. RealizePalette(hDC);
  876.  
  877. // Remap the current colors to the newly realized palette
  878. UpdateColors(hDC);
  879. return 0;
  880. }
  881. break;
  882.  
  883. // Key press, check for arrow keys to do cube rotation.
  884. case WM_KEYDOWN:
  885. {
  886. if (wParam == VK_UP)
  887. xRot -= 5.0f;
  888.  
  889. if (wParam == VK_DOWN)
  890. xRot += 5.0f;
  891.  
  892. if (wParam == VK_LEFT)
  893. yRot -= 5.0f;
  894.  
  895. if (wParam == VK_RIGHT)
  896. yRot += 5.0f;
  897.  
  898. xRot = (const int)xRot % 360;
  899. yRot = (const int)yRot % 360;
  900.  
  901. InvalidateRect(hWnd, NULL, FALSE);
  902. }
  903. break;
  904.  
  905. // A menu command
  906. case WM_COMMAND:
  907. {
  908. switch (LOWORD(wParam))
  909. {
  910. // Exit the program
  911. case ID_FILE_EXIT:
  912. DestroyWindow(hWnd);
  913. break;
  914. }
  915. }
  916. break;
  917.  
  918.  
  919. default: // Passes it on if unproccessed
  920. return (DefWindowProc(hWnd, message, wParam, lParam));
  921.  
  922. }
  923.  
  924. return (0L);
  925. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement