fork download
  1. /*
  2. * This Code Was Created By Jeff Molofee 2000
  3. * A HUGE Thanks To Fredric Echols For Cleaning Up
  4. * And Optimizing This Code, Making It More Flexible!
  5. * If You've Found This Code Useful, Please Let Me Know.
  6. * Visit My Site At nehe.gamedev.net
  7. */
  8.  
  9. #include <windows.h> // Header File For Windows
  10. #include <gl\gl.h> // Header File For The OpenGL32 Library
  11. #include <gl\glu.h> // Header File For The GLu32 Library
  12. #include <glaux.h> // Header File For The Glaux Library
  13. #include<math.h>
  14. //#include<glut.h>
  15. HDC hDC = NULL; // Private GDI Device Context
  16. HGLRC hRC = NULL; // Permanent Rendering Context
  17. HWND hWnd = NULL; // Holds Our Window Handle
  18. HINSTANCE hInstance; // Holds The Instance Of The Application
  19.  
  20. bool keys[256]; // Array Used For The Keyboard Routine
  21. bool active = TRUE; // Window Active Flag Set To TRUE By Default
  22. bool fullscreen = TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
  23.  
  24. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
  25.  
  26. GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
  27. {
  28. if (height == 0) // Prevent A Divide By Zero By
  29. {
  30. height = 1; // Making Height Equal One
  31. }
  32.  
  33. glViewport(0, 0, width, height); // Reset The Current Viewport
  34.  
  35. glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
  36. glLoadIdentity(); // Reset The Projection Matrix
  37.  
  38. // Calculate The Aspect Ratio Of The Window
  39. gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
  40.  
  41. glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
  42. glLoadIdentity(); // Reset The Modelview Matrix
  43. }
  44.  
  45. int InitGL(GLvoid) // All Setup For OpenGL Goes Here
  46. {
  47. glShadeModel(GL_SMOOTH); // Enable Smooth Shading
  48. glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
  49. glClearDepth(1.0f); // Depth Buffer Setup
  50. glEnable(GL_DEPTH_TEST); // Enables Depth Testing
  51. glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
  52. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
  53. return TRUE; // Initialization Went OK
  54. }
  55.  
  56. float angle = 0;
  57. GLfloat xr = 0;
  58.  
  59.  
  60.  
  61. void DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
  62. {
  63.  
  64. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  65. glLoadIdentity(); // Reset The Current Modelview Matrix
  66.  
  67. //Rotate and change rotate angle
  68. glRotatef(angle, 0.0f, 0.0f, 1.0f);
  69. angle++;
  70.  
  71.  
  72.  
  73.  
  74. /*glBegin(GL_TRIANGLES);
  75.  
  76. glColor3f(1.0f, 0.0f, 0.0f);
  77. glVertex3i(0, 1, -6);
  78.  
  79. glColor3f(0.0f, 1.0f, 0.0f);
  80. glVertex3i(-1, -1, -6);
  81.  
  82. glColor3f(0.0f, 0.0f, 1.0f);
  83. glVertex3i(1, -1, -6);
  84. //glScalef(2, 1, 1);*/
  85.  
  86.  
  87.  
  88. glBegin(GL_POINTS);//????//
  89. for (angle = 0.33f; angle <= (15.0f*3.14f);angle+=0.1f) {
  90. float x = 2.8f * cos (angle);
  91. float y = 2.8f * sin(angle);
  92.  
  93. glColor3f(0.0f, 1.0f, 0.0f);
  94. glVertex3f(x, y, -10.0f);}
  95. glLoadIdentity();
  96. glEnd();
  97.  
  98. glBegin(GL_POINTS); //??? ??????? ??????? ?? ?????? ?????
  99. for (angle = 0.33f; angle <= (15.0f*3.14f); angle += 0.1f) {
  100. float x = 1.0f * cos(angle);
  101. float y = 1.0f * sin(angle);
  102. glColor3f(1.0f, 1.0f, 1.0f);
  103. glVertex3f(x, y, -7.0f);
  104. }
  105. glEnd();
  106.  
  107.  
  108. glLoadIdentity();
  109. //?????? ?? ????? ??????
  110. glBegin(GL_POINTS);
  111. for (angle = 0.33f; angle <= (15.0f*3.14f); angle += 0.1f) {
  112. float x = 3.0f * cos(angle);
  113. float y = 3.0f * sin(angle);
  114.  
  115. glColor3f(0.0f, 1.0f, 1.0f);
  116. glVertex3f(x, y, -15.0f);
  117.  
  118. }
  119.  
  120. glEnd();
  121. glLoadIdentity();
  122. //glRotated(xr, 0, 0, 1);
  123. //glTranslated(3.2, 0.0, 0.0);
  124. /*glBegin(GL_POLYGON);// the kernel
  125. for (angle = 0.24f; angle <= (14.0f*3.14f); angle += 0.1f)
  126. {
  127. float x = 0.4f * cos(angle);
  128. float y = 0.4f * sin(angle);
  129. glColor3f(1.0f, 2.0f, 1.0f);
  130. glVertex3d(x, y, -15.0f);
  131. }
  132. glEnd();
  133.  
  134. glLoadIdentity();
  135. xr += 1;// Done Drawing The Quad
  136. glRotated(xr,0,0,1);
  137. glTranslated(3.1, 0.0, 0.0);
  138.  
  139. glBegin(GL_POLYGON);//the blu circle
  140. for (angle = 0.24f; angle <= (14.0f*3.14f); angle += 0.1f)
  141. {
  142. float x = 0.2f * cos(angle);
  143. float y = 0.2f * sin(angle);
  144. glColor3f(0.0f, 0.0f, 2.0f);
  145. glVertex3d(x, y, -20.0f);
  146. }
  147.  
  148.  
  149. glEnd();
  150. glLoadIdentity();
  151. xr += 5;// Done Drawing The Quad
  152.   glRotated(xr, 0, 0, 1);
  153. glTranslated(3.1, 0.0, 0.0);
  154.   glBegin(GL_POLYGON);//the pink circle
  155. for (angle = 0.24f; angle <= (14.0f*3.14f); angle += 0.1f)
  156. {
  157. float x = 0.2f * cos(angle);
  158. float y = 0.2f * sin(angle);
  159. glColor3f(3.0f, 0.6f, 2.0f);
  160. glVertex3d(x, y, -15.0f);
  161. }
  162.  
  163. glEnd();
  164. xr += 1;// Done Drawing The Quad
  165.  
  166. glBegin(GL_POLYGON);//the yellow circle
  167. for (angle = 0.24f; angle <= (14.0f*3.14f); angle += 0.1f)
  168. {
  169. float x = 0.2f * cos(angle);
  170. float y = 0.2f * sin(angle);
  171. glColor3f(22.0f, 5.0f, 0.0f);
  172. glVertex3d(x, y, -11.4f);
  173. }
  174. glEnd();*/
  175. void Drow_circle(float CX, float CY, float r, int num_segments)
  176. {
  177. float theta = 2 * 3.14 / float(num_segments);
  178. float tangetial_factor = tanf(theta);
  179. float radial_factor = cosf(theta);
  180. float x = r;
  181. float y = 0;
  182. glBegin(GLU_LINE_loop);
  183. for (int i = 0; i < num_segments; i < ++)
  184. {
  185. glVertex2f(x + CX + y + CY);
  186. float tx = -y;
  187. float ty = x;
  188. x += tx * tangetial_factor;
  189. y += ty * tangetial_factor;
  190. }
  191. glEnd();
  192.  
  193. }
  194.  
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. glFlush();
  207. //DO NOT REMOVE THIS
  208. SwapBuffers(hDC);
  209. }
  210.  
  211. GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
  212. {
  213. if (fullscreen) // Are We In Fullscreen Mode?
  214. {
  215. ChangeDisplaySettings(NULL, 0); // If So Switch Back To The Desktop
  216. ShowCursor(TRUE); // Show Mouse Pointer
  217. }
  218.  
  219. if (hRC) // Do We Have A Rendering Context?
  220. {
  221. if (!wglMakeCurrent(NULL, NULL)) // Are We Able To Release The DC And RC Contexts?
  222. {
  223. MessageBox(NULL, "Release Of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
  224. }
  225.  
  226. if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
  227. {
  228. MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
  229. }
  230. hRC = NULL; // Set RC To NULL
  231. }
  232.  
  233. if (hDC && !ReleaseDC(hWnd, hDC)) // Are We Able To Release The DC
  234. {
  235. MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
  236. hDC = NULL; // Set DC To NULL
  237. }
  238.  
  239. if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
  240. {
  241. MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
  242. hWnd = NULL; // Set hWnd To NULL
  243. }
  244.  
  245. if (!UnregisterClass("OpenGL", hInstance)) // Are We Able To Unregister Class
  246. {
  247. MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
  248. hInstance = NULL; // Set hInstance To NULL
  249. }
  250. }
  251.  
  252. /* This Code Creates Our OpenGL Window. Parameters Are: *
  253. * title - Title To Appear At The Top Of The Window *
  254. * width - Width Of The GL Window Or Fullscreen Mode *
  255. * height - Height Of The GL Window Or Fullscreen Mode *
  256. * bits - Number Of Bits To Use For Color (8/16/24/32) *
  257. * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
  258.  
  259. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  260. {
  261. GLuint PixelFormat; // Holds The Results After Searching For A Match
  262. WNDCLASS wc; // Windows Class Structure
  263. DWORD dwExStyle; // Window Extended Style
  264. DWORD dwStyle; // Window Style
  265. RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
  266. WindowRect.left = (long)0; // Set Left Value To 0
  267. WindowRect.right = (long)width; // Set Right Value To Requested Width
  268. WindowRect.top = (long)0; // Set Top Value To 0
  269. WindowRect.bottom = (long)height; // Set Bottom Value To Requested Height
  270.  
  271. fullscreen = fullscreenflag; // Set The Global Fullscreen Flag
  272.  
  273. hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
  274. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
  275. wc.lpfnWndProc = (WNDPROC)WndProc; // WndProc Handles Messages
  276. wc.cbClsExtra = 0; // No Extra Window Data
  277. wc.cbWndExtra = 0; // No Extra Window Data
  278. wc.hInstance = hInstance; // Set The Instance
  279. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
  280. wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
  281. wc.hbrBackground = NULL; // No Background Required For GL
  282. wc.lpszMenuName = NULL; // We Don't Want A Menu
  283. wc.lpszClassName = "OpenGL"; // Set The Class Name
  284.  
  285. if (!RegisterClass(&wc)) // Attempt To Register The Window Class
  286. {
  287. MessageBox(NULL, "Failed To Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  288. return FALSE; // Return FALSE
  289. }
  290.  
  291. if (fullscreen) // Attempt Fullscreen Mode?
  292. {
  293. DEVMODE dmScreenSettings; // Device Mode
  294. memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
  295. dmScreenSettings.dmSize = sizeof(dmScreenSettings); // Size Of The Devmode Structure
  296. dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
  297. dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
  298. dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
  299. dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  300.  
  301. // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  302. if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  303. {
  304. // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
  305. if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe GL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
  306. {
  307. fullscreen = FALSE; // Windowed Mode Selected. Fullscreen = FALSE
  308. }
  309. else
  310. {
  311. // Pop Up A Message Box Letting User Know The Program Is Closing.
  312. MessageBox(NULL, "Program Will Now Close.", "ERROR", MB_OK | MB_ICONSTOP);
  313. return FALSE; // Return FALSE
  314. }
  315. }
  316. }
  317.  
  318. if (fullscreen) // Are We Still In Fullscreen Mode?
  319. {
  320. dwExStyle = WS_EX_APPWINDOW; // Window Extended Style
  321. dwStyle = WS_POPUP; // Windows Style
  322. ShowCursor(FALSE); // Hide Mouse Pointer
  323. }
  324. else
  325. {
  326. dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
  327. dwStyle = WS_OVERLAPPEDWINDOW; // Windows Style
  328. }
  329.  
  330. AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
  331.  
  332. // Create The Window
  333. if (!(hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window
  334. "OpenGL", // Class Name
  335. title, // Window Title
  336. dwStyle | // Defined Window Style
  337. WS_CLIPSIBLINGS | // Required Window Style
  338. WS_CLIPCHILDREN, // Required Window Style
  339. 0, 0, // Window Position
  340. WindowRect.right - WindowRect.left, // Calculate Window Width
  341. WindowRect.bottom - WindowRect.top, // Calculate Window Height
  342. NULL, // No Parent Window
  343. NULL, // No Menu
  344. hInstance, // Instance
  345. NULL))) // Dont Pass Anything To WM_CREATE
  346. {
  347. KillGLWindow(); // Reset The Display
  348. MessageBox(NULL, "Window Creation Error.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  349. return FALSE; // Return FALSE
  350. }
  351.  
  352. static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
  353. {
  354. sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
  355. 1, // Version Number
  356. PFD_DRAW_TO_WINDOW | // Format Must Support Window
  357. PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
  358. PFD_DOUBLEBUFFER, // Must Support Double Buffering
  359. PFD_TYPE_RGBA, // Request An RGBA Format
  360. bits, // Select Our Color Depth
  361. 0, 0, 0, 0, 0, 0, // Color Bits Ignored
  362. 0, // No Alpha Buffer
  363. 0, // Shift Bit Ignored
  364. 0, // No Accumulation Buffer
  365. 0, 0, 0, 0, // Accumulation Bits Ignored
  366. 16, // 16Bit Z-Buffer (Depth Buffer)
  367. 0, // No Stencil Buffer
  368. 0, // No Auxiliary Buffer
  369. PFD_MAIN_PLANE, // Main Drawing Layer
  370. 0, // Reserved
  371. 0, 0, 0 // Layer Masks Ignored
  372. };
  373.  
  374. if (!(hDC = GetDC(hWnd))) // Did We Get A Device Context?
  375. {
  376. KillGLWindow(); // Reset The Display
  377. MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  378. return FALSE; // Return FALSE
  379. }
  380.  
  381. if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) // Did Windows Find A Matching Pixel Format?
  382. {
  383. KillGLWindow(); // Reset The Display
  384. MessageBox(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  385. return FALSE; // Return FALSE
  386. }
  387.  
  388. if (!SetPixelFormat(hDC, PixelFormat, &pfd)) // Are We Able To Set The Pixel Format?
  389. {
  390. KillGLWindow(); // Reset The Display
  391. MessageBox(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  392. return FALSE; // Return FALSE
  393. }
  394.  
  395. if (!(hRC = wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
  396. {
  397. KillGLWindow(); // Reset The Display
  398. MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  399. return FALSE; // Return FALSE
  400. }
  401.  
  402. if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
  403. {
  404. KillGLWindow(); // Reset The Display
  405. MessageBox(NULL, "Can't Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  406. return FALSE; // Return FALSE
  407. }
  408.  
  409. ShowWindow(hWnd, SW_SHOW); // Show The Window
  410. SetForegroundWindow(hWnd); // Slightly Higher Priority
  411. SetFocus(hWnd); // Sets Keyboard Focus To The Window
  412. ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
  413.  
  414. if (!InitGL()) // Initialize Our Newly Created GL Window
  415. {
  416. KillGLWindow(); // Reset The Display
  417. MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
  418. return FALSE; // Return FALSE
  419. }
  420.  
  421. return TRUE; // Success
  422. }
  423.  
  424. LRESULT CALLBACK WndProc(HWND hWnd, // Handle For This Window
  425. UINT uMsg, // Message For This Window
  426. WPARAM wParam, // Additional Message Information
  427. LPARAM lParam) // Additional Message Information
  428. {
  429. static PAINTSTRUCT ps;
  430.  
  431. switch (uMsg) // Check For Windows Messages
  432. {
  433. case WM_PAINT:
  434. DrawGLScene();
  435. BeginPaint(hWnd, &ps);
  436. EndPaint(hWnd, &ps);
  437. return 0;
  438.  
  439. case WM_TIMER:
  440. DrawGLScene();
  441. return 0;
  442.  
  443. case WM_ACTIVATE: // Watch For Window Activate Message
  444. {
  445. if (!HIWORD(wParam)) // Check Minimization State
  446. {
  447. active = TRUE; // Program Is Active
  448. }
  449. else
  450. {
  451. active = FALSE; // Program Is No Longer Active
  452. }
  453.  
  454. return 0; // Return To The Message Loop
  455. }
  456.  
  457. case WM_SYSCOMMAND: // Intercept System Commands
  458. {
  459. switch (wParam) // Check System Calls
  460. {
  461. case SC_SCREENSAVE: // Screensaver Trying To Start?
  462. case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
  463. return 0; // Prevent From Happening
  464. }
  465. break; // Exit
  466. }
  467.  
  468. case WM_CLOSE: // Did We Receive A Close Message?
  469. {
  470. PostQuitMessage(0); // Send A Quit Message
  471. return 0; // Jump Back
  472. }
  473.  
  474. case WM_KEYDOWN: // Is A Key Being Held Down?
  475. {
  476. keys[wParam] = TRUE; // If So, Mark It As TRUE
  477. return 0; // Jump Back
  478. }
  479.  
  480. case WM_KEYUP: // Has A Key Been Released?
  481. {
  482. keys[wParam] = FALSE; // If So, Mark It As FALSE
  483. return 0; // Jump Back
  484. }
  485.  
  486. case WM_SIZE: // Resize The OpenGL Window
  487. {
  488. ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); // LoWord=Width, HiWord=Height
  489. return 0; // Jump Back
  490. }
  491. }
  492.  
  493. // Pass All Unhandled Messages To DefWindowProc
  494. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  495. }
  496.  
  497. int WINAPI WinMain(HINSTANCE hInstance, // Instance
  498. HINSTANCE hPrevInstance, // Previous Instance
  499. LPSTR lpCmdLine, // Command Line Parameters
  500. int nCmdShow) // Window Show State
  501. {
  502. MSG msg; // Windows Message Structure
  503. BOOL done = FALSE; // Bool Variable To Exit Loop
  504.  
  505. // Ask The User Which Screen Mode They Prefer
  506. //if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
  507. {
  508. fullscreen = FALSE; // Windowed Mode
  509. }
  510.  
  511. // Create Our OpenGL Window-
  512. if (!CreateGLWindow("Madhat NeHe Template", 640, 480, 16, fullscreen))
  513. {
  514. return 0; // Quit If Window Was Not Created
  515. }
  516.  
  517.  
  518. //Set drawing timer to 20 frame per second
  519. UINT timer = SetTimer(hWnd, 0, 50, (TIMERPROC)NULL);
  520.  
  521. while (GetMessage(&msg, NULL, 0, 0))
  522. {
  523. TranslateMessage(&msg);
  524. DispatchMessage(&msg);
  525. }
  526.  
  527. return 0;
  528.  
  529. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:9:10: fatal error: windows.h: No such file or directory
 #include <windows.h>  // Header File For Windows
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty