6、纹理
bool renderTeximageMap ()
{
CWaitCursor hourglass;
int size = 1 << teximageIter;
UINT pmapDim, smallDim; if (teximageFA && teximageFAdirty) {
freeFractArray (teximageFA);
teximageFA = NULL;
}
if (!teximageFA) {
teximageFA = alloc2DFractArray (size);
if (teximageFA==NULL) return (FALSE);
fill2DFractArray (teximageFA, size, randomSeed,
DEF_HEIGHT_SCALE, teximageH);
}
// Find the biggest power of two that will fit
// in the current window. This is where we will
// draw the image.
smallDim = (currentWidth > currentHeight) ?
currentHeight : currentWidth;
pmapDim = 1 << (UINT) (log ((float) smallDim) / log (2.));
glPushAttrib (0xfffffff);
// Only draw into biggest power of 2 square.
glViewport (0, 0, pmapDim, pmapDim);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-1.f, 1.f,
-1.f, 1.f, 1., -1.);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
// The array is rendered into the XZ plane. We need to
// rotate by 90 degrees so we will be looking at it
// "top down".
glRotatef (90.f, 1.f, 0.f, 0.f);
// Init lights now. Light position must be specified
// after the rotate, or else it won''''''''''''''''''''''''''''''''t be rotated.
initLights ();
glDisable (GL_DEPTH_TEST);
glDisable (GL_FOG);
glEnable (GL_LIGHTING);
glDisable (GL_BLEND);
glClear (GL_COLOR_BUFFER_BIT);
renderAsTriangles (teximageFA, size, 1, 0);
glFlush ();
glPopAttrib ();
if (teximageFAdirty) {
GLubyte *pmap;
pmap = (GLubyte *) malloc (pmapDim*pmapDim*3);
if (pmap==NULL) return (FALSE);
glReadPixels (0, 0, pmapDim, pmapDim,
GL_RGB, GL_UNSIGNED_BYTE, pmap);
glNewList (TEXIMAGEMAP, GL_COMPILE);
glTexImage2D (GL_TEXTURE_2D, 0, /* lod */
3, /* num components */
pmapDim, pmapDim, /* width, height */
0, /* border */
GL_RGB, GL_UNSIGNED_BYTE, /* format, type */
pmap);
glEndList ();
free (pmap);
}
teximageFAdirty = FALSE;
return (TRUE);
}