OpenGL 字体颜色问题
刚写了一个OpenGL字体显示类, 发现颜色总是不对, 部分代码如下:
//绘制文字
glPushAttrib(GL_LIST_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
//文字位置
if(m_screen)
{
double x=m_x,y=m_y;
switch(m_layout)
{
case(LEFT_BOTTOM):break;
case(LEFT_TOP):///左上角
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(RIGHT_BOTTOM):///右下角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
break;
case(RIGHT_TOP):///右上角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(LEFT_CENTER):///左侧中心
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(TOP_CENTER):///顶部中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(BOTTOM_CENTER):///底部中性
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
break;
case(RIGHT_CENTER):///右侧中心
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(CENTER):///屏幕中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
}
glWindowPos2d(x,y);
}
else
glRasterPos3d(m_x,m_y,m_z);
//文字颜色,必须在glWindowPos2d和glRasterPos3d前
glColor4fv(Color); //<------
//调用显示列表
glListBase(m_base);
glCallLists(m_list_len,GL_UNSIGNED_INT,m_lists);
glPopAttrib();
glPushAttrib(GL_LIST_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
//文字位置
if(m_screen)
{
double x=m_x,y=m_y;
switch(m_layout)
{
case(LEFT_BOTTOM):break;
case(LEFT_TOP):///左上角
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(RIGHT_BOTTOM):///右下角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
break;
case(RIGHT_TOP):///右上角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(LEFT_CENTER):///左侧中心
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(TOP_CENTER):///顶部中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(BOTTOM_CENTER):///底部中性
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
break;
case(RIGHT_CENTER):///右侧中心
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(CENTER):///屏幕中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
}
glWindowPos2d(x,y);
}
else
glRasterPos3d(m_x,m_y,m_z);
//文字颜色,必须在glWindowPos2d和glRasterPos3d前
glColor4fv(Color); //<------
//调用显示列表
glListBase(m_base);
glCallLists(m_list_len,GL_UNSIGNED_INT,m_lists);
glPopAttrib();
百思不得其解, 翻开Red Book找到了答案:
8.1.3 选择位图的颜色
... 光栅颜色状态变量被设置为调用函数glRasterPos*()时的颜色, ...
显然只需要调整glColor*和 glRasterPos*的位置即可, 修改后的代码如下:
//绘制文字
glPushAttrib(GL_LIST_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
//文字颜色,必须在glWindowPos2d和glRasterPos3d前
glColor4fv(Color);
//文字位置
if(m_screen)
{
double x=m_x,y=m_y;
switch(m_layout)
{
case(LEFT_BOTTOM):break;
case(LEFT_TOP):///左上角
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(RIGHT_BOTTOM):///右下角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
break;
case(RIGHT_TOP):///右上角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(LEFT_CENTER):///左侧中心
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(TOP_CENTER):///顶部中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(BOTTOM_CENTER):///底部中性
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
break;
case(RIGHT_CENTER):///右侧中心
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(CENTER):///屏幕中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
}
glWindowPos2d(x,y);
}
else
glRasterPos3d(m_x,m_y,m_z);
//调用显示列表
glListBase(m_base);
glCallLists(m_list_len,GL_UNSIGNED_INT,m_lists);
glPopAttrib();
glPushAttrib(GL_LIST_BIT | GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
//文字颜色,必须在glWindowPos2d和glRasterPos3d前
glColor4fv(Color);
//文字位置
if(m_screen)
{
double x=m_x,y=m_y;
switch(m_layout)
{
case(LEFT_BOTTOM):break;
case(LEFT_TOP):///左上角
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(RIGHT_BOTTOM):///右下角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
break;
case(RIGHT_TOP):///右上角
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(LEFT_CENTER):///左侧中心
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(TOP_CENTER):///顶部中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = m_parent->GetGLWindow()->GetSize().y-m_y;
break;
case(BOTTOM_CENTER):///底部中性
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
break;
case(RIGHT_CENTER):///右侧中心
x = m_parent->GetGLWindow()->GetSize().x-m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
case(CENTER):///屏幕中心
x = 0.5*m_parent->GetGLWindow()->GetSize().x+m_x;
y = 0.5*m_parent->GetGLWindow()->GetSize().y+m_y;
break;
}
glWindowPos2d(x,y);
}
else
glRasterPos3d(m_x,m_y,m_z);
//调用显示列表
glListBase(m_base);
glCallLists(m_list_len,GL_UNSIGNED_INT,m_lists);
glPopAttrib();
posted on 2009-06-20 13:18 codezhang 阅读(1784) 评论(1) 编辑 收藏 举报