Wednesday, August 12, 2009

Mipmapping RTT in Ogre3D and Look Up Table Filtering

Two things to write about in this post about the texture generation. First of all, when you create textures via render to texture in Ogre3d you do not get mipmaps auto-generated for you. Strangely enough, DirectX still seemed to do some kind of mipmapping, but OpenGL did not. To implement it in Ogre3d you have to do a render to texture for each mipmap level you want to generate. Here's how I did it...


for(unsigned int i = 0; i <= numMipMaps; ++i) {
// select mipmap level
target = mTexture->getBuffer(0,i)->getRenderTarget();
target->setAutoUpdated(false);

if(target->getNumViewports()) {
// get an existing viewport
mViewport = target->getViewport(0);
}
else {
// add a new viewport
mViewport = target->addViewport(mRTTCam);
mViewport->setClearEveryFrame(false);
mViewport->setOverlaysEnabled(false);
}
// render the quad
mSceneMgr->manualRender(
&mRenderOp,
mPass,
mViewport,
Matrix4::IDENTITY,
Matrix4::IDENTITY,
Matrix4::IDENTITY,
mBeginEnd //issue a _begin and _end render call
);
}


The results were decent - not as good as hardware generated mipmaps, but better than no mipmaps and yes, it does affect the speed. Rendering 2 mipmaps isn't so bad a hit but rendering 6+ becomes noticable - sorry I don't have any hard numbers.

Secondly, I was trying to combat texture banding due to the low resolution of the height maps, slope maps, and look up table. I use filtering on the height maps and slope maps so those are blended decently, but the look up table couldn't be filtered because the look up table is really 16 look up tables combined into one texture. When I introduced filtering on the look up table the banding would be lessened but I'd get artifacts where there was bleed between look up tables. Recall the look up table texture is a 1024x256 texture that is 4 256x256 textures end on end horizontally. So the horizontal access is all that really matters because vertically there is no bleed (the texture address mode is set to clamp, not wrap). The horizontal access is the slope value and so what I did was compress the slope value and center it on the middle of the look up table texture so instead of values being from 0 - 255, now they are from 15 - 239 so I don't have to worry about bleeding artifacts anymore (no pun intended).

Here's a couple before/after images showing how the trilinear filtering on the look up table improves the banding issue:

Without filtering...

(click to zoom in)

And with filtering...

(click to zoom in)

Thoughts:
1. How can I use Multiple Render Targets to render textures faster - group them up so I can render 4 textures with one shader?
2. How can I cheaply introduce noise to the altitude and slope values to make the blending better and more random looking?
3. Why is the terrain slightly blueish and dark?
4. When should I work atmosphere and water back in.

No comments: