Advertisement
Guest User

Untitled

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