Gradients?

BlitzMax Forums/BlitzMax Beginners Area/Gradients?

kronholm(Posted 2007) [#1]


I was wondering if something like the above is possible via code. Currently I have a png of the black to transparent circle, which is hooked up to the players x,y. I draw black rects around it.

I would like to not have to resort to a png for the gradient black to transparent circle, is that possible? Maybe someone knows of a function someone wrote?


FlameDuck(Posted 2007) [#2]
Sure. However it's unlikely to be faster than what you're doing now.


kronholm(Posted 2007) [#3]
Sure? Can you tell me how then?


RiK(Posted 2007) [#4]
If you're starting with a black screen you could optimise your tilemap drawing to only draw the tiles under the circle. Then you don't need to draw all the black squares, just your mask image.


kronholm(Posted 2007) [#5]
Good idea :) I'll have to work that in.

But my question on gradients still remains, the reason I need to be able to do it via code is I'll have multiple lightsources. For instance two players on the screen when it's dark will mean two of those lightcones/outline things. Also explosions should light up.. Not really possible with an image I think.


SculptureOfSoul(Posted 2007) [#6]
I don't remember whose code this was but here's an example using an OpenGL lightsource. Just replace the loadimage call with some tile (ideally 32x32) to use as the background

Strict
SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,0

Local img:TImage = LoadImage( your-image-here )

SetBlend(ALPHABLEND)

glEnable GL_LIGHTING		' Switch on lighting. All lights are off now.

' Let's set up a light
glEnable GL_LIGHT0		' Switch on the light
Local l0_diffuse#[] = [0.6,0.3,0.0,1.0]		' Light colour - R,G,B,i (keep i at 1.0)
glLightfv GL_LIGHT0, GL_DIFFUSE, l0_diffuse

' These control how the light intensity drops off with distance.
' These settings make it work like a spotlight.
glLightf GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0
glLightf GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.00000
glLightf GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.00001 


' Let's set up another light
'glEnable GL_LIGHT1
Local l1_diffuse#[] = [0.5, 0.0,0.5,1.0]		' R,G,B,i (keep i at 1.0)
glLightfv GL_LIGHT1, GL_DIFFUSE, l1_diffuse

glLightf GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.0
glLightf GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.001
glLightf GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0001 

' Lighting looks best if you give it a position out of the 2D plane. x=50.0 looks about right.
Local l0_position#[] = [0.0,0.0,50.0,1.0]	' X,Y,Z,w (keep w at 1.0)
Local l1_position#[] = [0.0,0.0,50.0,1.0]

Local a# = 0.0	' Angle of the moving light

While Not KeyHit(KEY_ESCAPE)
	Cls

	l0_position[0] = MouseX()
	l0_position[1] = MouseY()
	
	glLightfv GL_LIGHT0, GL_POSITION, l0_position	' actually move the light

'	l1_position[0] = 0.5 * (GraphicsWidth() + GraphicsHeight()*Cos(a))
'	l1_position[1] = 0.5 * (GraphicsHeight() + GraphicsHeight()*Sin(a))
	glLightfv GL_LIGHT1, GL_POSITION, l1_position
	a :+ 1.0
	glLightf GL_LIGHT0, GL_QUADRATIC_ATTENUATION, Rnd(0.00005, 0.00007)

	For Local x=0 To GraphicsWidth() Step 32
		For Local y=0 To GraphicsHeight() Step 32
			DrawImage img,x,y
		Next
	Next

	Flip
	
Wend