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.

Answers

  • 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:

    textNoGlitch

    Thanks

  • 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(){
      pushMatrix();
      // draw 3D
      popMatrix();
      // draw text
    }
    
  • edited November 2014 Answer ✓

    here

    void setup() {
      size(800, 800, OPENGL);
    }
    
    void draw() {
      background(0);
      lights();
    
      // reset some stuff that was set differently by the HUD
      // maybe you need to set the camera
      // hint(ENABLE_DEPTH_TEST);
      textSize(32);
    
    
      // main program
    
    
      // display a text in the corner 
      fill(255);
      textSize(12);
      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.
      camera();
      hint(DISABLE_DEPTH_TEST);
      noLights();
      textMode(MODEL);
      if (str1!=null)
        text(str1, 20, 20);
      hint(ENABLE_DEPTH_TEST);
    } // 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.
      camera();
      hint(DISABLE_DEPTH_TEST);
      noLights();
      textMode(MODEL);
      if (str1!=null)
        text(str1, x, y);
      hint(ENABLE_DEPTH_TEST);
    } // 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.