drawing text in front in 3d

edited May 2016 in How To...

My latest sketch is a 3D figure, that you can move around, and some text on the screen, the text is drawn at (-width,-height,0). and it works fine, as long as the figures z value is lower than the text's. But if i move closer to the figure, so that it's z value is greater than the text's, the figure is drawn in front of the text, so that you cant read it.

here are some pictures of it:

When the figure is far away the text is in front. text_glitch1 text_glitch2

Now the text is behind the figure (it shouldn't be) text_glitch3text_glitch4

I do know why this happens, when the text has a lower z value than the figure, it is drawn behind it. But i want a way to draw the text, as though it was drawn on the camera.


  • Answer ✓

    Can you draw the text without a z position? text("Test",20,20); Other possibilities: Draw the text in a separate PGraphics with a transparent background, then draw it as a 2D image. Disable and enable the depth test before drawing your text.

  • i do not use a z position, and the computer do therefor asumes that i wants to draw it at z = 0.

    I am thinking about doing something like your second sugestion, but i don't know how

  • Using different PGraphichs turned out to be much easier than i thought it would. But drawing the text on a 2D PGraphic and then displaying it as an image, do still require a z coordinate, and the figure would still sometimes be displayed in front of the text.

    Instead i tried to draw all 3D figures on a separate 3 dimemsional PGraphichs and then display that, as an image, and then draw the text on that. And it worked:



  • edited October 2014

    on the downside, that just made the framerate jump from 40 FPS in average before, to 30 FPS in average after.

  • Try using push and pop matrix e.g.

    void draw(){
      // draw 3D
      // draw text
  • edited November 2014 Answer ✓


    void setup() {
      size(800, 800, OPENGL);
    void draw() {
      // reset some stuff that was set differently by the HUD
      // maybe you need to set the camera
      // hint(ENABLE_DEPTH_TEST);
      // main program
      // display a text in the corner 
      showTextInHUD("Test ");
    // HUD funcs  ----------------------
    // Head-up-Display 
    // see http://de.wikipedia.org/wiki/Head-up-Display
    void showTextInHUD(String str1) {
      // A small 2D HUD for text in the
      // upper left corner. 
      // This func may only be called a the very end of draw() afaik.
      if (str1!=null)
        text(str1, 20, 20);
    } // func 
    void showTextInHUD(String str1, float x, float y) {
      // A small 2D HUD for text at
      // free pos.
      // This func may only be called a the very end of draw() afaik.
      if (str1!=null)
        text(str1, x, y);
    } // func 
  • Pop and Pushmatrix doesn't change that the text is drawn behind the figure

  • Oh, i did not know about disabeling and enabeling the debth test. Turns out i don't need to use different PGraphics at all. The text is now working perfectly, and the framerate is the same as without text.

  • yeah, common problem, should be FAQ


  • By the way, the project i was working on is finished and uploaded now: 3D human figure

Sign In or Register to comment.