Simple night day demo

Everything related to 3D programming
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Simple night day demo

Post by minimy »

Hello, i did a demo of night and day cycle with shadows.
Maybe a start point for more complex code...
Good night or good morning... i dont know... :mrgreen:

Code: Select all


;{ GLOBALES
  Global.b sal
  Global.l contador
  Global.b pantallaCompleta
;}

  ;{ FUENTES
  Global  fuenteLit= LoadFont(#PB_Any,"Arial",9)
  Global  fuenteMed= LoadFont(#PB_Any,"Arial",16)
  Global  fuenteBig= LoadFont(#PB_Any,"Arial",24)
  ;}
  
  ;{ DIA Y NOCHE
  Structure diaNoche_stru
    sol.i
    ang.d
    spd.d
    
    lunaTx3D.i
    lunaMate.i
    lunaBB.i

    solTx3D.i
    solMate.i
    solBB.i
  EndStructure
  Global diaNoc.diaNoche_stru
  
  Procedure   iniciaNocheDia(spd.d=0.125, size=256)
    ;luz
    diaNoc\sol=   CreateLight(#PB_Any,$ffffff,0,0,0,#PB_Light_Point)
    diaNoc\spd=   spd
    ;texturas
    diaNoc\solTx3D=  CreateTexture(#PB_Any,size,size)
    StartDrawing(TextureOutput(diaNoc\solTx3D))
      DrawingMode(#PB_2DDrawing_AllChannels)
      Box(0,0,OutputWidth(),OutputHeight(),$00000000)
      DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AlphaBlend)
      BackColor($ff00ffff)
      GradientColor(0.8,$ff00ffff)
      FrontColor($0000ffff)
      CircularGradient(size/2,size/2,size*0.49)
      Circle(size/2,size/2,size/2);, $ff00ffff)
      DrawingMode(#PB_2DDrawing_AlphaBlend)
;       Circle(size/2,size/2,size*0.2, $ff00ffff)
    StopDrawing()
    
    diaNoc\lunaTx3D= CreateTexture(#PB_Any,size,size)
    StartDrawing(TextureOutput(diaNoc\lunaTx3D))
      DrawingMode(#PB_2DDrawing_AllChannels)
      Box(0,0,OutputWidth(),OutputHeight(),$00000000)
      DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AlphaBlend)
      BackColor($ffffffaa)
      GradientColor(0.5,$ffffffaa)
      FrontColor($00ffaa44)
      CircularGradient(size/2,size/2,size*0.49)
      Circle(size/2,size/2,size/2);, $ff00ffff)
      DrawingMode(#PB_2DDrawing_AlphaBlend)
;       Circle(size/2,size/2,size*0.2, $ffffaa44)
    StopDrawing()
    
    ;materiales
    diaNoc\solMate=   CreateMaterial(#PB_Any,TextureID(diaNoc\solTx3D))
      MaterialBlendingMode(diaNoc\solMate,#PB_Material_AlphaBlend)
      DisableMaterialLighting(diaNoc\solMate,1)
      
    diaNoc\lunaMate=  CreateMaterial(#PB_Any,TextureID(diaNoc\lunaTx3D))
      MaterialBlendingMode(diaNoc\lunaMate,#PB_Material_AlphaBlend)
      DisableMaterialLighting(diaNoc\lunaMate,1)
    
    ;billboard y noodos
    diaNoc\solBB=   CreateBillboardGroup(#PB_Any, MaterialID(diaNoc\solMate),6,6, 0,0,0)
      AddBillboard(diaNoc\solBB,0,0,0)
    diaNoc\lunaBB=  CreateBillboardGroup(#PB_Any, MaterialID(diaNoc\lunaMate),4,4, 0,0,0)
      AddBillboard(diaNoc\lunaBB,0,0,0)
    
    
  EndProcedure
  Procedure   movimientoSolar(x.f=0,y.f=0,z.f=0, d.f=30)
    Protected.f nx,ny
    Protected.d aa, al, c
    Protected.d intensidad= Abs(Sin(Radian(diaNoc\ang)))
    
    aa=     Radian(diaNoc\ang)
    nx= x + (Cos(aa)*d)
    ny= z + (Sin(aa)*d)
;     MoveEntity(sol, nx,ny,0,#PB_Absolute)
    MoveBillboardGroup(diaNoc\solBB, nx,ny,0,#PB_Absolute)
    
    al= diaNoc\ang+180
    aa=     Radian(al)
    nx= x + (Cos(aa)*d)
    ny= z + (Sin(aa)*d)
;     MoveEntity(luna, nx,ny,0,#PB_Absolute)
    MoveBillboardGroup(diaNoc\lunaBB, nx,ny,0,#PB_Absolute)
    
    LightAttenuation(diaNoc\sol, 256*intensidad, intensidad)
    
    If diaNoc\ang >= 0 And diaNoc\ang <= 180
      MoveLight(diaNoc\sol,BillboardGroupX(diaNoc\solBB),BillboardGroupY(diaNoc\solBB),BillboardGroupZ(diaNoc\solBB),#PB_Absolute)
      SetLightColor(diaNoc\sol, #PB_Light_DiffuseColor, $ffffff)
      c= 55 + 150*intensidad
      AmbientColor(RGB(c,c,c))
;       Debug "SOL " +StrD(a,2)
    Else
      MoveLight(diaNoc\sol,BillboardGroupX(diaNoc\lunaBB),BillboardGroupY(diaNoc\lunaBB),BillboardGroupZ(diaNoc\lunaBB),#PB_Absolute)
      SetLightColor(diaNoc\sol, #PB_Light_DiffuseColor, $aa5500)
      AmbientColor($222222)
;       Debug "LUNA" +StrD(a,2)
    EndIf
;     
    diaNoc\ang + diaNoc\spd : If diaNoc\ang>360 : diaNoc\ang=0 : EndIf
  EndProcedure
  
  ;}
  
  ;{ SISTEMA 3D
  Procedure   showModosPantalla()
    If ExamineScreenModes()
      While NextScreenMode()
        Debug Str(ScreenModeWidth())+"x"+Str(ScreenModeHeight())+"x"+Str(ScreenModeDepth())+"@"+Str(ScreenModeRefreshRate())+"Hz"
      Wend
    EndIf
  EndProcedure
  Procedure   iniDX(title.s="3D", width=800,height=600, fullscreen.b=#False, AA.i=#PB_AntialiasingMode_None, shadowmode= #PB_Shadow_Modulative, shadowres=4096, param=#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
    Protected.b screenTestOK
    InitEngine3D()
      InitSprite()
      InitKeyboard()
      InitMouse()
      AntialiasingMode(AA)
      
      If width=0 And height=0
        ExamineDesktops()
        width=  DesktopWidth(0)
        height= DesktopHeight(0)
        pantallaCompleta= 0
        param= #PB_Window_BorderLess
      EndIf
      
      If fullscreen
        pantallaCompleta= 1
        If ExamineScreenModes()
          While NextScreenMode()
;             Debug Str(ScreenModeWidth())+"x"+Str(ScreenModeHeight())+"x"+Str(ScreenModeDepth())+"@"+Str(ScreenModeRefreshRate())+"Hz"
            If ScreenModeWidth()=width And ScreenModeHeight()=height And ScreenModeDepth()=32
              screenTestOK= 1
            EndIf
          Wend
        EndIf
        If screenTestOK= 0
          width=1280:height=720
        EndIf
        OpenScreen(width,height,32,title,#PB_Screen_NoSynchronization)
      Else
        OpenWindow(0, 0, 0, width, height, title, param)
          SetWindowColor(0,$444444)
          OpenWindowedScreen(WindowID(0), 0, 0, (width * DesktopResolutionX()), (height * DesktopResolutionY()),0,0,0,#PB_Screen_NoSynchronization)
      EndIf
      KeyboardMode(#PB_Keyboard_International)
      Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
      Add3DArchive("Data/Model", #PB_3DArchive_FileSystem)
      Parse3DScripts()

      Protected.b s= 100
      If shadowmode= #PB_Shadow_TextureAdditive Or shadowmode= #PB_Shadow_TextureModulative
        WorldShadows(shadowmode, -1, RGB(s,s,s), shadowres)
      Else  
        WorldShadows(shadowmode, -1, RGB(s,s,s))
    EndIf
  EndProcedure
  
;   iniDX("Base 3D",1280,800,#True) ;pantalla completa
  iniDX("Base 3D - WASD=move (good morning/good night) :)",960,540,#False,#PB_AntialiasingMode_None,#PB_Shadow_Additive, 256) ;modo ventana
;   iniDX("Base 3D",0,0,#False,#PB_AntialiasingMode_None,#PB_Shadow_Modulative, 512) ; pantalla completa en veentana
  ;}
  
  Procedure   creaSuelo(size=50, col1.l=$0,col2.l=$aaaaaa, res=256)
    Protected.i suelo_tx3D, suelo_mate, suelo_mesh, suelo
    suelo_tx3D=   CreateTexture(#PB_Any,res,res)
    StartDrawing(TextureOutput(suelo_tx3D))
      Box(0,0,OutputWidth(),OutputHeight(),col1)
      Box(2,2,OutputWidth()-4,OutputHeight()-4,col2)
    StopDrawing()
    
    suelo_mesh=   CreatePlane(#PB_Any,size,size,1,1,1,1)
    suelo_mate=   CreateMaterial(#PB_Any,TextureID(suelo_tx3D))
                  ScaleMaterial(suelo_mate,1/size,1/size)
    suelo=        CreateEntity(#PB_Any, MeshID(suelo_mesh), MaterialID(suelo_mate))
  ProcedureReturn suelo
  EndProcedure
  
  ;{ HUD sprites
  sprInfo=  CreateSprite(#PB_Any,100,100,#PB_Sprite_AlphaBlending)
  Procedure   spr2DText(spr, txt.s, font, ink.l=$ff00ff00)
    Protected n,y=2,tl= CountString(txt,#CR$)
    Protected tx.s
    StartDrawing(SpriteOutput(spr))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0,0,OutputWidth(),OutputHeight(),$00000000)
    DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Transparent)
      Box(0,0,OutputWidth(),OutputHeight(),$aa000000)
      DrawingFont(FontID(font))
      For n=1 To tl+1
        tx= StringField(txt,n,#CR$)
        DrawText(5,y,tx,ink)
        y+16
      Next n
      DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Outlined)
      Box(0,0,OutputWidth(),OutputHeight(),ink)
    StopDrawing()
  EndProcedure
  ;}
  
  ;{ CONTROL DE EVENTOS
  Procedure   eventosWindows()
    Repeat
      event= WindowEvent()
      Select event
        Case #PB_Event_Gadget
          EventGadget=  EventGadget()
          EventType=    EventType()
          
        Case #PB_Event_CloseWindow
          sal= 1
      EndSelect
    Until event= 0
  EndProcedure
  Procedure   eventos3D(camara, speed.f=1, mouseSpd.f=0.05)
    Protected.f KeyX,KeyY, MouseX,MouseY
    If ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Escape)
        sal=1
      EndIf
      
      If KeyboardPushed(#PB_Key_A)
        KeyX= -speed
      ElseIf KeyboardPushed(#PB_Key_D)
        KeyX= speed
      Else
        KeyX= 0
      EndIf
      
      If KeyboardPushed(#PB_Key_W)
        KeyY= -speed
      ElseIf KeyboardPushed(#PB_Key_S)
        KeyY= speed
      Else
        KeyY= 0
      EndIf
    EndIf
    
    If ExamineMouse()
      MouseX = -MouseDeltaX() * mouseSpd
      MouseY = -MouseDeltaY() * mouseSpd
      mouseW =  MouseWheel()
    EndIf
    
    RotateCamera(camara, MouseY, MouseX, 0, #PB_Relative)
    MoveCamera (camara, KeyX, 0, KeyY)
  EndProcedure
  ;}
  
  ;{ DECORADO 3D
  creaSuelo()
  
  cubo_mesh=    CreateCube(#PB_Any,2)
  cubo_mate=    CreateMaterial(#PB_Any, #Null,$0000ff)
  cubo_enti=    CreateEntity(#PB_Any, MeshID(cubo_mesh), MaterialID(cubo_mate),0,1,0)
  ;}
  
  ;{ CÁMARA
  camara=       CreateCamera(#PB_Any,0,0,100,100)
                MoveCamera(camara,0,5,50)
                CameraRange(camara, 0.1,1000)
  ;}
                
  iniciaNocheDia()
  movimientoSolar(0,0,0, 30)

  ;{ BUCLE PRINCIPAL
  Repeat
    ;{ PROCESO EVENTOS
    If pantallaCompleta= 0
      eventosWindows()
    EndIf
    ;}
        
    ;{ PROCESOS 3D, RATÓN Y TECLADO DEL JUEGO
    eventos3D(camara, 0.1)
    ;}
    
    movimientoSolar(0,0,0, 30)
    
    ElapsedTime = RenderWorld()
    
    ;{ HUD
    If contador % 25 = 0
      txt_info.s= Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) + #CR$ +
                  "S: "+Str(ScreenWidth())+" x "+Str(ScreenHeight())
;                   "SOL: "+StrD(sol_ang,2) + #CR$ +
;                   "SOL: "+StrD(Abs(Sin(Radian(sol_ang))),2)
      spr2DText(sprInfo,txt_info,fuenteLit)
    EndIf
    DisplayTransparentSprite(sprInfo,10,10)
    ;}
    
    FlipBuffers()
    contador +1
    Delay(1)
  Until sal=1
  ;}
  
  ;{ LIMPIEZA
  
  ;}
  
If translation=Error: reply="Sorry, Im Spanish": Endif
dige
Addict
Addict
Posts: 1405
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Simple night day demo

Post by dige »

Nice! Thx for sharing
"Daddy, I'll run faster, then it is not so far..."
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple night day demo

Post by minimy »

Thanks to you dige!
If translation=Error: reply="Sorry, Im Spanish": Endif
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple night day demo

Post by BarryG »

Flat earth, eh? :lol: Just kidding. Good example.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple night day demo

Post by minimy »

Flat earth, eh? :lol: Just kidding. Good example.
Hi BarryG, thanks!. Then... earth is not flat? :lol:
If translation=Error: reply="Sorry, Im Spanish": Endif
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple night day demo

Post by BarryG »

It's flat in your demo. ;)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Simple night day demo

Post by Psychophanta »

You made flat my Earth :!: :shock: :lol:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply