https://blog.csdn.net/qq_35040828/article/details/51758258
具体解释直接看这个链接吧
#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
void drawstring(const char* str){
static int isfirstcall = 1;
static GLuint lists;
if(isfirstcall){
isfirstcall = 0;
lists = glGenLists(128);
wglUseFontBitmaps(wglGetCurrentDC(),0,128,lists);
}
for( ; *str !='\0';++str){
glCallList(lists + *str);
}
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
glRasterPos2f(0.5,0.5);
drawstring("hello, world");
glutSwapBuffers();
}
int main(int argc,char * argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutCreateWindow("word");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
运行结果
写中文字符(原链接找不到了,只能先这么发了,如果有谁找到的话麻烦评论一下我改掉)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
void selectFont(int size, int charset, const char* face){
HFONT hFont = CreateFontA(size, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face);
HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(), hFont);
DeleteObject(hOldFont);
}
void drawCNString(const char* str){
int len, i;
wchar_t* wstring;
HDC hDC = wglGetCurrentDC();
GLuint lista = glGenLists(1);
// 计算字符的个数
// 如果是双字节字符的(比如中文字符),两个字节才算一个字符
// 否则一个字节算一个字符
len = 0;
for (i = 0; str[i] != '\0'; ++i)
{
if (IsDBCSLeadByte(str[i]))
++i;
++len;
}
// 将混合字符转化为宽字符
wstring = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str, -1, wstring, len);
wstring[len] = L'\0';
// 逐个输出字符
for (i = 0; i<len; ++i)
{
wglUseFontBitmapsW(hDC, wstring[i], 1, lista);
glCallList(lista);
}
// 回收所有临时资源
free(wstring);
glDeleteLists(lista, 1);
}
void display(){
glColor3f(0.1,1,0.1);
glRectf(-0.3,0.1,0.3,-0.15);
glRectf(-0.3,-0.3,0.3,-0.55);
glColor3f(1,1,1);
selectFont(40,DEFAULT_CHARSET,"华文仿宋");
glRasterPos2f(-0.3,-0.03);
drawCNString("开始游戏");
glRasterPos2f(-0.1,-0.45);
drawCNString("exit");
glutSwapBuffers();
}
int main(int argc, char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,700);
glutCreateWindow("plane");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
运行结果: