
ParticleGenerator Class
Posted Saturday, 20 October, 2012 - 17:05 by devintelo inHello , today I am releasing my particle generator class , which I used in my last experiment . It took me long time (Imma noob ^^) so please at least credit me !
Code :
Public Structure Particles Public fade As Double Public r, g, b, a As Double Public x, y, z As Double Public xi, yi, zi As Double Public xg, yg, zg As Double End Structure Public Class ParticleGenerator 'By Devintelo Public Shared AssignedTexture As Integer = 0 Public Shared MaxParticles As Integer = 500 Public Shared UsePoint As Boolean = True Public Shared Life As Single = 1 Public Shared Source As Vector3 = New Vector3(0, 0, 0) Public Shared Direction As Vector3 = New Vector3(0, 100, 0) Public Shared Gravity As Vector3 = New Vector3(0, -1, 0) Public Shared RandomPos As Single = 1 Public Shared RandomDir As Single = 10 Public Shared VecUp As New Vector3(0, 1, 0) Public Shared VecDown As New Vector3(0, -1, 0) Public Shared Particle(MaxParticles) As Particles Public Sub New(ByVal Texture As Integer, ByVal Src As Vector3, ByVal _Direction As Vector3, _ ByVal Grvt As Vector3, Optional ByVal RndDir As Integer = 1, _ Optional ByVal _Life As Single = 1, Optional ByVal Max As Integer = 500, Optional ByVal _UsePoint As Boolean = True) AssignedTexture = Texture Source = Src Direction = _Direction Gravity = Grvt RandomDir = RndDir Life = _Life MaxParticles = Max UsePoint = _UsePoint ReDim Particle(Max) InitParticles() End Sub Public Sub InitParticles() For i As Integer = 0 To MaxParticles With Particle(i) .fade = CDbl(Random(50, 100)) / 3000 .r = Random(0, 10) / 10 .g = Random(0, 10) / 10 .b = Random(0, 10) / 10 .a = Life .x = Source.X .y = Source.Y .z = Source.Z .xi = Direction.X + (Random(-RandomDir * 100, RandomDir * 100) / 100) 'Choisis ton facteur de hazard .yi = Direction.Y + (Random(-RandomDir * 100, RandomDir * 100) / 100) .zi = Direction.Z + (Random(-RandomDir * 100, RandomDir * 100) / 100)' *100 and / 100 is because it returns as integer and we need more probability ! .xg = Gravity.X .yg = Gravity.Y .zg = Gravity.Z End With Next End Sub Public Sub ReviveParticle(ByVal i As Integer) With Particle(i) .fade = CDbl(Random(50, 100)) / 3000 .r = Random(0, 10) / 10 .g = Random(0, 10) / 10 .b = Random(0, 10) / 10 .a = Life .x = Source.X .y = Source.Y .z = Source.Z .xi = Direction.X + (Random(-RandomDir * 100, RandomDir * 100) / 100)'Pareil .yi = Direction.Y + (Random(-RandomDir * 100, RandomDir * 100) / 100) .zi = Direction.Z + (Random(-RandomDir * 100, RandomDir * 100) / 100)'The same .xg = Gravity.X .yg = Gravity.Y .zg = Gravity.Z End With End Sub Public Sub DrawParticles() GL.Disable(EnableCap.Lighting) GL.BindTexture(TextureTarget.Texture2D, Form1.textures(8)) GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha) If UsePoint = True Then GL.TexEnv(TextureEnvTarget.PointSprite, TextureEnvParameter.CoordReplace, 1) GL.PointParameter(PointParameterName.PointDistanceAttenuation, New Single() {0.0, 1 / 5.0, 0.0}) GL.PointParameter(PointParameterName.PointSizeMax, 50) GL.PointParameter(PointParameterName.PointSizeMin, 1) GL.Enable(EnableCap.PointSprite) End If For i As Integer = 0 To MaxParticles Dim x As Single = Particle(i).x Dim y As Double = Particle(i).y Dim z As Double = Particle(i).z GL.PointSize(35) If UsePoint = True Then GL.Color4(1, 1, 1, Particle(i).a) GL.Begin(BeginMode.Points) GL.Vertex3(x, y, z) GL.End() Else GL.Color4(1, 1, 1, Particle(i).a)'Useless , because quads can't rotate (too hard for me) GL.Begin(BeginMode.TriangleStrip) GL.TexCoord2(1, 1) : GL.Vertex3(x + 0.1, y + 0.1, z) GL.TexCoord2(1, 0) : GL.Vertex3(x + 0.1, y - 0.1, z) GL.TexCoord2(0, 1) : GL.Vertex3(x - 0.1, y + 0.1, z) GL.TexCoord2(0, 0) : GL.Vertex3(x - 0.1, y - 0.1, z) GL.End() End If 'Deplacement Particle(i).x += Particle(i).xi / 50 Particle(i).y += Particle(i).yi / 50 Particle(i).z += Particle(i).zi / 50 'Gravité Particle(i).xi -= Particle(i).xg / 10 Particle(i).yi -= Particle(i).yg / 10 Particle(i).zi -= Particle(i).zg / 10 'Vie Particle(i).a -= Particle(i).fade 'Si elle est morte , on la régénère If Particle(i).a < 0 Then ReviveParticle(i) End If Next GL.Enable(EnableCap.Lighting) GL.Color4(1, 1, 1, 0.999) End Sub Function Random(ByVal min As Integer, ByVal max As Integer) As Integer'The random function used at the reviveparticle sub Dim generator As New Random Return generator.Next(min, max) End Function End Class
I know that there are few useless things but I don't really have the time to fix them ... if you do , pliz post here your work !
And as you can see , I have a little problem with the last 'GL.Color4' , when I put GL.Color4(1,1,1,1) , the whole scene is rendered at max translucent ... Any help would be appreciated !

