AC3D Plugin: Material by Crease Angle and Planar Map by Material

June 15th, 2009

Two new plugins today!

The first plugin, Set Material by Crease Angle, sets the materials of a model based on the crease angle of the mesh. For each selected area, the plugin creates a new color in the palette and “flood-fills” the area until the angle between the surfaces exceeds the crease angle of the mesh. The result is that contiguous areas are all set with the same palette material, and a new material is applied wherever a discontinuity occurs. This allows you to easily break a model into sections along its creases for easier texture mapping, or many other purposes.

The second plugin, Planar Map by Material, actually includes three new commands.

The first command is Fit UV Coordinates to Map. This command is essentially the same as the “max” button in the TCE, forcing all UV coordinates into the 0-1 range, but unlike “max” this scales the map proportionally instead of independently on each axis.

The second command is Adjust UVs for Bilinear Filter. This is useful for game developers. This command scales your texture coordinates by a ratio of 240/256. The purpose is to create a small seam along the edge of the texture map so that if your game is using mipmapping with a bilinear or trilinear filter, the texture won’t bleed into its neighbors in your texture cache nor will it bleed into itself if you haven’t clamped the edges.

The last command, Planar Map by Surface Material, is probably the most useful of the three. Planar Map by Surface Material applies a “best fit” planar projection to all surfaces grouped by material in the current selection. If you section your model by material, this will treat each material color as a contiguous group and apply whichever planar projection fits it best in the TCE. You’ll still need to do some manual adjustment after you map it this way–especially texture packing, as this leaves plenty of room between areas so the surfaces aren’t too difficult to select–but it can save a lot of time in laying down a base mapping before you manually refine each area.

More information about each plugin is available in the readme.

Download the Material by Crease Angle plugin. (Requires Windows XP, AC3D 6.2 or above.)

Download the Planar Map by Material plugin. (Requires Windows XP, AC3D 6.2 or above.)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • NewsVine

Tower Defense as a Thread Model

June 11th, 2009

I keep having the strangest reoccurring dream. In my dream, I’m playing a turret defense game. 1 However, it is no ordinary tower game. It’s actually a thread process model. In the dream, each turret type represents a thread synchronization mechanism, generally some sort of atomic primitive. The units marching along the paths represent the threads that require synchronization. Most are worker threads calculating a result set, but some may be polling threads reading device updates or doing other jobs. The range circle of each tower and the position of the unit along the path represent states when it is appropriate for the thread to be accessed, either quiescent states or work states where the thread has completed some useful task and has a result ready to be read. If the unit\thread moves out of the turret\synchronization range circle, the thread result is lost or held and operation on the next work cycle begins. Shooting a unit\thread with a turret is a read\synchronization operation and gathers the result.

Yeah, I know, clearly I’ve had threads on the mind a little too much lately. :) I’ve been working on a major refactor of the engine at work and thread synchronization problems are tricky.

Usually when I have dreams like this I wake up in the morning and have a good laugh wondering what the heck I was thinking. This time, however, I think there may be something to it. Now, I’m not suggesting a design built literally on the model exactly as it is in the dream but I think there are elements that could be useful.

The part that is intriguing about the turret defense thread model is that you can guarantee completion (all units hit) without guaranteeing really very much at all about either the turrets or the units. The units are not synchronized with each other; in fact, in many tower games faster units can overtake slower ones. A unit does not guarantee a minimum speed–it may even be allowed to stop entirely on occasion–although it does guarantee a maximum speed. The turrets are not synchronized with each other, nor do they communicate any information to each. A turret does not guarantee a specific number of units hit, only that it will hit units at a specific rate. A turret also does not guarantee what order it will fire at units in. A unit likewise guarantees nothing about when or by whom it will be hit except that it can be hit when it is within a valid range.

Yet, with a sufficient number of correctly placed turrets all the units will be hit with none escaping. More importantly, most of the time you can have far fewer turrets than units and still complete successfully. The number of turrets required is a simple function of the maximum speed of the turret, the maximum speed of the unit, and the number of times the turret must hit the unit for completion. Placement is trickier, but with a thread model you could surely replicate that with signals.

Fascinating, isn’t it?

I’m not sure yet where to go with all this, but it seems like there’s a useful idea to be gleaned. It’s not that different that a traditional worker pool, but the lack of communication seems important. I’m sure there’s something useful that can be done with this.


1. For anyone who cares, the specific game seems to be Crystal Defenders, but with some elements of Bloons Tower Defense 3.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • NewsVine

YouTube XL

June 4th, 2009

YouTube just released a new “game-console optimized” version, YouTube XL.

Somehow I was out-of-the-loop on that one, so the release caught me by surprise. I like to watch videos on the Wii sometimes. I don’t use the Wii for web surfing regularly, but now and again the comfort of flopping down on the couch instead of sitting in a desk chair at the computer more than makes up for the small screen resolution of the television. Today when I went to YouTube, however, I was greeted by an all new interface. Designed specifically for consoles such as the Wii and PS3, YouTube XL is a smaller, sleeker interface targeted specifically at television viewing.

I must say, I found YouTube XL a big improvement for game console-based video browsing. It seems many of the early reviews are mixed; many reviewers seem to be complaining about some of the missing features such as comments. Honestly as someone who actually uses YouTube on a game console on a semi-regular basis I think the new UI is far more practical. Certainly there are some missing features, such as comments, but I can’t really imagine tapping out a comment with a joystick anyway. Frankly I even avoid the search box when I can. (Thank goodness for auto-complete the rest of the time.) The far improved use of screen real estate plus larger, easier to target buttons made for a much more pleasant viewing experience. The new YouTube XL isn’t a replacement for PC-based video surfing, but it’s a nice alternative when a comfortable seat on the couch beckons. I vote thumbs up.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • NewsVine

memcpy_s

May 22nd, 2009

On Friday, an article ran on Slashdot making note that Microsoft is “banning” the C function memcpy in favor of the new memcpy_s. While memcpy_s has in truth been around for a while, the notion that it would now generate compiler warnings has created quite an uproar. Some people seem curious as to why so many people are upset by what seems a pretty innocuous change. The article author asked if was going to affect anyone’s creativity. I can assure you that’s not the concern.

In a nutshell: the problem is you can’t tape a cupholder to a formula one race car and declare it street legal. It still doesn’t have any bumpers, it’s too low to be safe around other cars, and if you aren’t an expert driver you still shouldn’t be driving it. A cupholder doesn’t change that.

So what? A cupholder is still an improvement… right? Who doesn’t want a cupholder? Imagine for a moment that having the cupholder suddenly disqualified you from a number of races. You couldn’t run that race if you have a cupholder. It also slowed down your car–not a lot, but a tiny fraction of a second. Maybe it doesn’t slow it down enough to matter, but in those few races won by hundredths… well, maybe it does. Suddenly that cupholder doesn’t seem so nice.

To be clear, I’m not suggesting giving up on attempts to improve memory management and prevent buffer overruns. I just question if this exact approach was the best one.

Memcpy_s is basically a race car with a cupholder. While memcpy_s will certainly prevent some bugs, the same people who got one parameter wrong are just as likely to get two parameters wrong… and cause all new bugs in the process. Garbage In = Garbage Out. It doesn’t matter what the calling convention is.

On two occasions I have been asked,—”Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” … I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.”

— Charles Babbage, 1864
discussing the first mechanical computer

The change also comes with a price: it breaks cross-compatibility with other compilers, including older compilers on the same operating system as well as other platforms. This means the same code can’t cross-compile anymore if you use memcpy_s. Someone with Microsoft suggested that GCC should pick up the change as well. Perhaps, but that assumes that GCC is the only other compiler that matters. Certainly, it’s the most popular other compiler that matters, but it’s far from the only one especially if you do embedded systems work or anything running on “exotic” hardware. And let’s be honest–if you’re not writing something that’s fast or exotic, you’re probably not writing it in C. The exception, of course, being legacy code… but surely that’s even less likely to be changed. The whole point of legacy code is that you don’t have the time\budget to update it, otherwise you wouldn’t be running it at all. These are the guys who will wrap memcpy_s in a macro named memcpy, defeating it entirely. Sigh.

As for that speed difference I mentioned? We’re obviously only talking about an extra comparison operator and perhaps a branch or two here. I know an extra comparison operator seems trivial, but if an extra comparison operator didn’t matter ASSERT macros wouldn’t exist. The speed difference is nominal, but it’s very, very real. On a lark I timed it. Copying 32 bytes 100,000 times with memcpy took on average 3952 counts, or about 0.001104 seconds, measured to nanosecond accuracy with QueryPerformanceCounter. Performing the identical operation with memcpy_s took on average 9706 counts, or about 0.002712 seconds. This was in “release mode” using Visual Studio 2008. (For the curious, in debug mode the difference averaged 8028 counts to 11340 counts, but you shouldn’t be shipping your code in debug mode anyway!)

Now hopefully you’ve done everything you can to avoid memcpy in your inner loop in the first place, but we all know there are rare occasions where it’s unavoidable. This all brings me back to my earlier point: if you’re not writing something that’s fast or exotic, you’re probably not writing it in C. Yes, you can write your own memcpy that’s probably faster than the system memcpy if you’re really worried about speed, but now we’re back to the portability issue. In short, I’m certain that to the people where the choice of language matters, those few clock cycles matter too. We’re talking folks writing device drivers here, not business applications.

Nobody is twisting anyone’s arm to use memcpy_s. You can easily disable the compiler warnings and continue to use memcpy all you like. But hopefully you see where I’m going with this: A lot of the reason that people continue to use C for certain projects has to do with performance and portability. This change sacrifices both for dubious return. If the change is not going to have tangible benefits that outweigh the problems, why do it at all?

Sooner or later, you need to trust that the programmer knows what they are doing. If the programmer can’t manage memory responsibly, you’ve got a bigger problem than an extra parameter is going to fix. To be honest, while a lot of people don’t like to admit it programmers sometimes leak or botch memory even in managed languages. There’s certainly a thesis paper or two in “possible alternatives” to the approach taken–perhaps checking the block size by querying the allocator instead of asking the programmer which would be even slower but more reliable, borrowing ideas from double entry accounting, etc, etc.–although the “real” fix surely involves more intelligent ways of thinking about memory, which realistically means a fundamental change in constructs, not a patched function.

As always, XKCD sums it up best (panel #2):

XKCD

Ah, I wait for the day… :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • NewsVine

Sculpted Prim Wine Glass

April 2nd, 2009

A wine glass is one of the first things most people make when they learn to use a 3D program. It’s fun and it’s easy.

Making a sculpted prim wine glass for Second Life is equally fun. In fact, it’s almost exactly the same except you need to UV map it before you upload it. A simple cylindrical map will do. Here’s a video to show you how using AC3D.

Wineglass Video
Click to download video

The step-by-step:

  1. Draw a polyline that will form the outer edge of your wineglass.
  2. (Optional) Use the spline tool to make your polyline into a smooth curve.
  3. Revolve the polyline around the Y axis 360 degrees. The more segments you use the smoother it will be, but the more polygons. You don’t need very many polygons, so don’t overdo it.
  4. Using the UV map tool, apply a cylindrical wrap around the Y axis.
  5. Export!


Here are the UV map settings I used.

When you import it into SL, be sure to set your mapping mode to cylindrical. If you don’t, the top and bottom of the glass will be solid instead of hollow.


Set the Stitching Type to Cylinder.

Download the model here.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • NewsVine