Light Direction

Everything related to 3D programming
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Light Direction

Post by J@ckWhiteIII »

Hey,
If i created a light using the #PB_Light_Directional flag, in what direction does the light look? And is there a way to rotate the light?
Other questions I have: If the light is attached to a node and the node is moved/rotated, the light is moved and rotated? (yep, in the manual they say that nodes are always rotated/moved relative to the node they're attached to, but does this work for lights, too?

Haven't worked with lights and nodes before, but I need a "torch" that always shines into the opposite direction of the camera

I hope I can get some help here. Thanks in advance
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Light Direction

Post by applePi »

Code: Select all

is there a way to rotate the light
hello
all orge issues makes me frustrated , there is an example StaticGeometry.pb in the 3D folder examples wich show a moving sun and a moving shadow, i haven't studied it yet, i need to read some ogre tutorials first. but here a simpler example i have posted here http://www.purebasic.fr/english/viewtop ... 37#p368537
download the fish mesh + skeleton and textures from http://www.mediafire.com/?rn4qquww12d8wj1
in the example the position of light are static at -100,40,30 : CreateLight(#LIGHT,RGB(255,255,255),-100,40,30)

to move it , i have stolen a formula from Michael library MP3D:
count.f + 0.03
LightLocate(#LIGHT, Sin(count+#PI/2) * 30, 20, Cos(count+#PI/2) * 30)
put the Data folder from the downloaded file in the same place as the following modified code, press stop button to watch a moving shadow around a static fish, change the y coordinate from 20 to 10 and the shadow will be more elongated.
you may add a moving sun , as in the StaticGeometry.pb official example.
i havn't tested LightLookAt() yet.

Code: Select all

Enumeration
   #MESH
   #TEX
   #TEX_plane
   #MAT
   #MAT_plane
   #plane
   #ENTITY_FISH
   #LIGHT
   #CAMERA_ONE
   #BUTTON
   #mainwin
EndEnumeration
Global Quit.b = #False

If OpenWindow(#mainwin, 0, 0, 500, 480, "press ESC to exit...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(#BUTTON, 0, 405, 50, 30, "stop")  

;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(#mainwin), 0, 0, 500, 400, 0, 0, 0)
WorldShadows(#PB_Shadow_Additive)
  
EnableWorldPhysics(#True)
EnableWorldCollisions(#True)
InitKeyboard()
SetFrameRate(60)

Add3DArchive("Data\", #PB_3DArchive_FileSystem)

CreateMaterial(#MAT_plane, LoadTexture(#TEX_plane, "mosaic.png"))
CreatePlane(#plane, 10, 10, 1, 1, 1, 1)
CreateEntity (#plane, MeshID(#plane), MaterialID(#MAT_plane))
  
LoadMesh(#MESH, "fish.mesh")
LoadTexture(#TEX, "steelhead.png")
CreateMaterial(#MAT, TextureID(#TEX))
CreateNode(#ENTITY_FISH)
CreateEntity(#ENTITY_FISH, MeshID(#MESH), MaterialID(#MAT))
ScaleEntity(#ENTITY_FISH,0.5,0.5,0.5)
AnimateEntity(#ENTITY_FISH, "swim")
AttachNodeObject(#ENTITY_FISH,EntityID(#ENTITY_FISH),#PB_Node_Entity)
MoveNode(#ENTITY_FISH,0,1.5,1)

CreateLight(#LIGHT,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))

CreateCamera(#CAMERA_ONE, 0, 0, 400, 400)
CameraLocate(#CAMERA_ONE, 0, 4, 9)
CameraLookAt(#CAMERA_ONE, 0, 2, 0)

RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf
rot.l=1
;Main loop
Repeat
  Event = WindowEvent()
  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case #BUTTON
        If rot = 0
          rot = 1
          SetGadgetText(#BUTTON,"stop")
        Else
          rot = 0
          SetGadgetText(#BUTTON,"rotate")
        EndIf
    EndSelect
   EndIf 
  y.l + rot
  x.l + rot
  z.l + rot
  RotateEntity(#ENTITY_FISH, 0, y, 0)
  count.f + 0.03
    
  LightLocate(#LIGHT, Sin(count+#PI/2) * 30, 20, Cos(count+#PI/2) * 30)
  ;LightLookAt(#LIGHT, 30, 0, 30)
   
   RenderWorld()
   
  FlipBuffers()

   ExamineKeyboard()
   If KeyboardReleased(#PB_Key_Escape)
      Quit = #True
   EndIf

Until Quit = #True Or Event = #PB_Event_CloseWindow

   
    
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Light Direction

Post by J@ckWhiteIII »

Great example, it helped me alot!

So, you made the light rotate on a certain radius (gonna need that later,too). But i need the light to always "look" into the same direction as the camera does.
it seems like a directional light looks down as default. so I'd probably have to use the lightlookat() before. if a light is attached to a node, that is attached to another node, the light would do everything relative to the node, right?
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Light Direction

Post by J@ckWhiteIII »

This is the link to download most of the resources and the actual game. you might have to change the paths. the rest of the resources should be in the Purebasic\Examples\3D... directory, as i only slightly modified an example.
This is the link:

https://dl.dropbox.com/u/76620573/Nightmare%20House.rar

Oh, and I'm experimenting a bit in that example...
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Light Direction

Post by applePi »

i have spent 1 minute before recognizing that i am inside a room since it is too dark, if you delete #PB_Light_Directional will be more suitable at first. i like the ball shooting physics.
about "LightLookA" yes it is great , i have made an experiment:
edit my example above :
edit CreateLight to have #PB_Light_Directional
CreateLight(#LIGHT,RGB(255,255,255),-100,40,30,#PB_Light_Directional )
now comment LightLocate(#LIGHT, ...) and add instead of it:
LightLookAt(#LIGHT, Sin(count+#PI/2) * 30, Sin(count+#PI/2) * 30, Cos(count+#PI/2) * 30)
the light are stable at fixed position -100,40,30 but it is looking at points in an orbit defined by the the formula (i have changed Y from fixed to variable)
and now you will see the light some times strong and sometimes fade away as it moves its direction.
if you have used #PB_Light_Point you will not notice the light fading since the light spread in all directions.
but #PB_Light_Spot there is an example LightLookAt.pb wich demonstrate it, i will look at it later.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Light Direction

Post by J@ckWhiteIII »

Checked lightlookat.pb and edited your example. I've seen the light moving, looks cool.
Now there's still the node problem .__. as you can see, I have tried to rotate the \Light node but it is completely fucked up. Can you have a look at it? that would be very nice
I keep trying to fix it
Post Reply