Advertisement
cristiano002

Robocik 3

May 27th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.99 KB | None | 0 0
  1. // gl_teplate.c
  2. // A template program for most WIGGLE applications
  3.  
  4. #include <windows.h> // Window defines
  5. #include <gl\gl.h> // OpenGL
  6. #include <gl\glu.h> // GLU library
  7. #include <math.h> // Define for sine and cosine
  8. #include "resource.h" // About box resource identifiers.
  9.  
  10. // Define a constant for the value of PI
  11. #define GL_PI 3.1415f
  12.  
  13. double rot1,rot2,rot3;
  14. int licznik;
  15.  
  16.  
  17. // Color Palette handle
  18. HPALETTE hPalette = NULL;
  19.  
  20.  
  21. // Application name and instance storeage
  22. static LPCTSTR lpszAppName = "GL TEMPLATE";
  23. static HINSTANCE hInstance;
  24.  
  25. // Rotation amounts
  26. static GLfloat xRot = 0.0f;
  27. static GLfloat yRot = 0.0f;
  28.  
  29.  
  30. // Declaration for Window procedure
  31. LRESULT CALLBACK WndProc( HWND hWnd,
  32. UINT message,
  33. WPARAM wParam,
  34. LPARAM lParam);
  35.  
  36. // Dialog procedure for about box
  37. BOOL APIENTRY AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam);
  38.  
  39. // Set Pixel Format function - forward declaration
  40. void SetDCPixelFormat(HDC hDC);
  41.  
  42.  
  43. // Change viewing volume and viewport. Called when window is resized
  44. void ChangeSize(GLsizei w, GLsizei h)
  45. {
  46. GLfloat nRange = 100.0f;
  47.  
  48. // Prevent a divide by zero
  49. if(h == 0)
  50. h = 1;
  51.  
  52. // Set Viewport to window dimensions
  53. glViewport(0, 0, w, h);
  54.  
  55. // Reset coordinate system
  56. glMatrixMode(GL_PROJECTION);
  57. glLoadIdentity();
  58.  
  59. // Establish clipping volume (left, right, bottom, top, near, far)
  60. if (w <= h)
  61. glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
  62. else
  63. glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
  64.  
  65. glMatrixMode(GL_MODELVIEW);
  66. glLoadIdentity();
  67. }
  68.  
  69.  
  70.  
  71. // This function does any needed initialization on the rendering
  72. // context.
  73. void SetupRC()
  74. {
  75. // STARY KOD
  76. // Light values and coordinates
  77. GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  78. GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  79. GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
  80. GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
  81. GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  82.  
  83.  
  84. glEnable(GL_DEPTH_TEST); // Hidden surface removal
  85. glFrontFace(GL_CCW); // Counter clock-wise polygons face out
  86. glEnable(GL_CULL_FACE); // Do not calculate inside of jet
  87.  
  88. // Enable lighting
  89. glEnable(GL_LIGHTING);
  90.  
  91. // Setup and enable light 0
  92. glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  93. glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  94. glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  95. glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  96. glEnable(GL_LIGHT0);
  97.  
  98. // Enable color tracking
  99. glEnable(GL_COLOR_MATERIAL);
  100.  
  101. // Set Material properties to follow glColor values
  102. glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  103.  
  104. // All materials hereafter have full specular reflectivity
  105. // with a high shine
  106. glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
  107. glMateriali(GL_FRONT,GL_SHININESS,128);
  108.  
  109.  
  110. // White background
  111. glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
  112. // Black brush
  113. glColor3f(0.0,0.0,0.0);
  114. //STARY KOD - KONIEC
  115.  
  116.  
  117.  
  118.  
  119.  
  120. }
  121.  
  122. void walec(double h, double r)
  123. {
  124. double angle,x,y;
  125. // podstawa cz 1
  126. glBegin(GL_TRIANGLE_FAN);
  127. glNormal3d(0,0,-1);
  128. glVertex3d(0.0f, 0.0f, 0.0f);
  129. for(angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI/8.0f))
  130. {
  131. x = r*sin(angle);
  132. y = r*cos(angle);
  133. glVertex3d(x, y, 0.0);
  134. }
  135. glEnd();
  136.  
  137. // łączenie
  138.  
  139. glBegin(GL_QUAD_STRIP);
  140. for(angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI/8.0f))
  141. {
  142. x = r*sin(angle);
  143. y = r*cos(angle);
  144. glNormal3d(sin(angle),cos(angle),0.0);
  145. glVertex3d(x, y, h);
  146. glVertex3d(x, y, 0);
  147. }
  148. glEnd();
  149.  
  150. // podstawa cz 2
  151. glBegin(GL_TRIANGLE_FAN);
  152. glNormal3d(0,0,1);
  153. glVertex3d(0.0f, 0.0f, h);
  154. for(angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI/8.0f))
  155. {
  156. x = r*sin(angle);
  157. y = r*cos(angle);
  158. glVertex3d(x, y, h);
  159. }
  160. glEnd();
  161.  
  162.  
  163. }
  164.  
  165.  
  166. void szescian(void)
  167. {
  168. glBegin(GL_QUADS);
  169.  
  170.  
  171. glColor3d(1,0.5,0);
  172. //pomarancz
  173. glNormal3d(0,0,1);
  174. glVertex3d( 20, 20, 20);
  175. glVertex3d(-20, 20, 20);
  176. glVertex3d(-20,-20, 20);
  177. glVertex3d( 20,-20, 20);
  178.  
  179. glColor3d(0,1,0);
  180. glNormal3d(0,-1,0);
  181. //zielony
  182. glVertex3d(-20,-20, 20);
  183. glVertex3d(-20,-20,-20);
  184. glVertex3d( 20,-20,-20);
  185. glVertex3d( 20,-20, 20);
  186.  
  187. glColor3d(0,0.5,1);
  188. glNormal3d(1,0,0);
  189. //niebieski
  190. glVertex3d( 20, 20, 20);
  191. glVertex3d( 20,-20, 20);
  192. glVertex3d( 20,-20,-20);
  193. glVertex3d( 20, 20,-20);
  194.  
  195. glColor3d(0,1,0);
  196. glNormal3d(0,1,0);
  197. //zielony
  198. glVertex3d(-20, 20,-20);
  199. glVertex3d(-20, 20, 20);
  200. glVertex3d( 20, 20, 20);
  201. glVertex3d( 20, 20,-20);
  202.  
  203. glColor3d(1,0.5,0);
  204. glNormal3d(0,0,-1);
  205. //pomarancz
  206. glVertex3d( 20, 20,-20);
  207. glVertex3d( 20,-20,-20);
  208. glVertex3d(-20,-20,-20);
  209. glVertex3d(-20, 20,-20);
  210.  
  211. glColor3d(0,0.5,1);
  212. glNormal3d(-1,0,0);
  213. //niebieski
  214. glVertex3d(-20,-20,-20);
  215. glVertex3d(-20,-20, 20);
  216. glVertex3d(-20, 20, 20);
  217. glVertex3d(-20, 20,-20);
  218.  
  219. glEnd();
  220. }
  221.  
  222. void ramie(double r1, double r2, double h, double d)
  223. {
  224. double angle,x,y;
  225. glBegin(GL_TRIANGLE_FAN);
  226. glNormal3d(0,0,1);
  227. glVertex3d(0.0f, 0.0f, 0.0f);
  228. for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  229. {
  230. x = r1*sin(angle);
  231. y = r1*cos(angle);
  232. glVertex3d(x, y, 0.0);
  233. }
  234. glEnd();
  235.  
  236. glBegin(GL_QUAD_STRIP);
  237. for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  238. {
  239. x = r1*sin(angle);
  240. y = r1*cos(angle);
  241. glNormal3d(sin(angle),cos(angle),0.0);
  242. glVertex3d(-x, -y, h);
  243. glVertex3d(-x, -y, 0);
  244. }
  245. glEnd();
  246.  
  247. glBegin(GL_TRIANGLE_FAN);
  248. glNormal3d(0.0,0.0,1.0);
  249. glVertex3d(0.0f, 0.0f, h);
  250. for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  251. {
  252. x = r1*sin(angle);
  253. y = r1*cos(angle);
  254. glVertex3d(-x, -y, h);
  255. }
  256. glEnd();
  257.  
  258. ////////////////////////////////////////////////////////////////////
  259.  
  260. glBegin(GL_TRIANGLE_FAN);
  261. glNormal3d(0,0,1);
  262. glVertex3d(0.0f - d, 0.0f, 0.0f);
  263. for(angle = 0.0f; angle <= (2.0f*GL_PI)/2; angle += (GL_PI/8.0f))
  264. {
  265. x = -r2*sin(angle);
  266. y = -r2*cos(angle);
  267. glVertex3d(x - d, y, 0.0);
  268. }
  269. glEnd();
  270.  
  271. glBegin(GL_QUAD_STRIP);
  272. for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  273. {
  274. x = r2*sin(angle);
  275. y = r2*cos(angle);
  276. glNormal3d(sin(angle),cos(angle),0.0);
  277. glVertex3d(x-d, y, h);
  278. glVertex3d(x-d, y, 0);
  279. }
  280. glEnd();
  281.  
  282. glBegin(GL_TRIANGLE_FAN);
  283. glVertex3d(0.0f -d , 0.0f, h);
  284. glNormal3d(0.0,0.0,1.0);
  285. for(angle = 0.0f; angle >= -(2.0f*GL_PI)/2; angle -= (GL_PI/8.0f))
  286. {
  287. x = -r2*sin(angle);
  288. y = -r2*cos(angle);
  289. glVertex3d(-x -d , -y, h);
  290. }
  291. glEnd();
  292.  
  293. ////////////////////////////////////////////////////////////////////
  294. glBegin(GL_LINES);
  295.  
  296.  
  297. // pierwsza sciana
  298. glBegin(GL_QUADS);
  299. glVertex3d(0.0f, 0.0f + r1, 0.0f);
  300. glVertex3d(0.0f -d, 0.0f + r2, 0.0f);
  301.  
  302. glVertex3d(0.0f, 0.0f + r1, 0.0f + h);
  303. glVertex3d(0.0f -d, 0.0f + r2, 0.0f + h);
  304.  
  305. glVertex3d(0.0f, 0.0f - r1, 0.0f);
  306. glVertex3d(0.0f -d, 0.0f - r2, 0.0f);
  307.  
  308. glVertex3d(0.0f, 0.0f - r1, 0.0f + h);
  309. glVertex3d(0.0f -d, 0.0f - r2, 0.0f + h);
  310.  
  311. glEnd();
  312.  
  313. }
  314.  
  315. void robot(double d1, double d2, double d3)
  316. {
  317. glColor3d(1,0,0);
  318. glPushMatrix();
  319.  
  320. glRotated(-90,1.0,0.0,0.0);
  321. glTranslated(0.0,0.0,-50.0);
  322. walec(5,30);
  323.  
  324. glTranslated(0.0,0.0,5.0);
  325. walec(40,10);
  326.  
  327. glTranslated(0,0,40);
  328. glRotated(d1,0,0,1);
  329. walec(40,10);
  330.  
  331. glTranslated(0,0,40);
  332. glRotated(90,0,1,0);
  333. glTranslated(0,0,-20);
  334. walec(40,10);
  335.  
  336. glTranslated(0,0,+40);
  337. glRotated(90+d2,0,0,1);
  338. ramie(15,10,5,30);
  339.  
  340. glTranslated(-30,0,-5);
  341. //glRotated(d3,0,0,1);
  342. //ramie(15,10,5,30);
  343. glPopMatrix();
  344.  
  345.  
  346. }
  347.  
  348. void dwa_roboty(void)
  349. {
  350. glPushMatrix();
  351. glTranslated(-50,0,0);
  352. robot(rot1,rot2,rot3);
  353. glPopMatrix();
  354. /*
  355. glPushMatrix();
  356. glTranslated(50,0,0);
  357. //glRotated(180,0,1,0);
  358. robot(rot1,rot2,rot3);
  359. glPopMatrix();
  360. */
  361.  
  362.  
  363.  
  364.  
  365.  
  366. }
  367. // Called to draw scene
  368. void RenderScene(void)
  369. {
  370. // Clear the window and the depth buffer
  371. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  372. glEnable(GL_CULL_FACE);
  373. // Save matrix state and do the rotation
  374. glPushMatrix();
  375. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  376. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  377.  
  378.  
  379. ////////////////////////////////////////////////////
  380. ////////////////////////////////////////////////////
  381. // WYWOLANIE FUNKCJI ROBOTA
  382. robot(rot1,rot2,rot3);
  383.  
  384. //szescian();
  385.  
  386. // All obiect are shown as nets
  387. glPolygonMode(GL_BACK,GL_LINE);
  388.  
  389.  
  390.  
  391. /////////////////////////////////////////////////////
  392. /////////////////////////////////////////////////////
  393. // Restore transformations
  394. glPopMatrix();
  395.  
  396. // Flush drawing commands
  397. glFlush();
  398. }
  399.  
  400.  
  401. // Select the pixel format for a given device context
  402. void SetDCPixelFormat(HDC hDC)
  403. {
  404. int nPixelFormat;
  405.  
  406. static PIXELFORMATDESCRIPTOR pfd = {
  407. sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
  408. 1, // Version of this structure
  409. PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
  410. PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
  411. PFD_DOUBLEBUFFER, // Double buffered
  412. PFD_TYPE_RGBA, // RGBA Color mode
  413. 24, // Want 24bit color
  414. 0,0,0,0,0,0, // Not used to select mode
  415. 0,0, // Not used to select mode
  416. 0,0,0,0,0, // Not used to select mode
  417. 32, // Size of depth buffer
  418. 0, // Not used to select mode
  419. 0, // Not used to select mode
  420. PFD_MAIN_PLANE, // Draw in main plane
  421. 0, // Not used to select mode
  422. 0,0,0 }; // Not used to select mode
  423.  
  424. // Choose a pixel format that best matches that described in pfd
  425. nPixelFormat = ChoosePixelFormat(hDC, &pfd);
  426.  
  427. // Set the pixel format for the device context
  428. SetPixelFormat(hDC, nPixelFormat, &pfd);
  429. }
  430.  
  431.  
  432.  
  433. // If necessary, creates a 3-3-2 palette for the device context listed.
  434. HPALETTE GetOpenGLPalette(HDC hDC)
  435. {
  436. HPALETTE hRetPal = NULL; // Handle to palette to be created
  437. PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
  438. LOGPALETTE *pPal; // Pointer to memory for logical palette
  439. int nPixelFormat; // Pixel format index
  440. int nColors; // Number of entries in palette
  441. int i; // Counting variable
  442. BYTE RedRange,GreenRange,BlueRange;
  443. // Range for each color entry (7,7,and 3)
  444.  
  445.  
  446. // Get the pixel format index and retrieve the pixel format description
  447. nPixelFormat = GetPixelFormat(hDC);
  448. DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  449.  
  450. // Does this pixel format require a palette? If not, do not create a
  451. // palette and just return NULL
  452. if(!(pfd.dwFlags & PFD_NEED_PALETTE))
  453. return NULL;
  454.  
  455. // Number of entries in palette. 8 bits yeilds 256 entries
  456. nColors = 1 << pfd.cColorBits;
  457.  
  458. // Allocate space for a logical palette structure plus all the palette entries
  459. pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
  460.  
  461. // Fill in palette header
  462. pPal->palVersion = 0x300; // Windows 3.0
  463. pPal->palNumEntries = nColors; // table size
  464.  
  465. // Build mask of all 1's. This creates a number represented by having
  466. // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
  467. // pfd.cBlueBits.
  468. RedRange = (1 << pfd.cRedBits) -1;
  469. GreenRange = (1 << pfd.cGreenBits) - 1;
  470. BlueRange = (1 << pfd.cBlueBits) -1;
  471.  
  472. // Loop through all the palette entries
  473. for(i = 0; i < nColors; i++)
  474. {
  475. // Fill in the 8-bit equivalents for each component
  476. pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
  477. pPal->palPalEntry[i].peRed = (unsigned char)(
  478. (double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
  479.  
  480. pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
  481. pPal->palPalEntry[i].peGreen = (unsigned char)(
  482. (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
  483.  
  484. pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
  485. pPal->palPalEntry[i].peBlue = (unsigned char)(
  486. (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
  487.  
  488. pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
  489. }
  490.  
  491.  
  492. // Create the palette
  493. hRetPal = CreatePalette(pPal);
  494.  
  495. // Go ahead and select and realize the palette for this device context
  496. SelectPalette(hDC,hRetPal,FALSE);
  497. RealizePalette(hDC);
  498.  
  499. // Free the memory used for the logical palette structure
  500. free(pPal);
  501.  
  502. // Return the handle to the new palette
  503. return hRetPal;
  504. }
  505.  
  506.  
  507. // Entry point of all Windows programs
  508. int APIENTRY WinMain( HINSTANCE hInst,
  509. HINSTANCE hPrevInstance,
  510. LPSTR lpCmdLine,
  511. int nCmdShow)
  512. {
  513. MSG msg; // Windows message structure
  514. WNDCLASS wc; // Windows class structure
  515. HWND hWnd; // Storeage for window handle
  516.  
  517. hInstance = hInst;
  518.  
  519. // Register Window style
  520. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  521. wc.lpfnWndProc = (WNDPROC) WndProc;
  522. wc.cbClsExtra = 0;
  523. wc.cbWndExtra = 0;
  524. wc.hInstance = hInstance;
  525. wc.hIcon = NULL;
  526. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  527.  
  528. // No need for background brush for OpenGL window
  529. wc.hbrBackground = NULL;
  530.  
  531. wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
  532. wc.lpszClassName = lpszAppName;
  533.  
  534. // Register the window class
  535. if(RegisterClass(&wc) == 0)
  536. return FALSE;
  537.  
  538.  
  539. // Create the main application window
  540. hWnd = CreateWindow(
  541. lpszAppName,
  542. lpszAppName,
  543.  
  544. // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
  545. WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  546.  
  547. // Window position and size
  548. 50, 50,
  549. 400, 400,
  550. NULL,
  551. NULL,
  552. hInstance,
  553. NULL);
  554.  
  555. // If window was not created, quit
  556. if(hWnd == NULL)
  557. return FALSE;
  558.  
  559.  
  560. // Display the window
  561. ShowWindow(hWnd,SW_SHOW);
  562. UpdateWindow(hWnd);
  563.  
  564. // Process application messages until the application closes
  565. while( GetMessage(&msg, NULL, 0, 0))
  566. {
  567. TranslateMessage(&msg);
  568. DispatchMessage(&msg);
  569. }
  570.  
  571. return msg.wParam;
  572. }
  573.  
  574.  
  575.  
  576.  
  577. // Window procedure, handles all messages for this program
  578. LRESULT CALLBACK WndProc( HWND hWnd,
  579. UINT message,
  580. WPARAM wParam,
  581. LPARAM lParam)
  582. {
  583. static HGLRC hRC; // Permenant Rendering context
  584. static HDC hDC; // Private GDI Device context
  585.  
  586. switch (message)
  587. {
  588. // Window creation, setup for OpenGL
  589. case WM_TIMER:
  590. if(wParam==101)
  591. {
  592. licznik++;
  593. if(licznik<15)
  594. {
  595. rot2+=4.0;
  596. }
  597.  
  598. if(licznik>15 && licznik < 30)
  599. {
  600. rot2-=4.0;
  601. }
  602.  
  603. if(licznik>30)
  604. {licznik=0;}
  605. InvalidateRect(hWnd,NULL,FALSE);
  606. }
  607. break;
  608.  
  609. case WM_CREATE:
  610.  
  611. SetTimer(hWnd,101,30,NULL);
  612. // Store the device context
  613. hDC = GetDC(hWnd);
  614.  
  615. // Select the pixel format
  616. SetDCPixelFormat(hDC);
  617.  
  618. // Create palette if needed
  619. hPalette = GetOpenGLPalette(hDC);
  620.  
  621. // Create the rendering context and make it current
  622. hRC = wglCreateContext(hDC);
  623. wglMakeCurrent(hDC, hRC);
  624.  
  625. // Call OpenGL setup code
  626. SetupRC();
  627.  
  628. break;
  629.  
  630. // Window is being destroyed, cleanup
  631. case WM_DESTROY:
  632.  
  633. KillTimer(hWnd,101);
  634. // Deselect the current rendering context and delete it
  635. wglMakeCurrent(hDC,NULL);
  636. wglDeleteContext(hRC);
  637.  
  638. ReleaseDC(hWnd,hDC);
  639.  
  640. // Delete the palette if it was created
  641. if(hPalette != NULL)
  642. DeleteObject(hPalette);
  643.  
  644.  
  645. // Tell the application to terminate after the window
  646. // is gone.
  647. PostQuitMessage(0);
  648. break;
  649.  
  650. // Window is resized.
  651. case WM_SIZE:
  652. // Call our function which modifies the clipping
  653. // volume and viewport
  654. ChangeSize(LOWORD(lParam), HIWORD(lParam));
  655. break;
  656.  
  657.  
  658. // The painting function. This message sent by Windows
  659. // whenever the screen needs updating.
  660. case WM_PAINT:
  661. {
  662. // Call OpenGL drawing code
  663. RenderScene();
  664.  
  665. SwapBuffers(hDC);
  666.  
  667. // Validate the newly painted client area
  668. ValidateRect(hWnd,NULL);
  669. }
  670. break;
  671.  
  672.  
  673. // Windows is telling the application that it may modify
  674. // the system palette. This message in essance asks the
  675. // application for a new palette.
  676. case WM_QUERYNEWPALETTE:
  677. // If the palette was created.
  678. if(hPalette)
  679. {
  680. int nRet;
  681.  
  682. // Selects the palette into the current device context
  683. SelectPalette(hDC, hPalette, FALSE);
  684.  
  685. // Map entries from the currently selected palette to
  686. // the system palette. The return value is the number
  687. // of palette entries modified.
  688. nRet = RealizePalette(hDC);
  689.  
  690. // Repaint, forces remap of palette in current window
  691. InvalidateRect(hWnd,NULL,FALSE);
  692.  
  693. return nRet;
  694. }
  695. break;
  696.  
  697.  
  698. // This window may set the palette, even though it is not the
  699. // currently active window.
  700. case WM_PALETTECHANGED:
  701. // Don't do anything if the palette does not exist, or if
  702. // this is the window that changed the palette.
  703. if((hPalette != NULL) && ((HWND)wParam != hWnd))
  704. {
  705. // Select the palette into the device context
  706. SelectPalette(hDC,hPalette,FALSE);
  707.  
  708. // Map entries to system palette
  709. RealizePalette(hDC);
  710.  
  711. // Remap the current colors to the newly realized palette
  712. UpdateColors(hDC);
  713. return 0;
  714. }
  715. break;
  716.  
  717.  
  718. // Key press, check for arrow keys to do cube rotation.
  719. case WM_KEYDOWN:
  720. {
  721. if(wParam == VK_UP)
  722. xRot-= 5.0f;
  723.  
  724. if(wParam == VK_DOWN)
  725. xRot += 5.0f;
  726.  
  727. if(wParam == VK_LEFT)
  728. yRot -= 5.0f;
  729.  
  730. if(wParam == VK_RIGHT)
  731. yRot += 5.0f;
  732.  
  733. if(xRot > 356.0f)
  734. xRot = 0.0f;
  735.  
  736. if(xRot < -1.0f)
  737. xRot = 355.0f;
  738.  
  739. if(yRot > 356.0f)
  740. yRot = 0.0f;
  741.  
  742. if(yRot < -1.0f)
  743. yRot = 355.0f;
  744.  
  745. if(wParam == '1')
  746. rot1 -= 5.0f;
  747.  
  748. if(wParam == '2')
  749. rot1 += 5.0f;
  750.  
  751. if(wParam == '3')
  752. rot2 -= 5.0f;
  753.  
  754. if(wParam == '4')
  755. rot2 += 5.0f;
  756.  
  757. if(wParam == '5')
  758. rot3 -= 5.0f;
  759.  
  760. if(wParam == '6')
  761. rot3 += 5.0f;
  762.  
  763. InvalidateRect(hWnd,NULL,FALSE);
  764. }
  765. break;
  766.  
  767. // A menu command
  768. case WM_COMMAND:
  769. {
  770. HANDLE hMenu = GetMenu(hWnd);
  771.  
  772. switch(LOWORD(wParam))
  773. {
  774. // Exit the program
  775. case ID_FILE_EXIT:
  776. DestroyWindow(hWnd);
  777. break;
  778.  
  779. // Display the about box
  780. case ID_HELP_ABOUT:
  781. DialogBox (hInstance,
  782. MAKEINTRESOURCE(IDD_DIALOG_ABOUT),
  783. hWnd,
  784. AboutDlgProc);
  785. break;
  786.  
  787. }
  788. }
  789. break;
  790.  
  791.  
  792. default: // Passes it on if unproccessed
  793. return (DefWindowProc(hWnd, message, wParam, lParam));
  794.  
  795. }
  796.  
  797. return (0L);
  798. }
  799.  
  800.  
  801.  
  802.  
  803. // Dialog procedure.
  804. BOOL APIENTRY AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam)
  805. {
  806.  
  807. switch (message)
  808. {
  809. // Initialize the dialog box
  810. case WM_INITDIALOG:
  811. {
  812. int i;
  813. GLenum glError;
  814.  
  815. // glGetString demo
  816. SetDlgItemText(hDlg,IDC_OPENGL_VENDOR,(LPCSTR)glGetString(GL_VENDOR));
  817. SetDlgItemText(hDlg,IDC_OPENGL_RENDERER,(LPCSTR)glGetString(GL_RENDERER));
  818. SetDlgItemText(hDlg,IDC_OPENGL_VERSION,(LPCSTR)glGetString(GL_VERSION));
  819. SetDlgItemText(hDlg,IDC_OPENGL_EXTENSIONS,(LPCSTR)glGetString(GL_EXTENSIONS));
  820.  
  821. // gluGetString demo
  822. SetDlgItemText(hDlg,IDC_GLU_VERSION,(LPCSTR)gluGetString(GLU_VERSION));
  823. SetDlgItemText(hDlg,IDC_GLU_EXTENSIONS,(LPCSTR)gluGetString(GLU_EXTENSIONS));
  824.  
  825.  
  826. // Display any recent error messages
  827. i = 0;
  828. do {
  829. glError = glGetError();
  830. SetDlgItemText(hDlg,IDC_ERROR1+i,(LPCSTR)gluErrorString(glError));
  831. i++;
  832. }
  833. while(i < 6 && glError != GL_NO_ERROR);
  834.  
  835.  
  836. return (TRUE);
  837. }
  838. break;
  839.  
  840. // Process command messages
  841. case WM_COMMAND:
  842. {
  843. // Validate and Make the changes
  844. if(LOWORD(wParam) == IDOK)
  845. EndDialog(hDlg,TRUE);
  846. }
  847. break;
  848.  
  849. // Closed from sysbox
  850. case WM_CLOSE:
  851. EndDialog(hDlg,TRUE);
  852. break;
  853. }
  854.  
  855. return FALSE;
  856. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement