Jukuma produced a great anaglyph stereoscopic shader https://forum.thegamecreators.com/thread/213006
EDITED
I had a problem converting his shader to use light and fog but think I now have it working
// Project: Anaglyph
// Created: 2014-12-19
// set window properties
SetWindowSize( 0, 0, -1 )
// set display properties
DevW = GetDeviceWidth()
DevH = GetDeviceHeight()
SetVirtualResolution( DevW, DevH )
SetOrientationAllowed( 0, 0, 1, 1 )
// set event buttons
SetPrintSize( 16 )
AddVirtualButton( 1, 56, 24, 16 )
SetVirtualButtonColor( 1, 64, 255, 64 )
AddVirtualButton( 2, 74, 24, 16 )
SetVirtualButtonColor( 2, 64, 255, 64 )
AddVirtualButton( 3, 56, 40, 16 )
SetVirtualButtonColor( 3, 64, 255, 64 )
AddVirtualButton( 4, 74, 40, 16 )
SetVirtualButtonColor( 4, 64, 255, 64 )
AddVirtualButton( 5, 56, 56, 16 )
SetVirtualButtonColor( 5, 64, 255, 64 )
AddVirtualButton( 6, 74, 56, 16 )
SetVirtualButtonColor( 6, 64, 255, 64 )
AddVirtualButton( 7, DevW - 8, 8, 16 )
SetVirtualButtonColor( 7, 255, 32, 32 )
// allocation global
type ktype
sx#, sy#, sz#, ex#, ey#, ez#
endtype
dim k[250] as ktype
global CamX# = 5.00
global CamY# = 1.00
global CamZ# = 2.50
// make 3d objects
for o = 1 to 250
select Random( 1, 5)
case 1:
CreateObjectBox( o, 100, 100, 100)
endcase
case 2:
CreateObjectPlane( o, 100, 100 )
endcase
case 3:
CreateObjectSphere( o, 100, 32, 32 )
endcase
case 4:
CreateObjectCylinder( o, 50, 50, 32)
endcase
case 5:
CreateObjectCone( o, 100, 100, 32 )
endcase
endselect
SetObjectColor(o,255,255,255,180)
next o
// set cam
SetCameraRange( 1, 1, 3000 )
SetCameraPosition( 1, 0, 0, 0 )
SetCameraRotation( 1, 0, 0, 0 )
// pre post process
CreateShader()
Quad = CreateObjectQuad()
Stereo = LoadShader( "Anaglyph.vs", "Anaglyph.ps" )
CreateRenderImage( 1, DevW, DevH, 0, 0 )
CreateRenderImage( 2, DevW, DevH, 0, 0 )
repeat
Update(0)
// left render
SetRenderToImage( 1, -1 )
ClearScreen()
SetCameraPosition( 1, -CamX#, -CamY#, -CamZ# )
DrawObj3D( DevW, DevH )
// right render
SetRenderToImage( 2, -1 )
ClearScreen()
SetCameraPosition( 1, CamX#, CamY#, CamZ# )
Render3D()
// post process
SetRenderToScreen()
SetObjectImage( Quad, 1, 0 )
SetObjectImage( Quad, 2, 1 )
SetObjectShader( Quad, Stereo)
DrawObject( Quad )
// 2d stuff
PrintC( "FPS: " )
Print( Trunc( ScreenFPS() ) )
PrintC( "CamX < >: " )
Print( CamX# )
PrintC( "CamY < >: " )
Print( CamY# )
PrintC( "CamZ < >: " )
Print( CamZ# )
Render2DFront()
Esc = VButton()
Swap()
until GetRawKeyPressed(32)
end
function VButton()
for i = 1 to 7
if GetVirtualButtonState( i )
select i
case 1:
CamX# = CamX# - 0.01
endcase
case 2:
CamX# = CamX# + 0.01
endcase
case 3:
CamY# = CamY# - 0.01
endcase
case 4:
CamY# = CamY# + 0.01
endcase
case 5:
CamZ# = CamZ# - 0.01
endcase
case 6:
CamZ# = CamZ# + 0.01
endcase
case 7:
Esc = 1
endcase
endselect
endif
next i
endfunction Esc
function CosInt(a#, b#, c#)
f# = ( 1 - cos( c# * 180.0 ) ) / 2
r# = a# * ( 1 - f# ) + b# * f#
endfunction r#
function DrawObj3D( w, h )
for o = 1 to 250
if Abs( k[o].sx# - k[o].ex# ) < 5 or Abs( k[o].sy# - k[o].ey# ) < 5 or Abs( k[o].sz# - k[o].ez# ) < 5
k[o].ex# = Random( 0, w * 2 ) - w
k[o].ey# = Random( 0, h * 2 ) - h
k[o].ez# = Random( 0, 3000)
endif
k[o].sx# = CosInt( k[o].sx#, k[o].ex#, 0.05 )
k[o].sy# = CosInt( k[o].sy#, k[o].ey#, 0.05 )
k[o].sz# = CosInt( k[o].sz#, k[o].ez#, 0.05 )
SetObjectPosition( o, k[o].sx#, k[o].sy#, k[o].sz# )
SetObjectRotation( o, k[o].sx# * 0.3, k[o].sy# * 0.3, k[o].sz# * 0.3 )
DrawObject( o )
next o
endfunction
function createShader()
file = OpenToWrite("Anaglyph.vs")
WriteLine(file,"attribute vec3 position;")
WriteLine(file,"attribute mediump vec3 normal;")
WriteLine(file,"varying vec2 uvVarying;")
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
WriteLine(file,"uniform vec4 uvBounds0;")
WriteLine(file,"uniform float agk_invert;")
WriteLine(file,"uniform highp mat3 agk_WorldNormal;")
WriteLine(file,"uniform highp mat4 agk_World;")
WriteLine(file,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"gl_Position = vec4(position.xy * vec2(1, agk_invert), 0.5, 1.0);")
WriteLine(file,"uvVarying = (position.xy * vec2(0.5, -0.5) + 0.5) * uvBounds0.xy + uvBounds0.zw;")
WriteLine(file,"mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(file,"highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(file,"posVarying = pos.xyz;")
WriteLine(file,"lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(file,"}")
CloseFile(file)
file = OpenToWrite("Anaglyph.ps")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"uniform sampler2D texture1;")
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 normalVarying;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
WriteLine(file,"mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(file,"mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );")
WriteLine(file,"uniform mediump vec4 agk_MeshDiffuse;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"mediump vec3 norm = normalize(normalVarying);")
WriteLine(file,"mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying );")
WriteLine(file,"vec3 LColor = texture2D(texture0, uvVarying).xyz*light;")
WriteLine(file,"vec3 RColor = texture2D(texture1, uvVarying).xyz*light;")
WriteLine(file,"vec3 Color;")
WriteLine(file,"Color.r = 0.299 * LColor.r + 0.587 * LColor.g + 0.114 * LColor.b;")
WriteLine(file,"Color.g = RColor.g;")
WriteLine(file,"Color.b = RColor.b;")
WriteLine(file,"Color = clamp(Color,0.0,1.0);") //restricts color range
WriteLine(file,"Color = ApplyFog(Color, posVarying );")
WriteLine(file,"gl_FragColor = vec4(Color,1.0)* agk_MeshDiffuse;")
WriteLine(file,"}")
CloseFile(file)
endfunction
fubar