我正在尝试在我正在渲染的3D纹理上显示文本覆盖(基本上是显示我的键盘快捷键的帮助屏幕)。纹理效果很好,我为用户提供了一些东向使用的旋转和翻译。
我的想法是使用
const unsigned char tmp[100] = "text to render";
glRasterPos2i(x, y);
glColor4b(255, 255, 255, 255);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, tmp);
根据How do I use glutBitmapString() in C++ to draw text to the screen?的建议。
除了文本现在随对象旋转而不是保留在屏幕上的静态位置之外,这种方法很有效。我阅读了一些文档,发现在操作模型视图矩阵时会操纵glRasterPos函数:
glRasterPos提供的对象坐标与glVertex命令的对象坐标一样:它们由当前模型视图和投影矩阵转换并传递到剪切阶段。
- Source
然后我通过另一篇文章发现你可以使用glPushMatrix和glPopMatrix来推送当前矩阵。
- Source
当我这样做时,文本一起消失。起初我以为我的文本可能有错误的坐标,但我尝试x = y = 0到x = y = 25,间隔为.01,从未看过文本。我仍然可能误解了应该在哪里绘制,但我不确定下一步该尝试什么。
我的绘图功能正在调用类似于:
的内容glLoadIdentity();
glPushMatrix();
glTranslatef(0,0,-sdepth);
glRotatef(-stheta, 1.0, 0.0, 0.0);
glRotatef(sphi, 0.0, 0.0, 1.0);
glRotatef(rotateX,0,1,1);
glRotatef(rotateY,1,0,0);
glTranslatef(-0.5,-0.5,-0.5);
glPopMatrix();
glRasterPos2i(2, 2);
glColor4b(255, 255, 255, 255);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, tmp);
任何人都有任何关于调试/故障排除步骤的建议,试图让这个文本显示在屏幕上的一个静态位置?
答案 0 :(得分:1)
好吧,如果glRasterPos是treated the same way as glVertex,那么你需要在调用glRasterPos之前设置正确的投影(GL_PROJECTION)矩阵(使用gluOrtho2D)。
答案 1 :(得分:0)
试一试:
#include <GL/glut.h>
#include <string>
using namespace std;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(255,0,0);
glPushMatrix();
glScalef(5,5,5);
glBegin(GL_QUADS);
glVertex2f(-1,-1);
glVertex2f(1,-1);
glVertex2f(1,1);
glVertex2f(-1,1);
glEnd();
glPopMatrix();
glColor3ub(0,255,0); // A
glRasterPos2i(0,0); // B
string tmp( "wha-hey!" );
for( size_t i = 0; i < tmp.size(); ++i )
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, tmp[i]);
}
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspect_ratio = (double)w / (double)h;
glOrtho(-10*aspect_ratio, 10*aspect_ratio, -10, 10, -1, 1);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800,600);
glutCreateWindow("Text");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
奇怪的交换行A
和B
导致glColor3ub()
调用无法生效。我认为这就是你发布的代码序列所遇到的。
作为旁边glColor4b()
取char
s,最大值为127.如果你想坚持传递255,你应该切换到glColor4ub()
。
Documented here(“glRasterPos()序列,glColor(),glBitmap()不会产生所需的位图颜色”),但没有给出解释:(
编辑:啊哈! The current raster position包含自己的颜色状态,仅在glRasterPos()
调用期间更新。