Start Stop Rotate Material

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

Start Stop Rotate Material

Post by falsam »

I thought I could use RotateMaterial(Material, 0.1, #PB_Material_Animated) to start the rotation of the material and RotateMaterial(Material, 0, #PB_Material_Animated) to stop the rotation. But it does not work !!

Here is the code I use to get to the start / stop rotation function. This code works but can we do better?

■ ShortKey : R : Run rotation - Space Bar : Stop Rotation.

Code: Select all

Enumeration
  #Mainform
EndEnumeration

Define.l Event

Global WindowStyle.i=#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered
Global Texture.i, Material.i, Mesh.i, Entity.i

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

OpenWindow(#Mainform,0,0,1024,768, "Start/Stop Rotate material", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)

KeyboardMode(#PB_Keyboard_International)
  
;
;Lumiere et ombre
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(151, 251, 151), -1.8, 10, 5)
WorldShadows(#PB_Shadow_Additive)

;
; Une camera 
Camera = CreateCamera(#PB_Any,0,0,100,100)
CameraBackColor(Camera, RGB(145, 182, 201))

MoveCamera(Camera, 2, 5, 15, #PB_Absolute)  
CameraLookAt(Camera, 0,0,0)

;
;Un cube (Texture + Material + Mesh + Entité)
Texture = CreateTexture(#PB_Any,256,256)
StartDrawing(TextureOutput(Texture))
Box(0,0,256,256, RGB(255, 215, 0))
Box(64, 64, 128, 128 ,RGB(0, 0, 0))
StopDrawing()
  
Material = CreateMaterial(#PB_Any, TextureID(Texture))
Mesh = CreateCube(#PB_Any, 3) 
Entity = CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material))  

Repeat
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
    
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
    
    ;Stop la rotation
    If KeyboardReleased (#PB_Key_Space)
      RemoveMaterialLayer(Material)
      AddMaterialLayer(Material, TextureID(Texture), 0)
      ;RotateMaterial(Material, 0, #PB_Material_Animated)
    EndIf
    
    ;Redémarre la rotation
    If KeyboardReleased (#PB_Key_R)
      RemoveMaterialLayer(Material)
      AddMaterialLayer(Material, TextureID(Texture), 0)
      RotateMaterial(Material, 0.1, #PB_Material_Animated)
    EndIf
    
  EndIf
  
  ; Affiche le rendu de la scène
  ClearScreen(RGB(0, 0, 0))
  RenderWorld(80)
  FlipBuffers()  
  
ForEver
Last edited by falsam on Sat Oct 19, 2013 12:33 am, edited 3 times in total.

➽ Windows 11 64-bit - PB 6.21 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: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Start Stop Rotate Material

Post by falsam »

Hello Alexi. Thank you for your answer.
Alexi wrote:Why not rotating manually with #PB_Material_Fixed?
I tried to run and stop rotation with this flag. When the material stops turning, I can't make rotations.

I need to rotate the material when I press a key on the keyboard and stop the rotation when I press another key.

If you have a little code to illustrate this request .... :)

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

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Start Stop Rotate Material

Post by Samuel »

Something like this?

Code: Select all

;#### PRESS E TO STOP ROTATION ####
;#### PRESS W TO RESUME ROTATION ####

InitEngine3D()
InitSprite()
InitKeyboard()


Width  = 800
Height = 600

If OpenWindow(0, 0, 0, Width, Height, "Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, Width, Height, 0, 0, 0)
    
    Add3DArchive(#PB_Compiler_Home+"\Examples\3D\Data\Textures",#PB_3DArchive_FileSystem)

    Texture=LoadTexture(#PB_Any,"clouds.jpg")

    Material=CreateMaterial(#PB_Any,TextureID(Texture))
    
    Mesh=CreateCube(#PB_Any,5)
    Entity=CreateEntity(#PB_Any, MeshID(Mesh), MaterialID(Material),0,0,0)

    Camera=CreateCamera(#PB_Any, 0,0,100,100)
    MoveCamera(Camera, 5, 0, 14)
    CameraLookAt(Camera, 0, 0, 0)
    CameraBackColor(Camera, RGB(55, 55, 55))

    
    Light=CreateLight(#PB_Any, RGB(0,0,0), 0,40,-60)
    
    Rotate=1
    
    Repeat
      If ExamineKeyboard()
        If KeyboardReleased(#PB_Key_W);#### RESUME ROTATION ####
           Rotate=1
        ElseIf KeyboardReleased(#PB_Key_E);#### STOP ROTATION ####
           Rotate=0
        EndIf
      EndIf

      If Rotate=1
        R=R+1
        RotateMaterial(Material, R, #PB_Material_Fixed)
      EndIf
      

      RenderWorld()
      FlipBuffers()

    Until WindowEvent() = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    
  EndIf
EndIf
End
User avatar
falsam
Enthusiast
Enthusiast
Posts: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Start Stop Rotate Material

Post by falsam »

Very strange, the same kind of code did not work. Or maybe I'm wrong :mrgreen:

thank you very much Samuel. Thread Solved (Oops no : ....... not Solved)
Last edited by falsam on Sat Oct 19, 2013 12:38 am, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.21 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: 635
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: [SOLVED] Start Stop Rotate Material

Post by falsam »

I understood why the same kind of code did not work. Insert WorldShadows(#PB_Shadow_Additive) after Light=CreateLight(#PB_Any, RGB(0,0,0), 0,40,-60) your code does not work.

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

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 756
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: [SOLVED] Start Stop Rotate Material

Post by Samuel »

Strange, I wonder if that's a bug of some sort.
I tried #PB_Shadow_Modulative and #PB_Shadow_TextureAdditive shadow types and they both worked fine.
Post Reply