在GLUT中绘制字符串

时间:2013-10-27 16:55:19

标签: c++ opengl glut

我正在尝试在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的值。 有什么问题?

1 个答案:

答案 0 :(得分:0)

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

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

等。

绘制如下文字: enter image description here