OpenGL Particles

For everything that's not in any way related to PureBasic. General chat etc...
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

OpenGL Particles

Post by DarkDragon »

Hello,

I have programmed a 3D particleeffect in OpenGL:

Code: Select all

XIncludeFile "C:\Programme\PureBASIC\Examples\Sources - Advanced\OpenGL Cube\OpenGL.pbi" ;change this to your OGL include

Global AngX.f, Zoom.f

#WindowWidth = 640
#WindowHeight = 480
#WindowFlags = #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_MaximizeGadget | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
Version = 1

Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  If Message = #WM_SIZE
    UseWindow(0)
    glViewport_(0, 0, WindowWidth(), WindowHeight())
    Result = 1
  EndIf
  ProcedureReturn Result
EndProcedure

If OpenWindow(0, 0, 0, #WindowWidth, #WindowHeight, #WindowFlags, "OpenGL Partikel v. "+Str(Version))

SetWindowCallback(@MyWindowCallback())

hWnd = WindowID(0)
pfd.PIXELFORMATDESCRIPTOR   ;OpenGL starten
hDC = GetDC_(hWnd)
pfd\nSize        = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion     = 1
pfd\dwFlags      = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType   = #PFD_MAIN_PLANE
pfd\iPixelType   = #PFD_TYPE_RGBA
pfd\cColorBits   = 8
pfd\cDepthBits   = 8
pfd\cAccumBits   = 8
pfd\bReserved = 1
pixformat = ChoosePixelFormat_(hDC, pfd)
SetPixelFormat_(hDC, pixformat, pfd)
hrc = wglCreateContext_(hDC)
wglMakeCurrent_(hDC, hrc)

SwapBuffers_(hDC)

gluObj = gluNewQuadric_()

Structure Point3D
  X.f
  Y.f
  Z.f
  SpeedY.f
  SpeedX.f
  SpeedZ.f
EndStructure

NewList Part.Point3D()

glEnable_(#GL_BLEND)
glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE)
glHint_(#GL_POLYGON_SMOOTH_HINT, #GL_FASTEST)

OpenWindow(1, WindowX(), WindowY()-110, WindowWidth(), 80, #PB_Window_TitleBar, "Partikelfarbe", WindowID(0))

CreateGadgetList(WindowID(1))
TrackBarGadget(0, 0, 10, WindowWidth(), 20, 0, 255)
TrackBarGadget(1, 0, 30, WindowWidth(), 20, 0, 255)
TrackBarGadget(2, 0, 50, WindowWidth(), 20, 0, 255)
SetGadgetState(1, 255/2)
SetGadgetState(2, 255)

Red.f = 0
Green.f = 0.5
Blue.f = 1.0

Repeat
  UseWindow(0)
  OGLSetCamera(45.0, 0, 0, Zoom.f-2, AngX, 0, 0)
  AddElement(Part())
  Part()\Y = -0.75
  Part()\SpeedY = (Random(5)/200)
  If Random(1) = 1
    Part()\SpeedX = -(Random(5)/550)
  Else
    Part()\SpeedX = (Random(5)/550)
  EndIf
  If Random(1) = 1
    Part()\SpeedZ = -(Random(5)/550)
  Else
    Part()\SpeedZ = (Random(5)/550)
  EndIf
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT | #GL_ACCUM_BUFFER_BIT | #GL_STENCIL_BUFFER_BIT)
  ForEach Part()
    Part()\Y + Part()\SpeedY
    Part()\X + Part()\SpeedX
    Part()\Z + Part()\SpeedZ
    Part()\SpeedY - 0.000225
    glLoadIdentity_()
    OGLParticle(gluObj, Part()\X, Part()\Y, Part()\Z, 0.025, RGB(Red*255, Green*255, Blue*255))
    If Part()\Y <= -0.9
      DeleteElement(Part())
    EndIf
  Next
  SwapBuffers_(hDC)
  Event = WindowEvent()
  If EventWindowID() = 1 And Event = #PB_Event_Gadget
    Select EventGadgetID()
      Case 0
        Red = (1/255)*GetGadgetState(0)
      Case 1
        Green = (1/255)*GetGadgetState(1)
      Case 2
        Blue = (1/255)*GetGadgetState(2)
    EndSelect
  ElseIf EventWindowID() = 0
    Select Event
      Case #WM_KEYDOWN
        Select EventwParam()
          Case #VK_UP
            Zoom + 0.05
          Case #VK_DOWN
            Zoom - 0.05
          Case #VK_PRIOR
            AngX - 1
          Case #VK_NEXT
            AngX + 1
        EndSelect
    EndSelect
  EndIf
Until Event = #PB_Event_CloseWindow
End
EndIf
You should have the glWrapper-library, the PBOGL library ;) and the GLInclude from the Examplecube.

Feedback please :)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: OpenGL Particles

Post by NoahPhense »

Nice.. I like it..

- np
Post Reply