CHAPTER 8: Putting It All Together (^285)
Why? Beats me. But the smooth lines we take for granted on desktops are absent on
the handhelds. The Retina display makes anti-aliasing pretty much moot simply because
of its resolution. However, on non-Retina devices, including the iPad (as of this writing),
it still looks pretty rough. There are a couple of workarounds, one more of a hack than
the other. The first and more hacky of the two, dumps the normal OpenGL lines support
and substitutes really long and thin textured objects, because they can be smoothed
out. A nice side effect is that (with a little extra work) you can get dotted lines as well.
The less hacky version, but a lot easier to do, is to use a feature found in iOS 4 and
after: multisampled anti-aliasing (MSAA to its friends).
Multisampling as a means toward anti-aliasing requires the addition of a special
multisample frame buffer object. Your image is initially written to the special buffer in a
resolution that is higher than your final display. It is then ‘‘resolved’’ into a smaller buffer,
with the final pixels representing blended versions of the originals. Typically the
multisampling buffer is four times larger than the final one; that is, each of the final pixels
is generally a weighted average of the four pixels in the former (not unlike texture filtering
covered in Chapter 5). This is also called full-screen anti-aliasing, because that is exactly
what it does. The benefits are a smoother image; the drawbacks include a performance
hit and extra memory used for the off-screen buffer. Figure 8-9 (left) shows one of our
lines with no MSAA, while Figure 8-9 (right) shows it with MSAA turned on.
Figure 8-9. Multisample anti-aliasing, before (left) and after (right)
Another nice addition in iOS5’s GLKit is support for MSAA that is built into GLKView.
Previously, it took about 40 lines of code, but now all you need to do is to add the
following to viewDidLoad() in your view controller:
view.drawableMultisample=GLKViewDrawableMultisample4X;
Adding a UI
Of course, any app that doesn’t have a means to interact with it is usually called a
demo. But here our little demo will in fact gain both a simple user-interface and HUD
graphics.
When it comes to adding UI elements to your OpenGL app, I have some very good
news. Because the GLKView is a subclass of UIView, you can treat it like any other view
object. This means you can create and add any other UIView object, hence practically
any UIKit controls, as subviews to your 3D scene. This also means you have full access
to animation properties of the views as well, to create a truly fluid interface. Figure 8-10
shows a quick UI I added to the example to turn on and off various objects along with an
animated sweep that goes across the screen. The buttons are UIButtons with custom
imagery, the text is a UILabel, and the green lines sweep across the screen like some
sort of radar sequence.
singke
(singke)
#1