PDA

View Full Version : opengl jogl problem with TextRenderer



thomasdk
04-03-2011, 01:22 AM
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)
{
}

Alfonse Reinheart
04-03-2011, 02:32 AM
I am using JOGL, and cannot show text using the TextRenderer for some reason.

OpenGL drawable surfaces are owned by OpenGL. Java rendering functions will have no effect on them. So if you want to draw text in an OpenGL window, you will have to draw text via OpenGL rendering commands. Which is not going to be a trivial process.

thomasdk
04-03-2011, 03:56 AM
Thanks for the response.

The non-trivial task is already performed by Sun, who wrote the TextRenderer java class to encapsulate the tasks involved in drawing text in opengl.
Accordingly, it should only be a simple matter of calling "textRenderer.draw(...)" or "textRenderer.draw3D(...)" to output text on the drawable area.

There must be some other error I don't see.

thomasdk
04-03-2011, 04:54 AM
It seems to be an issue with the libraries I use.It could also be caused by a software-only implementation, I guess. It doesn't seem to be a general opengl issue, so it is not relevant for this forum.
happy programming everybody.

(can I mark this thread as closed?)