Game Development Community

dev|Pro Game Development Curriculum

SunFlare (LensFlare) Tutorial

by Tim Newell · 02/15/2002 (9:58 am) · 45 comments

Download Code File

[Introduction]
This tutorial will guide you through modifying torque to display lens flares through flags set in the mission files. This tutorial will show you how to get a Sun Flare working with screen whiteout if camera is pointing towards the sun. You could also use the Lens Flare to make things like street lights and other uses of lens flares like bright glares off of metal from the sun. This tutorial contains all C++ changes and the only script changes would be modifying your current mission files to display flares.

[Modifying the Engine]

Adding supplied Code
Replace engine/dgl/lensFlare.cc and engine/dgl/lensFlare.h with the copies in the attached zip, and put the SunFlare.cc and SunFlare.h from the zip into engine/game/fx/ dir and add them to your project so they will get compiled.

Modify existing files
First open engine/game/game.h and add the following code to it.

#ifndef _SUNFLARE_H_
#include "game/fx/SunFlare.h"
#endif

static SunFlare *gSunFlare;

Next open engine/game/game.cc

At the end of GameInit() add:
gSunFlare = new SunFlare;

Close to the end of GameRenderWorld() after the line collisionTest.render(); add:

GameConnection* connection = GameConnection::getServerConnection();

   if (connection)  {
		//check to see if sunflare is turned on in mission file
		char* showflare = (char*)Con::evaluate("return Sun.showflare;",false);
		
		if (dStrcmp(showflare,"1") == 0)
			gSunFlare->Render();   
   }

Next open engine/game/main.cc

At the top of shutdownGame() after Con::executef(1, "onExit"); Add:
delete gSunFlare;
   gSunFlare = NULL;

Next open engine/terrain/sun.h

After LightInfo mLight; add:

Point3F        mPosition;
	bool           mShowFlare;

[**updated**]
Add this code as a public function in the Sun class:

Point3F getPosition() { return mPosition; }

Next open engine/terrain/sun.cc

In the Constructor add:

mPosition.set(0.0f,0.0f,0.0f);
   mShowFlare = false;


Then in Sun::unpackUpdate(...) Add with the other reads:
mathRead(*stream, &mPosition);
    stream->read(&mShowFlare);

Then in Sun::packUpdate(...) Add with the other writes:
mathWrite(*stream, mPosition);
    stream->write(&mShowFlare);

Then in Sun::initPersistFields() Add:
addField("position",    TypePoint3F,   Offset(mPosition, Sun));
   addField("showflare",    TypeBool,   Offset(mShowFlare, Sun));

[***updated***]
Then open engine/game/gameConnection.h

After F32 getWhiteOut() add:
void setWhiteOut(F32 amount) { mWhiteOut = amount; }
[Modifying the mission file]

Pick a mission file and open it. Go to the Sun entity (i.e. new Sun())

Change new Sun() { to new Sun(Sun) {

change position ="0 0 0"; to where you want the sun to be located..something like position = "-88.0618 -20.8792 510.364";

and then add a new variable showflare = "true"; to the sun entity.

[SunFlare Textures]
Now you need to have some flare images. I took the ones from this tutorial and added alpha channels to them and they are included in this zip. So they belong to the author of that tutorial. BTW, I used that tutorial's technique in writing the basis of the new flare code so you could also read it if you are looking into modifying this code. If someone would like to donate some new better looking images to this tutorial then feel free to send them to me.

The 4 images should be in the folder common/lensflare/

[Conclusion]
Now after you compile and start up the mission you edited you should have a sun with lensflare. There are a few quirks in the code (like all of my code :) ) you can see the sunflare through the main player...I had to do this or the flare wouldnt work in first person view...the ray is cast from the position of the camera so it really needs modifying better. If I get any optimizations I'll post them in the comments here.

[Beyond SunFlare]
The new lensflare code is heavily commented so you should be able to figure out how it works. For something like a corona on a street lamp you would need to pass it the location of the light and then only add one flare with a 0.0f offset, the light texture. Probably also want to integrate some kind of list if you did this too for processing and rendering. For something like a glare of metal you would need to know the position of the point at which the glare should be on the metal and add a flare with offset 0.0f. Youd probably want some kind of processing list for this too.
Page «Previous 1 2 3 Last »
#1
02/15/2002 (2:47 am)
If you have downloaded the zip before I posted this comment then you need to redownload it cause I just uploaded some fixes to the code.

-Tim aka Spock
#2
02/15/2002 (12:32 pm)
A very cool effect indeed! And also a nice step-by-step tutorial - works great and is easy to follow!! Keep up the good work! ;-)
#3
02/15/2002 (4:58 pm)
Setting the connection whiteout value... this will override any whiteout values sent from the server. The server sends whitout for flash grenades, etc. You'll probably want to add your whiteout to whatever the server's transmited.
#4
02/15/2002 (5:10 pm)
Edit: [Problem ommited since it is fixed.]

the code has several other small flaws...

Edit: [Problem ommited since it is fixed.]

Another problem is that I had to disable collision with models basically so it wouldn't collide with the player...the reason it was colliding with the player is because I was casting the ray from the feet...I tried doing a eye cast to the sun but had no successes...i'd appreciate if someone could help with this problem as well, or at least point me in the correct direction. This problem causes trees not to block the sun.

-Tim aka Spock
#5
02/16/2002 (4:24 am)
Yeah, just use 2 variables now.

mSunWhiteOut and mWhiteOut.

Then, instead of just applying a frame of intensity of mWhiteOut, make it apply it for mSunWhiteOut + mWhiteOut in order to ensure that other whiteout values aren't ignored.

I actually was messing with this (game.cc, where damageflash and whiteout is) code, and it's where I got most of my ideas for the rendering of the nightvision I'm working on.
#6
02/16/2002 (1:04 pm)
TimN, yeah, the check for sun in view is not working correctly. You need to only draw the flares whilst the sun is directly in view of the player/camera. There should be some frustum check we can use for this.

Also, Ive noticed the whiteout doesnt absolutely work right, it white's out a little just before pointing at the sun, but when pointing any close to the sun it becomes clear again. So the sun/player angle calc seems to have a flaw in it.

But its fun to have tho!

Phil.
#7
02/16/2002 (2:59 pm)
I'm not really a programmer so I probably shouldn't be touching the engine lol... but i did it anyway and now i have this problem:

Linking...
SunFlare.obj : error LNK2001: unresolved external symbol "public: void __thiscall LensFlare::render(class Point3F const &,class Point3F const &)" (?render@LensFlare@@QAEXABVPoint3F@@0@Z)
../example/torqueDemo.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I'm using the head release (i think) wich compiled fine before if followed this tutorial.

any idea what i did wrong, becuase to me this is chinese :)

another question I have is this: What if it depends on what player class you are to see the lens flare or not. For example we have player running around with protection shields for their eyes and others not. I only want those players to see a sunflare. Where should I start/what should I do?
#8
02/16/2002 (4:11 pm)
Matt: I think what I'm going to do is store the last whiteout value I added then subtract it each time (check to make sure I never go below 0.0f) I think that should work well.

Phil: The equation I wrote was quick and dirty but it works the majority of the time so I stuck with it...there are some angles you can go in that will get the result you stated...really I forgot about this and never went back and fixed it since it wasn't a really noticable thing at runtime.

Ward: did you overwrite the lensflare code with the code in the zip? looks like its trying to reference the new render function but finding an older one.
Also on your other question, all you should have to do is add code to SunFlare class to check and see if it is the correct class to render and then call the lensflare render only for those types. For example in the SunFlare::Render() you could do:

(this syntax may be a little off since Im doing this from memory)
Player *p = static_cast<Player*>(connection.getControllingObject());

if (p->classType == 3 || p->classType == 6) {
    gLenFlare.render(position1,position2);
}


-Tim aka Spock
#9
02/17/2002 (2:54 am)
I got it working now....
thanks for the tip about the classes, hope to get it working
#10
02/17/2002 (7:40 am)
Quote:
Another problem is that I had to disable collision with models basically so it wouldn't collide with the player...the reason it was colliding with the player is because I was casting the ray from the feet...I tried doing a eye cast to the sun but had no successes...i'd appreciate if someone could help with this problem as well, or at least point me in the correct direction. This problem causes trees not to block the sun.

SceneObject has a function that excludes a passed in object id (cContainerRayCast). We used it to disable selecting player if in 1st person. Sounds like it does something similar to what you want. You could cast it and see if you hit an object type that you want to block the sun.

Dave Myers
21-6 Productions
#11
02/17/2002 (1:52 pm)
Edit: Found out why but bug fix still pending.

-Tim aka Spock
#12
02/18/2002 (1:37 am)
Great tutorial but Ward im getting the same errors as you.

How did u solve it ?

----------------------

game.obj : error LNK2001: unresolved external symbol "public: __thiscall SunFlare::SunFlare(void)" (??0SunFlare@@QAE@XZ)
game.obj : error LNK2001: unresolved external symbol "public: void __thiscall SunFlare::Render(void)" (?Render@SunFlare@@QAEXXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall SunFlare::~SunFlare(void)" (??1SunFlare@@QAE@XZ)
../example/torqueDemo_DEBUG.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
#13
02/19/2002 (5:08 am)
I didn't change much.... I nticed I forgot to add a file to the workspace... but that wasn't causing the error. I just did a "rebuild all" and it worked fine :)
#14
02/19/2002 (5:43 am)
I'm having some problems with the position tough... can someone explain me the 3 numbers?
Please illustrate with an example. For example my sun is somewhere in the middle of skybox picture 2... what should the position be? and in 3/4?
#15
02/19/2002 (6:16 am)
Well I cant figure it out.

Ive Readded every file

double checked every code line.

Rebuilt the damn thing about 10 times and im still getting the same 4 link errors.

was working fine before starting on this tutorial.

Any ideas ?????
#16
02/19/2002 (1:17 pm)
i tried around with the postition and my findings are that this effects almost cannot be used as a sunflare. This becuase its a coordinate in a 3d space.... while the skybox is soemthing that satys the same no matter where you are standing.... moving it to far away or too far up screws up the sunflare... so it turns out to be rather useless.... unless I'm doing something wrong here
#17
02/19/2002 (1:43 pm)
Samea Wood: Did you include both SunFlare.h and SunFlare.cc in your project? and did you put them in the correct folder?

Ward De Langhe: what i did was spawn some object in the editor...move it to where you want your sun...write down its coordinates and then delete the object.
#18
02/20/2002 (12:50 am)
/me slaps himself

DOH got it in one tim. Forgot to add the sunflare files :P

Oh well ya live and learn
#19
02/20/2002 (2:03 am)
I edited two things in this tutorial. I changed about 9 lines of code that slowly accessed the script and parsed the position into 2 lines of quick C++ code :) I also made the whiteout friendly with the rest of the system so it doesn't block out grenades or anything like that.

To update your code download the zip and overwrite with all 4 files and add the new function to the Sun class...you should see it above under [***update***] in the sun.h section.

-Tim aka Spock
#20
02/20/2002 (3:45 am)
I was using and object... i moved it to the position the sun was.. but when I move on the ground, so does that model in relation to the real sunposition. Now if I move it high enough and far enough, it might work, but then the flare stops working properly.
So I was wondering... you have a flare working that is on the sun position no matter where you are on the map? if so... can you give me its position?
Page «Previous 1 2 3 Last »