Game Development Community

dev|Pro Game Development Curriculum

fxLensFlare for TGEA

by CdnGater · 08/23/2007 (4:39 pm) · 19 comments

Download Code File

I found no LensFlare for TGEA, so I made this and submitted it as a resource. fxLensFlare is based
on a GameObject, and so can be attached to other objects, or added directly to a mission.

I have not included textures I used. You can find them anywhere on the web. I have included a FlareDataBlock.cs with 2 sample Flares, as well as the fxLensFlare.cpp and fxLensFlare.h.

Note: in fxLensFlare::renderFlare, you may have to change PlayGui to what you called your GuiTSCtrl.

Below is the definition of the fxLensFlareData used with fxLensFlare.

Radius - The overall radius in TGEA units
BorderAmount - The number of Pixels used for determining Border Fading
CenterAmount - The number of Pixels used for determining Off-Center Fading
ApplyWhiteout - Should this flare white out the screen when directly in front?

Currently, you can have up to 15 glows per Flare
GlowSize - Scale of the Glow in both width and height
GlowWidth - Overrides the width Scale (Default is zero which is disabled)
GlowRotate - Rotation in degrees
GlowColour - Color to be applied
GlowOffset - Distance offset (0 being source location, 1 being center of screen)
GlowTexture - Texture to be applied
Glow3D - Should this use 3D location for the Glow
GlowBlendMode - (0-2) Check code for what blending it does
BorderAffectToInScale - Should the Border distance affect the scale?
BorderAffectToIntensity - Should the Border distance affect the intensity?
OffCenterAffectToIntensity - Should the how far from center affect the intensity?
ZAffectToIntensity - Should the distance affect the intensity?
(Source - Destination) / VisibleDistance
ZAffectToScale - Should the distance affect the scale?
Source - Destination) / VisibleDistance


fxLensFlare has some fields as well

UseAzimuthElevation - Will place the flare visibleDistance away at the
supplied Azimuth & Elevation
Azimuth - -360/+360 in degrees
Elevation - -90 or +90 in degrees



===================
renderinstance/renderInstMgr.h

add between RIT_Translucent and RIT_Begin

RIT_Flare,


You will also need to add the whiteout ability back into TGEA by following these steps.

============================
game/GameConnection.h

Add right after F32 getWhiteOut() { return mWhiteOut; }

void setWhiteOut(const F32 flash) {mWhiteOut = flash;}


===================
game/Game.h

Add after void GameRenderWorld();

/// Renders overlays such as damage flashes, white outs, and water masks.  These are usually a color applied over the entire screen.
void GameRenderFilters(const CameraQuery& camq);

===================
game/Game.cpp

Add after the last include

#include "gfx/primBuilder.h"


Add after the function GameProcessCameraQuery

void GameRenderFilters(const CameraQuery& camq)
{
	// first get the game connection for this player
	GameConnection* connection = GameConnection::getConnectionToServer();
 
	F32 maxDamageFilter  = 0.77f;
	F32 damageFlash      = 0.f; ColorF damageFilterColor(1.f, 0.f, 0.f, 0.f);
	F32 whiteOut         = 0.f; ColorF whiteoutFilterColor(1.f, 1.f, 1.f, 0.f);
 
	// final color and alpha of filter + an adder
	ColorF Xcolor(0.f, 0.f, 0.f, 1.f);
 
	if(connection)
	{
		// grab the damage flash alpha value
		damageFlash = connection->getDamageFlash();
		if( damageFlash > maxDamageFilter )
			damageFlash = maxDamageFilter;
 
		damageFilterColor.alpha = damageFlash;
 
		// grab the whiteout value
		whiteoutFilterColor.alpha = connection->getWhiteOut();
 
		// compute the final color and alpha
		Xcolor         = ( Xcolor * ( 1.0f - damageFilterColor.alpha ) ) + ( damageFilterColor * damageFilterColor.alpha );
		Xcolor.alpha   =   Xcolor.alpha * ( 1.0f - damageFilterColor.alpha  );
 
		Xcolor         = ( Xcolor * ( 1.0f - whiteoutFilterColor.alpha ) ) + ( whiteoutFilterColor * whiteoutFilterColor.alpha );
		Xcolor.alpha   =   Xcolor.alpha * ( 1.0f - whiteoutFilterColor.alpha  );
 
 
		MatrixF projection = GFX->getProjectionMatrix();
 
		GFX->setProjectionMatrix( MatrixF( true ) );
		GFX->pushWorldMatrix();
		GFX->setWorldMatrix( MatrixF( true ) );
 
		GFX->setZEnable(false);
		GFX->setZWriteEnable(false);
 
		GFX->setAlphaBlendEnable(true);
		GFX->setSrcBlend(GFXBlendSrcAlpha);
		GFX->setDestBlend(GFXBlendOne);
 
		PrimBuild::color(Xcolor);
 
		PrimBuild::begin(GFXTriangleFan, 4);
			PrimBuild::vertex3f(-1, -1, 1);
			PrimBuild::vertex3f(-1,  1, 1);
			PrimBuild::vertex3f( 1,  1, 1);
			PrimBuild::vertex3f( 1, -1, 1);
		PrimBuild::end();
 
		GFX->popWorldMatrix();
 
		GFX->setProjectionMatrix(projection);
	}
}

===================
game/GameTSCtrl.cpp

Go to the function GameTSCtrl::onRender

Add after GFX->setViewport( updateRect );

CameraQuery camq = mLastCameraQuery;
   if(GameProcessCameraQuery(&camq))
      GameRenderFilters(camq);


Need to do the following to add fxLensFlare to the editor

===================
creator/objectBuilderGui.gui

after

//------------------------------------------------------------------------------
// Environment
//------------------------------------------------------------------------------

Add
function ObjectBuilderGui::buildfxLensFlare(%this){	
     %this.className = "fxLensFlare";	
     %this.addField("dataBlock", "TypeDataBlock", "fxLensFlare Data", "fxLensFlareData");
     %this.process();
}


===================
creator/EditorGui.cs

go to the function Creator::init

Scroll down until you see

%Environment_Item[0] = "Sky";
%Environment_Item[1] = "Sun";
%Environment_Item[2] = "Lightning";
%Environment_Item[3] = "Water";
%Environment_Item[4] = "Terrain";
%Environment_Item[5] = "AudioEmitter";
%Environment_Item[6] = "Precipitation";
%Environment_Item[7] = "ParticleEmitter";
%Environment_Item[8] = "fxSunLight";
%Environment_Item[9] = "fxShapeReplicator";
%Environment_Item[10] = "fxFoliageReplicator";
%Environment_Item[11] = "fxLight";
 
%Environment_Item[12] = "sgLightObject";
%Environment_Item[13] = "VolumeLight";
%Environment_Item[14] = "sgMissionLightingFilter";
%Environment_Item[15] = "sgDecalProjector";

I placed mine after fxLight because I wanted them close.

%Environment_Item[0]  = "Sky";   
%Environment_Item[1]  = "Sun";   
%Environment_Item[2]  = "Lightning";  
%Environment_Item[3]  = "Water";   
%Environment_Item[4]  = "Terrain";   
%Environment_Item[5]  = "AudioEmitter";   
%Environment_Item[6]  = "Precipitation";   
%Environment_Item[7]  = "ParticleEmitter";  
%Environment_Item[8]  = "fxSunLight";  
%Environment_Item[9]  = "fxShapeReplicator";   
%Environment_Item[10] = "fxFoliageReplicator";   
%Environment_Item[11] = "fxLight";   
%Environment_Item[12] = "fxLensFlare";      
 
%Environment_Item[13] = "sgLightObject";   
%Environment_Item[14] = "VolumeLight";   
%Environment_Item[15] = "sgMissionLightingFilter";   
%Environment_Item[16] = "sgDecalProjector";


It should be noted, that in the screenshot the flare on the right of the planet, is the new type. It will stay in that position realtive to the position of the camera. In this case, it's used to simulate a bright but far away star.

Edit: Updated code to reflect changes below

#1
07/24/2007 (10:23 am)
Spectacular, installing now.
#2
07/24/2007 (2:21 pm)
Compiling...
fxLensFlare.cpp
\Torque\TGEA 1.01\engine\game\fx\fxLensFlare.cpp(431) : error C2039: 'setWhiteOut' : is not a member of 'GameConnection'
        ../engine\game\gameConnection.h(41) : see declaration of 'GameConnection'
\Torque\TGEA 1.01\engine\game\fx\fxLensFlare.cpp(465) : error C2039: 'setWhiteOut' : is not a member of 'GameConnection'
        ../engine\game\gameConnection.h(41) : see declaration of 'GameConnection'

But even in your fxLensFlare.cpp code you have a comment about:
//Had to add setWhiteOut function to GameConnection

I followed the resource to the letter.
#3
07/24/2007 (3:28 pm)
Just add a function that looks like this:
void setWhiteOut( F32 whiteOut )
{
   mWhiteOut = whiteOut;
}

You should check the code next time :p This is just a simple set method...

Although the above example doesn't have any checking of any sort. So it may be a good idea to add that ;)
#4
07/24/2007 (3:34 pm)
Lol, hey, I don't like to think, was multitasking at the moment. :) Thanks for that.
#5
07/24/2007 (3:37 pm)
@ Chris

Sorry, guess I missed one part

@ Ross

Your right, its very simple. Here is what I had done.

============================
game/GameConnection.h

Add right after F32 getWhiteOut() { return mWhiteOut; }

void setWhiteOut(const F32 flash) {mWhiteOut = flash;}


I was thinking on my way home from work, there is another type of flare, a 3d relative type, so will probably work on that tonight.
#6
07/24/2007 (3:44 pm)
No problem :p

Also, inline = better as well. And const for the parameter of course. Should have thought of that.

BTW, very cool resource :)
#7
07/24/2007 (8:59 pm)
I have added the 3d relative to camera style of doing a lensflare now. Updated the source and even the screenshot.

Have fun.
#8
07/25/2007 (1:50 am)
Against which TGEA version was this one done?
The stuff meant to be added has quite nothing to do with TGEA 1.01 ...
common/editor isn't existing, it should be in creator/editor ...
environment items go up to 15 in stock TGEA 1.01 ...

And it does not work with it as well from what it seems
#9
07/25/2007 (5:11 am)
@ Marc

It works with the latest version of TGEA.

Are you talking about the editor stuff?

And what do you mean

Quote:
And it does not work with it as well from what it seems

Did you try it, or is this a guess?
#10
07/25/2007 (6:09 am)
Yeh, doesn't work with the editors it seems, not sure how to add one into the mission. It's not listed under Environment as it should be.
#11
07/25/2007 (6:43 am)
Well Here is what I got

in objectBuilderGui.gui

after

//------------------------------------------------------------------------------
// Environment
//------------------------------------------------------------------------------

I added

function ObjectBuilderGui::buildfxLensFlare(%this)
{
	%this.className = "fxLensFlare";
	%this.process();
}


in EditorGui.cs

go to the function Creator::init

Scroll down until you see


%Environment_Item[0] = "Sky";
%Environment_Item[1] = "Sun";
%Environment_Item[2] = "Lightning";
%Environment_Item[3] = "Water";
%Environment_Item[4] = "Terrain";
%Environment_Item[5] = "AudioEmitter";
%Environment_Item[6] = "Precipitation";
%Environment_Item[7] = "ParticleEmitter";
%Environment_Item[8] = "fxSunLight";
%Environment_Item[9] = "fxShapeReplicator";
%Environment_Item[10] = "fxFoliageReplicator";
%Environment_Item[11] = "fxLight";

%Environment_Item[12] = "sgLightObject";
%Environment_Item[13] = "VolumeLight";
%Environment_Item[14] = "sgMissionLightingFilter";
%Environment_Item[15] = "sgDecalProjector";

I placed mine after fxLight because I wanted them close

%Environment_Item[0]  = "Sky";
   %Environment_Item[1]  = "Sun";
   %Environment_Item[2]  = "Lightning";
   %Environment_Item[3]  = "Water";
   %Environment_Item[4]  = "Terrain";
   %Environment_Item[5]  = "AudioEmitter";
   %Environment_Item[6]  = "Precipitation";
   %Environment_Item[7]  = "ParticleEmitter";
   %Environment_Item[8]  = "fxSunLight";
   %Environment_Item[9]  = "fxShapeReplicator";
   %Environment_Item[10] = "fxFoliageReplicator";
   %Environment_Item[11] = "fxLight";
   %Environment_Item[12] = "fxLensFlare";
   
   %Environment_Item[13] = "sgLightObject";
   %Environment_Item[14] = "VolumeLight";
   %Environment_Item[15] = "sgMissionLightingFilter";
   %Environment_Item[16] = "sgDecalProjector";

These two files are in the creator directory.

Thats all I did and it worked for me. Should of been simple enough for you guys to figure out.
#12
07/25/2007 (11:21 am)
Woops, had fxLensFlare set as [12], and another thing set as [12]
#13
07/25/2007 (11:51 am)
Upon adding to mission (attempting to add)...
editor/newObject.cs (0): Register object failed for object (null) of class fxLensFlare.
#14
07/25/2007 (12:31 pm)
@ Chris

Thats funny, because i did that same thing first time with the [12].


humm, aww crap, Sorry I tend to do things by hand and missed this. But here is the fix, and I just tested to make sure. .LOL

open ObjectBuilderGui.gui again

change

function ObjectBuilderGui::buildfxLensFlare(%this)
{
	%this.className = "fxLensFlare";
	%this.process();
}

to

function ObjectBuilderGui::buildfxLensFlare(%this)
{
	%this.className = "fxLensFlare";
	%this.addField("dataBlock", "TypeDataBlock", "fxLensFlare Data", "fxLensFlareData");
	%this.process();
}
#15
07/25/2007 (1:14 pm)
Ah, dur, thought it was that. Works great now. :)
#16
10/26/2007 (6:04 pm)
Excellent resource - many thanks!

However, I'm having 2 small problems with it that I can't seem to work out. Firstly, The lens reflections are being rendered behind the terrain but in front of everything else. A picture will probably help explain better, but it's not that clear - hopefully you can see what I mean:

www.eikon-games.com/garagegames/flare.jpg
The other thing is that the reflections are being rendered long after the light source has gone off screen. In the example above (in the full screenshot) the sun has gone off the top of the screen by some margin but the reflections are still being rendered.

Any thoughts?

Thanks again...
#18
10/31/2007 (6:46 am)
@Eikon Games

How have you setup your flares?
#19
11/28/2008 (12:56 am)
if anyone is interested, with 5 minor changes, this works with 1.7.1