0
\$\begingroup\$

I am using the LibGDX library to render a 3D model in my Android application. Now, I am stuck, where I want to render multiple dynamic texts on to different 3D boxes located on different coordinates of the screen. I am able to render a single text using SpriteBatch, but I need to render different string values on different boxes.

SpriteBatch spriteBatch = new SpriteBatch();
BitmapFont font = new BitmapFont();
spriteBatch.maxSpritesInBatch = 5;

spriteBatch.setProjectionMatrix(camera.combined.cpy().scale(camera.combined.cpy()
    .getScaleX()/25,camera.combined.cpy().getScaleY()/25, 0.8f));

spriteBatch.begin();

font.setColor(1.0f, 0.0f, 0.0f, 1.0f);
float xPos = -90;
int count = 0;

for (Widget widgets : display.getWidgets()) 
{
    float boxWidth = widgets.getBounds().getWidth();
    float boxHeight = widgets.getBounds().getHeight();
    float boxHeightCenter = widgets.getBounds().getCenterY();

    if(count==0) 
    {
        font.draw(spriteBatch, "Hello"+count, xPos, boxHeightCenter-25, 
            boxWidth, 0, true);
        ++count;
    }
    else
    {
        font.draw(spriteBatch, "Hello"+count, xPos+boxWidth, boxHeightCenter-25, 
            boxWidth, 0, true);
        xPos = xPos+boxWidth;
        ++count;
    }
}

spriteBatch.end();` 

The above code renders only one string value. While debugging, the "for" loop is getting traversed, but no positive result is obtained. How do I fix this?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Initially i was not able to render multiple texts based on the number of nodes present in model. I wanted to render multiple text at different position so I achieved with this code. There was no error, but I was not able to achieve what I wanted i.e. rendering multiple text.

for (Widget widgets: display.getWidgets()) {

    for (int i = 0; i < widgets.nodes.size; i++) {

        Node node = widgets.nodes.get(i);
        if (node.id.equalsIgnoreCase("Camera") || node.id.equalsIgnoreCase("lamp")) {
        } 
        else {
            BoundingBox nodeBox = new BoundingBox();
            final Vector3 center = new Vector3();

            node.calculateBoundingBox(nodeBox);
            nodeBox.getCenter(center);
            if (count == 0) {
                font.draw(
                    spriteBatch, "Hello" + count, 
                    center.x, center.y, nodeBox.getWidth() * 25, Align.center, true);

                ++count;
            }
            else {
                font.draw(
                    spriteBatch, "Hello" + count, 
                    center.x, center.y, nodeBox.getWidth() * 25, Align.center, true);
                xPos = xPos + (boxWidth * 2); ++count;
            }
        }
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .