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

flare.cpp File Reference

#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include "flare.h"
#include "texture.h"
#include "vector.h"
#include "camera.h"

Go to the source code of this file.

Compounds

struct  TEXTURE_DEF

Defines

#define HEIGHTFROMWIDTH(w)   ((w)*SCREENheight/SCREENwidth)
#define isqrt(x)   (int)((double)(x))
#define FLARE_MINCOLOUR   MAKEID(60, 255, 200, 0)
#define FLARE_MAXCOLOUR   MAKEID(220, 235, 235, 64)
#define FLARE_MINELEMENTSPERFLARE   8
#define FLARE_MAXSIZE   0.2f

Typedefs

typedef TEXTURE_DEF TEXTURE_DEF

Enumerations

enum  {
  TEX_CRCL, TEX_HXGN, TEX_RING, TEX_FLAR,
  NPIECETEXTURES, TEX_SUN = NPIECETEXTURES, TEX_BACK, NTEXTURES
}

Functions

TEXTURE_DEFTM_getNthTexture (int n)
TEXTURE_DEFTM_getNamedTexture (char *name)
void TM_loadTextures (void)
void TM_setTexture (TEXTURE_DEF *tex)
void drawQuad (int x, int y, int width, int height, TEXTURE_DEF *tex, unsigned int colour)
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 loadFlareFile (FLARE_DEF *flare, char *filename)
void newFlare (int bFromFile)
void DrawLensFlare ()
void InitializeLensFlare ()
void ResetLensFlare ()
void CleanupLensFlare ()

Variables

float SunPosition [3]
TEXTUREtexture
CAMERAcamera
int currentCamera
HWND hWnd
RECT screen
PAINTSTRUCT ps
TEXTURE_DEF gTextures [NTEXTURES]
int SCREENwidth = screen.right
int SCREENheight = screen.bottom
FLARE_DEF renderFlare


Define Documentation

#define FLARE_MAXCOLOUR   MAKEID(220, 235, 235, 64)
 

Definition at line 288 of file flare.cpp.

Referenced by newFlare(), and ResetLensFlare().

#define FLARE_MAXSIZE   0.2f
 

Definition at line 290 of file flare.cpp.

Referenced by newFlare(), and ResetLensFlare().

#define FLARE_MINCOLOUR   MAKEID(60, 255, 200, 0)
 

Definition at line 287 of file flare.cpp.

Referenced by newFlare(), and ResetLensFlare().

#define FLARE_MINELEMENTSPERFLARE   8
 

Definition at line 289 of file flare.cpp.

Referenced by newFlare(), and ResetLensFlare().

#define HEIGHTFROMWIDTH      ((w)*SCREENheight/SCREENwidth)
 

Definition at line 48 of file flare.cpp.

Referenced by FLARE_render().

#define isqrt      (int)((double)(x))
 

Definition at line 50 of file flare.cpp.

Referenced by FLARE_render().


Typedef Documentation

typedef struct TEXTURE_DEF TEXTURE_DEF
 


Enumeration Type Documentation

anonymous enum
 

Enumeration values:
TEX_CRCL 
TEX_HXGN 
TEX_RING 
TEX_FLAR 
NPIECETEXTURES 
TEX_SUN 
TEX_BACK 
NTEXTURES 

Definition at line 64 of file flare.cpp.

00065 {
00066     TEX_CRCL,
00067     TEX_HXGN,
00068     TEX_RING,
00069     TEX_FLAR,
00070     NPIECETEXTURES,
00071     TEX_SUN = NPIECETEXTURES,
00072     TEX_BACK,
00073     NTEXTURES
00074 };


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
 

Definition at line 228 of file flare.cpp.

References TEXTURE_DEF::FlareTexture, and TEXTURE::TexID.

00229 {
00230     glBindTexture(GL_TEXTURE_2D, tex->FlareTexture->TexID);
00231 
00232     glBegin(GL_QUADS);
00233 
00234     glColor4ub( (GLbyte)(colour >> 16 & 0xff),
00235                 (GLbyte)(colour >>  8 & 0xff),
00236                 (GLbyte)(colour >>  0 & 0xff),
00237                 (GLbyte)(colour >> 24 & 0xff) );
00238 
00239     glTexCoord2f( 0.0f, 0.0f );
00240     glVertex3f( x, y, 1.0);
00241     glTexCoord2f( 1.0f, 0.0f );
00242     glVertex3f( x + width, y, 1.0);
00243     glTexCoord2f( 1.0f, 1.0f );
00244     glVertex3f( x + width, y + height, 1.0);
00245     glTexCoord2f( 0.0f, 1.0f );
00246     glVertex3f( x, y + height, 1.0);
00247 
00248     glEnd();
00249 }

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
 

Definition at line 262 of file flare.cpp.

References NTEXTURES.

00263 {
00264     int     i;
00265 
00266     for ( i = 0; i < NTEXTURES; ++i )
00267     {
00268         if ( strncmp( name, gTextures[i].FlareTexture->TexName, strlen( name ) ) == 0 )
00269             return &gTextures[i];
00270     }
00271 
00272     return NULL;
00273 }

TEXTURE_DEF * TM_getNthTexture int    n
 

Definition at line 254 of file flare.cpp.

References NTEXTURES.

00255 {
00256     if ( ( n < 0 ) || ( n >= NTEXTURES ) )
00257         return NULL;
00258 
00259     return &gTextures[n];
00260 }

void TM_loadTextures void   
 

Definition at line 275 of file flare.cpp.

References TEXTURE_DEF::FlareTexture, TEXTURE::LoadTGA(), and NTEXTURES.

Referenced by InitializeLensFlare().

00276 {
00277     int             i;
00278 
00279     TEXTURE_DEF *tex = &gTextures[0];
00280 
00281     for ( i = 0; i < NTEXTURES; ++i, ++tex )
00282     {
00283         tex->FlareTexture->LoadTGA();            
00284     }
00285 }

void TM_setTexture TEXTURE_DEF   tex
 


Variable Documentation

CAMERA* camera
 

Definition at line 37 of file flare.cpp.

int currentCamera
 

Definition at line 38 of file flare.cpp.

Referenced by DrawLensFlare().

TEXTURE_DEF gTextures[ NTEXTURES ]
 

Initial value:

            {
                { "crcl.tga" },
                { "hxgn.tga" },
                { "ring.tga" },
                { "flar.tga" },
                { "sun.tga"  },
                { "back.tga" }
            }

Definition at line 78 of file flare.cpp.

HWND hWnd
 

Definition at line 39 of file flare.cpp.

PAINTSTRUCT ps
 

Definition at line 41 of file flare.cpp.

Referenced by WndProc().

FLARE_DEF renderFlare
 

Definition at line 292 of file flare.cpp.

RECT screen
 

Definition at line 40 of file flare.cpp.

Referenced by DrawLensFlare(), InitializeLensFlare(), WinMain(), and WndProc().

int SCREENheight = screen.bottom
 

Definition at line 103 of file flare.cpp.

Referenced by DrawLensFlare(), and InitializeLensFlare().

int SCREENwidth = screen.right
 

Definition at line 102 of file flare.cpp.

Referenced by DrawLensFlare(), FLARE_render(), and InitializeLensFlare().

float SunPosition[3]
 

Definition at line 35 of file flare.cpp.

Referenced by DrawLensFlare(), InitGL(), and InitializeLensFlare().

TEXTURE* texture
 

Definition at line 36 of file flare.cpp.


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