我们从百度百科上摘抄了一部分介绍:
glutBitmapCharacter(
font ,
character)
2功能简介编辑
glut工具函数,用于在glut窗口某位置显示字符。
3参数介绍与实例编辑
font
设置
字符的字体,选择范围如下:
(注明两个参数,其他与此类似)
GLUT_BITMAP_8_BY_13
GLUT_BITMAP_9_BY_15
GLUT_BITMAP_TIMES_ROMAN_10 字体:TIMES_ROMAN 大小:10
GLUT_BITMAP_TIMES_ROMAN_24 字体:TIMES_ROMAN 大小:24
GLUT_BITMAP_HELVETICA_10
GLUT_BITMAP_HELVETICA_12
GLUT_BITMAP_HELVETICA_18
character
要显示的字符。
我们主要是使用函数实现
void drawtext()
{
glClear(GL_COLOR_BUFFER_BIT);
float x=200;
float y=200;
char *string="hello world! ";
char *c;
glRasterPos2f(x, y);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *c);//此处为作图函数。不必要在glbegin()函数之间的。
}
}
完整的函数
#include<stdlib.h>
#include <GL/glut.h>
void myinit(void)
{
/* attributes */
glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
glColor3f(1.0, 0.0, 0.0); /* draw in red */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
void drawtext()
{
glClear(GL_COLOR_BUFFER_BIT);
float x=200;
float y=200;
char *string="hello world! ";
char *c;
glRasterPos2f(x, y);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *c);
}
}
/* the display callback: */
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT); /*clear the window */
/*----------------------------------------*/
/* viewport stuff */
/*----------------------------------------*/
/* set up a viewport in the screen window */
/* args to glViewport are left, bottom, width, height */
glViewport(0, 0, 500, 500);
/* NB: default viewport has same coords as in myinit, */
/* so this could be omitted: */
drawtext();
/* and flush that buffer to the screen */
glFlush();
}
int main(int argc, char** argv)
{
/* Standard GLUT initialization */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(500,500); /* 500 x 500 pixel window */
glutInitWindowPosition(0,0); /* place window top left on display */
glutCreateWindow("写字"); /* window title */
glutDisplayFunc(display); /* display callback invoked when window opened */
myinit(); /* set attributes */
glutMainLoop(); /* enter event loop */
}