在opengl 里面写字, 无法显示的问题

在使用OpenGL进行文字绘制时遇到无法显示的问题,通过分析发现是将绘制文字的代码置于glBegin()和glEnd()之间导致的。将文字绘制代码移出该范围后,文字成功显示。


按照网上的说明,我们先写个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());//绘制文字
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值