Advertisement

Lens flair effect?

Started by June 11, 2003 12:08 AM
4 comments, last by skow 21 years, 8 months ago
I’m looking to add a lens flair to a space shooter. I figure you use a point and some how project it a few quads onto Ortho mode. I’m not sure how to go about how to calculate where to center the images on the ortho. Is there a tutorial for this any one knows of? I can’t find anything too good on it. Or if there are any ideas on how to calculate it I would appreciate it.
I am in a real hurry right now, but here is how I implement it:
LensFlare.h:
#pragma once#include "displayobject.h"struct t_SingleFlare{	NAN2DSprite* pSprite;	FLOAT fLengthScale;	FLOAT fImageScale;};typedef list<t_SingleFlare> t_SingleFlareList;struct t_LensFlareInfo{	t_SingleFlareList Flares;	NANStateBlock* Shader;	FLOAT fTransAlpha,fFullAlpha;};class CLensFlare :	public CObject{public:	t_LensFlareInfo* pInfo;	INT* pfLightX;	INT* pfLightY;	FLOAT fDrawAlpha;	BOOL  bKillFlag;	~CLensFlare(void);	CLensFlare(t_LensFlareInfo* pFlareInfo,INT* fFollowX,INT* fFollowY);	BOOL Process();};extern t_LensFlareInfo FlareInfo1;void InitLensFlares();
LensFlare.cpp:
#include "PreCompiled.h"#include "player.h"#include "game.h"#include "lensflare.h"#include "shared.h"#include "input.h"t_LensFlareInfo FlareInfo1;FLOAT Foo=300.0f,Bar=100.0f;void CompileFlareInfo(t_LensFlareInfo* pInfo,TCHAR* strFilename){	gaurd(CompileFlareInfo());	assert(pInfo);	NANIniFile* pIni = new NANIniFile;	LPSTR strSectionName = "Flare";	pIni->Open(strFilename);	LPSTR strShaderFile = pIni->GetString(strSectionName,"Shader");	if (strShaderFile) {		CompileStateBlock(strShaderFile,&pInfo->Shader);	}else{		pInfo->Shader = NULL;	}	INT NumFlares = pIni->GetInteger(strSectionName,"NumFlares");	for( INT i=1; i <= NumFlares; i++)	{		t_SingleFlare TheFlare;		TheFlare.pSprite = NULL;		TheFlare.fImageScale = pIni->GetFloat(strSectionName,fs("Flare%dImageScale",i));		TheFlare.fLengthScale = pIni->GetFloat(strSectionName,fs("Flare%dLengthScale",i));		N_NEWOBJ(TheFlare.pSprite,NAN2DSprite(pIni->GetString(strSectionName,fs("Flare%dImage",i))));		FlareInfo1.Flares.push_back(TheFlare);	}	pInfo->fFullAlpha = pIni->GetFloat(strSectionName,"FullAlpha");	pInfo->fTransAlpha = pIni->GetFloat(strSectionName,"TransAlpha");	SAFE_DELETE(pIni);	ungaurd;}void InitLensFlares(){	gaurd(InitLensFlares());	/*CompileFlareInfo(&FlareInfo1,"Scripts/Flare1.flr");	AddDisplayObject(new CLensFlare(&FlareInfo1,&MouseX,&MouseY) );*/	ungaurd;}CLensFlare::~CLensFlare(void){}CLensFlare::CLensFlare(t_LensFlareInfo* pFlareInfo,INT* fFollowX,INT* fFollowY):CObject(LensFlare){	pInfo = pFlareInfo;	pfLightX = fFollowX;	pfLightY = fFollowY;	fDrawAlpha = 255.0f;	bKillFlag = FALSE;}BOOL CLensFlare::Process(){	gaurd(CLensFlare::Process());	if(bKillFlag == TRUE){		return FALSE;	}	t_SingleFlareList::iterator iter;	FLOAT fLightX = *pfLightX;	FLOAT fLightY = *pfLightY;	FLOAT fDx = fLightX - (ScreenWidth_2);	FLOAT fDy = fLightY - (ScreenHeight_2);	//FLOAT fR = sqrt(fDx*fDx + fDy*fDy);	//fAlpha = (DWORD)fR % 255;	for(iter = pInfo->Flares.begin(); iter != pInfo->Flares.end(); iter++)	{		t_SingleFlare TheFlare = (*iter);		FLOAT fX = fDx*TheFlare.fLengthScale;		FLOAT fY = fDy*TheFlare.fLengthScale;		fX += ScreenWidth_2; /* center it */		fY += ScreenHeight_2;		fX -= ((TheFlare.pSprite->GetWidth()*TheFlare.fImageScale)*fWidthScale)*0.5f;		fY -= ((TheFlare.pSprite->GetHeight()*TheFlare.fImageScale)*fHeightScale)*0.5f;		TheFlare.pSprite->SetPosition(fX,fY);		TheFlare.pSprite->SetScaling(TheFlare.fImageScale,TheFlare.fImageScale);		if (pInfo->Shader) {			TheFlare.pSprite->SetStateBlock(pInfo->Shader);		}		Draw2DSpriteToGoodRes(TheFlare.pSprite);	}	ungaurd;	return TRUE;}


Look into `BOOL CLensFlare:rocess()'

Sorry i cant explain anything right now.



[edited by - aftermath on June 11, 2003 1:29:50 AM]
Rate me up.
Advertisement
Thanks, that code is helpfull. After reading though it i dunt fully understand what is going on. Does any one have a good explination on the calculations?
Here is the first thing I found when searching "opengl lense flare". Seems to explain it well enough. If you want real physics instead of an easy trick, then it won''t help you.

-solo (my site)
-solo (my site)
In the NeHe download section is a good lens flare demo:
http://nehe.gamedev.net/data/downloads/download.asp?letter=F
Thanks for the help, that works great.

This topic is closed to new replies.

Advertisement