【问题标题】:Set color of glutBitmapCharacter()?设置 glutBitmapCharacter() 的颜色?
【发布时间】:2019-07-17 23:42:51
【问题描述】:

我正在尝试增强 MoonLander 游戏程序 - 只是想更好地理解事物。

我想在燃油降至某一水平以下时添加警告,并且我希望该警告改变颜色。我的游戏循环以大约 30 帧/秒的速度运行。所以我创建了一个成员变量,它在特定的帧数上“设置”一个 INT。我已经做了一些检查——我的帧计数器代码工作正常——我的 setWhichColour 函数也是如此。文本确实在屏幕上绘制 - 但每次都以白色绘制......

这里是设置“color”成员变量的代码

   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

我已经在上面提到了我从该答案中复制的代码,这似乎应该有所帮助。

不幸的是 - 我的文字仍然是白色的。它根本不设置颜色。我怀疑我遗漏了一些基本的东西(而且可能很简单),但我就是看不到什么。

任何人都可以确定我应该怎么做才能让文本以不同的颜色显示 - 老实说 - 我什至很高兴它只显示白色以外的任何颜色,即使它没有改变...

这里是游戏的截图:

【问题讨论】:

    标签: c++ opengl colors glut


    【解决方案1】:

    设置颜色后调用glRasterPos

    【讨论】:

    • 嗨,我想你是想让我移动 glRasterPos2f(topLeft.getX(), topLeft.getY());在我设置颜色的部分之后?我试过这个,它似乎没有任何区别?你可能还有别的意思吗?
    • OK - 看来我的问题肯定出在某处的 case 语句中。您的回答似乎也对事情有所帮助
    • @kiltannen 你启用GL_LIGHTING了吗?在这种情况下,颜色取自材质参数。
    【解决方案2】:

    最后,它结合了上述 datenwolf 的答案,并将代码更改为不使用 case 语句来解决问题。 (以及来自另一个堆栈答案的另一个提示)

    我想我应该发布一个答案来给出最终使它工作的确切代码......

    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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多