Hi List
I hope you can help me out.
I am using JOGL, and cannot show text using the TextRenderer for some reason. The program should be simple enough, so there must be a basic rule that I have not understood yet.
The program show a square and attemps to show the text “Hello World”. Only the square is visible.
Thanks,
— program text below —
package test;
import com.sun.opengl.util.awt.TextRenderer;
import java.awt.Font;
import javax.media.opengl.;
import javax.media.opengl.glu.;
public class TestHW implements GLEventListener {
public void init(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
{
GL2 gl = drawable.getGL().getGL2();
GLU glu = new GLU();
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable drawable)
{
GL2 gl = drawable.getGL().getGL2();
// Clear the drawing area
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -10f);
/*
* show a square, just to prove that I can actually show something!
*/
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3f(-4.0f, 0.0f, 0.0f);
gl.glVertex3f(0.0f, 0.0f, 0.0f);
gl.glVertex3f(0.0f, 2.0f, 0.0f);
gl.glVertex3f(-4.0f, 2.0f, 0.0f);
gl.glEnd();
/*
* now draw some text (it doesn't show on screen :-( )
*/
String text = "Hello World";
TextRenderer renderer = new TextRenderer(new Font("Serif", Font.PLAIN, 72), true, true);
renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
gl.glLoadIdentity();
gl.glTranslatef(2.0f, 2.0f, 0.0f);
renderer.setColor(0.0f, 1.0f, 1.0f, 1);
renderer.draw(text, 0, 0);
renderer.flush();
gl.glFlush();
}
/**
* No explicit cleanup necessary.
*/
public void dispose(GLAutoDrawable drawable)
{
}