使用 glutBitmapCharacter() 绘制文本关闭 opengl 程序
using glutBitmapCharacter() to draw text closing opengl program
当我尝试使用名为 drawText()
的函数绘制文本时,该函数运行顺利,但当它到达 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
时,程序意外关闭。我已经测试过它是函数的这个特定部分导致的,就好像我在哪里删除它并用 printf("Hello World!\n");
替换它程序不会崩溃。
主要功能:
int main(void) {
GLFWwindow* window;
int width = 840, height = 640;
if (!glfwInit()) {
printf("failed to init glfw");
return -1;
}
window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
glfwMakeContextCurrent(window);
if (!window) {
printf("failed to start window");
glfwTerminate();
return -1;
}
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (!glfwWindowShouldClose(window)) {
glClearColor(53.0f / 255.0f, 81.0f / 255.0f, 92.0f / 255.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0, height-80);
glVertex2f(width, height - 80);
glEnd();
string text;
text = "Hello World!";
//glutBitmapCharacter(GLUT_BITMAP_9_BY_15, int(text[1]));
drawText(text.data(), text.size(), 50, 100);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 1;
}
drawText 函数:
void drawText(const char* text, int length, int x, int y) {
glMatrixMode(GL_PROJECTION);
double* matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x, y);
for (int i=0; i < length; i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
printf("Hello world!\n");
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
我不确定是我的代码不是最好的还是 freeglut 库中有错误。需要说明的是,我正在使用 freeglut。
当我尝试使用名为 drawText()
的函数绘制文本时,该函数运行顺利,但当它到达 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
时,程序意外关闭。我已经测试过它是函数的这个特定部分导致的,就好像我在哪里删除它并用 printf("Hello World!\n");
替换它程序不会崩溃。
主要功能:
int main(void) {
GLFWwindow* window;
int width = 840, height = 640;
if (!glfwInit()) {
printf("failed to init glfw");
return -1;
}
window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
glfwMakeContextCurrent(window);
if (!window) {
printf("failed to start window");
glfwTerminate();
return -1;
}
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (!glfwWindowShouldClose(window)) {
glClearColor(53.0f / 255.0f, 81.0f / 255.0f, 92.0f / 255.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0, height-80);
glVertex2f(width, height - 80);
glEnd();
string text;
text = "Hello World!";
//glutBitmapCharacter(GLUT_BITMAP_9_BY_15, int(text[1]));
drawText(text.data(), text.size(), 50, 100);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 1;
}
drawText 函数:
void drawText(const char* text, int length, int x, int y) {
glMatrixMode(GL_PROJECTION);
double* matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x, y);
for (int i=0; i < length; i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
printf("Hello world!\n");
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
我不确定是我的代码不是最好的还是 freeglut 库中有错误。需要说明的是,我正在使用 freeglut。