Sprite Blending

Just starting out? Need help? Post your questions and find answers here.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Sprite Blending

Post by Nubcake »

I was messing around with the blending mode creating some fire particles of different colors with textures I found online. I managed to create these nice looking flames:
Image
With red/green/blue textures like this one:
Image

However I can't remember what blending mode I used so I tried most of the combinations but I cannot seem to replicate the effect. I think it was additive blending so I tried doing that with the following code:

PB 5.10

Code: Select all


Enumeration
  #WINDOW         = 0
  #WINDOW_WIDTH   = 640
  #WINDOW_HEIGHT  = 480
  #AUTO_STRETCH   = 1
EndEnumeration

Enumeration
  #SCREEN_COLOR = #Black;$D0975E
  #FRAME_RATE   = 60
EndEnumeration

Enumeration
  #D3DBLEND_ZERO            = 1
  #D3DBLEND_ONE             = 2
  #D3DBLEND_SRCCOLOR        = 3
  #D3DBLEND_INVSRCCOLOR     = 4
  #D3DBLEND_SRCALPHA        = 5
  #D3DBLEND_INVSRCALPHA     = 6
  #D3DBLEND_DESTALPHA       = 7
  #D3DBLEND_INVDESTALPHA    = 8
  #D3DBLEND_DESTCOLOR       = 9
  #D3DBLEND_INVDESTCOLOR    = 10
  #D3DBLEND_SRCALPHASAT     = 11
  #D3DBLEND_BOTHSRCALPHA    = 12
  #D3DBLEND_BOTHINVSRCALPHA = 13
EndEnumeration


;Allow loading of PNG files
UsePNGImageDecoder()

;Initialise Sprite system
InitSprite()

;Open window and windowed screen 
OpenWindow(#WINDOW,0,0,#WINDOW_WIDTH,#WINDOW_HEIGHT,"Window",#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(#WINDOW),0,0,#WINDOW_WIDTH / 2,#WINDOW_HEIGHT / 2,#AUTO_STRETCH,0,0)


Flare = LoadSprite(#PB_Any,"Sprites/green.png",#PB_Sprite_Texture)
TransparentSpriteColor(Flare,RGB(255,0,255))


Structure Vector
  x.f
  y.f
  l.f
EndStructure

Structure Particle
  ID.i
  Position.Vector
  Life.i
  Velocity.Vector
  Acceleration.Vector
  Scale.f
EndStructure

NewList Particle.Particle()

InitSprite3D()

;Main loop
Repeat
  
  ;Create fire
  
  AddElement(Particle())
  Particle()\ID = CreateSprite3D(#PB_Any,Flare)
  Particle()\Position\x = 140
  Particle()\Position\y = 140
  
  
  Particle()\Velocity\x = Random(4,2)*Cos(ElapsedMilliseconds())
  Particle()\Velocity\y = Random(8,3)
  Particle()\Velocity\l = Sqr((Particle()\Velocity\x*Particle()\Velocity\x) + (Particle()\Velocity\y*Particle()\Velocity\y))
  
  
  Particle()\Velocity\x / Particle()\Velocity\l
  Particle()\Velocity\y / Particle()\Velocity\l
  
  
  Particle()\Scale = 1
  Particle()\Life = Random(200,160)
 
  
  ;Render
  ClearScreen(#SCREEN_COLOR)

  ForEach Particle()
    
    
    Start3D()
    
     Sprite3DQuality(1)
     ZoomSprite3D(Particle()\ID,64 * Particle()\Scale,64* Particle()\Scale)
     
     
     ;Blending
     Sprite3DBlendingMode(#D3DBLEND_SRCALPHA,#D3DBLEND_DESTALPHA)
     
     ;Draw
     DisplaySprite3D(Particle()\ID,Particle()\Position\x,Particle()\Position\y,Particle()\Life)
     
    Stop3D()
    
    Particle()\Position\x  + 2*Particle()\Velocity\x + Particle()\Acceleration\x
    Particle()\Position\y  - 4*Particle()\Velocity\y + Particle()\Acceleration\y
    
    Particle()\Acceleration\y - 0.70710678118654757/4
    

    
    Particle()\Life - 8
    ;Particle()\Scale + 0.5
    
    
    
    If Particle()\Life <= 0 
      If ListSize(Particle())
        If IsSprite3D(Particle()\ID)
          FreeSprite3D(Particle()\ID)
        EndIf
        DeleteElement(Particle())
      EndIf
    EndIf
    
  Next
 
  
  FlipBuffers()
  
  
  ;Events
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Break
  EndSelect
  
  If GetKeyState_(#VK_ESCAPE)&%10000000
    Break
  EndIf
  
  
ForEver

But now most combinations of blending yield the following effect:
Image
Which is exactly red RGB(255,0,0) , green RGB(0,255,0) or blue RGB(0,0,255). I don't understand why the other colours are set to 0 and there's no additive blending.

Why doesn't the blending work properly ?

Edit: Ok i decided to check the RGB values of some pixels in the texture and it turns out that they are exactly shades of red and no green or blue values but I still managed to pull off additive blending using this exact texture just I don't know how.