Advertisement
Guest User

Untitled

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