Piston lunchbreak example

Everything related to 3D programming
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Piston lunchbreak example

Post by DK_PETER »

Code: Select all

;Piston lunchbreak example...
;A REAL easy way to mimic pistons (block moving), which is often seen in demos
;Missing some groovy tune :-)
;-----------------------------------------------------------------------------
InitEngine3D()
InitSprite()
InitKeyboard()

Structure _object
  id.i
  ma.i
  im.i
  tx.i
  ms.i
EndStructure

Structure _nodeData
  no.i
  up.i
  speed.f
EndStructure

Declare.i NewColor()
Declare.f RandomF(Min.f, Max.f, Res.i = 100000)

Global NewList o._object(), base._object, Quit.i = 0
Global Dim nod._nodeData(9)

OpenWindow(0, 0, 0, 1024, 768, "Piston lunchbreak...DK_PETER", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 1024, 768)
WorldShadows(#PB_Shadow_Additive)
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 5, -11, #PB_Absolute)
CameraDirection(0, 0, -0.7, 1)
CreateLight(0, $FFFFFF, 0, 10, 0)
LightDirection(0, 0, 1, 0)

With base
  \ms = CreateCube(#PB_Any, 1)
  \im = CreateImage(#PB_Any, 1024, 1024, 32)
  StartDrawing(ImageOutput(\im))
  DrawingMode(#PB_2DDrawing_Gradient)
  FrontColor($FFFFFF) :BackColor($222222)
  LinearGradient(0, 0, 512, 512)
  Box(1, 1, 1023, 1023)
  StopDrawing()
  ResizeImage(\im, 512, 512)
  \tx = CreateTexture(#PB_Any, 512, 512)
  StartDrawing(TextureOutput(\tx))
  DrawImage(ImageID(\im), 0, 0)
  StopDrawing()
  FreeImage(\im)
  \ma = CreateMaterial(#PB_Any, TextureID(base\tx))
  MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
  SetMaterialAttribute(\ma, #PB_Material_DepthCheck , #True)
  \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
  HideEntity(\id, #True)
EndWith

For x = 0 To 9 
  nod(x)\no = CreateNode(#PB_Any, 0, 0, 0) 
  nod(x)\up = Random(1,0) 
  nod(x)\speed = RandomF(0.02,0.005)
Next x

For x = -5 To 5 
  For z = -5 To 5 
    AddElement(o())
    o()\id = CopyEntity(base\id, #PB_Any)
    MoveEntity(o()\id, x, 0, z, #PB_Absolute)
    AttachNodeObject(nod(Random(9,0))\no, EntityID(o()\id))
  Next z
Next x

Repeat
  Repeat
    ev = WindowEvent()
    If ev = #PB_Event_CloseWindow : Quit = 1 : EndIf
  Until ev = 0
  For x = 0 To 9
    If nod(x)\up = #True 
      MoveNode(nod(x)\no, 0, -nod(x)\speed, 0, #PB_Relative)
    Else
      MoveNode(nod(x)\no, 0, nod(x)\speed, 0, #PB_Relative)
    EndIf
    If NodeY(nod(x)\no,#PB_Absolute) >= 0.5
      nod(x)\up = #True
      SetLightColor(0, #PB_Light_SpecularColor, NewColor())
    ElseIf NodeY(nod(x)\no,#PB_Absolute) <= -0.5
      nod(x)\up = #False
      SetLightColor(0, #PB_Light_DiffuseColor, NewColor())
    EndIf
    RotateNode(nod(x)\no, 0, 0.1, 0, #PB_Relative)
  Next x
  RenderWorld()
  
  ExamineKeyboard()
  
  FlipBuffers()
  
Until Quit = 1 Or KeyboardPushed(#PB_Key_Escape)

Procedure.i NewColor()
  ProcedureReturn RGB(Random(255,40), Random(255,40), Random(255,40))
EndProcedure

Procedure.f RandomF(Min.f, Max.f, Res.i = 100000)
  ProcedureReturn (Min + (Max - Min) * Random(Res) / Res)
EndProcedure
 
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5524
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Piston lunchbreak example

Post by Kwai chang caine »

Splendid !!! :D
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Piston lunchbreak example

Post by Joris »

Nice.

I needed to place OpenGL in the compiler options to get it running (XP sp3).
But then everything ok.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Post Reply