Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a faster way to do this glow circle effect?
#11
I created this a while back for a project. It uses a look up table of distances to set the intensity of the pixel, giving it a soft edge. With very little modification to the color mixing, you can create different colors of spot lights.

Code: (Select All)
'#######################################################
'#                Spolight Demo                      #
'#######################################################
_TITLE "Spotlight Demo"
CONST spotLightSize = 128

DIM AS DOUBLE distLUT(spotLightSize * 2, spotLightSize * 2)
DIM AS LONG scr, img, colour, iter
DIM AS STRING fl
DIM AS DOUBLE xp, yp, xl, yl
DIM AS DOUBLE dist, percentage

'#######################################################
'#          Setup Screen and load Image                #
'#######################################################
' main draw screen
scr = _NEWIMAGE(1000, 600, 32)
' hidden reference image
backscr = _NEWIMAGE(1000, 600, 32)
' Change this to whatever image you want to open.
fl = "cactus.jpg"
img = _LOADIMAGE(fl, 32)
' Draw it to the hidden screen
_PUTIMAGE (100, 50)-(900, 550), img, backscr,
SCREEN scr
'#######################################################
'#          Lookup Table for distance                  #
'#######################################################
FOR yp = 0 TO spotLightSize * 2
FOR xp = 0 TO spotLightSize * 2
  distLUT(xp, yp) = SQR(((xp - spotLightSize) ^ 2) + ((yp - spotLightSize) ^ 2))
NEXT
NEXT
'#######################################################
'#                  Main Loop                          #
'#######################################################
xl = _WIDTH / 2: yl = _HEIGHT / 2
DO
CLS
' Scan over the area of the spotlight and lighten the pixel
FOR yp = yl - spotLightSize TO yl + spotLightSize
  FOR xp = xl - spotLightSize TO xl + spotLightSize
  dist = distLUT(xp - xl + spotLightSize, yp - yl + spotLightSize)
  IF dist < spotLightSize THEN
    percentage = clamp((dist / spotLightSize), 0, 1)
    _SOURCE backscr
    colour = colorMixer(POINT(xp, yp), 0, 1 - percentage)
    PSET (xp, yp), colour
  END IF
  NEXT
NEXT
' Mouse routine
DO WHILE _MOUSEINPUT
  xl = _MOUSEX: yl = _MOUSEY
LOOP
_DISPLAY
LOOP

'#######################################################
'#              Helper Functions                      #
'#######################################################
FUNCTION colorChannelMixer%% (colorChannelA AS _UNSIGNED _BYTE, colorChannelB AS _UNSIGNED _BYTE, amountToMix AS DOUBLE)
DIM AS DOUBLE channelA, channelB
channelA = colorChannelA * amountToMix
channelB = colorChannelB * (1 - amountToMix)
colorChannelMixer = INT(channelA + channelB)
END FUNCTION

FUNCTION colorMixer& (rgbA AS LONG, rgbB AS LONG, amountToMix AS DOUBLE)
DIM AS _UNSIGNED _BYTE r, g, b
r = colorChannelMixer(_RED(rgbA), _RED(rgbB), amountToMix)
g = colorChannelMixer(_GREEN(rgbA), _GREEN(rgbB), amountToMix)
b = colorChannelMixer(_BLUE(rgbA), _BLUE(rgbB), amountToMix)
colorMixer = _RGB(r, g, b)
END FUNCTION

FUNCTION clamp# (a AS DOUBLE, hi AS DOUBLE, low AS DOUBLE)
DIM AS DOUBLE NDa
NDa = a
IF hi < low THEN SWAP hi, low
IF NDa < low THEN NDa = low
IF NDa > hi THEN NDa = high
clamp# = NDa
END FUNCTION
[Image: cactus.jpg]
Reply
#12
Well that works very well also, @justsomeguy.  Good coding.  

This forum is great, so many quick replies with great code!

- Dav

Find my programs here in Dav's QB64 Corner
Reply




Users browsing this thread: 4 Guest(s)