FrameBuffer and BitmapText

Short version:
Does anybody have a small working example of how to render BitmapText into a texture using the FrameBuffer, subsequently using that texture on a cube for example?

Long version:
I’ve been splitting my hairs for a few hours yesterday, as I can render 3d objects and regular Picture into textures and then onto a cube (box), however whenever I try to add BitmapText as a child node, nothing shows up.

What size is the bitmap text? You know, it’s infinitely easier to help someone with their code if we can see that code…

I have the code lying around at home, so I can post that later today. However the code I’m writing now is rendering the final texture onto a custom model so I guess it will deviate slightly from the simplest example. I am though using default Unshaded material and default BitmapFont.

Well, the most common problem is that you haven’t scaled the text down. You talk about rendering 3D objects but those tend to be a meters scale and bitmap text is in pixel scale. So unless you scale it down then you won’t see anything because it’s huge.

But see, I have to guess because no useful information was provided in your post. So, to answer the question that was asked. “Yes, bitmap text can be rendered to a frame buffer. I’ve done it many times.”

Every page in this book UI is a render-to-texture using bitmap text: Mythruna - Book User Interface 2 - YouTube

But see, that really isn’t helpful because it doesn’t show you what’s wrong with your code. If I sound a little frustrated it’s because half the posts these days seem to be the “How long is this piece of string I’m holding?” variety and now I’m two responses into another one. Help us help you.

Sorry I didn’t have time to post the code in question until now, but here is the problematic snippet:


        Camera offCamera = new Camera(512, 512);
        
        float frustumSize = 1;
        float aspect = (float) offCamera.getWidth() / offCamera.getHeight();

        offCamera.setParallelProjection(true);
        offCamera.setFrustum(-1000, 1000, -aspect * frustumSize, aspect * frustumSize, frustumSize, -frustumSize);

        //ViewPort offView = renderManager.createPreView("Offscreen View", offCamera);
        ViewPort offView = renderManager.createPostView("Offscreen View", offCamera);
        //ViewPort offView = renderManager.createMainView("Offscreen View", offCamera);
        
        offView.setClearFlags(false,false,false);
        offView.setBackgroundColor(ColorRGBA.Green);

        // create offscreen framebuffer
        FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);

        //setup framebuffer's texture
        Texture2D offTex = new Texture2D(512, 512, Format.RGBA8);
        offTex.setMinFilter(Texture.MinFilter.Trilinear);
        offTex.setMagFilter(Texture.MagFilter.Bilinear);

        //setup framebuffer to use texture
        offBuffer.setDepthBuffer(Format.Depth);
        offBuffer.setColorTexture(offTex);
        
        //set viewport to render to offscreen framebuffer
        offView.setOutputFrameBuffer(offBuffer);

        Node node = new Node();
        node.setQueueBucket(Bucket.Gui);
        node.setCullHint(CullHint.Never);

        Picture p = new Picture("Picture");
        p.setPosition(0, 0);
        p.setWidth(256);
        p.setHeight(256);
        p.setImage(assetManager, "com/jme3/app/Monkey.png", false);

        BitmapFont font = assetManager.loadFont("Interface/Fonts/Default.fnt");
        
        BitmapText hudText = new BitmapText(font, false);    
        hudText.setSize(12);
        hudText.setColor(ColorRGBA.Red);
        hudText.setText("A");
        hudText.move(0,hudText.getLineHeight(),0);
        
        node.attachChild(hudText);

        offView.attachScene(node);
        node.updateLogicalState(1);
        node.updateGeometricState();     

Does the text show up if you get rid of the picture or move it back so that it isn’t in the same plane as the text?

First i tried without adding picture or any other things, and when that didn’t work i tried adding the picture to see if i had screwed up the flow entirely, but the picture seems to work while the bitmaptext does not, even without the picture.

Im not sure if understand the same plane part of your question, but ive been moving both the picture and bitmaptext coords around to see if the positions were the problem. Ive also tried fiddling with the text size, however im not fully grasping the valid size range of text for a 512x512 texture.

I have a feeling that im missing something small and silly =/

@pspeed I had a chance to back and look at the problem and sure it was something silly on my part and at least halfway a correct guess on your part. The size of my bitmaptext was off so it was not showing up. I said I had fiddled with the text size, but apparantly not on a numerical scale I saw as logical. I ramped up the size to 128 for a 512x512 texture and sure enough it showed up.

In any case thanks for the help, I’m not sure if I completely understand the text size differences between using the standard SimpleApplication gui node and rendering to a texture. Do you have any wisdom to impart on that subject?

I don’t know. I don’t really know what size you were rendering the final texture so I can’t say. But if you were rendering 512 texels over a 200 pixel span section of screen, 14 texels is going to be pretty small… and since I don’t know what kind of figure you were painting it on, I can’t guess as to where it fell in the field of view, etc…

Anyway, glad you got it working at least.