3D 空间中的文本 - 如何回车?(openGL, C++, OSX)

text in 3d space - how to carriage return? (openGL, C++, OSX)

本文关键字:openGL 空间 OSX C++ 回车 文本 何回车 3D      更新时间:2023-10-16

这是我检测到炸弹威胁的if语句(只是我正在制作的游戏)...

if (exodus2_bomb == 1)
{
    float exodus2_theta_math = (exodus2_theta)/10.0*M_PI;
    float exodus2_phi_math = (exodus2_phi)/10.0*2*M_PI;
    r_exodus2_x = radius_exodus_pos * sin(exodus2_theta_math) * cos(exodus2_phi_math);
    r_exodus2_y = radius_exodus_pos * sin(exodus2_theta_math) * sin(exodus2_phi_math);
    r_exodus2_z = radius_exodus_pos * cos(exodus2_theta_math);  
    glPushMatrix();
    glTranslatef(r_exodus2_x,r_exodus2_y,r_exodus2_z);
    glColor3f (1.0, 0.0, 0.0);        
    glRasterPos3i(exodus2_x/10,exodus2_y/10,exodus2_z/10);
    string exodus2 = "BOMB!!";
    void * fontexodus2 = GLUT_BITMAP_HELVETICA_10;
    for (string::iterator i = exodus2.begin(); i != exodus2.end(); ++i)
    {
        char c = *i;
        glutBitmapCharacter(fontexodus2, c);
    }
    glEnd();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(r_exodus2_x,r_exodus2_y,r_exodus2_z);
    glColor3f (1.0, 0.0, 0.0);        
    glRasterPos3i(exodus2_x/10,exodus2_y/10,exodus2_z/10);
    string exodus2b = "n THREAT LEVEL 1";
    void * fontexodus2b = GLUT_BITMAP_HELVETICA_10;
    for (string::iterator i = exodus2b.begin(); i != exodus2b.end(); ++i)
    {
        char c = *i;
        glutBitmapCharacter(fontexodus2b, c);
    }
    glEnd();
    glPopMatrix();
}

我想要的是"string exodus2"和"string exodus2b"之间的回车符。但是,它始终在一行上。而且我在屏幕上没有这方面的房地产。

是否可以在我的字符串迭代器中的某个位置添加回车符?

不幸的是,使用"glTranslatef"和"glRasterPos3i"只会导致两行文本之间的距离在我移动相机时不一致。

您需要单独渲染每个字符串,并修改相应的栅格位置以重置字符串的位置。 如果使用 glutBitmapHeight ,则可以确定需要将栅格位置"向下"移动多远才能模拟新线。

如果希望字符串在 3D 中定位,但随后又"操作",就像它们在 2D 中被限制在同一个平面上一样,则可以使用 glBitmap 命令修改屏幕空间中的栅格位置。

int lineHeight   = glutBitmapHeight( font );  // number of pixels between "lines"
int stringLength = glutBitmapLength( font, s1 );  // returns length of string 1 in pixels
glutBitmap( 0, 0, 0, 0, -stringLength, lineHeight, NULL );  // move raster position
glutBitmapString( font, s2 );

glBitmap的第五个和第六个参数控制栅格位置的移动距离。

或者,如果您希望将这些字符串锚定在相对于窗口的位置(而不是 3D 空间中的点),请查看glWindowPos

顺便说一下,所有这些函数在 OpenGL 3.0 中都已弃用,在 OpenGL 3.1 中删除,除非您在大于 3.2 的 OpenGL 版本中使用兼容性上下文。