Monday, March 07, 2005

Print-Quality Screenshots

For high-quality prints you need high-quality screenshots. This means very high resolution, and nice anti-aliasing. Just grabbing the screen produces rather unpleasant results (screenshot, 80 KB, 800x600 pixels). With normal OpenGL rendering you get rarely more than screen resolution, and anti-aliasing quality very much depends on your graphics board. So what to do?

Tiled Rendering comes to the rescue. Instead of rendering the whole image at once, we render smaller portions of the scene, and then arrange the tiles into a large picture. However, just pointing the camera at each tile will not work as intended, the perspective would change from tile to tile. What is needed instead is to construct partial viewing frustums that together exactly recreate the whole frustum. This sounds like a lot of math, but actually it is quite simple:
gluPerspective: fov aspect: aspect zNear: near zFar: far tile: rect
| cotangent radians w h |
radians := (fov/2.0) degreesToRadians.
cotangent := radians cos / radians sin.
h := near / cotangent.
w := h * aspect.
self glFrustum(rect left*w, rect right*w, rect bottom*h, rect top*h, near, far).
So I just extended the existing gluPerspective:aspect:zNear:zFar: method with a tile argument that gives the sub-rectangle in the viewing plane, where the whole picture ranges from -1 to 1. The only problem is to hand down that extra argument into the method. I ended up copying all the methods in the call chain, adding a tile: parameter. With the methods in place and a utility method to construct the right sub-rectangles, I could render the image from the first screenshot again:
| m c f |
m := CroquetGlobals theTeapotMorph.
c := m activeCamera.
f := c root snapShot: m ogl camera: c tiled: 12.
f := f magnify: f boundingBox by: 0.5 smoothing: 4.
JPEGReadWriter2 putForm: f quality: 90 progressiveJPEG: false onFileNamed: 'croquet-bf-4800.jpg'.
Instead of 800x600, this renders 12x12 tiles, creating a 9600x7200 pixels image. The result is scaled down with smoothing to yield an anti-aliased 4800x3600 image, finally compressing as JPEG gives a 1.5 MB hires screenshot.

Beauty, eh? Never noticed that guy on the bridge before ;-)

7 comments:

Bernd said...

yeah, nice detail. Looks like a soldier with his machinegun. But when it's so, there is a soldier on every treepeak...scary vision

Anonymous said...

Okay, I've added the tile:rect parameter to the gluPerspective and all the methods in the call chain. How is the rect passed from snapshot:tile to the lower level calls? Is there additional code that's missing or left as an exercise for the reader :)

I'd love to be able to take hi-res shots.

CroqueTweaker said...

Well, TSpace snapShot:camera:tiled: calls
TSpace snapShot:camera:tile: for each rectangle. Then it goes down like this:
TSpace doRender:camera:tile:
TCamera initFrustum:tile:
OpenGL initFrustum:bounds:zNear:zFar:tile:
OpenGL gluPerspective:aspect:zNear:zFar:tile:

And because that is so ugly and boring I didn't want to post it. The only slightly interesting part is the construction of the sub rectangles from the integer tiles parameter:

ext := (2.0@2.0) / tiles.
0 to: tiles - 1 do: [ :x |
0 to: tiles - 1 do: [ :y |
pos := (x@y) * ext - (1.0@1.0).
snapshot := self snapShot: ogl camera: cam tile: (pos * (1@-1) extent: ext * (1@-1)).
...]].

Hope that helps?

Anonymous said...

Thanks. The missing piece was the construction of the sub-rectangles. I managed to compose the snapshots into a single Form and the result is a beautiful hi-rec picture.

Many thanks.

Anonymous said...

Thanks. The missing piece was the construction of the sub-rectangles. I managed to compose the snapshots into a single Form and the result is a beautiful hi-res picture.

Many thanks.

Howard Stearns said...

In the titled pictures, there are some discontinuities in the water. I assume these are at seams between tiles, and that they occur because each tile is rendered at a slightly different tea time?

CroqueTweaker said...

The particle rendering in the waterfall is hacked to take rendering time into account for speed reasons (fewer particles are shown on a slower machine). If it was purely tea-time based there should be no discontinuities.