Gradients?

BlitzMax Forums/BlitzMax Programming/Gradients?

Hi,
Anyone knows why Fredborgs old Gradient sample doesn't work anymore?
Or, Does anyone have a better way of doing gradients in Max2D using OpenGL? ^^

Graphics 640,480,0,0

Repeat

	x:Float = rnd(0,GraphicsWidth())
	y:Float = rnd(0,GraphicsHeight())
	w:Float = rnd(1,GraphicsWidth())
	h:Float = rnd(1,GraphicsHeight())

	r0:Float = rnd(0,255)
	g0:Float = rnd(0,255)
	b0:Float = rnd(0,255)

	r1:Float = rnd(0,255)
	g1:Float = rnd(0,255)
	b1:Float = rnd(0,255)

	direction:Int = Rand(0,1)

	DrawGradient(x,y,w,h,r0,g0,b0,r1,g1,b1,direction)

	Flip

Until KeyHit(KEY_ESCAPE)
End

Function DrawGradient(x:Float,y:Float,w:Float,h:Float,r0:Float=255.0,g0:Float=0.0,b0:Float=0.0,r1:Float=255.0,g1:Float=255.0,b1:Float=0.0,direction:Int=0)

	r0 = r0/255.0
	g0 = g0/255.0
	b0 = b0/255.0
	
	r1 = r1/255.0
	g1 = g1/255.0
	b1 = b1/255.0

	glBegin GL_QUADS
		glColor3f r0,g0,b0
		glVertex2f x,y

		If direction = 0
			glColor3f r0,g0,b0
		Else
			glColor3f r1,g1,b1
		EndIf
		glVertex2f x+w,y

		glColor3f r1,g1,b1
		glVertex2f x+w,y+h

		If direction = 0
			glColor3f r1,g1,b1
		Else
			glColor3f r0,g0,b0
		EndIf
		glVertex2f x,y+h
	glEnd
	
EndFunction


Thanks :)

Oh, Using the correct GraphicsDriver could be good :P
Now I have another issue though, Using DrawText in the same loop as this function has some strange effects, the Gradient just disappears..
Any thoughts on why? :S

SetGraphicsDriver(GLMax2DDriver())
Graphics 640,480,0,0

Repeat

	DrawGradient( 100, 100, 100, 100, 255, 0, 0, 255, 255, 255 )
	
	If KeyDown( KEY_SPACE )
		DrawText( "THIS IS TEXT!", 0, 0 )
	EndIf
	
	Flip( 1 );Cls

Until KeyHit(KEY_ESCAPE)
End

Function DrawGradient(x:Float,y:Float,w:Float,h:Float,r0:Float=255.0,g0:Float=0.0,b0:Float=0.0,r1:Float=255.0,g1:Float=255.0,b1:Float=0.0,direction:Int=0)

	r0 = r0/255.0
	g0 = g0/255.0
	b0 = b0/255.0
	
	r1 = r1/255.0
	g1 = g1/255.0
	b1 = b1/255.0

	glBegin GL_QUADS
		glColor3f r0,g0,b0
		glVertex2f x,y

		If direction = 0
			glColor3f r0,g0,b0
		Else
			glColor3f r1,g1,b1
		EndIf
		glVertex2f x+w,y

		glColor3f r1,g1,b1
		glVertex2f x+w,y+h

		If direction = 0
			glColor3f r1,g1,b1
		Else
			glColor3f r0,g0,b0
		EndIf
		glVertex2f x,y+h
	glEnd
	
EndFunction


Drawing text probably enables texturemapping which `hides` the smooth shading that you'd see in the quads you're drawing. glDisable GL_TEXTURE_2D ?

Trying to use some gl commands mixed with Max2d is in many cases a conflict of interests.

I came up with a better way of doing Gradients now I think :P
Just create an image 1x2 pixels, One white and one Black or Dark Grey, Load the image and use DrawImageRect() and OpenGL with smooth the image to a Gradient :P (And the best thing is that you can just use SetColor before to get the color you want if you just want a fading color)
I wonder how fast this is though..

That's a good way to do it. It does use more computation because each pixel still has to do a texture lookup, but the texture is very small and there are caches which help speedup accesses which surely will be helping in this case. You don't really need the texture to do such a gradient but it's probably not much slower.

[EDIT] I'm a dork and need to read.


Why not just use OpenGL commands?

Something like:

glBegin(GL_QUAD)

glColor4f(1.0, 0.0, 0.0, 1.0)
glVertex2f(0.0, 0.0)

glColor4f(0.0, 1.0, 0.0, 1.0)
glVertex2f(0.0, GraphicsHeight())

glColor4f(0,0, 0.0, 1.0, 1.0)
glVertex2f(GraphicsWidth(), GraphicsHeight())

glColor4f(1.0, 1.0, 1.0, 1.0)
glVertex2f(GraphicsWidth(), 0.0)

glEnd()