PDA

View Full Version : Need help with Java OpenGL TextRenderer



Furrage
01-04-2011, 08:59 AM
Hi all, how do I get the Java OpenGL TextRenderer to respect the depth buffer?

I want my text drawn with depth (z) = 0 but to respect the depth function GL_LESS. That means it should not overwrite pixels already drawn with depth = 0 and not be overwritten by pixels drawn later with depth = 0.

To be more specific, I am experimenting with OpenGL and was trying to build a basic GUI component (textbox/label) for my application. I have reached the point where I can do a box using gl.glBegin(GL_LINE_LOOP), fill in the background using gl.glBegin(GL_QUADS), and want to use a TextRenderer to render text. However, if I let it disable the depth test using textRenderer.Draw(...) the control's text draws on top of everthing, even if the box is hidden by something. If I allow depth testing it appears that the text is being drawn with a z value of -0.5 which is behind the control's background and other images. Neither of these results are desireable.

I also tried using TextRenderer.Draw3D(x, y, z = 0...) and it seems to draw properly, but I get artifacts around the letters, like the letters are on a rectangle with a black background. If I could even get that background transparent that would help. That looks like something to do with drawing textures, but I barely understand textures and I can't see how to specify texture coordinates and bindings for the text from the TextRenderer.

The code is below



/** Displays the control.
* @param gl Reference to the OpenGL context.
*/
public void Display(GL gl) {

/* If control's border should be shown... */
if (options.IsSet(OPTION_VISIBLE_BORDER)) {

/* Set drawing colour for control's border. */
gl.glColor4fv(currentBorderColour, 0);

/* Draw control's borders. */
gl.glBegin(GL.GL_LINE_LOOP);
gl.glVertex3d(left, bottom, 0);
gl.glVertex3d(right, bottom, 0);
gl.glVertex3d(right, top, 0);
gl.glVertex3d(left, top, 0);
gl.glEnd();

}

/* If control's text should be shown... */
if (options.IsSet(OPTION_VISIBLE_TEXT) && (null != textRenderer) && (null != text) && !text.equals("")) {

/* Prepare text renderer to display text. */
textRenderer.beginRendering(app.windowWidth, app.windowHeight);

/* Set drawing colour for control's text. */
textRenderer.setColor(currentTextColour[0], currentTextColour[1], currentTextColour[2], currentTextColour[3]);

/* Draw control's text. */
textRenderer.draw(text, (int)(left + margin), (int)(bottom + margin + textOffset));

/* Complete text rendering. */
textRenderer.endRendering();

}

/* If control's background should be shown... */
if (options.IsSet(OPTION_VISIBLE_BACKGROUND)) {

/* Set drawing colour for control's background. */
gl.glColor4fv(currentBackgroundColour, 0);

/* Draw control's background. */
gl.glBegin(GL.GL_QUADS);
gl.glVertex3d(left, bottom, 0);
gl.glVertex3d(right, bottom, 0);
gl.glVertex3d(right, top, 0);
gl.glVertex3d(left, top, 0);
gl.glEnd();

}

/* (TODO) complete drawing code. */

}

ZbuffeR
01-04-2011, 02:55 PM
Simple, draw text last, after everything has been drawn.
Same problem as transluscent quads with blending, depth buffer only store nearest distance, so you can not "render behind".