Advertisement
Guest User

Untitled

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