C++ 如何仅使用 OpenGL 方法绘制文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8847899/
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
How to draw text using only OpenGL methods?
提问by sathish v
I don't have the option to use but OpenGL methods (that is glxxx()methods). I need to draw text using gl methods only. After reading the red book, I understand that it is possible only through the glBitmap()method. If this is the only possible way, then can any one help me with the pixel array information for all the characters. Is there is any other way to draw text?
我没有选择使用但 OpenGL 方法(即glxxx()方法)。我只需要使用 gl 方法绘制文本。看完红书,明白只有通过glBitmap()方法才有可能。如果这是唯一可能的方法,那么任何人都可以帮助我处理所有字符的像素阵列信息。有没有其他方法可以绘制文本?
回答by larsmoa
Drawing text in plain OpenGL isn't a straigth-forward task. You should probably have a look at libraries for doing this (either by using a library or as an example implementation).
在普通 OpenGL 中绘制文本并不是一项直接的任务。您可能应该查看用于执行此操作的库(通过使用库或作为示例实现)。
Some good starting points could be GLFont, OpenGL Font Surveyand NeHe Tutorial for Bitmap Fonts (Windows).
一些好的起点可能是GLFont、OpenGL Font Survey和NeHe Tutorial for Bitmap Fonts (Windows)。
Note that bitmaps are not the only way of achieving text in OpenGL as mentioned in the font survey.
请注意,位图并不是字体调查中提到的在 OpenGL 中实现文本的唯一方法。
回答by Software_Designer


Use glutStrokeCharacter(GLUT_STROKE_ROMAN, myCharString).
使用glutStrokeCharacter(GLUT_STROKE_ROMAN, myCharString).
An example: A STAR WARS SCROLLER.
一个例子:星球大战卷轴。
#include <windows.h>
#include <string.h>
#include <GL\glut.h>
#include <iostream.h>
#include <fstream.h>
GLfloat UpwardsScrollVelocity = -10.0;
float view=20.0;
char quote[6][80];
int numberOfQuotes=0,i;
//*********************************************
//* glutIdleFunc(timeTick); *
//*********************************************
void timeTick(void)
{
if (UpwardsScrollVelocity< -600)
view-=0.000011;
if(view < 0) {view=20; UpwardsScrollVelocity = -10.0;}
// exit(0);
UpwardsScrollVelocity -= 0.015;
glutPostRedisplay();
}
//*********************************************
//* printToConsoleWindow() *
//*********************************************
void printToConsoleWindow()
{
int l,lenghOfQuote, i;
for( l=0;l<numberOfQuotes;l++)
{
lenghOfQuote = (int)strlen(quote[l]);
for (i = 0; i < lenghOfQuote; i++)
{
//cout<<quote[l][i];
}
//out<<endl;
}
}
//*********************************************
//* RenderToDisplay() *
//*********************************************
void RenderToDisplay()
{
int l,lenghOfQuote, i;
glTranslatef(0.0, -100, UpwardsScrollVelocity);
glRotatef(-20, 1.0, 0.0, 0.0);
glScalef(0.1, 0.1, 0.1);
for( l=0;l<numberOfQuotes;l++)
{
lenghOfQuote = (int)strlen(quote[l]);
glPushMatrix();
glTranslatef(-(lenghOfQuote*37), -(l*200), 0.0);
for (i = 0; i < lenghOfQuote; i++)
{
glColor3f((UpwardsScrollVelocity/10)+300+(l*10),(UpwardsScrollVelocity/10)+300+(l*10),0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, quote[l][i]);
}
glPopMatrix();
}
}
//*********************************************
//* glutDisplayFunc(myDisplayFunction); *
//*********************************************
void myDisplayFunction(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 30.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
RenderToDisplay();
glutSwapBuffers();
}
//*********************************************
//* glutReshapeFunc(reshape); *
//*********************************************
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1.0, 1.0, 3200);
glMatrixMode(GL_MODELVIEW);
}
//*********************************************
//* int main() *
//*********************************************
int main()
{
strcpy(quote[0],"Luke, I am your father!.");
strcpy(quote[1],"Obi-Wan has taught you well. ");
strcpy(quote[2],"The force is strong with this one. ");
strcpy(quote[3],"Alert all commands. Calculate every possible destination along their last known trajectory. ");
strcpy(quote[4],"The force is with you, young Skywalker, but you are not a Jedi yet.");
numberOfQuotes=5;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 400);
glutCreateWindow("StarWars scroller");
glClearColor(0.0, 0.0, 0.0, 1.0);
glLineWidth(3);
glutDisplayFunc(myDisplayFunction);
glutReshapeFunc(reshape);
glutIdleFunc(timeTick);
glutMainLoop();
return 0;
}
回答by B?ови?
This articledescribes how to render text in OpenGL using various techniques.
本文介绍了如何使用各种技术在 OpenGL 中渲染文本。
With only using opengl, there are several ways:
只使用opengl,有几种方法:
- using glBitmap
- using textures
- using display lists
- 使用 glBitmap
- 使用纹理
- 使用显示列表
回答by RolanDecoy
Load an image with characters as texture and draw the part of that texture depending on what character you want. You can create that texture using a paint program, hardcode it or use a window component to draw to an image and retrieve that image for an exact copy of system fonts.
加载带有字符作为纹理的图像,并根据您想要的字符绘制该纹理的一部分。您可以使用绘图程序创建该纹理,对其进行硬编码或使用窗口组件绘制图像并检索该图像以获得系统字体的精确副本。
No need to use Glut or any other extension, just basic OpenGL operability. It gets the job done, not to mention that its been done like this for decades by professional programmers in very succesfull games and other applications.
无需使用 Glut 或任何其他扩展,只需基本的 OpenGL 可操作性。它完成了工作,更不用说专业程序员在非常成功的游戏和其他应用程序中这样做了几十年。
回答by Yury
I think that the best solution for drawing text in OpenGL is texture fonts, I work with them for a long time. They are flexible, fast and nice looking (with some rear exceptions). I use special program for converting font files (.ttf for example) to texture, which is saved to file of some internal "font" format (I've developed format and program based on http://content.gpwiki.org/index.php/OpenGL:Tutorials:Font_Systemthough my version went rather far from the original supporting Unicode and so on). When starting the main app, fonts are loaded from this "internal" format. Look link above for more information.
我认为在 OpenGL 中绘制文本的最佳解决方案是纹理字体,我与它们一起工作了很长时间。它们灵活、快速且外观漂亮(有一些后方例外)。我使用特殊程序将字体文件(例如.ttf)转换为纹理,然后将其保存到某种内部“字体”格式的文件中(我已经基于http://content.gpwiki.org/index开发了格式和程序.php/OpenGL:Tutorials:Font_System虽然我的版本与支持 Unicode 的原始版本相去甚远,等等)。启动主应用程序时,字体会从此“内部”格式加载。查看上面的链接了解更多信息。
With such approach the main app doesn't use any special libraries like FreeType, which is undesirable for me also. Text is being drawn using standard OpenGL functions.
通过这种方法,主应用程序不使用任何像 FreeType 这样的特殊库,这对我来说也是不可取的。使用标准 OpenGL 函数绘制文本。

