glutbitmapcharacter
时间: 2023-04-25 16:03:38 浏览: 55
glutbitmapcharacter是一个函数,用于在OpenGL窗口中绘制一个位图字符。它需要一个参数来指定要绘制的字符,以及当前的字体。可以使用glutbitmapcharacter来创建简单的文本标签和用户界面元素。
相关问题
用Opengl Drawstring 输出字符串
在 OpenGL 中,要输出字符串可以使用 glutBitmapCharacter 函数。该函数可以输出 ASCII 码对应的字符。
以下是一个简单的示例代码:
```c++
void drawString(const std::string& str, float x, float y, void* font = GLUT_BITMAP_8_BY_13) {
glRasterPos2f(x, y);
for (size_t i = 0; i < str.size(); ++i) {
glutBitmapCharacter(font, str[i]);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
drawString("Hello, World!", 0.0, 0.0);
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL DrawString Example");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
在上面的示例中,我们定义了一个名为 drawString 的函数,接受一个字符串、x 坐标、y 坐标、字体参数等,用于输出字符串。在 display 函数中,我们调用 drawString 函数输出了一个字符串。最后在 main 函数中,我们创建了一个 OpenGL 窗口,并将 display 函数作为回调函数传递给 GLUT,以便在窗口中绘制内容。
使用OpenGL 实现 代码弹幕” 200312225 OpenGL”从上到下,循环反复
以下是使用OpenGL实现代码弹幕的示例代码:
```c++
#include <GL/glut.h>
#include <string.h>
char* message = "200312225 OpenGL";
int messageLength = strlen(message);
int screenWidth = 640;
int screenHeight = 480;
int messageX = 0;
int messageY = 0;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRasterPos2i(messageX, messageY);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < messageLength; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[i]);
}
glutSwapBuffers();
}
void timer(int value) {
messageY -= 5;
if (messageY < -18) {
messageY = screenHeight;
}
glutPostRedisplay();
glutTimerFunc(1000 / 60, timer, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutCreateWindow("Code Danmu");
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
```
解释:
在`display`函数中,我们使用`glRasterPos2i`函数设置弹幕的起始位置,并使用`glColor3f`函数设置颜色。然后使用`glutBitmapCharacter`函数逐个绘制字符。
在`timer`函数中,我们每次将弹幕的Y坐标减少5个像素,当弹幕超出屏幕范围时,将其重新设为屏幕高度。然后使用`glutPostRedisplay`函数刷新窗口。
在`main`函数中,我们设置窗口大小、背景色,并注册`display`和`timer`函数。最后进入GLUT的事件处理循环。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)