Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

flare.h File Reference

Go to the source code of this file.

Compounds

struct  FLARE_DEF
struct  FLARE_ELEMENT_DEF

Defines

#define FLARE_MAXELEMENTSPERFLARE   15
#define FLARE_RANGE(A, B)   ( (rand()%((B)-(A)+1)) + (A) )
#define FLARE_FRANGE(A, B)   ( ((float)(rand()&0xffffff)/(float)0xfffffe)*((B)-(A)) + (A) )
#define MAKEID(a, b, c, d)   (((a)<<24) | ((b)<<16) | ((c)<<8) | ((d)<<0))

Typedefs

typedef FLARE_ELEMENT_DEF FLARE_ELEMENT_DEF
typedef FLARE_DEF FLARE_DEF

Functions

void FLARE_randomize (FLARE_DEF *flare, int nTextures, int nPieces, float fMaxSize, unsigned int minColour, unsigned int maxColour)
void FLARE_render (FLARE_DEF *flare, int lx, int ly, int cx, int cy)
void drawQuad (int x, int y, int width, int height, TEXTURE_DEF *tex, unsigned int colour)
TEXTURE_DEFTM_getNthTexture (int n)
TEXTURE_DEFTM_getNamedTexture (char *name)
void TM_loadTextures (void)
void loadFlareFile (FLARE_DEF *flare, char *filename)
void newFlare (int bFromFile)
void DrawLensFlare ()
void InitializeLensFlare ()
void ResetLensFlare ()
void CleanupLensFlare ()


Define Documentation

#define FLARE_FRANGE A,
     ( ((float)(rand()&0xffffff)/(float)0xfffffe)*((B)-(A)) + (A) )
 

Definition at line 29 of file flare.h.

Referenced by FLARE_randomize().

#define FLARE_MAXELEMENTSPERFLARE   15
 

Definition at line 27 of file flare.h.

Referenced by newFlare(), and ResetLensFlare().

#define FLARE_RANGE A,
     ( (rand()%((B)-(A)+1)) + (A) )
 

Definition at line 28 of file flare.h.

Referenced by FLARE_randomize(), newFlare(), and ResetLensFlare().

#define MAKEID a,
b,
c,
     (((a)<<24) | ((b)<<16) | ((c)<<8) | ((d)<<0))
 

Definition at line 30 of file flare.h.


Typedef Documentation

typedef struct FLARE_DEF FLARE_DEF
 

typedef struct FLARE_ELEMENT_DEF FLARE_ELEMENT_DEF
 


Function Documentation

void CleanupLensFlare  
 

Definition at line 441 of file flare.cpp.

References TEXTURE_DEF::FlareTexture, and NTEXTURES.

Referenced by WndProc().

00442 {
00443     for (int loop = 0; loop < NTEXTURES; loop++)
00444     {
00445         delete gTextures[loop].FlareTexture;
00446     }
00447 }

void DrawLensFlare  
 

Definition at line 341 of file flare.cpp.

References currentCamera, FLARE_render(), screen, SCREENheight, SCREENwidth, SunPosition, and VECTOR::x.

Referenced by DrawGLScene().

00342 {       
00343     VECTOR suns_position(25.0 + camera[currentCamera].Position.x, 20.0 + camera[currentCamera].Position.y, -35.0 + camera[currentCamera].Position.z);
00344 
00345     SunPosition[1] = -1.0;
00346     SunPosition[2] = -1.0;
00347 
00348     glRenderMode(GL_FEEDBACK);
00349     glBegin(GL_POINTS);
00350         glVertex3fv(&suns_position.x);
00351     glEnd();
00352     glRenderMode(GL_RENDER);
00353 
00354     SunPosition[2] = screen.bottom - SunPosition[2];
00355 
00356 //  Set for orthographic projection.
00357     glPushMatrix();
00358     glMatrixMode(GL_PROJECTION);
00359     glPushMatrix();
00360        gluOrtho2D(0, SCREENwidth, SCREENheight, 0);
00361     glMatrixMode(GL_MODELVIEW);
00362     glLoadIdentity();
00363 
00364     glDisable(GL_DEPTH_TEST);
00365     glDisable(GL_LIGHTING);
00366     glDisable(GL_CULL_FACE);
00367     glEnable(GL_BLEND);
00368     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00369 
00370     if ((SunPosition[1] != -1.0))
00371     {
00372         // Calculate alpha value for sun glare
00373         int position[2];
00374         position[1] = abs((int)SunPosition[1] - 400);
00375         position[2] = abs((int)SunPosition[2] - 300);
00376         float glare_distance = sqrt(position[1] * position[1] + position[2] * position[2]);
00377         float alpha = 1.0 - glare_distance / 500.0;
00378 
00379         // Render sun glare - I know it's a 32 bit texture but this is just a demo
00380         glBindTexture(GL_TEXTURE_2D, texture[6].TexID);
00381 
00382         glBegin(GL_QUADS);
00383 
00384         glColor4f(1.0, 1.0, 1.0, alpha - 0.3);
00385 
00386         glTexCoord2f( 0.0f, 0.0f );
00387         glVertex3f(0, 0, 1.0);
00388         glTexCoord2f( 1.0f, 0.0f );
00389         glVertex3f(800, 0, 1.0);
00390         glTexCoord2f( 1.0f, 1.0f );
00391         glVertex3f(800, 600, 1.0);
00392         glTexCoord2f( 0.0f, 1.0f );
00393         glVertex3f(0, 600, 1.0);
00394 /*
00395         glTexCoord2f( 0.0f, 0.0f );
00396         glVertex3f(SunPosition[1] - 400, SunPosition[2] - 300, 2.0);
00397         glTexCoord2f( 1.0f, 0.0f );
00398         glVertex3f(SunPosition[1] + 400, SunPosition[2] - 300, 2.0);
00399         glTexCoord2f( 1.0f, 1.0f );
00400         glVertex3f(SunPosition[1] + 400, SunPosition[2] + 300, 2.0);
00401         glTexCoord2f( 0.0f, 1.0f );
00402         glVertex3f(SunPosition[1] - 400, SunPosition[2] + 300, 2.0);
00403 */
00404         glEnd();
00405         
00406         FLARE_render(&renderFlare, (int)SunPosition[1], (int)SunPosition[2], SCREENwidth/2, SCREENheight/2);
00407     }
00408 
00409     glMatrixMode(GL_PROJECTION);
00410     glPopMatrix();
00411     glMatrixMode(GL_MODELVIEW);
00412     glPopMatrix();
00413 
00414     glEnable(GL_DEPTH_TEST);
00415     glEnable(GL_LIGHTING);
00416     glEnable(GL_CULL_FACE);
00417     glDisable(GL_BLEND);
00418 }

void drawQuad int    x,
int    y,
int    width,
int    height,
TEXTURE_DEF   tex,
unsigned int    colour
 

Referenced by FLARE_render().

void FLARE_randomize FLARE_DEF   flare,
int    nTextures,
int    nPieces,
float    fMaxSize,
unsigned int    minColour,
unsigned int    maxColour
 

Definition at line 115 of file flare.cpp.

References FLARE_ELEMENT_DEF::argb, FLARE_DEF::element, FLARE_ELEMENT_DEF::fDistance, FLARE_FRANGE, FLARE_RANGE, FLARE_DEF::fMaxSize, FLARE_DEF::fScale, FLARE_ELEMENT_DEF::fSize, FLARE_DEF::nPieces, FLARE_ELEMENT_DEF::texture, and TM_getNthTexture().

Referenced by newFlare(), and ResetLensFlare().

00121 {
00122     int             i;
00123     float           fFracDist;
00124     float           fEnvelopeSize;
00125     FLARE_ELEMENT_DEF *element;
00126 
00127     srand((unsigned)time(NULL));
00128 
00129     // Initialize flare with requested number of elements, with
00130     // random placement, colour, and size of elements.
00131     flare->nPieces = nPieces;
00132     flare->fScale = fMaxSize;
00133     flare->fMaxSize = fMaxSize;
00134     fFracDist = 1.0f/(float)(flare->nPieces - 1);
00135     
00136     for (i = 0; i < flare->nPieces; ++i)
00137     {
00138         element = &flare->element[i];
00139         element->fDistance = (fFracDist*i) + FLARE_FRANGE(0,fFracDist);
00140 
00141         // Envelope size is maximum at ends of line, minimum in the middle (i.e. two
00142         // cones, touching at the tips).
00143         fEnvelopeSize = (float)fabs(1.0f - 2*element->fDistance);
00144 
00145         element->fSize = FLARE_FRANGE(0.6f, 1.0f) * fEnvelopeSize;
00146         element->argb = FLARE_RANGE(minColour & 0xff000000, maxColour & 0xff000000) |
00147                         FLARE_RANGE(minColour & 0x00ff0000, maxColour & 0x00ff0000) |
00148                         FLARE_RANGE(minColour & 0x0000ff00, maxColour & 0x0000ff00) |
00149                         FLARE_RANGE(minColour & 0x000000ff, maxColour & 0x000000ff);
00150         element->texture = TM_getNthTexture( FLARE_RANGE(0, nTextures - 1) );
00151     }
00152 }

void FLARE_render FLARE_DEF   flare,
int    lx,
int    ly,
int    cx,
int    cy
 

Definition at line 166 of file flare.cpp.

References FLARE_ELEMENT_DEF::argb, drawQuad(), FLARE_DEF::element, FLARE_ELEMENT_DEF::fDistance, FLARE_DEF::fMaxSize, FLARE_DEF::fScale, FLARE_ELEMENT_DEF::fSize, HEIGHTFROMWIDTH, isqrt, FLARE_DEF::nPieces, SCREENwidth, and FLARE_ELEMENT_DEF::texture.

Referenced by DrawLensFlare().

00167 {
00168     int     dx, dy;          // Screen coordinates of "destination"
00169     int     px, py;          // Screen coordinates of flare element
00170     int     maxflaredist, flaredist, flaremaxsize, flarescale;
00171     int     width, height, alpha;    // Piece parameters;
00172     int     i;
00173     FLARE_ELEMENT_DEF    *element;
00174 
00175     // Compute how far off-center the flare source is.
00176     maxflaredist = isqrt(cx*cx + cy*cy);
00177     flaredist = isqrt((lx - cx)*(lx - cx)+
00178                       (ly - cy)*(ly - cy));
00179     flaredist = (maxflaredist - flaredist);
00180     flaremaxsize = (int)(SCREENwidth * flare->fMaxSize);
00181     flarescale = (int)(SCREENwidth * flare->fScale);
00182 
00183     // Destination is opposite side of centre from source
00184     dx = cx + (cx - lx);
00185     dy = cy + (cy - ly);
00186 
00187     // Render each element.
00188     for (i = 0; i < flare->nPieces; ++i)
00189     {
00190         element = &flare->element[i];
00191 
00192         // Position is interpolated along line between start and destination.
00193         px = (int)((1.0f - element->fDistance)*lx + element->fDistance*dx);
00194         py = (int)((1.0f - element->fDistance)*ly + element->fDistance*dy);
00195 
00196         // Piece size are 0 to 1; flare size is proportion of
00197         // screen width; scale by flaredist/maxflaredist.
00198         width = (int)((flaredist*flarescale*element->fSize)/maxflaredist);
00199 
00200         // Width gets clamped, to allows the off-axis flares
00201         // to keep a good size without letting the elements get
00202         // too big when centered.
00203         if (width > flaremaxsize)
00204         {
00205             width = flaremaxsize;
00206         }
00207 
00208         // Flare elements are square (round) so height is just
00209         // width scaled by aspect ratio.
00210         height = HEIGHTFROMWIDTH(width);
00211         alpha = (flaredist*(element->argb >> 24))/maxflaredist;
00212 
00213         if ( width > 1 )
00214         {
00215             unsigned int    argb = (alpha << 24 ) | (element->argb & 0x00ffffff);
00216 
00217             drawQuad( px - width/2, py - height/2, width, height, element->texture, argb );
00218         }
00219     }
00220 }

void InitializeLensFlare  
 

Definition at line 420 of file flare.cpp.

References TEXTURE_DEF::filename, TEXTURE_DEF::FlareTexture, newFlare(), NTEXTURES, screen, SCREENheight, SCREENwidth, SunPosition, and TM_loadTextures().

Referenced by InitGL().

00421 {
00422     SCREENwidth = screen.right;
00423     SCREENheight = screen.bottom;
00424 
00425     for (int loop = 0; loop < NTEXTURES; loop++)
00426     {
00427         gTextures[loop].FlareTexture = new TEXTURE;
00428         sprintf(gTextures[loop].FlareTexture->TexName, "%s", gTextures[loop].filename);
00429     }
00430     TM_loadTextures();
00431     newFlare( 0 );
00432 
00433     glFeedbackBuffer(3, GL_2D, SunPosition);
00434 }

void loadFlareFile FLARE_DEF   flare,
char *    filename
 

Definition at line 294 of file flare.cpp.

References FLARE_ELEMENT_DEF::argb, FLARE_DEF::element, FLARE_ELEMENT_DEF::fDistance, FLARE_DEF::fMaxSize, FLARE_DEF::fScale, FLARE_ELEMENT_DEF::fSize, FLARE_DEF::nPieces, FLARE_ELEMENT_DEF::texture, and TM_getNamedTexture().

Referenced by newFlare().

00295 {
00296     int     n = 0;
00297     FILE    *f;
00298     char    buf[256];
00299 
00300     memset( flare, 0, sizeof( FLARE_DEF ) );
00301 
00302     f = fopen( filename, "r" );
00303     if ( f )
00304     {
00305         fgets( buf, sizeof( buf ), f );
00306         sscanf( buf, "%f %f", &flare->fScale, &flare->fMaxSize );
00307 
00308         while ( !feof(f) )
00309         {
00310             char            name[8] = { '\0', };
00311             double          dDist = 0.0, dSize = 0.0;
00312             unsigned int    a = 0, r = 0, g = 0, b = 0;
00313 
00314             fgets( buf, sizeof( buf ), f );
00315             if ( sscanf( buf, "%s %lf %lf ( %d %d %d %d )",
00316                     name, &dDist, &dSize, &a, &r, &g, &b ) )
00317             {
00318                 flare->element[ n ].texture = TM_getNamedTexture( name );
00319                 flare->element[ n ].fDistance = (float)dDist;
00320                 flare->element[ n ].fSize = (float)dSize;
00321                 flare->element[ n ].argb = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | ( b << 0 );
00322 
00323                 ++n;
00324             }
00325         }
00326 
00327         flare->nPieces = n;
00328 
00329         fclose( f );
00330     }
00331 }

void newFlare int    bFromFile
 

Definition at line 333 of file flare.cpp.

References FLARE_MAXCOLOUR, FLARE_MAXELEMENTSPERFLARE, FLARE_MAXSIZE, FLARE_MINCOLOUR, FLARE_MINELEMENTSPERFLARE, FLARE_randomize(), FLARE_RANGE, loadFlareFile(), and NPIECETEXTURES.

Referenced by InitializeLensFlare().

void ResetLensFlare  
 

Definition at line 436 of file flare.cpp.

References FLARE_MAXCOLOUR, FLARE_MAXELEMENTSPERFLARE, FLARE_MAXSIZE, FLARE_MINCOLOUR, FLARE_MINELEMENTSPERFLARE, FLARE_randomize(), FLARE_RANGE, and NPIECETEXTURES.

Referenced by WinMain().

TEXTURE_DEF* TM_getNamedTexture char *    name
 

Referenced by loadFlareFile().

TEXTURE_DEF* TM_getNthTexture int    n
 

Referenced by FLARE_randomize().

void TM_loadTextures void   
 


Generated on Fri Dec 23 05:21:21 2005 for Skybox by doxygen1.2.15