【问题标题】:JOGL/OpenGL: "\n" (newline) does not working for text renderingJOGL/OpenGL:“\n”(换行符)不适用于文本渲染
【发布时间】:2012-04-13 17:44:46
【问题描述】:

我有这个函数可以在我的画布上呈现文本:

public void renderString(String s, int x, int y){
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glPushMatrix();
        gl.glLoadIdentity();
        gl.glOrtho(0,WIDTH,0,HEIGHT,-1, 1);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glPushMatrix();
            gl.glLoadIdentity();
            gl.glTranslatef(x,y,0);
            gl.glScalef(textScalingFactor, textScalingFactor, 1f);
            glut.glutStrokeString(GLUT.STROKE_ROMAN, s);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glPopMatrix();
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glPopMatrix();
}

问题是,换行符 (\n) 不起作用并且以下所有字符串:

String s1 = "bla"+"\n"+"bla";
String s2 = "bla\nbla";

char newLine = new char(10);
String s3 = "bla"+newLine+"bla";

呈现如下:

废话

有什么想法吗?

附:制表符 (\t) 也无法正常工作。

附言glutBitmapString, glutBitmapCharacter, glutStrokeCharacter也是同样的问题。

【问题讨论】:

    标签: opengl newline glut jogl


    【解决方案1】:

    看起来是just not implemented鉴于您的示例运行此代码...)翻译总是移动x,而不是y。我稍微缩短了以下代码。

    public void glutStrokeString(int font, String string) {
        /* setup */
        for (int pos = 0; pos < len; pos++) {
            /* single char setup */
            StrokeCharRec ch = fontinfo.ch[c];
            if (ch != null) {            
                /* draw strokes */
            }
            gl.glTranslatef(ch.right, 0.0f, 0.0f);
        }
    }
    

    其他方法很可能存在相同的问题。

    无论如何,您可以自己拆分字符串,用增加的y 移位绘制每个部分,并用一些额外的x 偏移量替换\t

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2021-06-03
      • 2014-04-15
      相关资源
      最近更新 更多