c++屏幕上未显示文本

c++ Text not showing on screen

本文关键字:显示 文本 屏幕 c++      更新时间:2023-10-16

我正试图使用此代码将文本打印到屏幕上。

int main(int argc, char* argv[]) {      
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
int WindowHandle = glutCreateWindow("Maze");
if (WindowHandle < 1) {
    fprintf(stderr, "ERROR: Could not create a new rendering window.n");
    exit(EXIT_FAILURE);
}
//Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Initialize the viewport
glViewport(0, 0, 640, 480);
//Initialize OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 640.0f, 480.0f, 0.0f, -2.0f, 2.0f);
//Clear back buffer
glClear(GL_COLOR_BUFFER_BIT);
//Create font
GLuint textureName;
glGenTextures(1, &textureName);
PixelPerfectGLFont font;
try {
    font.Create("fonts\quad20.glf", textureName);
}
catch(GLFontError::InvalidFile) {
    cerr << "Cannot load fontn";
    abort();
}
//Draw some stuff
glMatrixMode(GL_MODELVIEW);
glLoadIdentity( );
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 0.0f, 0.0f);
try {
    font.Begin();
    font.TextOut("hello world", 50, 50, 0);
}
catch(GLFontError::InvalidFont) {
    cerr << "Trying to draw with an uninitialized fontn";
    abort();
}
glDisable(GL_TEXTURE_2D);
glLoadIdentity();

glutMainLoop();
return 0;
}

把这个代码放在一个main.cpp文件中,它就会运行(假设你有正确的链接器)

在代码的顶部,我有

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#include "glfont.h"

要使用glfont.h,您可以在顶部添加using namespace std;(否则会引发编译错误)

所以。。。当我运行应用程序时,它只显示一个白色屏幕,没有显示文本。

有人能告诉我我做错了什么吗?

p.s.glfont可以在这里找到http://students.cs.byu.edu/~bfish/glfontdl.php

有人能告诉我我做错了什么吗?

一切。

这根本不是使用GLUT渲染的方式。您应该注册当GLUT准备好显示内容时将被调用的回调。你从不交换缓冲区或任何东西,所以你不会看到东西。等等。

看看你能找到的任何GLUT例子,你会发现它看起来不是这样的。

在开始玩文本渲染之前,您应该首先了解常规渲染。