如何在 C++ 中使用 GLUT/OpenGL 绘制文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/538661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:51:30  来源:igfitidea点击:

How do I draw text with GLUT / OpenGL in C++?

c++opengltextglut

提问by KingNestor

How do I draw a text string onto the screen using GLUT / OpenGL drawing functions?

如何使用 GLUT / OpenGL 绘图功能在屏幕上绘制文本字符串?

回答by epatel

There are two ways to draw strings with GLUT

用 GLUT 绘制字符串有两种方法

glutStrokeStringwill draw text in 3D

glutStrokeString将在 3D 中绘制文本

alt text
(source: uwa.edu.au)

替代文字
(来源:uwa.edu.au

and glutBitmapStringwill draw text facingthe user

glutBitmapString将绘制面向用户的文本

alt text
(source: sourceforge.net)

替代文字
(来源:sourceforge.net

回答by Andrew Grant

void RenderString(float x, float y, void *font, const char* string, RGB const& rgb)
{  
  char *c;

  glColor3f(rgb.r, rgb.g, rgb.b); 
  glRasterPos2f(x, y);

  glutBitmapString(font, string);
}

And you can call it like;

你可以这样称呼它;

RenderString(0.0f, 0.0f, GLUT_BITMAP_TIMES_ROMAN_24, "Hello", RGB(1.0f, 0.0f, 0.0f));

回答by Adam Rosenfield

If you don't like the built-in stroke font or bitmap font that comes with GLUT as per epatel's answer, you'll have to roll your own solution.

如果您不喜欢根据epatel 的回答随 GLUT 一起提供的内置笔划字体或位图字体,则必须推出自己的解决方案。

NeHehas some good tutorials (along with fully-working sample code) on this:

NeHe 在这方面有一些很好的教程(以及完整的示例代码):

回答by notnot

It's generally a bit nasty and not straightforward. Give this tool a try:

它通常有点讨厌,并不简单。试试这个工具:

http://students.cs.byu.edu/~bfish/glfontdl.php

http://students.cs.byu.edu/~bfish/glfontdl.php