简体   繁体   English

如何使用OpenGL在窗口上绘制字符串?

[英]How to draw a String to the window with OpenGL?

I'm creating a game but I have one problem: how can I draw text to the screen, as I want to print something every time the player picks up something or gets hurt. 我正在创建游戏,但有一个问题:我如何在屏幕上绘制文本,因为我想在玩家每次捡起东西或受伤时打印一些东西。 I already know the drawString() function for Graphics, but I am not sure if that is OK if I am using OpenGL. 我已经知道Graphics的drawString()函数,但是如果使用OpenGL,我不确定是否可以。 Is there a method from OpenGL that lets me easily print text to the screen? OpenGL是否有一种方法可以让我轻松地将文本打印到屏幕上?

In OpenGL there is no function / method you can call to render text to the screen. 在OpenGL中,没有可以调用的用于将文本呈现到屏幕的函数/方法。

You have two choices: either 1) use a library; 您有两种选择:1)使用库; 2)使用库。 or 2) roll your own, creating your own calls to render text to the display. 或2)自己滚动,创建自己的调用以将文本呈现到显示器。

1) There are a number of different libraries out there that you could use to render text to the display; 1)您可以使用许多不同的库将文本呈现到显示器; however the most obvious and accessible to you is within GLUT. 但是,最明显和最容易使用的是GLUT。 There are two calls within that you could use: 您可以使用两个呼叫:

glutBitmapString() and glutStrokeString()

The first of these will render text to the display in what I guess could be referred to as a 2d manner (looking like it's pasted right of the display). 其中的第一个将以我认为可以称为2d的方式将文本呈现到显示器(看起来像是粘贴在显示器的右侧)。 The latter with display 3d text. 后者带有显示3d文本。

You can refer to 你可以参考

http://freeglut.sourceforge.net/docs/api.php

for API references. API参考。

2) You could create a function / method that renders character textures to the display using the standard OpenGL functions, or you could potentially use stencil buffers using calls to glDrawPixels() to generate the characters. 2)您可以使用标准的OpenGL函数创建一个将字符纹理渲染到显示器的函数/方法,或者可能通过调用glDrawPixels()来使用模具缓冲区来生成字符。

I haven't tried these: I'm just thinking off the top of my head so you're mileage may vary. 我没有尝试过这些:我只是想过要顶一下,所以您的里程可能会有所不同。 Good luck. 祝好运。

No, there is no method in OpenGL for easily printing text. 不,OpenGL中没有轻松打印文本的方法。 You must either use one of the libraries that exist for this purpose or you must write it yourself. 为此,您必须使用现有的一种库,也必须自己编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM