OpenGL Study Notes
I. currently, OpenGL learning notes circulating on the Internet can be divided into three types: red books and Blue Books, using GLUT to draw windows, and nehe using Windows APIs, the third method is the MFC class library-based tutorial used in Windows. The second method is used in my notes.
2. Draw a window
The Code is as follows:
# Include <windows. h> <br/> # include <Gl/GL. h> <br/> # include <Gl/Glu. h> <br/> # include <Gl/Glaux. h> <br/> # include <Gl/glut. h> </P> <p> hglrc HRC = NULL; <br/> HDC = NULL; <br/> hwnd = NULL; <br/> hinstance; <br/> boolkeys [0, 256]; <br/> boolactive = true; <br/> boolfullscreen = true; </P> <p> lresultcallback wndproc (hwnd, uint, wparam, lparam); </P> <p> glvoid resizeglscene (glsizei width, glsizei height) <br/> {<Br/> If (Height = 0) <br/>{< br/> Height = 1; <br/>}< br/> glviewport (0, 0, width, height); <br/> glmatrixmode (gl_projection); <br/> glloadidentity (); <br/> gluperspective (45.0f, (glfloat) width/(glfloat) height, 0.1f, 100366f); <br/> glmatrixmode (gl_modelview); <br/> glloadidentity (); <br/>}</P> <p> int initgl (glvoid) <br/>{< br/> glshademodel (gl_smooth); <br/> glclearcolor (1.0f, 0.0f, 0.0f, 0.0f); <br/> glcleardep Th (1.0f); <br/> glable (gl_depth_test); <br/> gldepthfunc (gl_lequal); <br/> glhint (gl_perspective_correction_hint, gl_nicest ); <br/> return true; <br/>}</P> <p> int drawglscene (glvoid) <br/>{< br/> glclear (gl_color_buffer_bit | gl_depth_buffer_bit ); <br/> glloadidentity (); <br/> return true; <br/>}</P> <p> glvoid killglwindow (glvoid) <br/>{< br/> If (fullscreen) <br/>{< br/> changedisplaysettings (null, 0); <br /> Showcursor (true); <br/>}< br/> If (HRC) <br/>{< br/> If (! Wglmakecurrent (null, null) <br/>{< br/> MessageBox (null, text ("failed to release DC or RC. "), Text (" Close error "), mb_ OK | mb_iconinformation); <br/>}< br/> If (! Wgldeletecontext (HRC) <br/>{< br/> MessageBox (null, text ("failed to release DC or RC. "), Text (" Close error "), mb_ OK | mb_iconinformation); <br/>}< br/> HRC = NULL; <br/>}< br/> If (HDC &&! Releasedc (hwnd, HDC) <br/>{< br/> MessageBox (null, text ("failed to release DC or RC. "), Text (" Close error "), mb_ OK | mb_iconinformation); <br/> HDC = NULL; <br/>}< br/> If (hwnd &&! Destroywindow (hwnd) <br/>{< br/> MessageBox (null, text ("failed to release the window handle. "), Text (" Close error "), mb_ OK | mb_iconinformation); <br/> hwnd = NULL; <br/>}< br/> If (! Unregisterclass (text ("OpenGL"), hinstance) <br/>{< br/> MessageBox (null, text ("cannot cancel the window class. "), Text (" Close error "), mb_ OK | mb_iconinformation); <br/> hinstance = NULL; <br/>}< br/> bool createglwindow (char * Title, int width, int height, int bits, bool fullscreenflag) <br/>{< br/> gluintpixelformat; <br/> wndclasswc; <br/> dworddwexstyle; <br/> dworddwstyle; </P> <p> rect windowrect; <br/> windowrect. left = (long) 0; <br/> windowrect. right = (long) width; <br/> windowrect. top = (long) 0; <br/> windowrect. bottom = (Long) height; <br/> fullscreen = fullscreenflag; <br/> hinstance = getmodulehandle (null); <br/> WC. style = cs_hredraw | cs_vredraw | cs_owndc; <br/> WC. lpfnwndproc = (wndproc) wndproc; <br/> WC. cbclsextra = 0; <br/> WC. cbwndextra = 0; <br/> WC. hinstance = hinstance; <br/> WC. hicon = loadicon (null, idi_winlogo); <br/> WC. hcursor = loadcursor (null, idc_arrow); <br/> WC. hbrbackground = NULL; <br/> WC. lpszmenuname = NULL; <br /> WC. lpszclassname = text ("OpenGL"); </P> <p> If (! Registerclass (& WC) <br/>{< br/> MessageBox (null, text ("registration window failed"), text ("error"), mb_ OK | mb_iconexclamation ); <br/> return false; <br/>}< br/> If (fullscreen) <br/>{< br/> devmode dmscreensettings; <br/> memset (& dmscreensettings, 0, sizeof (dmscreensettings); <br/> dmscreensettings. dmsize = sizeof (dmscreensettings); <br/> dmscreensettings. dmpelswidth = width; <br/> dmscreensettings. dmpelsheight = height; <br/> dmscreense Ttings. Outputs = bits; <br/> dmscreensettings. dmfields = dm_bitsperpel | dm_pelswidth | dm_pelsheight; <br/> If (changedisplaysettings (& dmscreensettings, cds_fullscreen )! = Disp_change_successful) <br/>{< br/> If (MessageBox (null, text ("setting in full screen mode on the current video card failed! \ N use window mode? "), Text (" nehe G "), mb_yesno | mb_iconexclamation) = idyes) <br/>{< br/> fullscreen = false; <br/>}< br/> else <br/> {<br/> MessageBox (null, text ("program will be closed"), text ("error "), mb_ OK | mb_iconstop); <br/> return false; <br/>}</P> <p> If (fullscreen) <br/>{< br/> dwexstyle = ws_ex_appwindow; <br/> dwstyle = ws_popup; <br/> showcursor (false ); <br/>}< br/> else <br/> {<br/> dwexstyle = ws_ex_appwindow | ws_ex_1_wedge; <br /> Dwstyle = ws_overlappedwindow; <br/>}</P> <p> adjustwindowrectex (& windowrect, dwstyle, false, dwexstyle); <br/> If (! (Hwnd = createjavaswex (dwexstyle, <br/> text ("OpenGL"), <br/> title, <br/> ws_clipsiblings | <br/> ws_clipchildren | <br/> dwstyle, <br/> 0, 0, <br/> windowrect. right-WindowRect.left, <br/> windowrect. bottom-WindowRect.top, <br/> null, <br/> null, <br/> hinstance, <br/> null ))) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("cannot create a window device description table "), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}< B R/> staticpixelformatdescriptor PFD = <br/>{< br/> sizeof (pixelformatdescriptor), <br/> 1, <br/> pfd_draw_to_window | <br/> pfd_support_opengl | <br/> pfd_doublebuffer, <br/> pfd_type_rgba, <br/> bits, <br/> 0, 0, 0, 0, 0, 0, <br/> 0, <br/> 0, <br/> 0, <br/> 0, 0, 0, <br/> 16, <br/> 0, <br/> 0, <br/> pfd_main_plane, <br/> 0, <br/> 0, 0, 0 <br/>}; <br/> If (! (HDC = getdc (hwnd) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("cannot create a matching pixel format"), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}< br/> If (! (Pixelformat = choosepixelformat (HDC, & PFD) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("pixel format not set"), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}</P> <p> If (! Setpixelformat (HDC, pixelformat, & PFD) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("pixel format not set"), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}</P> <p> If (! (HRC = wglcreatecontext (HDC) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("cannot create OpenGL rendering description table"), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}< br/> If (! Wglmakecurrent (HDC, HRC) <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("Current OpenGL cannot be activated"), text ("error"), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}< br/> showwindow (hwnd, sw_show); <br/> setforegroundwindow (hwnd); <br/> setfocus (hwnd ); <br/> resizeglscene (width, height); <br/> If (! Initgl () <br/>{< br/> killglwindow (); <br/> MessageBox (null, text ("initialization failed. "), text (" error "), mb_ OK | mb_iconexclamation); <br/> return false; <br/>}</P> <p> return true; <br/>}</P> <p> lresult callback wndproc (hwndhwnd, uintumsg, wparamwparam, lparamlparam) <br/>{< br/> switch (umsg) <br/>{< br/> case wm_activate: <br/>{< br/> If (! Hiword (wparam) <br/>{< br/> active = true; <br/>}< br/> else <br/>{< br/> active = false; // The program is no longer activated <br/>}</P> <p> return 0; // return message loop <br/>}</P> <p> case wm_syscommand: // system interruption command <br/>{< br/> switch (wparam) // Check System Call <br/>{< br/> case SC _screensave: // is screensaver running? <Br/> case SC _monitorpower: // do you want the monitor to enter power-saving mode? <Br/> return 0; // prevent occurrence <br/>}< br/> break; // exit <br/>}< br/> case wm_close: // receive the close message? <Br/>{< br/> postquitmessage (0); // send an Exit message <br/> return 0; // return <br/>}< br/> case wm_keydown: // is there a key to press it? <Br/>{< br/> keys [wparam] = true; // If yes, set it to true <br/> return 0; // return <br/>}< br/> case wm_keyup: // do you have a key to open it? <Br/>{< br/> keys [wparam] = false; // If yes, set it to false <br/> return 0; // return <br/>}< br/> case wm_size: // adjust the OpenGL window size <br/> {<br/> resizeglscene (loword (lparam ), hiword (lparam); // loword = width, hiword = height <br/> return 0; // return <br/>}< br/> return defwindowproc (hwnd, umsg, wparam, lparam ); <br/>}</P> <p> int winapi winmain (hinstancehinstance, hinstancehprevinstance, lpstrlpcmdline, intncmdshow) <br/>{</P> <p> msg MSG; // windowsx message structure <br/> booldone = false; // The bool variable used to exit the loop <br/> // This code is completely optional. The program prompts a message window asking whether you want to run in full screen mode. If you click the no button, the fullscreen variable is changed from true to false by default, and the program is also changed to run in window mode. <Br/> // prompt the user to select the running mode <br/> If (MessageBox (null, text ("do you want to run in full screen mode? "), Text (" set full screen mode "), mb_yesno | mb_iconquestion) = IDNO) <br/>{< br/> fullscreen = false; // false indicates window mode <br/>}< br/> // creates an OpenGL window. <br/> If (! Createglwindow ("nehe's OpenGL program framework", 640,480, 16, fullscreen) <br/>{< br/> return 0; // exit upon failure <br/>}< br/> while (! Done) // keep the loop until done = true <br/>{</P> <p> If (peekmessage (& MSG, null, pm_remove )) // is there a message waiting? <Br/>{</P> <p> If (msg. Message = wm_quit) // receives the Exit message? <Br/>{< br/> done = true; // yes, then done = true <br/>}< br/> else // No, process Window message <br/>{< br/> translatemessage (& MSG); // translate the message <br/> dispatchmessage (& MSG ); // send a message <br/>}</P> <p> else // if no message exists <br/>{< br/> If (active) // is the Program activated? <Br/>{< br/> If (Keys [vk_escape]) // is ESC pressed? <Br/>{< br/> done = true; // when ESC sends an exit signal <br/>}< br/> else // It is not the exit time, refresh the screen <br/>{< br/> drawglscene (); // draw a scenario <br/> swapbuffers (HDC); // swap cache (dual cache) <br/>}< br/> If (Keys [vk_f1]) // is the F1 key pressed? <Br/> {<br/> keys [vk_f1] = false; // If yes, set the value in the corresponding key array to false <br/> killglwindow (); // destroy the current window <br/> fullscreen =! Fullscreen; // switch to full screen/window mode <br/> // recreate the OpenGL window <br/> If (! Createglwindow ("nehe's OpenGL program framework", 640,480, 16, fullscreen) <br/>{< br/> return 0; // if the window cannot be created, program exit <br/>}< br/> killglwindow (); // destroy window <br/> return (MSG. wparam); // exit the Program <br/>}</P> <p>
III. All code can be summarized
Glvoid resizeglscene (glsizei width, glsizei height );
// Refactor the window size, which is valid when moving or scaling the window
Int initgl (glvoid );
// Initialize GL attributes
Int drawglscene (glvoid );
// Specific plotting function
Glvoid killglwindow (glvoid );
// Close various objects
Bool createglwindow (char * Title, int width, int height, int bits, bool fullscreenflag );
// Create a window function
Lresult callback wndproc (hwnd, uint umsg, wparam, lparam );
// Wndproc of the GL window
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow );
// Main function of the program
Iv. Summary
In fact, this mode is very classic for window construction, but it is very different from the classic "Hello World" program under Win32. When you are interested or free, you can refactor the window program.