Mirror Experience

Everything related to 3D programming
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Mirror Experience

Post by falsam »

Create a mirror using a second camera. The rendering of the camera will be updated in a texture.

Code: Select all

EnableExplicit

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

Global Window, Event
Global Camera, CameraMirror, Animation.s
Global Texture, Material, Entity, Player, PlayerSpeed.f, CameraSpeed = 30, Mirror

Window = OpenWindow(#PB_Any,0,0,1024,768,"Mon beau miroir")
OpenWindowedScreen(WindowID(window),0,0,1024,768)

Add3DArchive(#PB_Compiler_Home + "Examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home+"Examples/3D/Data/Packs/Sinbad.zip", #PB_3DArchive_Zip)
Parse3DScripts()

;Ambiance
SkyBox("desert07.jpg")
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -10, 100, -500)
WorldShadows(#PB_Shadow_Additive)

;Camera 
Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)

;Ground
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 5000, 5000, 100, 100, 400, 400)), MaterialID(Material))
EntityPhysicBody(Entity, #PB_Entity_StaticBody, 0,0,1)
Fog(RGB(139, 69, 19), 20, 40, 200)

;Player
Player = CreateEntity(#PB_Any, MeshID(LoadMesh(#PB_Any, "Sinbad.mesh")), #PB_Material_None, 0, 5, -25)
EntityPhysicBody(Player, #PB_Entity_CylinderBody, 1, 1, 1)

;Mirror - Camera 
CameraMirror = CreateCamera(#PB_Any, 0, 0, 100, 100)
MoveCamera(CameraMirror, 0, 4, 0, #PB_Absolute)
CameraLookAt(CameraMirror,  EntityX(Player), EntityY(Player), EntityZ(Player))
CameraFOV(CameraMirror, 95) 

;Mirror - Background 
Material = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "Wood.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(CreateCube(#PB_Any, 1)), MaterialID(Material), 0, 5, 0.7)
ScaleEntity(Entity, 10, 10, 1)

;Mirror - Texture & Material 
Texture = CreateRenderTexture(#PB_Any, CameraID(CameraMirror), 1024, 768, #PB_Texture_AutomaticUpdate)
Material = CreateMaterial(#PB_Any, TextureID(Texture))
SetMaterialColor(Material, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))
MaterialCullingMode(Material, #PB_Material_NoCulling)

;Mirror - Entity
Mirror = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 8, 8, 1, 1, 1, 1)), MaterialID(Material), 0, 5, 0)
EntityPhysicBody(Mirror, #PB_Entity_StaticBody)
RotateEntity(Mirror, -90, 180, 0)

Repeat
  Repeat   
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
      
  If ExamineKeyboard()
    
    If KeyboardPushed (#PB_Key_Escape)
     Break
    EndIf
    
    Animation = "IdleBase"
    
    If KeyboardPushed(#PB_Key_F1)
      Animation = "SliceVertical"
    EndIf
    
    If KeyboardPushed(#PB_Key_F2)
      Animation = "SliceHorizontal"
    EndIf
    
    If KeyboardPushed(#PB_Key_F3)
      Animation = "Dance"
    EndIf
        
    
    If KeyboardPushed (#PB_Key_Up)
      PlayerSpeed = 5
      Animation = "IdleBase"
      
    ElseIf KeyboardPushed (#PB_Key_Down)
      PlayerSpeed = -5
      Animation = "IdleBase"
    Else
      PlayerSpeed = 0
    EndIf
    
    If KeyboardPushed (#PB_Key_Left)
      RotateEntity(Player, 0, 1.5, 0, #PB_Relative)
    ElseIf KeyboardPushed (#PB_Key_Right)
      RotateEntity(Player, 0, -1.5, 0, #PB_Relative)
    EndIf    
    
    If PlayerSpeed <> 0 
      DisableEntityBody(Player, #False) ; Wake up entity (BugWare)
      MoveEntity(Player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
      
      If EntityAnimationStatus(Player, "RunBase") = #PB_EntityAnimation_Stopped
        StartEntityAnimation(Player, "RunBase")
      EndIf
    Else
      If EntityAnimationStatus(Player, Animation) = #PB_EntityAnimation_Stopped
        StartEntityAnimation(Player, Animation)
      EndIf
    EndIf 
    
    CameraFollow(Camera, EntityID(PLayer), -180, EntityY(Player)+3, 15, 0.5, 0.5, #True)   
    
  EndIf
  
  RenderWorld(40)
  FlipBuffers()  
ForEver
Use the arrows on your keyboard to move and rotate the player. F1, F2 and F3 Animations
Last edited by falsam on Mon Dec 22, 2014 1:54 pm, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Bananenfreak
Enthusiast
Enthusiast
Posts: 519
Joined: Mon Apr 15, 2013 12:22 pm

Re: Mirror test

Post by Bananenfreak »

Thank you for sharing this.
Another way would be to use CubeMapTextures.
Image
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Mirror test

Post by applePi »

you have an art painter taste falsam, the misty horizon, the mirror which is like a painting
a good new year code. thank you.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Mirror test

Post by falsam »

Bananenfreak wrote:Another way would be to use CubeMapTextures.
Yes but the result is not good. A very good year for you Bananenfreak :)
applePi wrote:a good new year code.
ApplePi, Thank you for your codes and Happy New Year 2015 for you, your family and friends.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Mirror Experience

Post by falsam »

I added three animations. You can use F1, F2 and F3. The first message is changed.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Mirror test

Post by heartbone »

applePi wrote:you have an art painter taste falsam, the misty horizon, the mirror which is like a painting
a good new year code. thank you.
I agree.
Yesterday when I ran the demo in Windows® I was impressed by how pretty the scene was.

If you have the time falsam, the code does not work in Linux. 8)
Keep it BASIC.
Post Reply