Antialiasing Demo Shows How To

BlitzMax Forums/BlitzMax Beginners Area/Antialiasing Demo Shows How To

ninjarat(Posted 2006) [#1]
'antialiasing demo by ninjarat. very simple and easy.

'switch to gl driver. can't do antialiasing in directx for some reason
SetGraphicsDriver GLMax2DDriver()

Graphics 640,480,32 'make graphics context

'enable smoothing (antialiasing)
glEnable GL_POLYGON_SMOOTH
glEnable GL_LINE_SMOOTH

SetBlend ALPHABLEND 'enable alpha blending

'intialize line arrays
Local qp1#[]=[0.0,0.0]
Local qv1#[]=[Rand(1,10)/7.0*10,Rand(1,10)/7.0*10]
Local qp2#[]=[GraphicsWidth()-1.0,0.0]
Local qv2#[]=[Rand(1,10)/7.0*10,Rand(1,10)/7.0*10]
Local qp3#[]=[GraphicsWidth()-1.0,GraphicsHeight()-1.0]
Local qv3#[]=[Rand(1,10)/7.0*10,Rand(1,10)/7.0*10]
Local qp4#[]=[0.0,GraphicsHeight()-1.0]
Local qv4#[]=[Rand(1,10)/7.0*10,Rand(1,10)/7.0*10]

'start main loop
Repeat
sctr:+1 'increment our frame counter

'various counts to be used in sine waves
a#:+1.1
b#:+2.2
c#:+3.3

'fluxuate colors
r#=128+Sin(a)*127
g#=128+Sin(b)*127
b#=128+Sin(c)*127

'set coords for the message
x#=215+Sin(a/2)*127
y#=235+Sin(c/2)*127

'use alpha blending to slowly fade the background instead
'of an abrupt clear, like with cls()
SetColor 0,0,0
SetAlpha 0.065
DrawRect 0,0,GraphicsWidth(),GraphicsHeight()
SetAlpha 1.0
SetColor 255,255,255

'if our frame counter exceeds three, then increment the lines
If sctr>3 Then
'apply x movement
qp1[0]:+qv1[0]
qp2[0]:+qv2[0]
qp3[0]:+qv3[0]
qp4[0]:+qv4[0]

'apply y movement
qp1[1]:+qv1[1]
qp2[1]:+qv2[1]
qp3[1]:+qv3[1]
qp4[1]:+qv4[1]

'check x bounary to make sure we don't go off the screen
If qp1[0]>GraphicsWidth() Or qp1[0]<0 Then qv1[0]=-qv1[0]
If qp2[0]>GraphicsWidth() Or qp2[0]<0 Then qv2[0]=-qv2[0]
If qp3[0]>GraphicsWidth() Or qp3[0]<0 Then qv3[0]=-qv3[0]
If qp4[0]>GraphicsWidth() Or qp4[0]<0 Then qv4[0]=-qv4[0]

'check y bounary to make sure we don't go off the screen
If qp1[1]>GraphicsHeight() Or qp1[1]<0 Then qv1[1]=-qv1[1]
If qp2[1]>GraphicsHeight() Or qp2[1]<0 Then qv2[1]=-qv2[1]
If qp3[1]>GraphicsHeight() Or qp3[1]<0 Then qv3[1]=-qv3[1]
If qp4[1]>GraphicsHeight() Or qp4[1]<0 Then qv4[1]=-qv4[1]

sctr=0 'reset frame counter
EndIf

'draw the lines
SetTransform 0,1,1
SetColor r,g,b
DrawLine qp1[0],qp1[1],qp2[0],qp2[1]
DrawLine qp2[0],qp2[1],qp3[0],qp3[1]
DrawLine qp3[0],qp3[1],qp4[0],qp4[1]
DrawLine qp4[0],qp4[1],qp1[0],qp1[1]

SetTransform 0,2,2 'set size to double
DrawText "Antialiasing, yo!",x,y 'display message at coordinates
SetTransform 0,1,1 'set size back down to single
SetColor 255,255,255 'set color to white
DrawText "Press ESC to end.",0,0 'display another message at coords 0, 0

Flip 'swap buffers

Until KeyHit(KEY_ESCAPE) 'keep going until someone presses escape.

EndGraphics() 'delete graphics context
End 'end execution


ImaginaryHuman(Posted 2006) [#2]
Nice, althouth to antialiase polygons you're supposed to use a blend mode that is different from just alphablending. I think its the SRC_ALPHA_SATURATE mode.