[2.1] Rendering text to a 3D object

Discussion area about developing with Ogre2 branches (2.1, 2.2 and beyond)
Post Reply
davidykay
Gnoblar
Posts: 2
Joined: Tue Dec 01, 2020 8:05 pm

[2.1] Rendering text to a 3D object

Post by davidykay »

Hello everyone,

My team and I are working on rendering text onto the surface of a 3D object.

We need the ability re-render the texture at runtime as the text changes.

Our current approach is to:

- Create a Texture -> textureManager.createTexture()
- Write text onto the texture -> [project-specific code]
- Create a Datablock -> hlmsPbs.createDatablock()
- Set the Texture as the diffuse texture in the datablock -> datablock.setTexture()
- Set the Datablock on an Item for rendering -> item.setDatablock()

Question 1: Is this a sane approach?

Question 2: Is there any sample code for how to correctly call datablock.setTexture()? We are having trouble with this step.

We have successfully rendered text onto a texture, verified by calling image.convertFromTexture(), image.save().

We have successfully used datablock.setDiffuse() to apply a color to an Item, but we have yet to change an Item's texture by calling datablock.setTexture().

==========

FYI: We began our journey by following this OGRE 1.X example: http://wiki.ogre3d.org/HowTo%3A+Write+text+on+texture.

==========

Please let me know if either:

A. Our approach is fundamentally flawed.
B. You need more information on our implementation to identify the particular issue.

Thank you very much!
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 4971
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1164
Contact:

Re: [2.1] Rendering text to a 3D object

Post by dark_sylinc »

Your approach sounds correct. In fact changes to the texture should appear immediately (i.e. no need to call datablock->setTexture again)

Two remarks:

1. Depending on what you're doing, you may prefer to use Unlit instead of Pbs.

2. Unlit supports 2D and 2DArray textures, but Pbs only supports 2D Array textures.
Ogre tries to batch as much as possible, so when you e.g. load a 1024x1024 texture, we create a 1024x1024x4 2D array texture and put subsequent loaded textures in the next slices.

e.g. it's like calling std::vector::reserve( 4 ) but with textures.

You need to be aware of this detail if you're manipulating the contents of the texture (eg. the texture you create MUST be 2D Array, and you must upload it to the right slice. Creating an array with just 1 slice is fine btw)

In Ogre 2.1 you can use HlmsPbsDatablock::_getTextureIdx to retrieve an already-set array index.
In Ogre 2.2 you can use TextureGpu::getInternalSliceStart to retrieve that index, although Ogre 2.2 does a much better job at hiding this detail and pretend 2D array textures are 2D, and knowing this info is often unnecessary. But it's still there.
davidykay
Gnoblar
Posts: 2
Joined: Tue Dec 01, 2020 8:05 pm

Re: [2.1] Rendering text to a 3D object

Post by davidykay »

Great advice. We had been using a 2D texture and had to change to 2DArray. We haven't moved to Unlit yet, but may do so in the future.

We have a rough version now working. We'll continue to refine and will be in touch with any significant updates.

Thank you very much! :D
Post Reply