gpt4 book ai didi

c++ - 文字颜色不正确

转载 作者:行者123 更新时间:2023-11-30 04:11:02 24 4
gpt4 key购买 nike

void text(string str)
{
for (int i = 0; i < str.length(); i++)
{
glColor3f(0.0f, 0.0f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
}
}

void render(void)
{
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)height;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);

// Top view - top left
glViewport(0, 0, width/2, height/2);
glScissor(0, 0, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, -0.1f, 4.0f);
text("Front");
diode();

// Corner view - top right
glViewport(width/2, 0, width/2, height/2);
glScissor(width/2, 0, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 0.0f, -90.0f, 0.0f, 1.0f);
glRasterPos3f(4.0f, -0.1f, 0.1f);
text("Right");
diode();

// Front view - bottom left
glViewport(0, height/2, width/2, height/2);
glScissor(0, height/2, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 90.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, 4.0f, 0.0f);
text("Top");
diode();

// Right view - bottom right
glViewport(width/2, height/2, width/2, height/2);
glScissor(width/2, height/2, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 20.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, 4.0f, 0.0f);
text("Fro4nt");
diode();

glDisable(GL_SCISSOR_TEST);
glutSwapBuffers();
}

我不确定白色的“正面”和黄色的“顶部”/“右侧”来自哪里(就颜色而言)。他们都应该是黑色的。有谁知道问题出在哪里?

输出如下: enter image description here

最佳答案

正如所怀疑的那样,这可能会令人震惊,但 glRasterPos (...) 实际上会在您调用该函数时跟踪“当前”颜色。也就是说,在调用 glRasterPos (...) 之前设置的任何颜色都将用作该位置绘图操作的“当前颜色”。几乎可以将其视为光栅化器对 glVertex (...) 的模拟,正如我将在下面解释的那样。

您需要在调用 glRasterPos (...) 之前设置当前颜色,为此您应该完全删除 glColor3f (...) 调用您的 text (...) 函数,或者修改该函数以执行这两项操作 - 设置颜色,然后设置光栅位置,然后绘制文本。


glRasterPos — specify the raster position for pixel operations :

The current raster position consists of three window coordinates (x, y, z), a clip coordinate value (w), an eye coordinate distance, a valid bit, and associated color data and texture coordinates.

关于c++ - 文字颜色不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20365304/

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