// draw text on OpenGL for wglUseFontBitmaps // // Last updated: <2024/01/12 21:36:58 +0900> #include #include #include #define WIDTH 512 #define HEIGHT 288 static int count = 0; class GLFONT { public: HFONT Hfont; HDC Hdc; // create font GLFONT(LPCTSTR fontname, int size) { Hfont = CreateFont( size, // font height 0, // font width 0, // テキストの角度 0, // Orientation FW_REGULAR, // font weight FALSE, // italic FALSE, // underline FALSE, // strikeout ANSI_CHARSET, // charset OUT_DEFAULT_PRECIS, // OutPrecision CLIP_DEFAULT_PRECIS, // ClipPrecision ANTIALIASED_QUALITY, // Quality FIXED_PITCH | FF_MODERN, // Pitch And Family fontname // face name ); Hdc = wglGetCurrentDC(); SelectObject(Hdc, Hfont); } // draw string void DrawString(int x, int y, char *format, ...) { int Length = 0; int list = 0; if (format == NULL) return; Length = strlen(format); list = glGenLists(Length); for (int i = 0; i < Length; i++) { wglUseFontBitmaps(Hdc, format[i], 1, list + i); } glDisable(GL_LIGHTING); glRasterPos2i(x, y); // draw for (int i = 0; i < Length; i++) { glCallList(list + i); } glEnable(GL_LIGHTING); // delete displey list glDeleteLists(list, Length); list = 0; Length = 0; } }; GLFONT *font; // Draw OpenGL void display(void) { glClearColor(0.2, 0.4, 0.8, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw text glColor4f(1.0, 1.0, 1.0, 1.0); // set text color font->DrawString(40, 40, (char *)"Hello World"); { char buf[256]; sprintf(buf, "count %d", count); font->DrawString(40, 100, buf); } glutSwapBuffers(); count++; } void idle(void) { glutPostRedisplay(); // redraw } void keyboard(unsigned char key, int x, int y) { switch (key) { case '\x1B': case 'q': // Exit on escape or 'q' key press glutLeaveMainLoop(); // exit(EXIT_SUCCESS); break; } } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(WIDTH, HEIGHT); // glutInitWindowPosition(100, 100); glutCreateWindow("Draw string"); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutIdleFunc(idle); glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); font = new GLFONT(_T("Courier New"), 24); glutMainLoop(); return 0; }