【问题标题】:draw string in GLUT在 GLUT 中绘制字符串
【发布时间】:2013-10-27 16:55:18
【问题描述】:

我正在尝试在 GLUT 中绘制字符串。我需要将int 值转换为unsigned char*,然后才能在glutBitmapString 中使用它。但是当我喜欢这样的时候

glBegin(GL_POINTS);
    glRasterPos2i(15, 20);
    char text[10];
    sprintf(text, "%d", 5);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, (unsigned char*)text);   
glEnd();

屏幕上没有文字。但在调试器中text 包含5 的值。 怎么了?

【问题讨论】:

  • 这可能和这个问题是同一个问题:stackoverflow.com/questions/16873561/…
  • 另外,我认为 glutBitmapString 不能在任何 glBegin/glEnd 语句之间调用。
  • 是的。 glBegin/glEnd之间好像不行。
  • glBegin/glEnd之间有效的东西列表,大概单手能数。如果它与指定顶点属性无关,则它不属于它们之间。 glutBitmapString (...) 是一个栅格函数(管道中完全不同的阶段),所以它与顶点无关。

标签: c++ opengl glut


【解决方案1】:

不知道这是否有帮助,但我个人使用:

std::uint32_t Base = 0;

void SetupFonts(int FontSize)
{
    HFONT Font = CreateFont(-MulDiv(FontSize, GetDeviceCaps(DC, LOGPIXELSY), 72), 0, 0, 0, FW_BOLD, false, false, false, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Arial");
    HFONT OldFont = static_cast<HFONT>(SelectObject(DC, Font));
    wglUseFontBitmaps(DC, 32, 96, Base);
    //wglUseFontBitmaps(DC, 32, 96, Base); //May or may not need this line twice.
    SelectObject(DC, OldFont);
    DeleteObject(Font);
}

template<typename... Args>
void Print(float X, float Y, float R, float G, float B, const char* Text, Args... args)
{
    using namespace std;
    std::size_t Position = 0;
    std::string Container(Text);
    std::vector<std::string> Arguments;
    std::initializer_list<int> {(Arguments.push_back(to_string(args)), 0)...};

    for (auto it = Arguments.begin(); it != Arguments.end(); ++it)
    {
        if ((Position = Container.find("%")) != std::string::npos)
        {
            Container.replace(Position, 1, *it);
        }
    }

    glColor3f(R, G, B);
    glRasterPos2f(X, Y);
    glPushAttrib(GL_LIST_BIT);
    glListBase(this->Base - 32);
    glCallLists(Container.size(), GL_UNSIGNED_BYTE, Container.data());
    glPopAttrib();
    glFlush();
}

if (Base == 0) SetupFonts(12);
Print(200, 200, 1.0f, 0.0f, 0.0f, "Hello %", "World");
Print(200, 250, 0.0f, 1.0f, 0.0f, "Your FPS is: %", CalculateFPS());

等等。

绘制如下所示的文本:

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多