Window OpenGL program C ++ version

xiaoxiao2021-03-31  202

The program structure needs to be improved, and it can work properly.

COpenGL.h #ifndef _COPENGL_H # define _COPENGL_H #include "OpenGLFrame.h" class COpenGL {private: HDC m_hDC; // device context HGLRC m_hRC; // render description int m_iWidth; int m_iHeight; public: HWND m_hWnd; // window handle HINSTANCE m_hInstance; // program instance bool m_bFullScreen; // displayed in full screen bool m_bActive; // is the active window bool m_bKeyState [256]; // key state double m_fRotated; double m_fTranslate; char m_szWindowClass [32]; char m_szTitle [32]; int m_iBits; PIXELFORMATDESCRIPTOR m_pfd; public: COpenGL (); ~ COpenGL (); int initializeGL (void); int CreateGLWindow (int width, int height, int bits); int RenderingScene (void); void SwapBuffers () ; int SetPixelFormat (PIXELFORMATDESCRIPTOR * pfd); int ChangeDisplaySettings (int width, int height, int bits); void KillWindow (void); void ResizeScene (GLsizei width, GLsizei height);}; COpenGL.cpp #include "COpenGL.h" Extern Lresult Callback MsgProc (HWND, UINT, WPARAM, LPARAM); Copengl :: Copengl () {this-> m_hwnd = null; this-> m_hinstance = null; this-> m_hdc = null; this-> m_bactiv E = true; this-> m_bfullscreen = true; this-> m_ibits = 16; this-> m_frotated = 0.0; this-> m_ftranslate = 0.0; Memset (m_bkeystate, 0, sizeof (bool) * 256); Memcpy (m_szwindowclass, "OpenGL", 7); memcpy (m_szTitle, "OpenGL Frame", 13); // pixel format PIXELFORMATDESCRIPTOR m_pfd.nSize = sizeof (PIXELFORMATDESCRIPTOR); m_pfd.nVersion = 1; // version m_pfd.dwFlags = PFD_DRAW_TO_WINDOW | / / Pixel format support form drawing pfd_support_opENGL | // pixel format must support OpenGL PFD_Doublebuffer; // Dual Cache m_pfd.ipixelType = PFD_TYPE_RGBA; // RGBA Color m_pfd.ccolorbits = this-> m_ibits; // color deep m_pfd.credbits = 0 ; // ignore color color m_pfd.credshift =

0; m_pfd.cgreenBITS = 0; m_pfd.cgreenshift = 0; m_pfd.cbluebits = 0; m_pfd.cblueshift = 0; // ignore color color position m_pfd.calphabits = 0; // no alpha cache m_pfd.calphashift = 0; // Shift bit ignored m_pfd.caccumbits = 0; // None cumulative cache m_pfd.caccumredbits = 0; // ignore the cumulative curtain bit m_pfd.caccumgreenbits = 0; // m_pfd.caccumbluebits = 0; // m_pfd.caccumalphabits = 0; // Ignore the cumulative slow storage m_pfd.cdepthbits = 16; // 16-bit depth cache m_pfd.cstencilbits = 0; // no block cache m_pfd.cauxbuffers = 0; // no auxiliary cache m_pfd.ilaryType = PFD_MAIN_PLANE; // Main layer M_PFD .Breserved = 0; // Reserved bit m_pfd.dwlayermask = 0; // ignore layer mask m_pfd.dwvisiblemask = 0; // ignore layer mask m_pfd.dwdamagemask = 0; // ignore layer mask} Copengu :: ~ Copengl () {} int Copengl :: CreateGLWindow (int Width, int Height, int bits) {m_iheight = height; m_iwidth = width; m_ibits = b dWord dwexStyle = 0; // Extension Form DWORD DWSTYLE = 0 ; // Form-style WNDCLASS WC; Rect WindowRect; WindowRect.Left = (long) 0; Wi NDOWRECT.TOP = (long) 0; windowRect.right = (long) width; windowRect.bottom = (long) Height; // For the form acquisition instance m_hinstance = :: getModuleHandle (NULL); // cs_hredraw, cs_vredraw is represented Change the form size in the horizontal direction, CS_OWNDC indicates that the form yourself has DC wc.style = cs_hredraw | cs_vredraw | cs_owndc; wc.lpfnwndProc = (wcproc) msgproc; wc.cbclsextra = 0; wc.cbwndextra = 0 Wc.hinstance = m_HINSTANCE; wc.hcursor = :: loadcursor (null, idc_arrow); wc.hicon = :: loadicon (null, idi_winlogo); wc.hbrbackground = null; // OpenGL function can set wc.lpsz GeneName = null ; Wc.lpszclassname = m_szwindowclass; if (! Registerclass (&)

WC)) {MessageBox (NULL, "Failed to Register THE WINDOW Class!", "Error", MB_ok | MB_ICONEXCLAMATION; RETURN 0;} if (! this-> ChangeDisplaySettings (Width, Height, Bits)) Return 0; IF (m_bFullScreen) {dwExStyle = WS_EX_APPWINDOW; dwStyle = WS_POPUP; // pop up suddenly ShowCursor (false); // hide the mouse above} else {// WS_EX_APPWINDOW form the uppermost layer of the taskbar, WS_EX_WINDOWDGE develop forms the border prominent dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle = WS_OVERLAPPEDWINDOW;} // window style form is resized according to the actual use of the customer area is covered, the full screen of invalid AdjustWindowRectEx (& windowRect, dwStyle, FALSE, dwExStyle); // create a form m_hWnd =: : CreateWindowEx (dwExStyle, m_szWindowClass, m_szTitle, dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, // window position WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, m_hInstance, NULL); if (Null == m_hwnd) {this-> killwindow (); MessageBox (Null, "Window Creation Error!", "Error", MB_ok | MB_ICONEXCLAMATION; RETURN 0;} if (null ==: Getdc ( m_hwnd)))) This-> Killwindow (); MessageBox (NULL, "Can't create a GL Device Context!", "Error", MB_OK | MB_ICONEXCLAMATION; RETURN 0;}}}}}}}}}}} // Rendering the description table and device description table related IF (! (M_hrc = wglcreatecontext (m_hdc))) {this-> killwindow (); MessageBox (Null, "Can't create a gl rendering context!", "Error" , MB_OK | MB_ICONEXCLAMATION; RETURN 0;} // Set the current rendering description table if (! WGLmakecurrent (m_hrc)) {this-> killwindow (); MessageBox (null, "can't activate the gl rendering context", "Error", MB_OK | MB_ICONEXCLAMATION; RETURN 0;} // Display Form ShowWindow (M_HWND, SW_SHOW);

SetForegroundWindow (m_hwnd); // Improve the priority setfocus (m_hwnd) of the current form; // Setting the focus form THIS-> Resizescene (width, height); // Set the OpenGL form size if (! This-> initializegl) )) {This-> killwindow (); :: MessageBox (Null, "Failed to Initialize The Window!", "Error", MB_OK | MB_ICONEXCLAMATION); Return 0;} Return 1;} int Copengl :: InitializeGL () { GlclearColor (0.0, 0.0, 0.0); GLCLEARDEPTH (1.0); // Depth Cache Glshademodel (GL_SMOOTH); // GL_SMOOTH smoothing, GL_FLAT constant color glenable (GL_DEPTH_TEST); // Open Deep Test GLDEPTHFUNC (GL_LEQUAL); / / GL_LEQUAL smaller than the Z value is equal to the stored set point to allow rendering // prompt implementation details, the control behavior OpenGL revised and designated P162 // GL_PERSPECTIVE_CORRECTION_HINT color interpolation perspective // ​​GL_NICEST the image quality is preferably glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return 1;} void COpenGL :: ResizeScene (GLsizei width, GLsizei height) {if (0 == height) height = 1; glViewport (0, 0, width, height); // given viewport glMatrixMode (GL_PROJECTION); glLoadIdentity ( ); // Initialize the projection matrix // set the projection body to prevent the graphic variation when the window size changes, the second parameter value is the aspect ratio of the vision than Gluperspective (GLFLOAT) 45.0, (GLFLOAT) Width / (GLFLOAT) Height (GLFLOAT) 1.0, (GLFLOA t) 100.0); glMatrixMode (GL_MODELVIEW);} int COpenGL :: RenderingScene (void) {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color buffer and depth buffer glLoadIdentity (); // initialize the model view matrix return 1;} void CopENGL :: KillWindow (Void) {if (m_bfullscreen) {:: changeisplaySettings (null, 0); // Return desktop showcursor (TRUE); // Display mouse} if (m_hrc) {if (! WGLmakecurrent (null, null) Isolate the RC and DC tests to successfully separate {MessageBox (Null, "Release Of DC and RC Failed!", "Shutdown Error", MB_OK | MB_ICONIONFORMATION);}} (! WGLDELETECONTEXT (M_HRC)) // Release RC { MessageBox (NULL, "Release Rendering Context Failed!", "Shutdown Error", MB_OK | MB_ICONITION;} m_hrc = null;} IF (M_HDC &&

! ReleaseDC (M_HWND, M_HDC)) // && operator from left to right, release DC {MessageBox (Null, "Release Device Context Failed!", "Shutdown Error", MB_OK | MB_ICONICONFORMATION; M_HDC = NULL;} IF m_hWnd && DestroyWindow (m_hWnd)) // window is destroyed! {MessageBox (NULL,, "Shutdown Error", MB_OK | MB_ICONINFORMATION) "Could Not Release hWnd!"; m_hWnd = NULL;} if (UnregisterClass (m_szWindowClass, m_hInstance!)) {MessageBox (NULL, "Could Not Unregister Class!", "Shutdown Error", MB_OK | MB_ICONINFORMATION); m_hInstance = NULL;}} void COpenGL :: SwapBuffers () {:: SwapBuffers (m_hDC);} int COpenGL :: ChangeDisplaySettings (int width, int height, int bits) {if (m_bFullScreen) {DEVMODE dmScreenSettings; memset (& dmScreenSettings, 0, sizeof (DEVMODE)); dmScreenSettings.dmSize = sizeof (DEVMODE); dmScreenSettings.dmBitsPerPel = bits; // bit pixel DMSCREENSETTINGS.DMPELSWIDTH = Width; // Screen width dmscreensettings.dmpelsheight = height; // screen high DMSCREENSETTINGS.DMFIELDS = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; IF (DISP _CHANGE_SUCCESSFUL! = :: ChangeDisplaySettings (& dmScreenSettings, CDS_FULLSCREEN)) // switch the display mode {if (IDYES == MessageBox (NULL, "The Requested FullScreen Mode Is Not" "Supported by / nYour Video Card. Use Windowed Mode Instead?", "My Windows OpenGL Frame", MB_YESNO | MB_ICONEXCLAMATION)) {m_bFullScreen = false;} else {MessageBox (NULL, "Program Will Now Close", "ERROR", MB_OK | MB_ICONSTOP); return 0;}}} return 1;} INT CopENGL :: SetPixelformat (Pixelformatdescriptor * PFD) {Gluint Pixelformat = 0; // Pixel Format Memcpy (&

M_PFD, PFD, SIZEOF (PixelformatDescriptor); // Find no suitable pixel format IF (! (pixelformat = :: choosepixelformat (m_hdc, pfd))) {this-> killwindow (); MessageBox (null, "can) T Find A SuiTable Pixel Format! "," Error ", MB_ok | MB_ICONEXCLAMATION; RETURN 0;} // Set pixel format IF (! :: setpixelformat (m_hdc, pixelformat, pfd)) // Set pixel format {this-> KillWindow (); MessageBox (NULL, "Can not Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return 0;} return 1;} COpenGLFrame.h #ifndef OPENGLFRAME_H # define OPENGLFRAME_H #include #include #include #ENDIF OpenGLFrame_H / CopengLFrame.cpp #include "OpenGLFrame.h" #include "Copengl.h" Copengl TheopenGL; LRESULT CALLBACK MSGPROC (hwnd, uint , WPARAM, LPARAM); int WINAPI WinMain (hINSTANCE hInstance, hINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) {MSG msg; bool done = false; if (IDNO == MessageBox (NULL, "Would You Like To Run In Fullscreen Mode? "," START FULLSCREEN? ", MB_YESNO | MB_ICONQUESTION) TheopENGL.M_ Bfullscreen = false; if (! theopengl.createglwindow (800, 600, 16)) Return 0; While (! done) {if (PEEKMESSAG, NULL, 0, 0, PM_REMOVE)) / / Waiting message { if (WM_QUIT == msg.message) done = true; else {TranslateMessage (& msg); DispatchMessage (& msg);}} else {if (theOpenGL.m_bActive) {if (theOpenGL.m_bKeyState [VK_ESCAPE]) // If the ESC is Press DONE = true; Else / / Otherwise, update screen images {TheopenGl.renderingscene (); theopengl.swapBuffers ();

转载请注明原文地址:https://www.9cbs.com/read-130850.html

New Post(0)