我已经在GLC库的帮助下在OpenGL窗口上渲染了字体,但现在如果我想对渲染字体应用某些操作,那么它将作为分段错误终止。
呈现字体的代码如下,
char * msg = "ABCD";
void drawString(char* name, int x, int y){
ctx = glcGenContext();
glcContext(ctx);
myFont = glcGenFontID();
glcNewFontFromFamily(myFont, "Courier");
glcFont(myFont);
glEnable(GL_LINE_SMOOTH);
glViewport(0, 0, 500, 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 500.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glcScale(20,20);
glColor3f(1,0,0);
glPushMatrix();
glRasterPos3f(x,y,0);
glcRenderString(name);
glPopMatrix();
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_E && action == GLFW_PRESS)
msg = "abcd";
}
int main()
{
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
GLFWwindow* window = glfwCreateWindow(500, 500, "font rendering", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glfwSwapInterval(1);
if ( GLEW_OK != glewInit( ) )
{
std::cout << "Failed to initialize GLEW" << std::endl;
}
glfwSetKeyCallback(window, key_callback);
GLint ctx, myFont;
while(!glfwWindowShouldClose(window)){
drawString(msg, 100, 100);
glfwPollEvents();
glfwSwapBuffers(window);
}
}
在按E
时,它应该将ABCD
更改为abcd
,但它不会替换原始文本,而是混合两个文本。
我理解,在创建上下文并正确使用它时存在一些问题,我也尝试使用glcDeleteContext()
,但它也在做同样的事情。
答案 0 :(得分:1)
您必须清除默认帧缓冲区的颜色缓冲区才能绘制场景。如果未清除颜色缓冲区,则它将保持不变,并在其上绘制新的渲染。
使用glClear
:
filter(val: string): string[] {
return this.options.map(x => x.color).filter(option =>
option.toLowerCase().includes(val.toLowerCase()));
}