Neheレッスン13画像フォント



Nehe Lesson 13 Image Font



ソースリンク

美術プログラムの期間、画像に書かれたテキストをロードし、次にこの画像がOpenGLプログラムをロードし、ブレンドオプションを開いてテキストを画面に表示することができます。ただし、このアプローチには非常に時間がかかります。また、選択したフィルターのタイプに応じて、最終結果が非常にぼやけたり、モザイクがたくさんあることがよくあります。加えて、 画像にアルファチャンネルが含まれていない限り それ以外の場合、画面に描画すると、それらの単語は不透明になります(画面内の他のオブジェクトと混合されます)。



投影モデル、-0.5から+0.5の可視範囲から離れた単位。座標系を1つの単位にスクリーンする場合、テキストの位置を-0.5〜 + 0.5X軸の範囲内で調整できます。 10ユニットを掘削する場合、-5から+5の範囲で移動します。指定したテキストの正確な場所の代わりに小数を使用するためのより多くのオプションが提供されます。 glScale(x、y、z)関数を呼び出しても、テキストサイズを変更することはできません。フォントサイズを変更したい場合は、作成時にのみ変更できます。

stdarg.hヘッダーは、テキストを解析し、変数をテキストに変換するために使用されます。



glGenLists(96)を使用して、96個のディスプレイリストのセットを作成しました。
glGenLists(96)

#include // Windows header files #include // contains the latest gl.h, glu.h library #include // include OpenGL utility library #include // variable parameter used to define header files #include //vsprintf #include //sin cos HGLRC hRC = NULL // Window handle colored description HDC hDC = NULL // render the device description table handle HWND hWND = NULL // window handle HINSTANCE hInstance // save the program examples bool keys[256] bool active = TRUE // active window, the default is true bool fullscreen = TRUE // default = true fullscreen // Draw the starting position of the display font list GLuint base GLfloat cnt1 // font move counter 1 GLfloat cnt2 // font move counter 2 LRESULT CALLBACK WndProc(HWND, UINT,WPARAM, LPARAM) GLvoid BuildFont (GLvoid) // create a bitmap fonts DEFAULT_PITCH, // Family And Pitch 'Courier New') // Font Name oldfont = (HFONT) SelectObject (hDC, font) // select the desired font wglUseFontBitmaps (hDC, 32, 96, base) // create 96 displays a list, drawn from the character of ASCII32-128 SelectObject(hDC,oldfont) DeleteObject(font) GLvoid KillFont(GLvoid) { glDeleteLists (base, 96) // delete 96 displays a list of } GLvoid glPrint (const char * fmt, ...) // custom function GL output font character is stored in the character string * fmt { char text [256] // save the character string va_list ap // pointer to point to a list of variables VariantArgument if (fmt==NULL) { return } va_start (ap, fmt) // variable parameter analysis vsprintf (text, fmt, ap) // write the string parameter value va_end (ap) // End Analysis ////////////////////////////////////////////////////////////////////////// / * The GL_LIST_BIT pushed attribute stack, which prevents glListBase affect other displays a list of our program. GlListBase (base-32) is a somewhat difficult to interpret commands. For example, write the letter 'A', its corresponding number is 65. If no glListBase (base-32) command, OpenGL not know where to find the letter. It will display a list of The 65th position finding it, however, if the base value equal to 1000, then the 'A' 1065 is an actual storage position of. So by setting a base starting point, OpenGL knows where to go to find the right show the list. Minus 32 because We have not been constructed to display a list of the first 32, then skip them better. So, we had to pass values ​​from the base of Minus 32 to let OpenGL know it. I hope this makes sense. */ ////////////////////////////////////////////////////////////////////////// glPushAttrib (GL_LIST_BIT) // Properties pushed into the display list attribute stack glListBase (base - 32) // set the display list base value glCallLists (strlen (text), GL_UNSIGNED_BYTE, text) // call the display list to draw the string glPopAttrib () // attribute stack pop } GLvoid ReSizeGLScene(GLsizei width,GLsizei height) { if (height==0) { } glViewport (0,0, width, height) // reset the current viewport glMatrixMode(GL_PROJECTION) glLoadIdentity() // set the size of the viewport, set perspective projection matrix gluPerspective (45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f) // y-axis tilt angle, aspect ratio, z-axis close> 0, z-axis distance 'near' 0 glMatrixMode(GL_MODELVIEW) glLoadIdentity () // reset the model view matrix } int InitGL (GLvoid) // start of GL make all settings { glShadeModel (GL_SMOOTH) // enable smooth shadow glClearColor (0.0f, 0.0f, 0.0f, 0.0f) // Black Background glClearDepth (1.0f) // Set the depth buffer glEnable (GL_DEPTH_TEST) // Enable Depth Buffer glDepthFunc (GL_LEQUAL) // depth test of the type made by The best perspective correction // glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) BuildFont () // create the font return TRUE } int DrawGLScene(GLvoid) GL_DEPTH_BUFFER_BIT) // clear screen and depth buffer glLoadIdentity () // reset the model view matrix glTranslatef(0.0f,0.0f,-1.0f) glColor3f (1.0f * float (cos (cnt1)), 1.0f * float (sin (cnt2)), 1.0f-0.5f * float (cos (cnt1 + cnt2))) // color position set according to the font glRasterPos2f (-0.45f + 0.05f * float (cos (cnt1)), 0.35f * float (sin (cnt2))) // set rasterization position, i.e. the position of the font glPrint ( 'Active OpenGL Text With NeHe -% 7.2f', cnt1) // text output to the screen cnt1 + = 0.051f // incremented counter value cnt2 + = 0.005f // incremented counter value return TRUE GLvoid KillGLWindow(GLvoid) { if (fullscreen) { ChangeDisplaySettings (NULL, 0) // Windows API set the default display apparatus by changing a parameter setting pattern of the first pattern ShowCursor (TRUE) // Window32API display the mouse } if (hRC) // whether it has rendered description { if (!wglMakeCurrent(NULL,NULL)) MB_ICONINFORMATION) if (! wglDeleteContext (hRC)) // delete the Rendering Context MB_ICONINFORMATION) hRC = NULL // will hRC set to NULL } if (hDC&&!ReleaseDC(hWND,hDC)) MessageBox (NULL, 'can not fail to release DC', 'Shutdown Error', MB_OK if (hWND &&! DestroyWindow (hWND)) // if that removes the window MB_ICONINFORMATION) hWND=NULL if (! UnregisterClass ( 'OpenGL', hInstance)) // window class cancellation MB_ICONINFORMATION) hInstance=NULL KillFont () // delete fonts } // Create OpenGL window width and height of the window title color bit full-screen logo BOOL CreateGLWindow(char* title,int width,int height,int bits,bool fullscreenflag) { // Save pixel format that matches the value of the variable Windows GLuint PixelFormat // window class structure WNDCLASS wc DWORD dwStyle // window style DWORD dwExStyle // extended window style RECT WindowRect WindowRect.left=(long)0 WindowRect.right=(long)width WindowRect.top=(long)0 WindowRect.bottom=(long)height fullscreen=fullscreenflag hInstance = GetModuleHandle (NULL) // Get an application or a module handle dynamically linked library, NULL return their application handle wc.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC // repaint when moving, and made for the window DC wc.lpfnWndProc = (WNDPROC) WndProc // WndProc message processing wc.cbClsExtra = 0 // no additional data window wc.cbWndExtra = 0 // no additional data window wc.hInstance = hInstance // setting examples wc.hIcon = LoadIcon (NULL, IDI_WINLOGO) // default icon charged wc.hCursor = LoadCursor (NULL, IDC_ARROW) // Load the mouse pointer wc.hbrBackground = NULL // GL does not need background wc.lpszMenuName = NULL // do not need a menu wc.lpszClassName = 'OpenGL' // set the class name // Register the window class if (!RegisterClass(&wc)) MB_ICONEXCLAMATION) // EXCLAMATION exclamation point return FALSE if (fullscreen) // Full Screen Mode { DEVMODE dmScreenSettings // device mode memset (& dmScreenSettings, 0, sizeof (dmScreenSettings)) // ensure that the memory is empty 0 dmScreenSettings.dmSize = sizeof (dmScreenSettings) // DEVMODE structure size dmScreenSettings.dmPelsWidth=width dmScreenSettings.dmPelsHeight = height // selected screen height dmScreenSettings.dmBitsPerPel = bits // number of colors per pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSHEIGHT|DM_PELSWIDTH if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If the pattern fails, offers two options: quit or run in a window. if (MessageBox (NULL, 'Failed to set current display full-screen mode on a card n Use window mode!', 'Nehe GL', MB_YESNO | MB_ICONEXCLAMATION) == IDYES) { fullscreen=false } else // user opt-out MB_ICONSTOP) return FALSE } } if (fullscreen) { dwExStyle=WS_EX_APPWINDOW dwStyle=WS_POPUP ShowCursor (FALSE) // hide the mouse pointer } else // not full screen dwExStyle=WS_EX_APPWINDOW AdjustWindowRectEx (& WindowRect, dwStyle, FALSE, dwExStyle) // Windows API really suitable window if (!(hWND=CreateWindowEx(dwExStyle, 'OpenGL', title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, // window style attributes must 0,0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, // calculate the adjusted window height NULL, // no parent window NULL, // no submenu hInstance, NULL))) // message is not transmitted to the WM_CREATE KillGLWindow() MessageBox (NULL, 'can not create window', 'Error', MB_OK static PIXELFORMATDESCRIPTOR pfd = // / pfd tells windows what we want stuff, pixel format that is used in window sizeof (PIXELFORMATDESCRIPTOR), // Format Descriptor size above 1, // version number PFD_DRAW_TO_WINDOW if (!(hDC=GetDC(hWND))) MB_ICONEXCLAMATION) return FALSE if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) KillGLWindow() MessageBox (NULL, 'can not create pixel format', 'Error', MB_OK if (! SetPixelFormat (hDC, PixelFormat, & pfd)) // you can set the pixel format it? KillGLWindow () // reset the display region MessageBox (NULL, 'can not set the pixel format', 'Error', MB_OK if (!(hRC=wglCreateContext(hDC))) MB_ICONEXCLAMATION) return FALSE if (!wglMakeCurrent(hDC,hRC)) KillGLWindow () // reset the display region MessageBox (NULL, 'not activate the current OpenGL rendering natural description', 'Error', MB_OK ShowWindow(hWND,SW_SHOW) SetForegroundWindow (hWND) // increase the priority SetFocus (hWND) // Set Focus ReSizeGLScene(width,height) if (! InitGL ()) // initialize the new GL window KillGLWindow () // reset the display region MessageBox(NULL,'Initialization Failed.','ERROR',MB_OK return TRUE } Handle LRESULT CALLBACK WndProc (HWND hWnd, // window UINT uMsg, // window messages WPARAM wParam, // Additional Message Content LPARAM lParam) // Additional Message Content { switch (uMsg) // Check the Windows message { case WM_ACTIVATE: // Watch Window activation message { if (! HIWORD (wParam)) // check minimized { active = TRUE // program is active } else { active = FALSE // program no longer activated } return 0 // return message loop } case WM_SYSCOMMAND: // interrupt command system { switch (wParam) // Check System Call { case SC_SCREENSAVE: // screensavers you want to run? case SC_MONITORPOWER: // monitor to enter a power save mode? return 0 // prevent the occurrence of } break // Exit } case WM_CLOSE: // Close message received? { PostQuitMessage (0) // news out return 0 // return } case WM_KEYDOWN: // a key is it? { keys [wParam] = TRUE // if yes, set to TRUE return 0 // return } case WM_KEYUP: // keyed release it? { keys [wParam] = FALSE // if yes, set to FALSE return 0 // return } case WM_SIZE: // OpenGL adjust the window size { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)) // LoWord=Width,HiWord=Height return 0 // return } } return DefWindowProc(hWnd,uMsg,wParam,lParam) } int WINAPI WinMain (HINSTANCE hInstance, // instance of the current window A window instance before HINSTANCE hPrevInstance, // LPSTR lpCmdLine, // command line parameters int nCmdShow) // window display state { MSG msg // Windowsx message structure BOOL done = FALSE // is used to exit the loop variable Bool if (MessageBox (NULL, 'you want to run in full-screen mode What?', 'set full-screen mode', MB_YESNO | MB_ICONQUESTION) == IDNO) { fullscreen = FALSE // FALSE windowed mode } // Create OpenGL window if (! CreateGLWindow ( 'NeHe's image fonts', 640,480,16, fullscreen)) { return 0 // fail to exit } while (! done) // kept in circulation until done = TRUE { if (PeekMessage (& msg, NULL, 0,0, PM_REMOVE)) // there is a message waiting for you? { if (msg.message == WM_QUIT) // quit message received? { done = TRUE // is then done = TRUE } else // not, the processing window messages { TranslateMessage (& msg) // Message translation DispatchMessage (& msg) // send a message } } else // If there is no message { // Draw the scene. ESC key and exit monitoring messages from DrawGLScene () of if (active) // program activated it? { if (keys [VK_ESCAPE]) // ESC pressed it? { done = TRUE // ESC sends a quit signal } else // not quit when the screen is refreshed { DrawGLScene () // draw scene SwapBuffers (hDC) // change buffers (double buffer) } } if (keys [VK_F1]) // F1 key is pressed it? { keys [VK_F1] = FALSE // if the Key array corresponding to FALSE KillGLWindow () // destroy the current window ! Fullscreen = fullscreen // Toggle full screen / window mode if (! CreateGLWindow ( 'NeHe's image fonts', 640,480,16, fullscreen)) // OpenGL window reconstruction { return 0 // if the window could not be created, the program exits } } } } // turn off an app KillGLWindow () // destroy window return (msg.wParam) // exit the program }