OpenGL Chinese display

xiaoxiao2021-03-06  41

"OpenGL Chinese shows Chen Feng (CNFER@sina.com)

-------------------------------------------------- ------------------------------

Source: http://www2.ccw.com.cn/2000/0026/0026B10.ASP

---- This article discusses several ways to display text in OpenGL in this paper.

---- Most programmers use OpenGL more to focus on dynamic 3D graphics applications, so the text in OpenGL is often overlooked, making people feel the feelings of the salary. This article introduces several text display methods, hoping to help with the programmer using OpenGL.

Establish and modify the program - "Create a MFC SDI Windows application engineering Text, except single document properties, using all of the other default selections. In the menu Project Open the Settings dialog, add OpenGL32.lib Glu32.lib Glaux.lib GL library in the Object / Library Modules edit box of the LINK Properties page. We use these library functions to complete graphics editing. ---- To make the SDI application generated by the VC AppWizard can use OpenGL drawing, you also need to make some modifications, explain the following.

---- 1. Introduce PrecreateWindow functions

---- OpenGL window must have WS_CLIPCHILDREN (Windows style used by the parent window, the area covered when the clipping sub-window is used) and WS_CLIPIBLINGS (Windows style used to create sub-windings for redrawing other children) The area covered by the window is two style. In addition, window class properties cannot include a CS_ParentDC style. The specific procedure is achieved as follows:

Bool CTextview :: PrecreateWindow (CreateStruct & Cs) {// Todo: Modify The window class or styles Here by modify // The createStruct CS

// An OpenGL WINDOW MUST BE CREATED with THE FOLLOWING FLAG / / AND MUST NOT INCLUDE CS_PARENTIDC for The Class Style. Cs.style | = WS_CLIPSIBLINGS | WS_CLIPCHILDREN

Return CView :: PrecreateWindow (CS);

---- 2. Define pixel format PixelFormat and create RC in the oncreate function

---- To enable the window to support the OpenGL drawing, you must initialize the window. These include defining pixel format PixelFormat and creating RCs, specifying a suitable pixel format for OpenGL, create colored contexts and associates the device context of it and windows. Coloring context saves information about the current coloring environment. You can call a self-built viewport member function setuppixelformat () in OnCreate, the specific functions are as follows:

Bool ctextview :: setuppixelformat () {// create a rendering contextcdc * m_pdc = getdc (); if (m_pdc == null) // Failure to Get DC {MessageBox ("Could't get a valid dc."); Return False;}

// Default pixel format is a single-buffered, // OpenGL support hardware-accelerated, RGBA mode formatPIXELFORMATDESCRIPTOR pfd = {sizeof (PIXELFORMATDESCRIPTOR), // Structure size 1, // Structure version number.Property flags (flag characteristics): PFD_DRAW_TO_WINDOW | // Support window pfd_support_opengl | // support opengl pfd_doublebuffer, pfd_type_rgba, // rgba type 24, // 32-bit color.0, 0, 0, 0, 0, 0, // NOT Concerned with these: not involved Attribute 0, // no alpha: No alpha cache 0, // shift bit ignored: ignore the conversion bit 0, 0, 0, 0, 0, // no Accum buffer: No cumulative cache 32, // 32-bit depth Buffer.0, // no stencil: Non-template cache 0, // no auxliliary buffers: non-auxiliary cache pfd_main_plane, // main layer type .: Main layer type 0, // reserved .: Reserved structure 0, 0, 0 // Unsupported .: UNKNOWN structure}; int nPixelFormat = ChoosePixelFormat (m_pDC-> GetSafeHdc (), & pfd); if (nPixelFormat == 0) {MessageBox ( "ChoosePixelFormat failed."); return FALSE;} if (SetPixelFormat (m_pdc-> getsafehdc (), npixelformat, & pfd) == 0) {MessageBox ("setpixelformat failed."); returnaf

IF ((m_hrc = wglcreateContext (m_pdc-> getsafehdc ())) == 0) {MessageBox ("WGLCReateContext Failed."); return false;} if ((wglmakecurrent (m_pdc-> getsafeHDC (), m_hrc)) == 0) {MessageBox ("WGLmakecurrent Failed."); Return False;}

IF (m_pdc) releasedc (m_pdc); return true;}

---- 3. Call the initialization background function in oncreate () function InitializeOpenGL ()

Void ctextview :: initializeopENGL () {GlclearColor (0.2F, 0.2F, 0.2F, 0.0F); GLDEPTHFUNC (GL_SMOD); GLSHADEMODEL (GL_SMOOTH); GLSHADEMODEL (GL_SMOOTH);

---- 4. Start the animation timer in OnCreate ()

Settimer (0,40, null);

---- 5. To recall the scene size when receiving the WM_SIZE message, set the graphic display mode with Onsize --- In order to make the object can be displayed properly, you must proceed and determine the work of the viewport.

void CTextView :: OnSize (UINT nType, int cx, int cy) {CView :: OnSize (nType, cx, cy); // TODO: Add your message handler code here // Save the wide and height of the current window ClientGLsizei NWIDTH = (Glsizei) CX; GLSIZEI NHEIGHT = (Glsizei) cy; ratio = (double) CX / (double) CY;

// coupute the aspect = (gldouble) NWIDTH / (GLDOUBLE) NHEIGHT

glViewport (0,0, nWidth, nHeight); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (FOV, dAspect, NEARPLANE, FARPLANE); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();}

---- 6. Simple call DrawScene () in OnDraw () to perform OpenGL functions

Void ctextview :: Ondraw (CDC * PDC) {ctextdoc * pdoc = getDocument (); assert_valid (pdoc);

// Todo: add draw code for native data here drawscene (); // invalidate ();

---- 7. Delete the context and revoke the timer when revoking the window

Void ctextview :: overdestroy () {cview :: Ondestroy (); // Todo: add your message handler code here // this call makess the current rc NOT CURRENT IF (Wglmakecurrent (0) == false) messageBox (" Wglmakecurrent Failed. "); // delete the RC IF (M_HRC && (M_HRC) == false)) MessageBox (" WGLDeleteContext Fail. "); KillTimer (1);}

---- 8. When the 40ms timer time is time, simply seize the customer of the entire scene is invalid, make it heavy

Void ctextview :: ONTIMER (uint nidevent) {// Todo: add your message handler code Here and / or call defaultInvalidate (); cview ::Ontimer (nidevent);

Modify the interface ---- Delete the File in the menu idR_mainframe to remove all the options outside the Exit menu item, add the "Show GDI text" list creation text "list 3D text" three menu items and assigns them to the appropriate ID. Use classwizard to add a command handler and interface update function to these three menu items, where the corresponding functions of the GDI text are displayed as follows: void ctextview :: overgditext () {// Todo: add Your command handler code her_iwhchtext = 0; invalidate ();

Void ctextview :: onupdategditext (ccmdui * pcmdui) {// Todo: add your command update ui handler code hereif (m_iwhchtext == 0) PCMDUI-> setCheck (); else pcmdui-> setcheck (0);} ---- Increase Draw3dText (), DrawListText () and DrawGditext () three functions are used for three different text drawing methods. Increase the DrawScene () function, which is called by the ONDRAW function to draw the scene. In the DrawScene () function, when m_iwhichtext is 0, 1, 2, DrawGditext (), DrawListText (), and Draw3DText () display text are called separately. The specific function is achieved as follows:

Void ctextview :: Ondraw (CDC * PDC) {ctextdoc * pdoc = getDocument (); assert_valid (pdoc);

// Todo: add draw code for native data here drawscene (); // invalidate ();

void CTextView :: DrawScene () {glClear (GL_COLOR_BUFFERBIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glTranslatef (0.0f, 0.0f, -FARPLANE); // TextureMap (); glPopMatrix (); glPushMatrix (); glTranslatef (0.0f, 0.0F, - (FarPlane NearPlane) / 2);

IF (m_iwhchtext == 1) DrawlistText (); if (m_iwhichtext == 2) Draw3dText (); glpopmatrix (); glfinish (); swapbuffrs (wglgetcurrentdc ());

IF (m_iwhchtext == 0) DrawGditext ();

GDI Display Text ---- Call the wglgetCurrentDC () function gets the current device context, use the TextOut function to display text, but pay attention to the DoubleBuffer mode, the plot function should be called after GLFINISH () and swapbuffrs (WGLGetCurrentDC ()) function. Otherwise it will generate, use the GDI function before drawing OpenGL, you can use the SingleBuffer mode, the specific function is as follows: void ctextview :: DrawGditext () {HDC HDC = wglgetcurrentdc (); :: setBkmode (HDC, Transparent ) ;: setTextColor (HDC, RGB (250, 0)); CSTRING SSTATE ("Show GDI text."); :: Textout (HDC, 5, 5, sState, sState.getLength ());}

WGLUSEFontBitmaps Functions Display Text ---- Using WGLUSEFontBitMaps () puts the ASCII character into the display list, and then display the text using the GlcallLists () function using the display list sequence. WGluseFontBitmaps have four parameters, which are currently used DCs, starting from the first ASCII character to start the list, the number of ASCII characters, and the start list number. GLListBase () Specifies the start list serial number executed by GlcallLists. GlcallLists () contains three parameters: perform the number of list sequences, the type of list value, and the text you want to display. Note If the text you want to display is a string, the information it provides is the offset of the ASCII character, so the finally displayed ASCII character is from the list specified from GLListBase (). The list after the shiftLists (), so the WGLUSEFontBitmaps starts the list parameters from the first ASCII character starting the list parameters, the start list sequence number executed by GLListBase (), and the text parameters to display in GLCALLISTS () You can affect the final display result. Due to the displayed ASCII character, Chinese characters cannot be displayed. The GLRASTERPOS3F function determines the offset of the OpenGL view. Specific functions to achieve the following: void CTextView :: DrawListText () {wglUseFontBitmaps (wglGetCurrentDC (), 0,256,1000); glListBase (1000); glRasterPos3f (-5.0f, 0.0f, 0.0f); glCallLists (20, GL_UNSIGNED _BYTE, " Draw with list text. ");} The WGLUSEFONTOUTOUTLINES function displays 3D text ---- WGLUSEFONTOUTLINES makes OpenGL to display 3D text. Its usage is substantially the same as the WGLUSEFontBitmaps function, but more interpolated calculation parameters, font depth, display mode, and load-bearing cache four parameters, and can only display the TrueType font, and should select the font type before display. Specific functions to achieve the following: void CTextView :: Draw3DText () {GLYPHMETRICSFLOAT agmf [256]; // create display lists for glyphs 0 through 255 // with 0.1 extrusion and default deviation // The display list numbering starts at 1000 (it could. BE Any Number) (WGlgetCurrentDC (), 0,255,1000, 0.3F, 0.8F, WGL_FONT_LINES, AGMF); // SET UP TRANSFORMATION TO DRAW THE STRING GLTRANSLATEF (-15.0F, 0.0F, 0.0F); Glscalef (4.0 f, 4.0f, 4.0f); // Display a string glListBase (1000); // Indicates the start of display lists for the glyphs // Draw the characters in a string glCallLists (26, GL_UNSIGNED_BYTE, "Draw outline list 3D text ");

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

New Post(0)