Mirror Experience

Généralités sur la programmation 3D
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Mirror Experience

Message par falsam »

La solution est de créer une seconde camera et d'utiliser la fonction CreateRenderTexture() afin d'obtenir le rendu de cette caméra dans une texture qui sera afficher sur la face cachée d'un plan.

Code : Tout sélectionner

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))
CreateEntityBody(Entity, #PB_Entity_PlaneBody, 1,1,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)
CreateEntityBody(Player, #PB_Entity_ConvexHullBody, 1, 0, 1)
EntityAngularFactor(Player, 0, 1, 0)

;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)
CreateEntityBody(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 = 8
      Animation = "IdleBase"
      
    ElseIf KeyboardPushed (#PB_Key_Down)
      PlayerSpeed = -8
      Animation = "IdleBase"
    Else
      PlayerSpeed = 0
    EndIf
    
    If KeyboardPushed (#PB_Key_Left)
      RotateEntity(Player, 0, 3, 0, #PB_Relative)
    ElseIf KeyboardPushed (#PB_Key_Right)
      RotateEntity(Player, 0, -3, 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(50)
  FlipBuffers()  
ForEver
Utilisez les flèches pour vous déplacer et pivoter. F1 F2 & F3 pour quelques animations et Esc pour quitter
Dernière modification par falsam le lun. 22/déc./2014 15:33, modifié 2 fois.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
klystron
Messages : 149
Inscription : mar. 02/déc./2014 12:16

Re: Se voir dans un miroir

Message par klystron »

Bonjour Falsam,
super ton code.

bonne journée.
kelebrindae
Messages : 579
Inscription : ven. 11/mai/2007 15:21

Re: Se voir dans un miroir

Message par kelebrindae »

Hé, hé! Marrant, cet exemple; s'il y avait des animations pour que le perso puisse faire des pitreries devant le miroir, je crois que j'y passerais des heures... :wink:

As tu essayé de placer un second miroir en face du premier, pour voir si ça explose?
Les idées sont le souvenir de choses qui ne se sont pas encore produites.
klystron
Messages : 149
Inscription : mar. 02/déc./2014 12:16

Re: Se voir dans un miroir

Message par klystron »

ha oui tiens ... dans un monde parallèle 8O
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Se voir dans un miroir

Message par falsam »

kelebrindae et klystron merci pour vos commentaires.
kelebrindae a écrit :s'il y avait des animations pour que le perso puisse faire des pitreries devant le miroir, je crois que j'y passerais des heures.
Tu ne passeras peut-être pas quelques heures mais j'ai ajouté 3 animations accessibles depuis les touches F1, F2 et F3. Ces animations sont issus des exemples de Pure Basic.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
klystron
Messages : 149
Inscription : mar. 02/déc./2014 12:16

Re: Se voir dans un miroir

Message par klystron »

oui super.
bonne apres-midi Falsam.
kelebrindae
Messages : 579
Inscription : ven. 11/mai/2007 15:21

Re: Se voir dans un miroir

Message par kelebrindae »

Ah ben non, tiens; ça n'explose pas... (presque déçu :( )

Code : Tout sélectionner

EnableExplicit

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

Global Window, Event
Global Camera, CameraMirror, CameraMirror2
Global Animation.s
Global Texture, Material, Entity, Mirror
Global Texture2, Material2, Entity2, Mirror2
Global Player, PlayerSpeed.f, CameraSpeed = 30, materialBg

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)
;MoveCamera(Camera,10,0,0)

;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, -10)
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
MaterialBg = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any, "Wood.jpg")))
Entity = CreateEntity(#PB_Any, MeshID(CreateCube(#PB_Any, 1)), MaterialID(MaterialBg), 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)


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

;Mirror2 - Background
Entity2 = CreateEntity(#PB_Any, MeshID(CreateCube(#PB_Any, 1)), MaterialID(MaterialBg), 0, 5, -20 - 0.7)
ScaleEntity(Entity2, 10, 10, 1)

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

;Mirror2 - Entity
Mirror2 = CreateEntity(#PB_Any, MeshID(CreatePlane(#PB_Any, 8, 8, 1, 1, 1, 1)), MaterialID(Material2), 0, 5, -20)
EntityPhysicBody(Mirror2, #PB_Entity_StaticBody)
RotateEntity(Mirror2, -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), 0, EntityY(Player)+0.5, -15, 0.5, 0.5, #True)   
   
  EndIf
 
  RenderWorld(40)
  FlipBuffers() 
ForEver
Les idées sont le souvenir de choses qui ne se sont pas encore produites.
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Se voir dans un miroir

Message par falsam »

Merci kelebrindae:)

Un labyrinthe avec plein de miroirs ? :mrgreen:
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
graph100
Messages : 1318
Inscription : sam. 21/mai/2005 17:50

Re: Mirror Experience

Message par graph100 »

ya un piti problème avec vos miroirs :lol:

Surtout celui qu'a ajouté Kelebrindae : il ne montre pas le personnage du bon coté.
Et le 'miroir' n'a pas les propriétés d'un miroir.
_________________________________________________
Mon site : CeriseCode (Attention Chantier perpétuel ;))
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Mirror Experience

Message par falsam »

Modification du code pour être en conformité avec PureBasic 5.60.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
venom
Messages : 3071
Inscription : jeu. 29/juil./2004 16:33
Localisation : Klyntar
Contact :

Re: Mirror Experience

Message par venom »

Bonjour,

Mince chez moi le compilateur plante des l'ouverture de la fenêtre. 8O :(
Sa doit être mon ordi en carton :lol:






@++
Windows 10 x64, PureBasic 5.73 x86 & x64
GPU : radeon HD6370M, CPU : p6200 2.13Ghz
Shadow
Messages : 1373
Inscription : mer. 04/nov./2015 17:39

Re: Mirror Experience

Message par Shadow »

Sympa tous ça :D
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Avatar de l’utilisateur
SPH
Messages : 4722
Inscription : mer. 09/nov./2005 9:53

Re: Mirror Experience

Message par SPH »

Dans les 2 codes, erreur de engine3D.dll avec mon ordi portable qui date de 5 ans. :oops:
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.00 - 64 bits
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Mirror Experience

Message par falsam »

venom a écrit :Mince chez moi le compilateur plante des l'ouverture de la fenêtre.
Si tu es sous windows7 il est possible que DirectX9c ne soit pas installer. Dans ce cas il faut soit installer DirectX9c ou bien compiler avec la librairie OpenGL.
:idea: menu Compiler => option compilation => Library SubSystem : OpenGL
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Mindphazer
Messages : 635
Inscription : mer. 24/août/2005 10:42

Re: Mirror Experience

Message par Mindphazer »

Impressionnant 8O
Ca marche nickel sur mon Mac :wink:

Merci Falsam
Bureau : Win10 64bits
Maison : Macbook Pro M1 14" SSD 512 Go / Ram 16 Go - iPad Pro 32 Go (pour madame) - iPhone 15 Pro Max 256 Go
Répondre