PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Preparing gfx for a game

I am involved in the production of an advanture game for the PPC platform and my department is graphics and integration thereof into the game. The following issue has come up and I was wondering if you guys can help me out.
A good deal of the graphics are 3d rendered. Now, I want to be able to have good-looking, smooth objects in the game (aka anti-aliased). However, rendering w/ antialiasing produces colour-bleeding around the object's outlines, which is readily visible when using simple masking routines. It has been suggested that I use alpha-blending instead of masks, as those could allow for a range of blending/transparency levels. But no one really clarified what kind of input I should have (ie. should my sprites be already anti-aliased and the a-blending will fix the colour bleeding or should they be simply rendered and somehow smoothed on-the-run?)
Finally, I would really appreciate if someone could provide me with code snippets that show how to do what I just described. That is, take a bitmap, a-blend/mask it, and output it to the screen (probably using GAPI).

Thanks in advance for your help and suggestions.
Hey, I'm not a 'guest'...;)

Also, to those who are planning to reply to this post:
-yes, I have already read the discussion on the board regarding alpha-blending (there's no code there, but some good ideas)
-yes, I have already seen EasyCE and its routines
Moose or Chuck · 30 Aug 2001, 18:28 UTC
My understanding if that you should just create the anti-aliased image like normal, the programmers will ned to alpha-blend out the areas that should be.
Then there really shouldn't be a problem. Anti-aliasing only works on an entire image (like the whole screen), rather than individual sprites.

Alpha channels are supported in most paint programs (even Photogenics on the PPC). Basically it is a greyscale layer that tells the renderer how transparent each pixel is. I can't remember if Black is 100% transparent or 100% opaque.

Sometimes, the Alpha channel is integrated into the RGB as a FOURTH color.

Since the alpha takes care of the blending, the sprite doesn't need to have any kind of fade or antialiasing around its edges.
I can also post a pic of what an alpha channel looks like seperated from the composite, since I just did something like that recently.
In my case, anti-aliasing effects just the sprite, as it is the picture itself (ie one single animation frame).
I would appreciate the example picture.
Also, any idea on how I would go about creating alpha-levels in a program like PSPor Photoshop?
Just to clarify. Yes you should render your image
with anti-aliasing on. This will obvioulsy produce an anti-aliased alpha (providing you render with an image format that supports alpha channels). This provides the best composite image for sprites, as it reduces the fringing that occurs.

Some of the highend 3d packages support more advanced anti-aliasing methods, which produce less of a general blur to the image. I recommend turning on highest quality providing your 3d renderer supports this.

Also, its best to render your sprites at bigger resolution (640x480 for example) then scale down.

Avoid using bi-linear, or other quicker scaling methods. If you have access to more powerfull tools, (digital fusion is a good example), then using a catmull-rom reducing alogrithm will produce by far the cleanest and less blurred image.

Good for rendered sprites :)

John

Just to clarify. Yes you should render your image
with anti-aliasing on. This will obvioulsy produce an anti-aliased alpha (providing you render with an image format that supports alpha channels). This provides the best composite image for sprites, as it reduces the fringing that occurs.


So what image format would that be? I don't believe normal bitmaps have an alpha channel, maybe something like PSD/PNG/TIFF?


Some of the highend 3d packages support more advanced anti-aliasing methods, which produce less of a general blur to the image. I recommend turning on highest quality providing your 3d renderer supports this.

Also, its best to render your sprites at bigger resolution (640x480 for example) then scale down.

Avoid using bi-linear, or other quicker scaling methods. If you have access to more powerfull tools, (digital fusion is a good example), then using a catmull-rom reducing alogrithm will produce by far the cleanest and less blurred image.


I don't see the necessity for resizing, really. I can make the rendering output small enough to produce a proper-sized image, and that removes the need for any kind of resizing methods, all of which degrade the quality to some extent. That and the fact that I don't have Digital Fusion you speak of.
BTW, I've tried exporting an image in PNG format and that produced an alpha-channel. However, it seems to be a simple b&w channel, rather than gradient/shades of gray image I would need for proper blending. Any thoughts on the matter?
What format is the PNG image?  If it's 1555 (16-bpp), then it only has one bit for alpha (black or white).

PNG format:
24 bits per plane
max # of colours: 1677716
Well that format can certainly handle 8-bits of alpha, so it must be something else.

Lomax,

If you render your 3D image on a solid black background and then enable anti-aliasing, what you'll end up with is an image that has been pre-multiplied by the alpha component.  You can create an alpha mask by re-rendering after cranking the ambient lighting up to solid white, then make the color of your polys white, and leave anti-alias enabled.  Depending on how your engine performs alpha blending, you might want to invert the resulting alpha mask.

From the canonical equation for alpha blending:

dC = (sC * A) + dC * (1 - A)

Since we have pre-multiplied the alpha by the source color (sC), and we're storing the inverse alpha in the mask, the equation above becomes:

dC = sC + dC * A

I think I went over all of this in some previous posts about how to implement alpha blending efficiently, but I never thought about it from an artist's perspective on how to create those alpha masks and pre-multiplied source images.  Turns out it's easier than I thought.  One of these days I should write the code to do this and see if it actually works.

Here's some test images I created quickly with Rhino3D (don't laugh, I'm a programmer, not an artist)

Image
Source image (sprite)

Image
Alpha mask

Image
Inverse alpha mask

Grab the images and then zoom-in to view the anti-aliased edges.



there ya go.

that is very similar to the alpha channel work I was just doing. It is kind of difficult to see the blending along the edges. It looks strictly b/w but it isn't - there are definately a couple shades of grey in there.
File formats that support alpha channels are .tga, .tiff as examples. you really need a 32 bit image format (like targa, which most 3d packages support anyway).

There is no need to re-render with the ambient light turned up, or re-color the polys etc, virtually all 3d software supports alpha channels (its vital for compositing work). I cant remember if you put down what 3d software you use but simply render to a file format that supports it, with anti-aliasing enabled.

Targas are my prefered format.

Sometimes other 3d packages have options to enable alpha channels etc in the render process, so check with that first.

On argentum Wiro used max (rendering to 32bit tiffs). I uses maya (rendering to 32bit targas), worked fine using ag's alpha blending.

The images are 8bit palletized, and then a seperate 8bit alpha image. (extracted from the 32bit targa)
Actually just checked in rhino3d and the .tga
it renders out is 24bit no alpha.

Doesnt suprise me as rhino is mainly a nurbs modelling package, with very limited rendering capability. Kick-ass modeller though.

So if you are using rhino then you will have to do what digby says. Unless rhino has an incandescence material, then just make it pure white and assign that to the surface.

Would work just as good.

John.
Btw looking at the png file format, it does say it supports an optional alpha channel.

However my photoshop doesnt support the saving of alpha channels in the png file format (dunno about importing them though as I havent rendered out one in that format)

John.
That's some great info, I'll make sure to export to TIFF format, but...
No one did tell me what I should do with the image afterwards. Can I somehow use the TIFFs (or whatever format w/ alpha channel) in the game? Or do I seperate the alpha from the image and somehow use that? Please clarify :) (and backup by some code, hehehe ;)
BTW, in regards to the 3d packages, I use the following:
Poser 4 + Pro Pack for initial character design
Bryce 5 for some basic texture rendering (coz it's so damn simple)
Animatek's World Builder for creating advanced models of plants/vegitations/ground/etc...

I don't particularly like to export from one format to another unless I absolutely have to, as there is a quality loss in every conversion. So if anyone can give me advice with regard to the 3 programs I use, I would really appreciate it...
Not all at once now! :( Heh...So, can anyone reply to the above 2 posts?

Thanks...
I haven't used any of those 3D packages you've listed so I can't help you.  Maybe no one here has used those packages and that's why you aren't getting any replies?


It doesn't really matter whether you separate the alpha channel into another image or use the packed pixel format of a TIFF or PNG.  I personally use my own format where the entire color data comes first in the file, followed by the alpha mask (if there is one).  This keeps me from having to unpack the alpha when I'm blitting.  Probably not too good for cache locality though.

I don't have any code for reading TIFF or PNG images.  As I said, I use my own image format so that my application and graphics routines do as little work as possible.  I try to preprocess everything I can and modify content so that it is in a format that better matches the target platform.

Let's forget the formats for a second. Let's say I have a raw bitmap of the sprite and a raw bitmap of alpha-channel. These could be in the same file or a different file, it doesn't matter. Now let's say I loaded both of these into program's memory (into a BITMAP structure, for example). Finally, let's say I have a 3rd bitmap that is the background.
What I want to do is display an alpha-blended sprite (which is a result of the sprite + alpha) on this background.
How do I do it? With bitblt routines this would require some logical operations with masks, with GAPI it would be something different. Since I am only familiar with the former, the question is regarding the GAPI...
If you're trying to mimic the masking technique with BitBlt's raster ops, then the easiest and fastest way to do this with GAPI is to use a color-key.  Choose a color that isn't used in your sprite's image (magenta usually works) and set every transparent pixel in your sprite to that color.  When you copy the sprite to the screen buffer (or back buffer), if you come across a pixel that matches the color-key, then you don't write a pixel to the screen and go on to the next pixel.

This is extremely easy to code, doesn't involve masks, or the addition of an alpha channel and should be much faster than the multiple BitBlt calls you were previously making.

If you want to perform actual alpha blending, that's a bit more involved.  You should try the above method first, if for no better reason to familiarize yourself with GAPI and copying pixel data around manually.  After that, read over my previous posts on alpha blending (I seem to explain this once a week these days).  I'm happy to answer questions here or in e-mail but I'm not too hip on writing code for people.  It seems to cause more problems than it's worth.


Lol, I'm sorry but I love Digby's replies, he just seems to know Everything and it still amazes me.