[OpenGL]使用OpenGL顯示文字 - 標準英文字

此處參考了Nehe的L13來進行實作。

繪製的部份一樣是在DrawScene裡面處理,但需新增方便用來顯示文字的glPrint()。
建立OpenGL繪圖視窗的方式可以參考[MFC]使用Picture Control來當做DrawScene

建立流程如下:
1.先建立可以存放字集的變數於header檔內。
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
 //================ Data member ==================  
 //------------------ Set Font Head ---------------  
 //BOOK:Draw Font  
         GLuint m_gluiBaseDisplayList;       //For the font set  
                                             //base will hold the number of the first display list we create. Each character requires it's own display list.   
                                             //The character 'A' is 65 in the display list, 'B' is 66, 'C' is 67, etc. So 'A' would be stored in display list base+65.   
 //------------------ Set Font Foot ---------------  
 ....  
 };  

2.接下來的是產生字表的glBuildFont()。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
  protected:  
   GLvoid glBuidFont(GLvoid);  
 ....  
 };  

CPP:
 GLvoid COpenGLDrawer::glBuidFont(GLvoid)  
 {  
     HFONT hFont;  
     HFONT holdFont;    //stores the previously used font.   
                        //SelectObject will return the font (or pen, or filler, or whatever GDI objects) that was previously set when it switches to the new font.  
                        //The way that GDI works is such that the use of the return value of SelectObject is not very apparent.   
                        //At first glance it looks as if the code is selecting the new font and returning a pointer and storing it within oldFont.   
     this->m_gluiBaseDisplayList = glGenLists(CHAR_LIST_NUM);                //Storage for 96 characters (NEW)  
     hFont = ::CreateFont(        FONT_HIEGHT,    //Height of Font (NEW)  
                                                  //By putting a minus, we're telling windows to find us a font based on the CHARACTER height.   
                                                  //If we use a positive number we match the font based on the CELL height.   
                                            0,    //Width of Font  
                                            0,    //Angle of Escapement  
                                            0,    //Orientation Angle  
                                    FW_NORMAL,    //Font Weight  
                                                  //You can put a number from 0 - 1000 or you can use one of the predefined values.   
                                                  //FW_DONTCARE is 0, FW_NORMAL is 400, FW_BOLD is 700 and FW_BLACK is 900.   
                                                  //There are alot more predefined values, but those 4 give some good variety. The higher the value, the thicker the font (more bold).   
                                        FALSE,    //Italic  
                                        FALSE,    //Underline  
                                        FALSE,    //Strikeout  
                                 ANSI_CHARSET,    //Character Set Identifier  
                                                  //Character set Identifier describes the type of Character set you wish to use.   
                                                  //There are too many types to explain.   
                                                  //CHINESEBIG5_CHARSET, GREEK_CHARSET, RUSSIAN_CHARSET, DEFAULT_CHARSET, etc.   
                                                  //ANSI is the one I use, although DEFAULT would probably work just as well.  
                                                  //If you're interested in using a font such as Webdings or Wingdings,   
                                                  //        you need to use SYMBOL_CHARSET instead of ANSI_CHARSET.   
                                OUT_TT_PRECIS,    //Output Precision  
                                                  //It tells Windows what type of character set to use if there is more than one type available.   
                                                  //OUT_TT_PRECIS tells Windows that if there is more than one type of font to choose from with the same name,   
                                                  //        select the TRUETYPE version of the font. Truetype fonts always look better, especially when you make them large.   
                                                  //You can also use OUT_TT_ONLY_PRECIS, which ALWAYS trys to use a TRUETYPE Font.   
                          CLIP_DEFAULT_PRECIS,    //Clipping Precision  
                                                  //Clipping Precision is the type of clipping to do on the font if it goes outside the clipping region.   
                                                  //Not much to say about this, just leave it set to default.   
                          ANTIALIASED_QUALITY,    //Output Quality  
                                                  //Output Quality is very important. You can have PROOF, DRAFT, NONANTIALIASED, DEFAULT or ANTIALIASED.   
                                                  //We all know that ANTIALIASED fonts look good :)   
                                                  //Antialiasing a font is the same effect you get when you turn on font smoothing in Windows. It makes everything look less jagged.   
                    FF_DONTCARE|DEFAULT_PITCH,    //Family and Pitch  
                                                  //Next we have the Family and Pitch settings.   
                                                  //For pitch you can have DEFAULT_PITCH, FIXED_PITCH and VARIABLE_PITCH,   
                                                  //    and for family you can have FF_DECORATIVE, FF_MODERN, FF_ROMAN, FF_SCRIPT, FF_SWISS, FF_DONTCARE.   
                                                  //Play around with them to find out what they do. I just set them both to default.   
                            DISPLAY_FONT_TYPE     // Font Name  
                                                  //Can replace the value with the font you would rather use.  
             );  
     holdFont = (HFONT)::SelectObject(this->m_hdc, hFont);          //Selects the Font we want.  
     wglUseFontBitmaps(this->m_hdc, CHAR_LIST_START_POINT, CHAR_LIST_NUM, this->m_gluiBaseDisplayList);        //Build 96 Characters Starting at Character 32.  
     ::DeleteObject(hFont);                                                                                    //Delete the Font  
 }  

3.當然也是需要釋放記憶體用的glKillFont。呼叫時機就看使用情況,目前是在Desctructor時進行。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
  protected:  
   GLvoid glKillFont(GLvoid);  
 ....  
 };  

CPP:
GLvoid COpenGLDrawer::glKillFont(GLvoid)
{
glDeleteLists(this->m_gluiBaseDisplayList, CHAR_LIST_NUM); //Delete all 96 Characeters
}

4.用來繪製文字的glPrint(),作者為了方便使用,就包裝成printf()模式。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
  protected:  
   GLvoid glPrint(const char* szFormat, ...);  
 ....  
 };  

CPP:
 GLvoid COpenGLDrawer::glPrint(const char* szFormat, ...)  
 {  
     char szText[256];        //Holds Our String  
     va_list argList;            //Pointer to List of arguments  
     if ( szFormat == NULL ) {  
         return; };  
     va_start(argList, szFormat);  
     vsprintf(szText, szFormat, argList);  
     va_end(argList);  
     //We then push the GL_LIST_BIT, this prevents glListBase from affecting any other display lists we may be using in our program.   
     //The command glListBase(base-32) is a little hard to explain. Say we draw the letter 'A', it's represented by the number 65.   
     //Without glListBase(base-32) OpenGL wouldn't know where to find this letter.   
     //It would look for it at display list 65, but if base was equal to 1000, 'A' would actually be stored at display list 1065.   
     //So by setting a base starting point, OpenGL knows where to get the proper display list from. The reason we subtract 32 is because we never made the first 32 display lists.   
     //We skipped them. So we have to let OpenGL know this by subtracting 32 from the base value. I hope that makes sense.   
     glPushAttrib(GL_LIST_BIT);                                                                            //Pushes the Display List Bits  
     glListBase( this->m_gluiBaseDisplayList-CHAR_LIST_START_POINT );        //Sets the base Character to 32  
     //Now that OpenGL knows where the Letters are located, we can tell it to write the text to the screen. glCallLists is a very interesting command.   
     //It's capable of putting more than one display list on the screen at a time.  
     //The line below does the following. First it tells OpenGL we're going to be displaying lists to the screen. strlen(text) finds out how many letters we're going to send to the screen.   
     //Next it needs to know what the largest list number were sending to it is going to be. We're not sending any more than 255 characters.   
     //The lists parameter is treated as an array of unsigned bytes, each in the range 0 through 255. Finally we tell it what to display by passing text (pointer to our string).  
     //In case you're wondering why the letters don't pile on top of eachother. Each display list for each character knows where the right side of the letter is.   
     //After the letter is drawn, OpenGL translates to the right side of the drawn letter.   
     //The next letter or object drawn will be drawn starting at the last location GL translated to, which is to the right of the last letter.  
     //Finally we pop the GL_LIST_BIT setting GL back to how it was before we set our base setting using glListBase(base-32).   
     glCallLists(strlen(szText), GL_UNSIGNED_BYTE, szText);                                //Draw the display list text  
     glPopAttrib();                                                                                                    //Pops the Display List Bits  
 }  

5.使用時機就是在DrawScene()裡面處理就可以了,先畫線或是先顯示文字部分,目前測試是都可以。
 void COpenGLDrawer::glDrawScene(void)  
 {  
 //**************** Draw Grid Head *******************  
     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);            //Set polygon rasterization mode  
     glBegin(GL_QUADS);                                                                    //Start to draw squares  
     for ( unsigned int i = 0 ; i < this->m_vcdsGrid.size() ; i++ )  
     {  
     //3D  
         //glVertex3f(this->m_vcdsGrid[i].LB.x, this->m_vcdsGrid[i].LB.y, this->m_vcdsGrid[i].LB.z);  
         //glVertex3f(this->m_vcdsGrid[i].RB.x, this->m_vcdsGrid[i].RB.y, this->m_vcdsGrid[i].RB.z);  
         //glVertex3f(this->m_vcdsGrid[i].RT.x, this->m_vcdsGrid[i].RT.y, this->m_vcdsGrid[i].RT.z);  
         //glVertex3f(this->m_vcdsGrid[i].LT.x, this->m_vcdsGrid[i].LT.y, this->m_vcdsGrid[i].LT.z);  
     //2D          
         glVertex2f(this->m_vcdsGrid[i].LB.x, this->m_vcdsGrid[i].LB.y);  
         glVertex2f(this->m_vcdsGrid[i].RB.x, this->m_vcdsGrid[i].RB.y);  
         glVertex2f(this->m_vcdsGrid[i].RT.x, this->m_vcdsGrid[i].RT.y);  
         glVertex2f(this->m_vcdsGrid[i].LT.x, this->m_vcdsGrid[i].LT.y);  
         if ( !this->m_vcdsGrid4AStar[i].IsWarkable ) {  
             this->m_vcdsGrid4DrawBlocking.push_back(m_vcdsGrid4AStar[i]);    };  
     }    //End of for ( unsigned int i = 0 ; i < this->m_vcpGrid.size() ; i++ )  
     glEnd();  
 //**************** Draw Grid Foot *******************  
 //**************** Draw Font Head *******************  
     CString cstrDisplay;  
     SIZE nLen;  
     for ( unsigned int i = 0 ; i < this->m_vcdsGrid4AStar.size() ; i++ )  
     {  
 //Pulsing colors based on text position  
         glColor3f( 1.0f, 1.0f, 1.0f );        //White  
 //Position the text on the screen  
         glRasterPos2f( (this->m_vcdsGrid4AStar[i].cdsGridInfo.LB.x + 0.1f), (this->m_vcdsGrid4AStar[i].cdsGridInfo.LB.y + 0.1f) );        //set to center  
 //Print GL text to the screen  
         this->glPrint("%d", this->m_vcdsGrid4AStar[i].nCostedAP);  
         glRasterPos2f( this->m_vcdsGrid4AStar[i].cdsGridInfo.LT.x + 0.1f, this->m_vcdsGrid4AStar[i].cdsGridInfo.LT.y - 0.2f);  
         this->glPrint("%d", this->m_vcdsGrid4AStar[i].nTotalCostAP);  
         cstrDisplay.Format("%d", this->m_vcdsGrid4AStar[i].nRequiredAP2Target+100);  
         ::GetTextExtentPoint32(this->m_hdc, cstrDisplay, cstrDisplay.GetLength(), &nLen);  
         glRasterPos2f( this->m_vcdsGrid4AStar[i].cdsGridInfo.RB.x - 0.03f*nLen.cx, this->m_vcdsGrid4AStar[i].cdsGridInfo.RB.y + 0.1f);  
         this->glPrint("%d", this->m_vcdsGrid4AStar[i].nRequiredAP2Target+100);  
     }    //End of for ( unsigned int i = 0 ; i < this->m_vcdsGrid.size() ; i++ )  
 //**************** Draw Font Foot *******************  
 }  


此處輸入的是在每個Cell內的左上、左下以及右下塞入各一個數字,結果如下圖:

[WinAPI]計算繪圖文字長度

目前是使用GetTextExtentPoint32()來取得文字長度。

回傳的參數為文字的長度跟高度,單位為Pixel。

測試結果之後再補上。

[Win]服務顯示MsgBox

僅需在使用MsgBox()時,在uType參數要下MB_SERVICE_NOTIFICATION。

同時在hWnd部分,一定要給NULL。

目前並沒有做很多測試,所以限制部分,稍後補上。

目前可以確認的是,僅有Windows XP可以達到此效果。

[MFC]使用Picture Control來當做DrawScene

參考來源:
1.http://blog.sina.com.cn/s/blog_6ac675410100o0t6.html
2.http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c10975__2/Setting-Up-OpenGL-in-an-MFC-Control.htm
PS.內部參數皆先參考範例內資料。

這邊使用的為Dialog-based的FormView。因此在Dialog上拉出一個Picture Control即可。
ID可以隨便設定,但是Visible的屬性要設成false
根據作者的說法,有些狀況下,如果把Visible的屬性設成True的話,那麼在畫面重新繪製的時候,OpenGL繪製的畫面,會被Windows繪製PictureControl的畫面蓋掉。
目前測試手頭機器的確會有這樣的狀況。

需載入的header為以及。並引用opengl32.lib。

接下來要做的,就是新增用來繪製OpenGL用的Class。名稱也是一樣可以自由設定,這邊是使用COpenGLDrawer。
1.需要預先宣告的是Timer的指標以及相關需用來繪圖的成員。
 class COpenGLDrawer :    public CWnd  
 {  
 //================ Data member ==================  
     public:  
         UINT_PTR m_unpTimer;      //timer  
     private:  
 //Window Information - for draw scene  
         CWnd *m_hWnd;           //Handle of parent window  
         HDC m_hdc;              //Handle of DC for OpenGL using  
         HGLRC    m_hrc;         //Handle of RC for OpenGL using  
         int m_nPixelFormat;     //Pixel format  
         CRect m_rect;           //For getting window rect  
         CRect m_rectOldWindow;  //rect of previous displayed window  
         CRect m_rectOrgRect;    //rect of original displayed window  
 };  
相關成員也要記得先初始化,避免使用到錯誤的數值。

2.建立glCreate()。此函式是用來向系統註冊Class、建立OpenGL使用之視窗屬性以及記錄視窗的初始設定。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
 //============== Member Function =================  
     public:  
         COpenGLDrawer(void);  
         ~COpenGLDrawer(void);  
         void glCreate(CRect rect, CWnd *parent);  
 ....  
 };  

CPP:
 void COpenGLDrawer::glCreate(CRect rect, CWnd *parent)  
 {  
     CString cstrClassName = ::AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_OWNDC,  
                                                                                 NULL,  
                                                                                 (HBRUSH)::GetStockObject(BLACK_BRUSH),  
                                                                                 NULL);  
     CreateEx(0, cstrClassName, "OpenGL", WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, rect, parent, 0);  
     //Set initail variable's value  
     this->m_rectOldWindow = rect;  
     this->m_rectOrgRect = rect;  
     this->m_hWnd = parent;  
 }  

3.由於OpenGL是使用Timer來進行畫面的繪製,因此就不適用於MFC所提供的繪製畫面的方法。因此需要跳過Windows繪圖的函式 - OnPaint()(WM_PAINT)。在OnPaint()內,使用ValidateRect()來讓Picture Control不被更新。
 void COpenGLDrawer::OnPaint()  
 {  
     //CPaintDC dc(this); // device context for painting  
     // TODO: Add your message handler code here  
     // Do not call CWnd::OnPaint() for painting messages  
     ValidateRect(NULL);  //Remove all of region of the client from the updatr region
 }  

4.建立產生OpenGL視窗的函式。裡面呼叫用來初始化OpenGL視窗的相關參數。這邊則改寫WM_CREATE之內容。
 int COpenGLDrawer::OnCreate(LPCREATESTRUCT lpCreateStruct)  
 {  
     if (CWnd::OnCreate(lpCreateStruct) == -1)  
         return -1;  
     // TODO: Add your specialized creation code here  
     this->glInitialize();  
     return 0;  
 }  

5.建立初始化OpenGL之函式。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
 //============== Member Function =================  
     public:  
         COpenGLDrawer(void);  
         ~COpenGLDrawer(void);  
         void glCreate(CRect rect, CWnd *parent);  
     protected:  
         void glInitialize(void);  
 };  

CPP:
 void COpenGLDrawer::glInitialize(void)  
 {  
 //Inital Setup  
     static PIXELFORMATDESCRIPTOR pfd =  
     {  
         sizeof(PIXELFORMATDESCRIPTOR),  
         1,  
         PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,  
         PFD_TYPE_RGBA,  
         32,                                //bit depth  
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
         16,                                //z-buffer depth  
         0, 0, 0, 0, 0, 0, 0  
     };  
 //Get device context only once  
     this->m_hdc = GetDC()->m_hDC;  
 //Pixel format  
     this->m_nPixelFormat = ::ChoosePixelFormat(this->m_hdc, &pfd);  
     ::SetPixelFormat(this->m_hdc, this->m_nPixelFormat, &pfd);  
 //Create the OpenGL Rendering Context  
     this->m_hrc = ::wglCreateContext(this->m_hdc);  
     ::wglMakeCurrent(this->m_hdc, this->m_hrc);  
 //Basisc Setup:  
     //Set color to use when clearing the background  
     ::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  
     ::glClearDepth(1.0f);  
     //Turn on backface culling  
     ::glFrontFace(GL_CCW);  
     ::glCullFace(GL_BACK);  
     //Turn on depth testing  
     ::glEnable(GL_DEPTH_TEST);  
     ::glDepthFunc(GL_LEQUAL);  
     //Send draw request  
     this->OnDraw(NULL);  
 }  

6.建立OnDraw函式。此函式內處理圖形的移動以及旋轉,也就是鏡頭的控制。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
 // View information variables if camera - for mouse move  
         float m_fLastX;  
         float m_fLastY;  
         float m_fPosX;  
         float m_fPosY;  
         float m_fZoom;  
         float m_fRotX;  
         float m_fRotY;  
 //============== Member Function =================  
 ....  
     public:  
         afx_msg void OnDraw(CDC *pDC);  
 ....  
 };  

CPP:
 void COpenGLDrawer::OnDraw(CDC *pDC)  
 {  
   // TODO: Camera controls.  
     glLoadIdentity();  
     glTranslatef( 0.f, 0.f, -this->m_fZoom );  
     glTranslatef( this->m_fPosX, this->m_fPosY, 0.f);  
     glRotatef( this->m_fRotX, 1.f, 0.f, 0.f );  
     glRotatef( this->m_fRotY, 0.f, 1.f, 0.f);  
 }  

7.新增圖像更新用的OnTimer()(WM_TIMER)。
 void COpenGLDrawer::OnTimer(UINT_PTR nIDEvent)  
 {  
     // TODO: Add your message handler code here and/or call default  
     switch (nIDEvent)  
     {  
         case 1:          
         //Clear color and depth buffer bits  
             ::glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  
         //Draw OpenGL scene  
             this->glDrawScene();  
         //Swap buffers  
             ::SwapBuffers(this->m_hdc);  
             break;  
         default:  
             break;  
     }    //End of switch (nIDEvent)  
     CWnd::OnTimer(nIDEvent);  
 }  

8.當視窗有需要自由變更大小時,則需改寫OnSize()(WM_SIZE)。
 void COpenGLDrawer::OnSize(UINT nType, int cx, int cy)  
 {  
     CWnd::OnSize(nType, cx, cy);  
     // TODO: Add your message handler code here  
     if ( 0>= cx || 0 >= cy || nType == SIZE_MINIMIZED ) {  
         return; };  
 //Resize  
     switch (nType)  
     {  
         case SIZE_MAXIMIZED:        // If widow resize token is "maximize"  
             //get the current window rect  
             GetWindowRect(m_rect);  
             //Move the window accordingly  
             MoveWindow(6, 6, (cx-14), (cy-14));  
             //Get the new window rect  
             GetWindowRect(this->m_rect);  
             //Store our old window as the new rect  
             this->m_rectOldWindow = this->m_rect;  
             break;  
         case SIZE_RESTORED:        //if window resize token is "restore"  
             //If the window is currently maximized  
             if ( this->m_bIsMaximized )  
             {  
                 //Get the current window rect  
                 GetWindowRect(this->m_rect);  
                 //Move the window accordingly ( to our stored old window)  
                 MoveWindow(this->m_rectOldWindow.left, (this->m_rectOldWindow.top-18), (this->m_rectOrgRect.Width()-4), (this->m_rectOrgRect.Height()-4));  
                 //Get the new window rect  
                 GetWindowRect(this->m_rect);  
                 //Store our old window as the new rect  
                 this->m_rectOldWindow = this->m_rect;  
             }    //End of if ( this->m_bIsMaximized )  
             break;  
     }    //End of switch (nType)  
 //Map the OpengGL coordinates  
     glViewport(0, 0, cx, cy);  
 //Projection view  
     glMatrixMode(GL_PROJECTION);  
     glLoadIdentity();  
 //Set our current view perspective  
     gluPerspective(35.0f, (float)cx / (float)cy, 0.01f, 2000.0f);  
 //Mode view  
     glMatrixMode(GL_MODELVIEW);  
 }  

當然也是要改寫Dialog的OnSize()。再由Dialog來呼叫OpenGL class來進行重新繪製。
 void CMainDlg::OnSize(UINT nType, int cx, int cy)  
 {  
     CDialog::OnSize(nType, cx, cy);  
     // TODO: Add your message handler code here  
     switch(nType)  
     {  
         case SIZE_RESTORED:  
             if ( this->m_glWindow.m_bIsMaximized ) {  
                 this->m_glWindow.OnSize(nType, cx, cy);  
                 this->m_glWindow.m_bIsMaximized = false;  
             }    //End of if ( this->m_glWindow.m_bIsMaximized )  
             break;  
         case SIZE_MAXIMIZED:  
             this->m_glWindow.OnSize(nType, cx, cy);  
             this->m_glWindow.m_bIsMaximized = true;  
             break;  
     }    //End of switch(ntype)  
 }  

9.當Timer設定完成後,即可定期的去做任何的更新動作。所以如有需要於畫面上繪製圖形,則需自行新增繪圖函式,並在OnTimer()內呼叫。此處宣告的繪圖函式為glDrawScene()。此處繪製一六面體。
Header:
 class COpenGLDrawer :    public CWnd  
 {  
 ....  
     protected:  
         void glInitialize(void);  
         void glDrawScene(void);  
 };  

CPP:
 void COpenGLDrawer::glDrawScene(void)  
 {  
 //Wireframe Mode  
     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  
     glBegin(GL_QUADS);  
 //Top side  
     glVertex3f( 1.0f, 1.0f, 1.0f );  
     glVertex3f( 1.0f, 1.0f, -1.0f );  
     glVertex3f( -1.0f, 1.0f, -1.0f );  
     glVertex3f( -1.0f, 1.0f, 1.0f );  
 //Bottom side  
     glVertex3f( -1.0f, -1.0f, -1.0f );  
     glVertex3f( 1.0f, -1.0f, -1.0f );  
     glVertex3f( 1.0f, -1.0f, 1.0f );  
     glVertex3f( -1.0f, -1.0f, 1.0f );  
 //Front side  
     glVertex3f( 1.0f, 1.0f, 1.0f );  
     glVertex3f( -1.0f, 1.0f, 1.0f );  
     glVertex3f( -1.0f, -1.0f, 1.0f );  
     glVertex3f( 1.0f, -1.0f, 1.0f );  
 //Back side  
     glVertex3f( -1.0f, -1.0f, -1.0f );  
     glVertex3f( -1.0f, 1.0f, -1.0f );  
     glVertex3f( 1.0f, 1.0f, -1.0f );  
     glVertex3f( 1.0f, -1.0f, -1.0f );  
 //Left side  
     glVertex3f( -1.0f, -1.0f, -1.0f );  
     glVertex3f( -1.0f, -1.0f, 1.0f );  
     glVertex3f( -1.0f, 1.0f, 1.0f );  
     glVertex3f( -1.0f, 1.0f, -1.0f );  
 //Right side  
     glVertex3f( 1.0f, 1.0f, 1.0f );  
     glVertex3f( 1.0f, -1.0f, 1.0f );  
     glVertex3f( 1.0f, -1.0f, -1.0f );  
     glVertex3f( 1.0f, 1.0f, -1.0f );  
     glEnd();  
 }  

10.如有需控制物體的選轉、移動,則需改寫OnMouseMove()(WM_MOUSEMOVE)。
 void COpenGLDrawer::OnMouseMove(UINT nFlags, CPoint point)  
 {  
     // TODO: Add your message handler code here and/or call default  
     int diffX = (int)(point.x - this->m_fLastX);  
     int diffY = (int)(point.y - this->m_fLastY);  
     this->m_fLastX = (float)point.x;  
     this->m_fLastY = (float)point.y;  
 //Left mouse button  
     if ( nFlags & MK_LBUTTON )  
     {  
         this->m_fRotX += (float)0.5f * diffY;  
         if ( (this->m_fRotX > 360.0f) || (this->m_fRotX < -360.0f) )    {  
             this->m_fRotX = 0.f;    };  
         this->m_fRotY += (float)0.5f * diffX;  
         if ( (this->m_fRotY > 360.f) || (this->m_fRotY < -360.f) ) {  
             this->m_fRotY = 0.f; };  
 //Right mouse button  
     }else if ( nFlags & MK_RBUTTON ){  
         this->m_fZoom -= (float)0.1f * diffY;  
 //Middle mouse button  
     }else if ( nFlags & MK_MBUTTON ){  
         this->m_fPosX += (float)0.05f * diffX;  
         this->m_fPosY -= (float)0.05f * diffY;  
     }    //End of if ( nFlags & MK_LBUTTON )  
     this->OnDraw(NULL);  
     CWnd::OnMouseMove(nFlags, point);  
 }  

如果希望可以透過滑鼠滾輪來縮放大小,則一樣要改寫OnMouseWheel()(WM_MOUSEWHEEL)。但目前測試結果為WM_MOUSEWHEEL會先被Dialog所接收,所以這邊改寫的OnMouseWheel()並不會被message queue呼叫。
因此,也一併改寫Dialog的OnMouseWheel(),當收到message時,則呼叫COpenGLDrawer的OnMouseWheel()來達到縮放大小之結果。
COpenGLDrawer.cpp
 BOOL COpenGLDrawer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  
 {  
     // TODO: Add your message handler code here and/or call default  
     int diffX = (int)(pt.x - this->m_fLastX);  
     int diffY = (int)(pt.y - this->m_fLastY);  
     this->m_fLastX = (float)pt.x;  
     this->m_fLastY = (float)pt.y;  
     if ( zDelta == WHEEL_DELTA )                    //Rolls to forward - Zoom in  
     {  
         this->m_fZoom -= (float)0.05f * diffY;  
     }else if (zDelta == -WHEEL_DELTA) {        //Rolls to back - Zoom out  
         this->m_fZoom += (float)0.05f * diffY;  
     }    //End of if ( zDelta > 0 )   
     this->OnDraw(NULL);  
     return CWnd::OnMouseWheel(nFlags, zDelta, pt);  
 }  

CMainDlg.cpp
 BOOL CMainDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  
 {  
     // TODO: Add your message handler code here and/or call default  
     this->m_glWindow.OnMouseWheel(nFlags, zDelta, pt);  
     return CDialog::OnMouseWheel(nFlags, zDelta, pt);  
 }  

不使用時,也是要釋放記憶體。
 COpenGLDrawer::~COpenGLDrawer(void)  
 {  
     wglMakeCurrent(NULL, NULL);  
     wglDeleteContext(this->m_hrc);
     DeleteDC(this->m_hdc);
 }  

到上述步驟完成後,OpenGL繪圖用的Class就已經完成了。接下來就要到主要的Dialog來使用此class。

11.建立OpenGL物件,以及設定參數。
Header:
 protected:  
     COpenGLDrawer m_glWindow;  

CPP:
 BOOL CMainDlg::OnInitDialog()  
 {  
     CDialog::OnInitDialog();  
     // Add "About..." menu item to system menu.  
     // IDM_ABOUTBOX must be in the system command range.  
     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
     ASSERT(IDM_ABOUTBOX < 0xF000);  
     CMenu* pSysMenu = GetSystemMenu(FALSE);  
     if (pSysMenu != NULL)  
     {  
         CString strAboutMenu;  
         strAboutMenu.LoadString(IDS_ABOUTBOX);  
         if (!strAboutMenu.IsEmpty())  
         {  
             pSysMenu->AppendMenu(MF_SEPARATOR);  
             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
         }  
     }  
     // Set the icon for this dialog. The framework does this automatically  
     // when the application's main window is not a dialog  
     SetIcon(m_hIcon, TRUE);            // Set big icon  
     SetIcon(m_hIcon, FALSE);        // Set small icon  

     // TODO: Add extra initialization here  
     CRect rect;  
 //Get size and position of the picture control  
     GetDlgItem(IDC_PIC_DRAWDC)->GetWindowRect(rect);  
 //Convert screen coordinates to client coordinate  
     ScreenToClient(rect);  
 //Create OpenGL Control window  
     this->m_glWindow.glCreate(rect, this);  
 //Setup the OpenGL Window's timer to render  
     this->m_glWindow.m_unpTimer = this->m_glWindow.SetTimer(1, 1, 0);  
     return TRUE; // return TRUE unless you set the focus to a control  
 }  

上述完成後,就可以在Picture Control裡繪製圖形了,至於效能部分,就待之後之測試再進行說明。

PS.如有要將FormView當做繪圖區,則只要在進行ScreenToClient()時,塞入FormView的rect即可。

[C]Locate character in string (Memo)

Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

The search does not include the terminating null-characters.

Return Value
A pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if none of the characters of str2 is found in str1 before the terminating null-character.
If none of the characters of str2 is present in str1, a null pointer is returned.

Reference:strpbrk()

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...