gpt4 book ai didi

opengl - 我有一个关于 opengl glut glutStrokeCharacter 的问题,代码不起作用?

转载 作者:行者123 更新时间:2023-12-01 01:29:10 28 4
gpt4 key购买 nike

我在 ubuntu 上写了一个 opengl 代码。我想在屏幕上绘制文本,但 output() 函数似乎不起作用。你能告诉我为什么吗?

http://pyopengl.sourceforge.net/documentation/manual/glutStrokeCharacter.3GLUT.html

void output(GLfloat x, GLfloat y, char *text)
{
char *p;

glPushMatrix();
glTranslatef(x, y, 0);
for (p = text; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}

void myDraw(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f,1.0f,0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.8,0.8);
glVertex2f(-0.2,0.8);
glVertex2f(-0.2,0.5);
glVertex2f(-0.8,0.5);
glEnd();
glFlush();

glColor3f(1.0f,0.0f,0.0f);
output(-0.8,0.8,"hello"); // why not show text

glFlush();
}

int main(int argc,char ** argv)
{
printf("init\n");
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCALE,SCALE);
glutInitWindowPosition(100,100);

glutCreateWindow("MENU");

glutDisplayFunc(myDraw);

glutMouseFunc(processMouse);

glutMainLoop();
return 0;
}

最佳答案

首先尝试将文本缩小一点:

#include <GL/glut.h>

double aspect_ratio = 0;
void reshape(int w, int h)
{
aspect_ratio = (double)w / (double)h;
glViewport(0, 0, w, h);
}

void output(GLfloat x, GLfloat y, char* text)
{
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(1/152.38, 1/152.38, 1/152.38);
for( char* p = text; *p; p++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
}
glPopMatrix();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10*aspect_ratio, 10*aspect_ratio, -10, 10, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glColor3ub(255,0,0);
output(0,0,"hello");

glutSwapBuffers();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

glutInitWindowSize(640,480);
glutCreateWindow("Text");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

如果您阅读您提供的文档,您会发现 GLUT_STROKE_ROMAN默认情况下相当大,大约 152 个单位高。

如果我没记错的话,您所依赖的默认投影和模型 View 矩阵会为您提供一个仅涵盖 (-1,-1) 到 (1,1) 的 View 区域。

关于opengl - 我有一个关于 opengl glut glutStrokeCharacter 的问题,代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5898922/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com