CHAPTER 8: Putting It All Together (^263)
glOrthof() in line 3 is a new routine that changes the projection matrix
otherwise set up in setClipping(). Here you establish a viewing
volume with the six sides similar to setting the viewing frustum
elsewhere. However, the zNear and zFar planes are a bit different. In
orthographic projection mode, the depth of your space is mapped
linearly, so a z of .5 is going to interpreted a lot differently than a
similar z value when in perspective mode. So, mixing the two can have
unexpected results if you are relying on depth buffering to manage
proper z-culling. If you want to ensure your 2D object is always visible,
then set zNear to 0 and the depth value to 0.
Setting ortho’s windows from -1 to 1 means that any 2D objects drawn
to the screen will use normalized coordinates, instead of traditional
screen coordinates. So something placed at 0,0 will be exactly in the
center of the screen. renderTextureAt() uses values based on actual
pixel positioning, but those are converted to the normalized ortho
coordinates when actually going out to the screen.
Note In this routine, a lot of necessary state calls are made all to ensure that the image will
render as expected, but they come with a fairly high overhead. You want to change the state as
little as possible if speed is critical. This illustrates one of the issues with a state machine such
as OpenGL, because it holds a particular state until explicitly changed elsewhere. That means
you shouldn’t expect a state to be what you really need, forcing lengthy and frequently
redundant code blocks to ensure you get what you want. A lot of optimizing tricks can be
employed to minimize state changes. One easy way would be to batch the texture calls
together as much as possible and then call the state routines only once for each batch
operation.
Both the lens flare manager, which I call LensFlare.mm, and the individual flare object
need to be modified. To the execute method of LensFlare.mm I’ve added two new
parameters. execute() should now look like this:
-(void)execute:(CGRect)frame source:(CGPoint)source scale:(float)scale
alpha:(float)alpha
scale sizes the various individual flare objects, and alpha sets their translucency. These
get passed on to the actual flare object when called. The flare’s own execute routine is
actually called renderFlareAt() and should look like this:
-(void)renderFlareAt:(CGPoint)position scale:(float)scale alpha:(float)alpha
{
[[OpenGLCreateTexture getObject]renderTextureAt:position
name:m_Name size:m_Sizescale r:m_Redalpha g:m_Greenalpha b:m_Bluealpha a:alpha];
}
singke
(singke)
#1