Advertisement
Guest User

Untitled

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