Recent Resources
-
GLSL 4.0: Discarding Fragments to Create a Perf...
-
GLSL 4.0: Using Subroutines to Select Shader Fu...
-
Building a Complete Board-based Puzzle Game wit...
-
JIRA: Programming Workflows
-
.NET Generics 4.0: Container Patterns and Best...
-
Raw Meat: Game Design Tips from Team Meat's...
-
Sedge: An Automated Error Reporting Tool
Lightning
By Ian McDougall | Published Oct 24 2001 05:17 PM in Graphics Programming and Theory
Download attached article resource
1.0 Abstract
One night when there was a lightning storm (2 or 3 nights ago) I decided I would write an article on lightning just for fun. This is the first time I have ever tried to simulate lightning before. If there are any spelling mistakes or similar please E-Mail then to me at javaman@telusplanet.net.
1.1 About this Article
All the demo code is in Visual Basic with DirectX8. There is a demo with code and exe that you can via the attached article resource.
2.0 Getting down to work
First off, you just set up a normal Direct3D scene. I added a bit of fog to make it look like it's raining, but you don’t have to do this if you don’t want to.
Enough of that on with the fun. I use 2 classes in this method: the first you only need on ref to its name is "clsLighteningBoltMain". It's more or less just a container class for the second class called "clsLighteningBolt".
2.1 clsLighteningBoltMain
Class variables
Functions
2.2 clsLighteningBolt
Variables
Functions
2.3 How the code works
2.3.1 clsLighteningMain
Get_YFromXZ
This function returns the height of the ground/strike plane at position (x, z).
This sub adds a bolt to the start of the bolt list with the specified position and color.
This sub draws all of the bolts in the bolt list and removes bolts that have hit the ground.
InitBolt
This sub just sets the first line segment up.
This sub draws and adds one more vertex to the line strip and returns true if its not done yet and false if it is.
3.0 Theory
3.1 How it works
The way this works is really simple when you think about it. There is a list of all the bolts that this container needs to think about. You add and remove bolts from there. Here's a picture to help out with this one:
Adding a new bolt
Step One:

Create a new bolt class and set its variables.
Step 2:

Insert the new bolt into the list right after the head.
Removing a bolt

Steps:
4.0 Conclusion
Well that’s all there is to it. Not hard, is it?
WepPage: http://www.telusplan...public/javaman/
E-Mail: javaman@telusplanet.net
Download attached article resource
1.0 Abstract
One night when there was a lightning storm (2 or 3 nights ago) I decided I would write an article on lightning just for fun. This is the first time I have ever tried to simulate lightning before. If there are any spelling mistakes or similar please E-Mail then to me at javaman@telusplanet.net.
1.1 About this Article
All the demo code is in Visual Basic with DirectX8. There is a demo with code and exe that you can via the attached article resource.
2.0 Getting down to work
First off, you just set up a normal Direct3D scene. I added a bit of fog to make it look like it's raining, but you don’t have to do this if you don’t want to.
Enough of that on with the fun. I use 2 classes in this method: the first you only need on ref to its name is "clsLighteningBoltMain". It's more or less just a container class for the second class called "clsLighteningBolt".
2.1 clsLighteningBoltMain
Class variables
Private BoltArray as clsLighteningBoltThis is the head of are singly linked list
Functions
Public Function Get_YfromXZ(ByVal X as single, ByVal Z as single) as SingleThis function returns the height of the ground at X, Z. (Note: This function only returns 0 because our ground is flat)
Public Sub AddBolt(ByVal X as single, ByVal Y as single, _This sub adds a bolt to the list at the starting pos of X, Y, Z with the color lColor (Note: for some unknown reason color is not working; if any one finds out why please e-mail me)
ByVal Z as single, ByVal lColor as single)
Public Sub DrawBolts()This sub draws the bolt list and removes any bolts that say they can be removed.
2.2 clsLighteningBolt
Variables
Public iNext as clsLighteningBoltPointer to the next bolt in the list
Public Parent as clsLighteningBoltMainPointer to the container class (so we can call Get_YFromXZ)
Functions
Public Sub Init_Bolt(ByVal X as single, ByVal Y as single, _Sets the bolt startup values
ByVal Z as Single, iColor as Long)
Public Function Draw_Bolt() as BooleanDraws the bolt and returns false if it's done (e.g. hit the ground)
2.3 How the code works
2.3.1 clsLighteningMain
Get_YFromXZ
This function returns the height of the ground/strike plane at position (x, z).
Public Function Get_YFromXZ(ByVal x As Single, _AddBolt
ByVal z As Single) As Single
Get_YFromXZ = 0
End Function
This sub adds a bolt to the start of the bolt list with the specified position and color.
Public Sub AddBolt(ByVal x As Single, ByVal y As Single, _DrawBolts
ByVal z As Single, lColor As Long)
Dim NewBolt As New clsLighteningBolt
NewBolt.Init_Bolt x, y, z, lColor
Set NewBolt.iNext = BoltArray
Set BoltArray = NewBolt
Set NewBolt.Parent = Me
End Sub
This sub draws all of the bolts in the bolt list and removes bolts that have hit the ground.
Public Sub DrawBolts()2.3.2 clsLighteningBolt
Dim Curr As clsLighteningBolt
Dim Last As clsLighteningBolt
Device.SetVertexShader FVF_LineVertex
Set Curr = BoltArray
Device.SetTexture 0, Nothing
Do
If Curr Is Nothing Then Exit Do
If Curr.Draw_Bolt() = False Then
If Not Last Is Nothing Then
Set Last.iNext = Curr.iNext
Set Curr = Curr.iNext
Else
Set BoltArray = Curr.iNext
Set Curr = BoltArray
End If
Else
Set Last = Curr
Set Curr = Curr.iNext
End If
Loop
End Sub
InitBolt
This sub just sets the first line segment up.
Public Sub Init_Bolt(ByVal x As Single, ByVal y As Single, ByVal z As Single, ByVal iColor As Long)DrawBolt
ReDim Lines(1)
NumberLines = 1
With Lines(0)
.x = x
.y = y
.z = z
.Color = iColor
End With
With Lines(1)
.x = x
.y = y - 0.06
.z = z
.Color = iColor
End With
End Sub
This sub draws and adds one more vertex to the line strip and returns true if its not done yet and false if it is.
Public Function Draw_Bolt() As Boolean
NumberLines = NumberLines + 1
ReDim Preserve Lines(NumberLines)
With Lines(NumberLines)
.x = Lines(NumberLines - 1).x + (1 - Rnd * 2) * 0.3
.y = Lines(NumberLines - 1).y - (Rnd + 0.55)
.z = Lines(NumberLines - 1).z + (1 - Rnd * 2) * 0.3
.Color = Lines(0).Color
End With
Device.DrawPrimitiveUP
D3DPT_LINESTRIP,NumberLines, Lines(0),_
Length_LineVertex
Draw_Bolt = True
If Lines(NumberLines).y <= Parent.Get_YFromXZ(Lines(1).x, Lines(1).z) Then
If Rnd > 0.95 Then
Draw_Bolt = False
End If
End If
End Function
3.0 Theory
3.1 How it works
The way this works is really simple when you think about it. There is a list of all the bolts that this container needs to think about. You add and remove bolts from there. Here's a picture to help out with this one:
Adding a new bolt
Step One:
Create a new bolt class and set its variables.
Step 2:
Insert the new bolt into the list right after the head.
Removing a bolt
Steps:
- Set 1.iNext = 2.iNext
- Set 2.iNext = Nothing
- Set 2 = Nothing
- Resize the Vertex array so you can fit one more vertex in it
- Set the X to X(index-1) + 1-(rnd * 2) * 0.3 and same for the z coordint
- Set y to Y(index-1) - (rnd + .55)
Device.DrawPrimitiveUP D3DPT_LINESTRIP, Ubound(Lines()), _
Lines(0),16
4.0 Conclusion
Well that’s all there is to it. Not hard, is it?
WepPage: http://www.telusplan...public/javaman/
E-Mail: javaman@telusplanet.net
Download attached article resource


















