Sign in to follow this  
AvengerDr

Dynamic generation of Lens Flare rays

Recommended Posts

AvengerDr    751
Hello there, I was implementing a HLSL lens flare generator, but I'm having some problems with the dynamic generation of the lens flare rays. Here it was suggested to generate these rays using particles, but since in a pixel shader the main function gets called for every pixel, it would become very ankward to write to specific areas of the texture. So the method that would apply on every pixel of the texture is the same method that on that page it is suggested to avoid [smile]. And that is calculating atan2 for every pixel in the texture and using the resulting angle as an index for a look-up array. This is the code I was basing myself on:
private idx3d_Texture createRays(int size, int rays, int rad, int color)
{
	int pos;	float relPos;
	idx3d_Texture texture=new idx3d_Texture(size,size);
	int[] radialMap=new int [1024];
	idx3d_Math.clearBuffer(radialMap, 0);
	for (int i=0;i<rays;i++)
	{
		pos=(int)idx3d_Math.random(rad,1023-rad);
		for (int k=pos-rad; k<=pos+rad; k++)
			{
				relPos=(float)(k-pos+rad)/(float)(rad*2);
				radialMap[k]+=(int)(255*(1+Math.sin((relPos-0.25)*3.14159*2))/2);
			}
		}
		int angle,offset,reldist;
		float xrel,yrel;
	for (int y=size-1;y>=0;y--)
	{
		offset=y*size;
		for (int x=size-1;x>=0;x--)
		{
			xrel=(float)(2*x-size)/(float)size;
			yrel=(float)(2*y-size)/(float)size;
			angle=(int)(1023*Math.atan2(xrel,yrel)/3.14159/2)&1023;
			reldist=Math.max((int)(255-255*idx3d_Math.pythagoras(xrel,yrel)),0);
			texture.pixel[x+offset]=idx3d_Color.scale(color,radialMap[angle]*reldist/255);
		}
	}
	return texture;
}


I managed to port the second for-loop of the function to a HLSL shader, while for the first part I was using a 1024 * 1 texture to simulate the radial map built in the first part. This texture was then filled with a radial gradient. It seems to work: I'm getting textures like Japan's naval flag [attention]. What I would like to know is what I'm really doing by using atan2 and why it is effectively displaying raylike features in my texture [smile]. I suppose that in the first for-loop it is creating a "radial map" in order to filter out some rays and add some randomness. In fact, since my "radial map" is a simple gradient, the line that computes the "angle" value, where it remaps the atan2 value to the [0..1024] range is in reality affecting the number of rays that are rendered. If I multiply that value by 2 I get two rays, while by 16, sixteen rays and so on. If the number becomes too big I get some circular patterns. I guess this depends on my radial map being not suitable, am I correct? So I should generate that radial map via code and then pass it to the shader and it should work. So is this the right way to do it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this