1. 程式人生 > >在opengl 裡面寫字, 無法顯示的問題

在opengl 裡面寫字, 無法顯示的問題



按照網上的說明,我們先寫個drawString函式
void TimeLine::drawString(const char* str)
{
wglUseFontBitmaps(wglGetCurrentDC(), 0, 256, 1000);
glListBase(1000);
glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
}
然後這樣呼叫
drawString("");//初始化
glRasterPos3f(x, y, 0.0f);//確定位置
drawString("hello worold");//寫出來字


按照這樣做,發現並沒有成功繪製文字,程式碼如下


glColor3f(1, 1, 1);
glBegin(GL_LINES);
for (int i = 0; i < total_month; i++)
{
glVertex3f(item_x[i], bottomLine, 0);
glVertex3f(item_x[i], bottomLine - 0.1, 0);
}




drawString("");
for (int i = 0; i < total_month; i += 4)
{
int month = (i + 6) % 12;
if (month == 0) month = 12;
int year = start_year + (i + 6) / 12;
QString str = QString::number(year) + "." + QString::number(month);
QByteArray ba = str.toLatin1();
glRasterPos3f(item_x[i] - 0.3, bottomLine - 0.35, 0.0f);//確定位置
drawString(ba.data());//繪製文字
}
glEnd();


最終找到了問題所在,我將 繪製文字的程式碼寫在了 glBegin() 與 glEnd() 裡面, 只要拿到外面,如下就可以了


glColor3f(1, 1, 1);
glBegin(GL_LINES);
for (int i = 0; i < total_month; i++)
{
glVertex3f(item_x[i], bottomLine, 0);
glVertex3f(item_x[i], bottomLine - 0.1, 0);
}
glEnd();


//繪製文字的單獨寫在下面
drawString("");
for (int i = 0; i < total_month; i += 4)
{
int month = (i + 6) % 12;
if (month == 0) month = 12;
int year = start_year + (i + 6) / 12;
QString str = QString::number(year) + "." + QString::number(month);
QByteArray ba = str.toLatin1();
glRasterPos3f(item_x[i] - 0.3, bottomLine - 0.35, 0.0f);//確定位置
drawString(ba.data());//繪製文字
}