Advertisement
Guest User

Untitled

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