Texts with AlphaState showing when they shouldn't

We use Text objects to display some information in our 3D models. The problem is that when the user rotates the view 180 degrees so that the texts are behind the camera the texts are rendered as they would be in front of the camera.



We create the texts as follows:







/

*

*/

protected void initText(String msg) {

Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer();

TextureState font = initFont(renderer);

AlphaState as1 = initTextAlphaState(renderer);



text = new Text("Text", msg);

text.setTextureCombineMode(TextureState.REPLACE);

text.setLightCombineMode(LightState.OFF);



text.setRenderState(font);

text.setRenderState(as1);

text.updateRenderState();



Node textNode = new Node("TextNode");

textNode.attachChild(text);

textNode.setRenderQueueMode(Renderer.QUEUE_SKIP);



//this.setRenderQueueMode(Renderer.QUEUE_ORTHO);



//this.attachChild(text);

this.attachChild(textNode);

}



/


*

*/

protected AlphaState initTextAlphaState(Renderer renderer) {

AlphaState as1 = renderer.createAlphaState();

as1.setBlendEnabled(true);

as1.setSrcFunction(AlphaState.SB_SRC_ALPHA);

as1.setDstFunction(AlphaState.DB_ONE);

as1.setTestEnabled(true);

as1.setTestFunction(AlphaState.TF_GREATER);

as1.setEnabled(true);

return as1;

}



Tracing this little further I noticed that LWJGLRenderer.draw(Text t) returns positive xy-coordinates for the text when it is behind the camera. When the coordinates are negative the text isn't rendered. So is the problem with the following line in LWJGLRenderer.draw?



font.print(this, (int) t.getWorldTranslation().x, (int) t

                .getWorldTranslation().y, t.getWorldScale(), t.getText(), 0);

Text is an ortho-based object.  Even if you put it in a different queue, the Font class will still shift to using ortho coordinates internally.  To do what you are talking about doing with Text, you'd have to convert the world coords to screen coords and apply those to the Text object on every frame (or at least whenever the object or your camera moves.)  In doing that translation you can use the z coordinate of the resulting screen coord to detect if the text is "behind" the camera.



LensFlare does exactly this in it's onDraw method:


        DisplaySystem display = DisplaySystem.getDisplaySystem();
        midPoint.set(r.getWidth() >> 1, r.getHeight() >> 1);
        // Locate light src on screen x,y
        flarePoint = display.getScreenCoordinates(worldTranslation, flarePoint)
                .subtractLocal(midPoint.x, midPoint.y, 0);
        if (flarePoint.z >= 1.0f) { // if it's behind us
            setCullHint(Spatial.CullHint.Always);
            return;
        }
           
        setCullHint(Spatial.CullHint.Dynamic);