1

我正在尝试在 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. 怎么了?

4

1 回答 1

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

ETC..

绘制如下所示的文本: 在此处输入图像描述

于 2013-10-27T18:01:07.983 回答