设置 glutBitmapCharacter() 的颜色?
Set color of glutBitmapCharacter()?
我正在尝试增强 MoonLander 游戏程序 - 只是想更好地理解事物。
我想在燃油低于特定水平时添加警告,并且我希望该警告改变颜色。我的游戏以大约 30 帧/秒的速度循环 运行s。所以我创建了一个成员变量 "sets" 一个 INT 在特定的帧数。我已经做了一些检查 - 我的帧计数器代码工作正常 - 我的 setWhichColour 函数也是如此。文本确实在屏幕上绘制 - 但每次都以白色绘制...
这里是设置"colour"成员变量
的代码
if (lander.getFuel() < 200)
{
incrementFrameCounter();
if (frameCounter % 15 == 0)
{
setWhichColour(0);
}
else if (frameCounter % 20 == 0)
{
setWhichColour(1);
}
else if (frameCounter % 50 == 0)
{
setWhichColour(2);
}
else
{}
drawText(Point(40, 40), "Warning: Fuel Below 200",getWhichColour());
}
这里是我用来在屏幕上绘制文本的 drawText 函数。 case 语句只是传递一个整数值来选择触发哪个 glColor3f 序列。 drawText 函数是 运行 一次,每次游戏循环 运行 秒。
void drawText(const Point & topLeft, const char * text, int iColour)
{
void *pFont = GLUT_BITMAP_HELVETICA_12;
// prepare to draw the text from the top-left corner
glRasterPos2f(topLeft.getX(), topLeft.getY());
glPushAttrib(GL_CURRENT_BIT); // <-- added this after finding another answer
switch (iColour)
{
case 0: // red
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */);
case 1: // green
glColor3f(0.0 /* red % */, 1.0 /* green % */, 0.0 /* blue % */);
case 2: //blue
glColor3f(0.0 /* red % */, 0.0 /* green % */, 1.0 /* blue % */);
default: //white
glColor3f(1.0 /* red % */, 1.0 /* green % */, 1.0 /* blue % */);
}
// loop through the text
for (const char *p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
glPopAttrib(); // <-- added this after finding another answer
}
我发现这个答案似乎是相关的:
How to Set text color in OpenGl
我在上面注意到我从那个答案中复制了代码,这似乎应该有所帮助。
不幸的是 - 我的文字仍然是白色的。它根本不设置颜色。我怀疑我遗漏了一些基本的东西(可能很简单),但我只是没能看到什么。
任何人都可以确定我应该怎么做才能让文本以不同的颜色显示 - 老实说 - 在这一点上我什至很高兴它只是以白色以外的任何颜色显示,即使它没有'改变...
这是游戏的截图:
设置颜色后调用glRasterPos
最后,它结合了上面 datenwolf 的回答,并将代码更改为不使用 case 语句来解决问题。 (以及来自另一个堆栈答案的另一个提示)
我想我应该 post 一个答案来给出最终使它工作的确切代码...
void drawText(const Point & topLeft, const char * text, int iColour)
{
void *pFont = GLUT_BITMAP_HELVETICA_12; // also try _18
glPushAttrib(GL_CURRENT_BIT);
if (iColour == 0)
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */); //red
else if (iColour == 1)
glColor3f(0.0 /* red % */, 1.0 /* green % */, 0.0 /* blue % */); //green
else if (iColour == 2)
glColor3f(0.0 /* red % */, 0.0 /* green % */, 1.0 /* blue % */); //blue
// prepare to draw the text from the top-left corner
glRasterPos2f(topLeft.getX(), topLeft.getY());
// loop through the text
for (const char *p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
// This line was located in a stackechange answer on how to get colour set
glPopAttrib();
}
我正在尝试增强 MoonLander 游戏程序 - 只是想更好地理解事物。
我想在燃油低于特定水平时添加警告,并且我希望该警告改变颜色。我的游戏以大约 30 帧/秒的速度循环 运行s。所以我创建了一个成员变量 "sets" 一个 INT 在特定的帧数。我已经做了一些检查 - 我的帧计数器代码工作正常 - 我的 setWhichColour 函数也是如此。文本确实在屏幕上绘制 - 但每次都以白色绘制...
这里是设置"colour"成员变量
的代码 if (lander.getFuel() < 200)
{
incrementFrameCounter();
if (frameCounter % 15 == 0)
{
setWhichColour(0);
}
else if (frameCounter % 20 == 0)
{
setWhichColour(1);
}
else if (frameCounter % 50 == 0)
{
setWhichColour(2);
}
else
{}
drawText(Point(40, 40), "Warning: Fuel Below 200",getWhichColour());
}
这里是我用来在屏幕上绘制文本的 drawText 函数。 case 语句只是传递一个整数值来选择触发哪个 glColor3f 序列。 drawText 函数是 运行 一次,每次游戏循环 运行 秒。
void drawText(const Point & topLeft, const char * text, int iColour)
{
void *pFont = GLUT_BITMAP_HELVETICA_12;
// prepare to draw the text from the top-left corner
glRasterPos2f(topLeft.getX(), topLeft.getY());
glPushAttrib(GL_CURRENT_BIT); // <-- added this after finding another answer
switch (iColour)
{
case 0: // red
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */);
case 1: // green
glColor3f(0.0 /* red % */, 1.0 /* green % */, 0.0 /* blue % */);
case 2: //blue
glColor3f(0.0 /* red % */, 0.0 /* green % */, 1.0 /* blue % */);
default: //white
glColor3f(1.0 /* red % */, 1.0 /* green % */, 1.0 /* blue % */);
}
// loop through the text
for (const char *p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
glPopAttrib(); // <-- added this after finding another answer
}
我发现这个答案似乎是相关的: How to Set text color in OpenGl
我在上面注意到我从那个答案中复制了代码,这似乎应该有所帮助。
不幸的是 - 我的文字仍然是白色的。它根本不设置颜色。我怀疑我遗漏了一些基本的东西(可能很简单),但我只是没能看到什么。
任何人都可以确定我应该怎么做才能让文本以不同的颜色显示 - 老实说 - 在这一点上我什至很高兴它只是以白色以外的任何颜色显示,即使它没有'改变...
这是游戏的截图:
设置颜色后调用glRasterPos
最后,它结合了上面 datenwolf 的回答,并将代码更改为不使用 case 语句来解决问题。 (以及来自另一个堆栈答案的另一个提示)
我想我应该 post 一个答案来给出最终使它工作的确切代码...
void drawText(const Point & topLeft, const char * text, int iColour)
{
void *pFont = GLUT_BITMAP_HELVETICA_12; // also try _18
glPushAttrib(GL_CURRENT_BIT);
if (iColour == 0)
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */); //red
else if (iColour == 1)
glColor3f(0.0 /* red % */, 1.0 /* green % */, 0.0 /* blue % */); //green
else if (iColour == 2)
glColor3f(0.0 /* red % */, 0.0 /* green % */, 1.0 /* blue % */); //blue
// prepare to draw the text from the top-left corner
glRasterPos2f(topLeft.getX(), topLeft.getY());
// loop through the text
for (const char *p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
// This line was located in a stackechange answer on how to get colour set
glPopAttrib();
}