I’m using the TextRenderer class to display text and using transformations on the ModelView matrix to position/rotate the text using the example code from the CustomText demo. I’ve found that it works properly on small pieces of text. But on a longer line of text, the Modelview transformation only seems to be effecting the first few characters, and the rest of the text is not effected by the transformation.
Below is code for a class called TextRendererMatrix that will reproduce the problem. Note that I have deliberately set the window size to 800x600 and the beginRendering width and height to 5000x5000 to illustrate the problem. This code was tested with the latest release build (JSR-231 1.1.1 - May 22) as well as the current nightly build and both display the same behaviour.
import java.awt.Color;
import java.awt.Font;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.sun.opengl.util.j2d.TextRenderer;
public class TextRendererMatrix extends JFrame implements GLEventListener
{
private GLU glu;
private GLCapabilities caps;
private GLCanvas canvas;
private TextRenderer renderer;
public TextRendererMatrix() {
super("TextRendererMatrix");
caps = new GLCapabilities();
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
getContentPane().add(canvas);
}
public void run() {
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.requestFocusInWindow();
}
public static void main(String[] args) {
new TextRendererMatrix().run();
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
glu = new GLU();
gl.glClearColor(0f, 0f, 0f, 1.0f);
renderer = new TextRenderer(new Font(Font.SANS_SERIF, Font.PLAIN, 100), true, false);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
renderer.beginRendering(5000, 5000); //drawable.getWidth(), drawable.getHeight());
renderer.setColor(Color.WHITE);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glRotatef(45f, 0f, 0f, 1f); // rotation around origin in x-y plane
renderer.draw("LOREM_IPSUM_DOLOR_SIT_AMET_CONSECTETUR_ADIPISCING_" +
"ELIT_PHASELLUS_VEL_LOREM_VIVAMUS_DICTUM_VESTIBULUM_" +
"BLANDIT_LACUS_EU_VIVERRA_ACCUMSAN_ERAT_DOLOR_CONVALLIS_" +
"JUSTO_ID_FERMENTUM_MAURIS_VELIT_LUCTUS_VELIT_ALIQUAM_VITAE" +
"_MAURIS_BLANDIT_LIBERO", 0, 0);
renderer.flush();
renderer.endRendering();
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged) {}
}
Something weird I have noted is that if the window is forced to redraw, for example by switching to another window and then back to this one, the entire text is correctly transformed.
Am I doing something wrong, or is this an issue with the TextRenderer?
Thanks for any help!