Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SuperStrict
  2.  
  3. Import sidesign.minib3d
  4.  
  5. SetGraphicsDriver GLGraphicsDriver(), GRAPHICS_BACKBUFFER | GRAPHICS_DEPTHBUFFER | GRAPHICS_STENCILBUFFER
  6.  
  7. Graphics3D 800,600,0,2
  8. Global camera:TCamera=CreateCamera()
  9. CameraZoom Camera, 1.0 / Tan(45.0/2.0)
  10.  
  11. THardwareInfo.displayinfo()
  12.  
  13. AmbientLight 128,128,128
  14.  
  15. Local light:TLight=CreateLight()
  16. LightColor light, 128,128,128
  17.  
  18. Local cone:TMesh=CreateCone(32)
  19. MoveEntity cone, 0, 1.05, 0
  20. EntityColor cone, 0,200,200
  21.  
  22. Local cube:TMesh=CreateCube()
  23. MoveEntity cube, 4, 1.1, 0
  24. EntityColor cube, 200,0,0
  25.  
  26. Local sphere:TMesh=CreateSphere(32)
  27. MoveEntity sphere, -3.5, 1.1, 0
  28. EntityColor sphere, 200,200,64
  29.  
  30. Local mirror:TMesh = TMirror.createMirror(False,True)
  31. ScaleEntity mirror, 6,0.01,6
  32. EntityColor mirror, 0,0,255
  33. EntityAlpha mirror, 0.5
  34.  
  35. MoveEntity cube, 0, 0.02, 0
  36. MoveEntity light, 0, 100, 0
  37.  
  38. Global p:TPivot=CreatePivot()
  39. Global angle:Float = 0
  40.  
  41. While Not KeyHit(KEY_ESCAPE)
  42.     UpdateWorld
  43.  
  44.     TMirror.renderMirror(camera)
  45.    
  46.     ' render everything normal now
  47.     RenderWorld
  48.    
  49.     Flip
  50.     Delay 1
  51.    
  52.     TurnEntity cube, 0, 0.75, 0
  53.     TurnEntity cone, 0, 0.75, 0
  54.     TurnEntity sphere, 0, 0.75, 0
  55.  
  56.     'PositionEntity cone, 0, Cos(angle)*2, 0
  57.     PositionEntity camera, Cos(angle)*20, 4*Cos(angle*2), Sin(angle)*20
  58.     PointEntity camera, p
  59.     angle:+0.5
  60.        
  61. Wend
  62.  
  63.  
  64. Type TMirror
  65.     Global hidden_geometry_list:TList=CreateList()
  66.     Global mirror:TMesh
  67.     Global showMesh%
  68.  
  69.     Function createMirror:TMesh(infinite%=False, show_Mesh%=False)
  70.            mirror=CreateCube()
  71.         showMesh = show_Mesh
  72.         Return mirror
  73.     End Function
  74.  
  75.     Function hideMeshes()
  76.         ClearList hidden_geometry_list
  77.         For Local fmesh:TMesh=EachIn TEntity.entity_list
  78.             If fmesh<>mirror And fmesh.parent=Null And fmesh.brush.alpha>0 And fmesh.hidden()=False Then
  79.                HideEntity fmesh
  80.                ListAddLast hidden_geometry_list, fmesh
  81.             End If
  82.         Next
  83.         ShowEntity mirror
  84.     End Function
  85.    
  86.     Function showMeshes()
  87.         For Local fmesh:TMesh=EachIn hidden_geometry_list
  88.             ShowEntity fmesh
  89.         Next
  90.         HideEntity mirror
  91.     End Function
  92.    
  93.     Function flipMeshes(sign%=1)
  94.         ' flip every light
  95.         For Local llight:TLight=EachIn TLight.light_list
  96.              PositionEntity llight,llight.px, -llight.py, llight.pz, True
  97.              ScaleEntity llight, llight.sx, -llight.sy, llight.sz, True
  98.         Next
  99.    
  100.         ' flip every mesh
  101.         For Local fmesh:TMesh=EachIn TEntity.entity_list
  102.           If fmesh.parent=Null Then
  103.              PositionEntity fmesh, fmesh.px, -fmesh.py+(sign*mirror.py), fmesh.pz
  104.              ScaleEntity fmesh, -fmesh.sx, -fmesh.sy, fmesh.sz
  105.           End If
  106.         Next
  107.     End Function
  108.    
  109.     Function renderMirror(cam:TCamera)
  110.         CameraClsMode cam,True,True
  111.  
  112.         If EntityY(camera)<mirror.py Then Return       
  113.    
  114.         ' prepare to use the stencil buffer
  115.         glClear(GL_ACCUM_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT)
  116.         glDisable(GL_DEPTH_TEST)
  117.         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
  118.    
  119.         ' Draw into the stencil buffer.
  120.         glEnable(GL_STENCIL_TEST)
  121.         glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE)
  122.         glStencilFunc(GL_ALWAYS, 1, $ffffffff)
  123.    
  124.         ' draw only the mirror (the actual 'mask' where objects will be drawn)
  125.         hideMeshes()
  126.            
  127.         RenderWorld
  128.    
  129.         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  130.         glEnable(GL_DEPTH_TEST);
  131.    
  132.         ' Now, only render where stencil is set To 1
  133.         glStencilFunc(GL_EQUAL, 1, $ffffffff)   ' draw If stencil ==1
  134.         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)
  135.    
  136.         ' draw everything but the plane
  137.         showMeshes()
  138.    
  139.         ' invert mesh and light position, to appear reflected
  140.         flipMeshes(1)
  141.        
  142.         RenderWorld
  143.            
  144.         ' okay! everything done!
  145.         glDisable(GL_STENCIL_TEST);
  146.            
  147.         ' shows plane, and re-render everything normally
  148.         CameraClsMode cam,False, False
  149.         If showMesh Then ShowEntity mirror Else HideEntity mirror
  150.         flipMeshes(-1)
  151.     End Function
  152. End Type
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement