Game Development Community

dev|Pro Game Development Curriculum

Programmatic DTS billboards

by Geom · 06/20/2007 (8:42 am) · 15 comments

Download Code File

Torque DTS shapes support a nifty feature called "billboards". A billboard is a 2-D representation of what the DTS shape looks like when viewed from a long way away. If a DTS shape has billboards turned on, then Torque will render the DTS as a 2-D bitmap (billboard) when the DTS reaches it's lowest detail level. This can can result in huge speed-ups in some cases, since rendering a 2-D bitmap is much faster than rendering a true 3-D shape.

Until now, billboards had to be turned on or off at the time the DTS was exported from an art tool such as Blender, 3D Studio Max, etc. With this resource, you can now turn on billboards in your game rather than in the art tool! It doesn't matter whether the DTS was exported with billboards on or off. You can also turn billboards off for a DTS that originally had them turned on.

API:
The API consists of one Torquescript function:

declareBillboards(shapeName, numEquatorSteps, numPolarSteps, bitmapSize, detailSize, polarAngle=0, includePoles=false, bitmapDetailLevel=0)

Parameters:

shapeName
the path to the .dts file. E.g. "~/data/shapes/trees/ftree01.dts"

numEquatorSteps
the number of intervals around the equator at which to take snapshots.

If this value is zero, declareBillboards() will attempt to turn billboards off rather than turn them on.

numPolarSteps
the number of intervals between the equator and the poles at which to take snapshots.

bitmapSize
the size of the snapshots to take, in pixels. (each snapshot will be bitmapSize x bitmapSize pixels.) This value *must* be a power of 2.

detailSize
the threshold at which to switch to rendering billboards. When the projected screen size of the DTS drops below this value, the DTS will be rendered as a billboard.

polarAngle
special top and bottom viewing angles for the DTS. See next param. Value is in degrees. Defaults to zero.

includePoles
if true, two additional snapshots are taken from directly above & below the DTS. These snapshots will be shown when the viewing angle is within 'polarAngle' degrees from the top or the bottom poles of the shape. Defaults to false.

bitmapDetailLevel
the detail level to use when creating the snapshots, if the DTS has multiple detail levels. Defaults to zero (the highest-poly detail level).

--------

Call declareBillboards() once for each DTS you want to add billboards to. If the DTS was exported with billboards already on, declareBillboards() will override the existing billboard parameters.

IMPORTANT: you must call declareBillboards() early on, before your DTSs get loaded. declareBillboards() creates a table that the DTS loading code reads. If you call declareBillboards() after DTSs are already loaded, it will have no effect.

All the usual caveats of billboards still apply. In particular, watch out for memory consumption. The total number of snapshots for a given DTS will be equal to numEquatorSteps x (numPolarSteps x 2 + 1) + (includePoles ? 2 : 0).

Example:
You can test this resource in the Torque 1.5 starter.fps example. Run Starter.FPS.bat, bring down the console window(~), and type

declareBillboards("starter.fps/data/shapes/trees/ftree01.dts",8,4,128,128);

Then start the mission "Content Pack Demo 1". Run around the trees, and you should notice some of them transitioning to and from billboards.

In your game, you can call declareBillboards() anytime before DTSs get loaded. E.g. you could call it in the onStart() function of your mod. In the starter.fps mod, this would look like:

function onStart()
{
   Parent::onStart();
   echo("\n--------- Initializing MOD: FPS Starter Kit ---------");

   // blah blah blah all the usual stuff...

   // Declare programmatic billboards here!
   declareBillboards("~/data/shapes/trees/ftree01.dts",8,4,128,128);
   declareBillboards("~/data/shapes/trees/FTREE04.DTS",8,4,64,64);
}

(from the file /example/starter.fps/main.cs)

Code:
The code for this resource should work out-of-the-box for TGE versions 1.3 - 1.5.2.

Download the attached .zip file. It contains two sets of source code files, one for TGE 1.3.0-1.5.0 and one for 1.5.2. (for 1.5.1, see below) Choose the right set and copy the files into your Torque SDK code base. The files will overwrite existing files of the same names, so you may want to make backups first. Re-compile, and you should be ready to go!

I couldn't get a hold of the Torque 1.5.1 SDK, so I'm not sure which set of files it uses.

The changes in the source code are all marked with the comment "BBAPI" for convenience.

Enjoy!!

About the author

My email address is my GG handle, at redbrickgames.com.


#1
06/20/2007 (12:16 am)
Thanx a lot!

You should comment the changes you made, though.
#2
06/20/2007 (10:45 am)
I hope you can help me out. I am using billboards to make a models eyes look like they are glowing, but when I look at the DTS in Show Tools Pro and in the game, the textures on the billboard seem to hide behind my model. The billboard stays in place but not the textures. If I use this will it help out on the texturing of the billboard. The texture is a gradient with a alpha channel.
#3
06/20/2007 (2:18 pm)
this sounds great.

are the billbaords per object instance or per datablock (or per something else like DTS file) ?

ie, would they work for two different instances of say a player where one has had textures dynamically changed ?

is there a way to flag an object as "dirty" w/r/t its billboard ?
#4
06/21/2007 (4:14 pm)
Thanks for the comments!

@James
you mean the source code changes, or ...? I'm not sure what you mean by that

@Kevin
that sounds like a cool use of billboards, but I don't think this resource will help you there...

All this resource does is fool Torque into thinking the DTS had been exported with billboards enabled. It doesn't add any new billboard capabilities or features, per se.

@Orion
you can think of billboards as being per-DTS file... so, no, they are not per-instance. All instances have to share the same billboard snapshots.

I think what you suggest is an interesting idea though: would it be possible for each DTS instance to cache a single billboard-bitmap of itself? Then, each time the TSShapeInstance gets drawn, it can draw its billboard bitmap instead of its 3-D shape, if its position has not moved relative to the camera.

I think the challenge to implement that would be to do to it in a way that avoids eating up bandwidth on the AGP / PCI-E bus... you wouldn't want those bitmaps going back and forth between the video card and main memory all the time
#5
06/26/2007 (7:02 am)
Hey Geom... Still haven't had time to try the new changes with TGEA. Will take a look tonight.

What I meant was, is that you should add comments in the files so people can see where you made changes. Drop-in files are cool, but if people already have changes to files, it's better, for them to add only the necessary. :)
#6
06/27/2007 (9:41 am)
Hey there I am using TGE 1.5.2 and getting lots of errors:
error C2039: 'getLightMap' : is not a member of 'TSMaterialList'	engine\ts\tsShapeInstance.cc	325
error C2039: 'tverts' : is not a member of 'TSMesh'	engine\ts\tsShape.cc	1007
error C2228: left of '.address' must have class/struct/union	engine\ts\tsShape.cc	1007
error C2039: 'tverts' : is not a member of 'TSMesh'	engine\ts\tsShape.cc	1048
error C2228: left of '.address' must have class/struct/union	engine\ts\tsShape.cc	1048
error C2039: 'tverts' : is not a member of 'TSSkinMesh'	engine\ts\tsShape.cc	1155
error C2228: left of '.address' must have class/struct/union	engine\ts\tsShape.cc	1155
error C2039: 'getLightMap' : is not a member of 'TSMaterialList'	engine\ts\tsMesh.cc	1032
error C2039: 'getLightMap' : is not a member of 'TSMaterialList'	engine\ts\tsMesh.cc	1454
error C2065: 'mLightMaps' : undeclared identifier	\engine\ts\tsMaterialList.cc	35
error C2511: 'TSMaterialList::TSMaterialList(U32,const char **,const U32 *,const U32 *,const U32 *,const U32 *,const U32 *,const F32 *,const F32 *)' : overloaded member function not found in 'TSMaterialList'	in engine\ts\tsMaterialList.cc	49
fatal error C1004: unexpected end-of-file found	in engine\ts\tsMaterialList.cc	317
Any help will be greatly appreciated.
#7
06/27/2007 (5:30 pm)
Those are odd errors. Odd because, that first error refers to a function named getLightMap, and I don't see a getLightMap function anywhere in my Torque 1.5 code base.

I think I have TGE 1.5.0. Lemme download TGE 1.5.2 this evening, and see if there is new code there that my patch files would break.
edit: typo
#8
06/27/2007 (8:50 pm)
Aha! The files tsShape*.{h,cc} did in fact change between 1.5.0 and 1.5.2. So unfortunately, the files attached to this resource don't work out-of-the-box in 1.5.2.

Edit: this is fixed now - see below
#9
06/28/2007 (4:24 am)
Geom: I appreciate your attention. Lots of stuff changed because of the many versions of Constructor that have been released. Once again thanks. I admit sheepishly I have never had any luck with diff.
#10
07/07/2007 (11:35 am)
Shon, no problem. I use winmerge myself, it seems to be a pretty good diff tool.

Anyway - I finally got around to working on this again, and I've updated it to work with 1.5.2.

The attached .zip file now contains *two* sets of files. One's for TGE versions 1.3.0 - 1.5.0, and one's for 1.5.2. Pick the right set, copy the files to your Torque code base, and things should go smoothly (fingers crossed).

The source code changes are now also marked with "BBAPI" for easier identification.
#11
12/19/2007 (12:47 am)
Thanks Geom! It was nice and easy to implement. The only issue I am having now is that the billboards are very transparent images of the tree which make it hard to see. The screenshot you provided shows a good comparison of what it should look like; mine doesn't look like that.

Any idea how to make the screenshots from your code appear more solid?
#12
12/20/2007 (8:39 pm)
I'm guessing here but... when a DTS switches from a 3-D shape to a billboard, I know Torque tries to make the transition appear smooth. It does that by gradually ramping up the alpha of the billboard, smoothly making it fade-in. My guess is that fade-in mechanism is somehow going wrong. I *don't* think theres anything wrong with the snapshots (bitmaps) themselves, b/c iirc they're stored with no alpha channel so it's impossible for them to be transparent.

do you notice any difference in transparency at all, as you move closer/farther from the object?
#13
12/24/2007 (1:58 am)
Anyone had any luck getting this working in TGEA?
#14
05/20/2008 (9:33 am)
Nice job, thanks Geom! This will be useful for all those content packs I bought without realising they had no LODs
#15
02/23/2009 (1:41 pm)
Anyone have a TGEA version for this? Pretty Please?!