Search code examples
javaopengl3djava-3d

How to draw a 2d overlay on a Java 3d scene?


I have a scene written in Java 3d, where the user's viewing position is set to some coordinate using the following code:

ViewingPlatform vp = simpleUniverse.getViewingPlatform();
TransformGroup steerTG = vp.getViewPlatformTransform();

Transform3D t3d = new Transform3D();
steerTG.getTransform(t3d);

t3d.lookAt(
        new Point3d(-5, 10, 25), new Point3d(0, 0, 0), new Vector3d(0, 1, 0));
t3d.invert();

steerTG.setTransform(t3d);

Now I need to put an overlay on top of the scene that is always visible, such as some text. I've tried the following, but to no avail:

PlatformGeometry pg = new PlatformGeometry();
Text2D text = new Text2D("Text to display", Cell.BLACK, "Verdana", 18, 1);
pg.addChild(text);
simpleUniverse.getViewingPlatform().setPlatformGeometry(pg);

When I run the above code, I don't see any text at all. Can anyone please help?


Solution

  • The problem is that you are displaying the text directly on top of the camera within the near clipping plane. You need something like this to translate -1 along the z axis.

        PlatformGeometry pg = new PlatformGeometry();
    
        TransformGroup objScale = new TransformGroup();
        Transform3D t3d = new Transform3D();
        t3d.setTranslation(new Vector3f(0f, 0f, -1f));
        objScale.setTransform(t3d);
    
        Text2D text = new Text2D("Text to display", Cell.BLACK, "Verdana", 18, 1);
    
        objScale.addChild(text);
        pg.addChild(objScale);
    
        simpleUniverse.getViewingPlatform().setPlatformGeometry(pg);
    

    Hope that helps.