Advertisement
Guest User

Untitled

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