Code archives/3D Graphics - Effects/3D glasses filter (Red-Cyan/Blue/Green)

This code has been declared by its author to be Public Domain code.

Download source code

3D glasses filter (Red-Cyan/Blue/Green) by P.Weijtenburg
(Posted 20 years ago)
Apply this filter to your game and add Tru3D effects to work with the red-cyan/blue/green glasses (green works best)

On the todo list is:
- Make it compatible with 2D (sprites and textures)
- Add extra perspective by adjusting the with between the red and cyan layers the deeper it gets.
(example: the offset at the point of the camera could be 10px, while it is 100px at 50 units deeper (on the Z axis). See how i tried this with the cubes.
Global black,red,blue,redt,bluet

Graphics3D 1220,900,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs() 
AmbientLight 10,10,10

cam=CreateCamera()
PositionEntity cam,0,2,-12
RotateEntity cam,2,0,0

light=CreateLight(3)
PositionEntity light,0,10,-64
LightColor light,5,5,5

;============example senario===============
cube1=CreateCube()
RotateEntity cube1, 45,45,45
EntityColor cube1, Rand(1,255), Rand(1,255), Rand(1,255)
PositionEntity cube1,0,2.2,20

cube2=CreateCube()
RotateEntity cube2, 45,45,45
EntityColor cube2, Rand(1,255), Rand(1,255), Rand(1,255)
PositionEntity cube2,0,.5,5

cube3=CreateCube()
RotateEntity cube3, 45,45,45
EntityColor cube3, Rand(1,255), Rand(1,255), Rand(1,255)
PositionEntity cube3,0,-.3,-5

;you may cut out the terrain code or replace it with your own texture.
ter=CreateTerrain(128)
tert=LoadTexture("texture.jpg")
EntityTexture ter, tert
PositionEntity ter,-64,-2,-16

;============example senario===============

map=Init3Dglass(cam,512)

While Not KeyDown(1)
	TurnEntity cube1,0.3,0.3,0.3
	TurnEntity cube2,0.2,0.2,0.2
	TurnEntity cube3,0.3,0.3,0.3
	
    Update3Dglass(cam,map,redt,bluet)

    UpdateWorld
    RenderWorld
    Flip
Wend
End

Function Init3Dglass(cam,map)
    black=MakeQuad(cam)
    EntityAlpha black,1
    PositionEntity black,0,0,1

	redt=CreateTexture(map,map)
    red=MakeQuad(cam)
    EntityColor red,255,0,0
    EntityFX red,1
    EntityBlend red,3
    PositionEntity red,0,0,1
    TextureBlend redt,5
    EntityTexture red,redt

	bluet=CreateTexture(map,map)
    blue=MakeQuad(cam)
    EntityColor blue,0,255,255
    EntityFX blue,1
    EntityBlend blue,3
    PositionEntity blue,0,0,1
    TextureBlend bluet,5
    EntityTexture blue,bluet
	
	Return map
End Function

Function MakeQuad(cam)
    q = CreateMesh(cam)
	s = CreateSurface(q)
	AddVertex s, -1, 1, 0, 0, 0
	AddVertex s, 1, 1, 0, 1, 0
	AddVertex s, -1,-1, 0, 0, 1
	AddVertex s, 1,-1, 0, 1, 1
   	AddTriangle s, 0, 1, 2
	AddTriangle s, 3, 2, 1
	
	Return q
End Function

Function Update3Dglass(cam,map,rtext,btext)
    HideEntity red
    HideEntity blue
    HideEntity black

	CameraViewport cam, 0, 0, map, map
    
	;Make red texture
    MoveEntity cam,-.2,0,0
    RenderWorld
    CopyRect 0,0,map,map,0,0,BackBuffer(),TextureBuffer(rtext)

    ;Make blue texture
    MoveEntity cam,.4,0,0
    RenderWorld
    CopyRect 0,0,map,map,0,0,BackBuffer(),TextureBuffer(btext)

	;Reset viewport
	CameraViewport Cam,0,0,GraphicsWidth(),GraphicsHeight()
    MoveEntity cam,-.2,0,0
    
	;Show textures
	ShowEntity red
    ShowEntity blue
    ShowEntity black
End Function