Sunday, April 15, 2012

Shinya techdemo for Linux

I took some time and managed to get Shinya techdemo to compile under Linux (more precisely 64 bit Ubuntu 11.10). It was surprisingly painless - seems that years of cross developing Sodipodi and Khayyam for Linux and Windows have payed off.

The demo can be downloaded from Shinya homepage.

City house at night


It requires SpiderMonkey, SDL and SDL_mixer libraries.
There is only 64 bit version currently. I have no 32 bit Linux computer and I do not trust cross-compiling...

The two biggest problems were NVidia drivers and fonts. There is some weird bug either in Shinya or NVidia drivers that causes Z value of directional light volume to go crazy. It happens only on some cards, and on certain NVidia cards only in release mode - whatever it means. The same bug appeared under Linux build too - as a temporary solution the light volume is clipped a bit closer than far plane. Thus some faraway mountains appear darker than they should.
Fonts are rendered using libnrtype frontend to Fontconfig and FreeType2. Unfortunately I am not sure, which fonts with eastern-asian glyphs I can expect to be installed, so sometimes texts may not be very good. One possibility is to include some free font with Shinya.

Also, for your viewing pleasure here is wolf walking animation rendered in Blender. It is still a bit stiff, but looks pretty good at Shinya resolution:


Have fun!

Thursday, April 5, 2012

Rendering foliage with highpass filter

If rendering foliage on has two options:
  1. Render it in opaque pass, using alpha test to discard transparent pixels
  2. Render it in transparent pass, using true alpha blending
Alpha blending gives the best results but kills your rendering performance very quickly. First - the blending itself is much more fillrate intensive operation than opaque rendering. Second - you cannot use deferred rendering. And third - you have to sort polygons.

Here is the transparent pass render of small test scene, as a reference:

A patch of Dactylis glomerata, rendered in transparent pass

Even small patch of forest or meadow will have tens of thousands of polygons. Something like realistic landscape simply is not doable using transparency on current generation hardware.


This leaves us with the other option - opaque rendering.


While writing foliage shaders for Shinya the biggest problem was, that scaled down textures (i.e. higher mipmap levels) looked totally crappy. Whatever was the original shape of plant - looking at it from distance it morphed into rounded blob.
The reason for it was the minimization algorithm, that averaged alpha. Thus the parts of foliage where there were only few "holes" - i.e. central parts - became completely opaque. And the parts, where there were only few leaves - i.e. outer regions - became fully transparent.

The "original" opaque render, using default mipmap generation (box filer):

The same image, rendered in opaque pass with box-filtered mipmaps

While messing with textures and trying to find the best alpha cutoff value, I finally got the right idea - we do not want to get the average image but want to preserve "features" of image in minimization. Features being both "holes" and "patches" (i.e. leaves). This can be done by increasing high frequencies of image. I.e. we want to replace the standard mipmap generation filter (normally box filter) with something better.
Fortunately I was already composing mipmaps by hand while creating packed textures. Thus I added very simple edge-detection filter with 3x3 kernel to the foliage mipmap generation (after the minimization with box filer). And the results were really good.

The "enhanced" opaque render with edge detection filter:

The same image, rendered in opaque pass with edge-detection filter

As you can see, edge detection successfully preserves the general features of the texture - stems and narrow leaves, while simple box filter tends to smear these out.

The kernel for edge-detection was:

-1 -1 -1
-1  9 -1
-1 -1 -1

And here is a screenshot of the foliage shader in action. The meadow size is about 40x40 meters, grass is about 50000 instanced polygons.

Sara in meadow


You can download the Dactylis glomerata texture from my DeviantArt gallery.

Have fun!