Establish a dialog-based project (name: OpenGL)
And join the library OpenGL32.lib glu32.lib Glaux.lib Glu32.lib Glaux.lib
Add a Picture Control Control for dialog, ID: IDC_Render
Main procedures:
// OpenGLDLG.CPP: Implementation File //
#include "stdafx.h" #include "opengl.h" #include "opengldlg.h"
#include
#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
/// Caboutdlg Dialog Used for App About
Class Caboutdlg: public cdialog {public: capoutdlg ();
// Dialog Data // {{AFX_DATA (Caboutdlg) Enum {IDD = IDD_ABOUTBOX}; //}} AFX_DATA
// classwizard generated virtual function overrides // {{AFX_VIRTUAL (Caboutdlg) protected: Virtual Void DodataExchange (CDATAEXCHANGE * PDX); // DDX / DDV Support //}} AFX_VIRTUAL
// ImplementationProtace: // {{AFX_MSG (Caboutdlg) //}} AFX_MSG DECLARE_MESSAGE_MAP ()};
Caboutdlg :: Caboutdlg (): CDIALOG (Caboutdlg :: Idd) {/ {{AFX_DATA_INIT (Caboutdlg) //}} AFX_DATA_INIT
}
Void Caboutdlg :: DODATAEXCHANGE (CDataExchange * PDX) {cdialog :: DODATAEXCHANGE (PDX); // {{AFX_DATA_MAP (CaboutDLG) //}} AFX_DATA_MAP}
Begin_MESSAGE_MAP (Caboutdlg, cdialog) // {{AFX_MSG_MAP (Caboutdlg) // No Message Handlers //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
/// CopENGLDLG DIALOG
COpenGLDlg :: COpenGLDlg (CWnd * pParent / * = NULL * /): CDialog (COpenGLDlg :: IDD, pParent) {// {{AFX_DATA_INIT (COpenGLDlg) // NOTE: the ClassWizard will add member initialization here //}} AFX_DATA_INIT // Note That Loadicon Does Not Require a Subsequent Destroyicon in Win32
Pixelformat = 0; m_yrotate = 0; m_hicon = AFXGetApp () -> loadicon (idR_mainframe);}
void COpenGLDlg :: DoDataExchange (CDataExchange * pDX) {CDialog :: DoDataExchange (pDX); // {{AFX_DATA_MAP (COpenGLDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}} AFX_DATA_MAP} BEGIN_MESSAGE_MAP (COpenGLDlg, CDIALOG) / / {{AFX_MSG_MAP (Copengldlg) ON_WM_SYSCOMMAND () ON_WM_PAINT () ON_WM_QUERYDRAGICON () ON_WM_TIMER () //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
/// Copengldlg Message Handlers
Bool CopENGLDLG :: OnNitdialog () {cdialog :: oninitdialog ();
// Add "About ..." Menu Item to System Menu.
// IDM_ABOUTBOX MUST BE IN THE SYSTEM Command Range. Assert ((idm_aboutbox & 0xff0) == idm_aboutbox; assert (idm_aboutbox <0xf000);
CMenu * pSysMenu = GetSystemMenu (FALSE); if (pSysMenu = NULL!) {CString strAboutMenu; strAboutMenu.LoadString (IDS_ABOUTBOX); if {pSysMenu-> AppendMenu (MF_SEPARATOR) (strAboutMenu.IsEmpty ()!); PSysMenu-> AppendMenu ( MF_String, IDM_AboutBox, Straboutmenu);}}
// set the icon for this dialog. The framework does this automatic or //en the application's main window is not a dialog seticon (m_hicon, true); // set big icon seticon (m_hicon, false); // set small icon / // OPENGL INIT / CWnd * wnd = GetDlgItem (IDC_RENDER); hrenderDC = :: GetDC (wnd-> m_hWnd); if (SetWindowPixelFormat (hrenderDC) == FALSE) return 0; if (CreateViewGLContext (hrenderDC) == FALSE) return 0; GLPOLYGONMODE (GL_FRONT, GL_FILL); GLPOLYGONMODE (GL_BACK, GL_FILL); ///
glEnable (GL_TEXTURE_2D); glShadeModel (GL_SMOOTH); glViewport (0,0,259,231); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (45,1,0.1,100.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glShadeModel (GL_SMOOTH) ; // Enable Smooth Shading glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth (1.0f); // Depth Buffer Setup glEnable (GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc (GL_LEQUAL) ; // the Type of Depth Testing to do / glenableclientState (GL_VERTEX_ARRAY); GLENABLECLIENTSTATE (GL_TEXTURE_COORD_ARRAY); SetTimer (1, 10, 0);
// Todo: Add Extra Initialization Here Return True; // Return True UnsS you set the focus to a control}
void COpenGLDlg :: OnSysCommand (UINT nID, LPARAM lParam) {if ((nID & 0xFFF0) == IDM_ABOUTBOX) {CAboutDlg dlgAbout; dlgAbout.DoModal ();} else {CDialog :: OnSysCommand (nID, lParam);}}
// if you add a minimize button to your dialog, you will need the code Below // to draw the icon. For mfc Applications Using the document / view model, // this is automaticly done for you by the framework.
Void CopENGLDLG :: OnPaint () {if (isiconic ()) {CPAINTDC DC (this); // Device Context for Painting
SendMessage (WM_ICONERASEBKGND, (WPARAM) dc.getsafehdc (), 0);
// Center icon in client rectangle int cxIcon = GetSystemMetrics (SM_CXICON); int cyIcon = GetSystemMetrics (SM_CYICON); CRect rect; GetClientRect (& rect); int x = (rect.Width () - cxIcon 1) / 2; int y = (Rect.height () - Cyicon 1) / 2;
// Draw the icon dc.drawicon (x, y, m_hicon);} else {cdialog :: onpaint ();}}
// The system calls this to obtain the cursor to display while the user drags // the minimized window.HCURSOR COpenGLDlg :: OnQueryDragIcon () {return (HCURSOR) m_hIcon;} BOOL COpenGLDlg :: SetWindowPixelFormat (HDC hDC) {PIXELFORMATDESCRIPTOR pixelDesc;
Pixeldesc.nsize = sizeof (pixelformatdescriptor); pixeldesc.nversion = 1;
Pixeldesc.dwflags = pfd_draw_to_window | pfd_support_opengl | pfd_doublebuffer | PFD_TYPE_RGBA;
pixelDesc.iPixelType = PFD_TYPE_RGBA; pixelDesc.cColorBits = 32; pixelDesc.cRedBits = 0; pixelDesc.cRedShift = 0; pixelDesc.cGreenBits = 0; pixelDesc.cGreenShift = 0; pixelDesc.cBlueBits = 0; pixelDesc.cBlueShift = 0; pixelDesc. cAlphaBits = 0; pixelDesc.cAlphaShift = 0; pixelDesc.cAccumBits = 0; pixelDesc.cAccumRedBits = 0; pixelDesc.cAccumGreenBits = 0; pixelDesc.cAccumBlueBits = 0; pixelDesc.cAccumAlphaBits = 0; pixelDesc.cDepthBits = 0; pixelDesc.cStencilBits = 1; Pixeldesc.cauxbuffers = 0; Pixeldesc.ilyertype = PFD_MAIN_PLANE; Pixeldesc.breserved = 0; Pixeldesc.dwLayermask = 0; Pixeldesc.dwvisiblemask = 0; Pixeldesc.dwdamageMask = 0;
PixelFormat = ChoosePixelFormat (hDC, & pixelDesc); if (PixelFormat == 0) // Choose default {PixelFormat = 1; if (DescribePixelFormat (hDC, PixelFormat, sizeof (PIXELFORMATDESCRIPTOR), & pixelDesc) == 0) {return FALSE;}}
IF (setpixelformat (hdc, pixelformat, & pixeldesc) == false)
{Return False;}
Return True;}
Bool CopENGLDLG :: CreateViewGlcontext (HDC HDC) {HRENDERRC = WGLCReateContext (HDC);
IF (HRENDERRC == NULL) RETURN FALSE;
IF (WGLmakecurrent (HDC, HRENDERRC) == false) Return False;
Return True;}
Void CopENGLDLG :: Renderscene () {
/ GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); glTranslatef (0.0f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0 glRotated (m_yRotate, 0.0, 1.0, 0.0); glBegin (GL_TRIANGLES ); // Drawing Using Triangles GlvertEX3F (0.0F, 1.0F, 0.0F); // Top GlvertEX3F (-1.0F, -1.0F, 0.0F); // bottom LEFT GLVERTEX3F (1.0F, -1.0F, 0.0 f); // bottom right glend (); // finished drawing the Triangle swapBuffers (HRENDERDC);
Void CopENGLDLG :: ONTIMER (uint nidevent) // Real-time Draw Scene {// Todo: Add Your Message Handler Code Here and / or Call Default Renderscene (); M_YROTATE = 3; cdialog :: Ontimer (nidevent);}
// OpenGLDLG.H: Header file //
#if! defined (AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED _) # Define AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
/// CopENGLDLG DIALOG
Class CopENGLDLG: PUBLIC CDIALOG {// Constructionpublic: CopengLDLG (CWND * PParent = NULL); // Standard Constructionor
Bool setWindowPixelformat (HDC HDC); // Set the pixel format BOOL CreateViewGlContext (HDC HDC); // View GL Context Void Renderscene (); // Draw Scenario
HDC HRENDERDC; / / DC HGLRC HRENDERRC; / / RC FLOAT M_YROTATE; // Speed
INT Pixelformat;
// Dialog Data // {{AFX_DATA (CopENGLDLG) ENUM {IDD = IDD_OPENGL_DIALOG}; // Note: The classwizard will add data members here //}} AFX_DATA
// classwizard generated virtual function overrides // {{AFX_VIRTUAL (COPENGLDLG) Protected: Virtual Void DodataExchange (CDATAEXCHANGE * PDX); // DDX / DDV Support ///}} AFX_VIRTUAL
// ImplementationProtace: hicon m_hicon;
// Generated message map functions // {{AFX_MSG (COpenGLDlg) virtual BOOL OnInitDialog (); afx_msg void OnSysCommand (UINT nID, LPARAM lParam); afx_msg void OnPaint (); afx_msg HCURSOR OnQueryDragIcon (); afx_msg void OnTimer (UINT nIDEvent) ; //}} AFX_MSG DECLARE_MESSAGE_MAP ()}; // {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.
#ndif //! defined (AFX_OPENGLDLG_H__8E962FCE_4DD3_4AE0_BA13_D93DE3FBA4A1__INCLUDED_)
http://www.7forever.com/rwdata/temp/opengl.rar
(Note: Since the hyperlink address is all, it may be deleted, if you can't, please contact me)
Mailto: zhucde@163.com