根據displayTimer()函數的標題,看起來您正試圖在屏幕上顯示z座標而不是您想要的實際計時器。
首先,我建議將'z'參數重命名爲'text'並將其數據類型更改爲std :: string。然後,您可以將您的計時器值作爲字符串傳遞給您的displayTimer()函數。接下來,您將不得不循環訪問「文本」字符串,以便在屏幕上逐字顯示它(字符)。
下面是一個例子你的函數會是什麼樣子:
void displayTimer(void * font, std::string text, float x, float y)
{
glRasterPos2f(x, y);
for (string::iterator i = text.begin(); i != text.end(); ++i)
{
char c = *i;
glutBitmapCharacter(font, c);
}
}
我不知道你的代碼是如何組織的,但如何與您的顯示器一起使用X()函數一例()函數到在屏幕上顯示的定時器值將是如下:
#include <string>
std::string timerValue;
void * textFont;
void X(int value)
{
timerValue = std::to_string(value);
}
void displayTimer(void * font, std::string text, float x, float y)
{
glColor3f(0.0f, 1.0f, 0.0f); // Set color to green purely for testing.
glRasterPos2f(x, y);
for (string::iterator i = text.begin(); i != text.end(); ++i)
{
char c = *i;
glutBitmapCharacter(font, c);
}
}
void Display()
{
displayTimer(textFont,timerValue,10.0f,20.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
timerValue = 0;
textFont = = GLUT_BITMAP_HELVETICA_18;
glutTimerFunc(10,X,0);
glutDisplayFunc(Display);
glutMainloop();
return 0;
}
通知數據如何炭傳遞給glutBitmapCharacter在displayTimer()函數而不是AZ整數()函數。提示:爲避免某些編譯器警告,將x和y displayTimer()函數參數的數據類型更改爲GLfloat,因爲這是用於glRasterPos2f()函數的數據類型。
「不能正常工作」對您的問題不是很好的描述。它顯示一個意外的價值,或在錯誤的位置,或其他? –
顧名思義,'glutBitmapCharacter'不是[應該寫一個數字](https://www.opengl.org/documentation/specs/glut/spec3/node76.html)。 – usr2564301