displaying text

Have jReality programming problems or questions? Post them here.
Post Reply
draperg
Posts: 5
Joined: Wed 5. Jul 2006, 19:06
Location: Salt Lake City, Utah, USA
Contact:

displaying text

Post by draperg » Thu 6. Jul 2006, 19:43

I have some sample code which goes something like this:
Font f = new Font("Times New Roman", Font.PLAIN, 12);
app.setAttribute("textShader.font", f);
ifsf.setFaceLabels(labels); //ifsf is an IndexedFaceSet, labels is an array of Strings

This approach works fine if I want a text label for every vertex or edge or face, but for complex objects with hundreds or thousands of vertices, this is more than what is needed. Do you have any sample code showing how to position text at arbitrary locations in the scene (i.e. not tied to each vertex of an IndexedFaceSet object)?

Thanks again for all your help!
Geoff

timh
Posts: 28
Joined: Wed 28. Jun 2006, 20:30
Contact:

Post by timh » Thu 6. Jul 2006, 23:27

There is no standard moethod for adding text to a scene right now.

If you want billboarded text (allways oriented towards the camera) then the default solution would be to make a PointSet with just one vertex at (0,0,0) (or two or three if you want more text...:-) ) and a label for it.

If you don't want the billboarding you can make a texture image from a string with methods in de.jreality.backends.label.LabelUtility and texture a rectangle with it (I think there are methods for that as well...but I don't remember rigth now)

Tim

User avatar
steffen
Posts: 186
Joined: Fri 16. Jun 2006, 13:30
Location: TU Berlin
Contact:

Re: displaying text

Post by steffen » Fri 7. Jul 2006, 00:04

Edit: Ah I was too slow ;-)

Hi Geoff,
We have no labels on geometry level. The easiest way would be a PointSet with one vertex, and setting the vertex color to transparent:

Code: Select all

    PointSetFactory psf = new PointSetFactory();
    psf.setVertexCount(1);
    psf.setVertexCoordinates(new double[]{0,0,0});
    psf.setVertexColors(new double[]{0,0,0,0});
    psf.setVertexLabels(new String[]{"my label"});
    psf.update();
    cmp.setGeometry(psf.getPointSet();
Then you can set the alignment to "center" and use the text shader's offset to keep the label in front of your geometry:

Code: Select all

    app.setAttribute("pointShader.textShader.alignment", SwingConstants.CENTER);
    app.setAttribute("pointShader.textShader.offset", new double[]{offx, offy, offz});
It is also possible to create a texture from the string and render a quad with this texture. But then you have no billboarding. If you want to try that version I'll post the code.

Bye, Steffen.

draperg
Posts: 5
Joined: Wed 5. Jul 2006, 19:06
Location: Salt Lake City, Utah, USA
Contact:

Post by draperg » Wed 2. Aug 2006, 00:37

Sure, if you could send me the version that creates a texture from a string, that would be great. It sounds interesting.

Thanks!
Geoff

User avatar
steffen
Posts: 186
Joined: Fri 16. Jun 2006, 13:30
Location: TU Berlin
Contact:

Post by steffen » Wed 2. Aug 2006, 19:40

Hi Geoff,

the code to create a label by hand:

Code: Select all

    // create the image
    Font f = new Font("Sans Serif", Font.PLAIN, 24);
    Image img = LabelUtility.createImageFromString("label text", f, Color.blue);

    Appearance app = new Appearance();
    app.setAttribute("showPoints", false);
    
    // otherwise there is a black border around the letters
    app.setAttribute("transparencyEnabled", true);

    // create the texture
    Texture2D tex = TextureUtility.createTexture(app, "polygonShader", new ImageData(img));

    // display it on a quad
    SceneGraphComponent cmp = new SceneGraphComponent();
    MatrixBuilder.euclidean().rotateX(Math.PI).assignTo(cmp);
    IndexedLineSet quad = Primitives.plainQuadMesh(0.01*img.getWidth(null), 0.01*img.getHeight(null),1,1);
    cmp.setAppearance(app);
    cmp.setGeometry(quad);

    ViewerApp.display(cmp);
If you want to display more text than a simple label then a swing text area might make more sense. Then you can format the text with HTML - I think the above example doesn't render text with more than one line.

Steffen.

Post Reply