Lightning Effects in DirectDraw

Started by
11 comments, last by Krush 23 years, 8 months ago
I want to write a 2D lightning effect for DirectDraw. Can anyone point me to an article or information that can help me achive this? Thanks! K
Advertisement
You can create lighting by alpha blending lightmaps.

Actually additive blending to get rid of the black area, or do both types of blending to create a light where you can change the intensity of the light. Whatever, it's all called alpha blending .

A lightmap is a bitmap that determines how the light will look, white being the brightest area, and black being invisible, and the area between them having different shades of grey. (To see an example of a lightmap search the direct x sdk directory, i think there's one in there).

+AA_970+

Edited by - +AA_970+ on August 10, 2000 4:23:17 PM
Thanks for your reply. The actual affect I was looking for is "light-ning". I''ve seen some really cool lightning effects and I''m wondering how they are done.

Thanks!
k
What exactly do you mean with "light-ning"? You could simulate the light from a torch or anything such as that by using an (additive) alpha blended lightmap. Of course there would be no shadows and it would be a bit tricky dealing with the light shining on objects and such.

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
By light-ning, Krush means that flashy burst of electricity that causes the big boom during thunderstorms
Well, how about an additivly alpha blended image of an lightning flash?

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
There are a few different lightning effects.

  • Bright flash
  • Forked lightning streak
  • Flashes in the clouds

The bright flash is achieved by drawing the entire screen as white for one frame. This is easily done using a colour fill.

Forked lightning streaks can be simulated using a sprite or multiple sprites in the shape of lightning streaks. Combine this with either of the other two effects (flashes) to enhance the overall effect.

Flashes in the clouds can be done by drawing the clouds a lot brighter than normal. This can be done by alpha blending (as mentioned before) or by storing two bitmaps for clouds. One dark and one bright. Then draw the appropriate cloud when rendering the frame.
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
Ooh, ooh!! I created a cool lightN ing effect!
I actually first made it in VB. But here's the basic idea. You start with your own line program. Like

Lightning X1, Y1, X2, Y2

Then what I did is find the slope of the line, BUT YOU WANT TO KEEP THIS IN FRACTION FORM. So have two different variable slike "Rise" and "Run" or "DeltaX" and "DeltaY".

In highschool you learn that slope is Rise over Run, which people seem to understand better than DeltaY over DeltaX.

So
DeltaX = X2-X1
where X2 is the greater of the two and
DeltaY = Y2-Y1
again where Y2 is the greater

Now you want to put this in lowest terms, like fraction in lowest terms. So here's how I did it wheather there's an easier way or not.

(Pseudo code)
 If DeltaX > DeltaY    For Count = 0 to DeltaY        If DeltaX mod Count == 0 ;DeltaX div by Count has no remainder            ;Then we've found the lowest common denominator            divide DeltaX by Count            divide DeltaY by Count            Exit Lowest Term Code ;We're done in other words        Endif    Next CountElse    For Count = 0 to DeltaX        If DeltaY mod Count == 0 ;DeltaY div by Count has no remainder            ;Then we've found the lowest common denominator            divide DeltaX by Count            divide DeltaY by Count            Exit Lowest Term Code ;We're done in other words        Endif    Next CountEndIf   


Does that make sense so far? So now we got the slope in fraction form in lowest terms. So now we are going to start the main loop, we are going to start with X1, Y1. So move these into our LastX and LastY variables (Where we'll start drawing the first line segment from) Then take that value and add DeltaX to the X1 position and DeltaY to the Y1 position for the other end of the line. Continue looping through this adding the slope each segment until you hit the target (Destination X and Y pos is within the target rect) or you've gone off the screen and missed or you've gone as far as your weapon can go.

So we're drawing a bunch of lines which go from X1, Y1 to X2, Y2 right. Well so far we've got perfect precision straight line. Now we're going to make the lightning look. Each time you add DeltaX and DeltaY every segment in the lightning strike, you can add a random number between like -5 and 5 to both the x and y position. Changing how large of a random number you add will change the accuracy and spuratic (sp?) look. Start with 5 and -5 and play.

So now each segments end will move a little and create a nice squigle from point 1 to point2, and will some times miss, a bit above or below, because all the random numbers made it go a little higher or lower!

Now if you don't want this randomness, you can create a lookup table with "strategically" chosen random values which create the squigle but in general goes straight...


Hope this helps!
Feel free to ask questions or if you want me to post my old vb code I will (Be forwarned I haven't programmed in vb since I started assembler and that vb code probably isn't very optimized!)

See ya,
Ben

Edit: I repeated myself. Usually I don't like it when people edit cause then you wonder what they took out. But I don't like repeating either!

__________________________
Mencken's Law:
"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."
- Popular Mechanics, forecasting the relentless march of science in 1949

Edited by - cyberben on August 10, 2000 7:26:00 PM
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Thanks for the replies guys. I guess I wasn''t too specific was I? I was looking for the forked lightning effect. Thanks for your post Cyberben. that''s exactly what I was looking for. I figured I could make a cool looking weapon or something with that effect.

I''ll let you know if I have any questions.

Thanks!
K
Ok. I have a question for Cyberben. what do you mean by Exit Lowest Term Code?

Thanks!
K

This topic is closed to new replies.

Advertisement