Page 1 of 1

3D light not working as expected

Posted: Tue Jun 06, 2023 11:38 pm
by williamvanhoecke
Hello,
1) I am not familiar with 3D programming, so I have a lot of questions, but the purebasic help-file does not explain a lot.
f.i.
the statement LightDirection(#Light, x, y, z) where x,y,z have values between -1 and 1 ???? what does the mean.
and what is the difference with the statement LightLookAt(#Light, x, y, z). etc....

Is there any comprehensive helpfile about the engine3D statements.

2)
In below example the mouse moves the CAMERA together with the LIGHT (black square) so the the top of the walls are highlighted
Anyone any idee why the longest horizontal wall is in the middel NOT affected by the light ?

Code: Select all

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

  OpenWindow(0, 0, 0, 1600, 900, "Cube example", #PB_Window_SystemMenu)
  OpenWindowedScreen(WindowID(0), 0, 0, 1600, 900, 0, 0, 0)

      ;CreateMaterial()
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): glass = CreateMaterial(#PB_Any, TextureID(0))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): red = CreateMaterial(#PB_Any, TextureID(0))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): green = CreateMaterial(#PB_Any, TextureID(0))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): Blue = CreateMaterial(#PB_Any, TextureID(0))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): Black = CreateMaterial(#PB_Any, TextureID(0))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): White = CreateMaterial(#PB_Any, TextureID(0)): SetMaterialColor(White,#PB_Material_DiffuseColor,RGB(255,255,255))
      CreateTexture(0, 128, 128): StartDrawing(TextureOutput(0)): Box(0, 0, 128, 128, RGB(255, 255, 255)): StopDrawing(): Yellow = CreateMaterial(#PB_Any, TextureID(0))
      
      
      ;camera
      camX.f = 0: camY.f = 2645: camZ.f = 0
      WH.f=250
      Camera = CreateCamera(#PB_Any,0,0,100,100)
      
      ;lights
      CreateLight(0, RGB(255, 255, 255), 0, WH, 0,#PB_Light_Point)
      ;CreateLight(1, RGB(255, 255, 255), 0, WH+80, 0,#PB_Light_Point)
      
      ;wall
      cube_mesh = CreateCube(#PB_Any,1)

      ;Ground
      CreateTexture(1, 128, 128): StartDrawing(TextureOutput(1)): Box(0, 0, 128, 128, RGB(180, 180, 180)): Box(0, 0, 64, 64, RGB(220, 220, 220)): Box(64, 64, 64, 64, RGB(120, 120, 120)): StopDrawing()
      CreateMaterial(1, TextureID(1))
      SetMaterialColor(1,#PB_Material_AmbientColor,RGB(0,0,255))
      CreatePlane(1, 2980, 2107, 50, 50, 50, 50)
      Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(1))
      EntityRenderMode(Ground,#PB_Shadow_None )
      
      ;create wall 
      wallclr = white
      ;wallclr = red
      
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), 0,WH/2,-210):ScaleEntity(Wall,400,WH,40)
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), 0,WH/2,210):ScaleEntity(Wall,400,WH,40)
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), -210,WH/2,0):ScaleEntity(Wall,400,WH,40): RotateEntity(Wall,0,90,0)
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), 210,WH/2,0):ScaleEntity(Wall,400,WH,40): RotateEntity(Wall,0,90,0)
      
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), 0,WH/2,0):ScaleEntity(Wall,2000,WH,40)
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), 1010,WH/2,0):ScaleEntity(Wall,400,WH,40): RotateEntity(Wall,0,90,0)
      Wall = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(wallclr), -1010,WH/2,0):ScaleEntity(Wall,400,WH,40): RotateEntity(Wall,0,90,0)
     
      cam1.f = CreateEntity(#PB_Any, MeshID(cube_mesh), MaterialID(black), 0,5000,0):ScaleEntity(cam1,20,1,20) :RotateEntity(cam1,0,45,0)
      
      Repeat
        Repeat: Event = WindowEvent(): Until Event = 0
        ExamineKeyboard()
        ExamineMouse()
          CamX -MouseDeltaX(): CamZ -MouseDeltaY(): camY -(MouseWheel()*20)
          MoveCamera(Camera,camX,camY,camZ, #PB_Absolute)
          CameraLookAt(Camera,CamX,0,CamZ-1)
          MoveLight(0,camX,WH+120,camZ, #PB_Absolute)
          LightLookAt(0,camX,0,camZ)
          MoveEntity(cam1,camX,1000,camZ, #PB_Absolute)
          RenderWorld(40)
          FlipBuffers()
        
      Until KeyboardPushed(#PB_Key_Escape)
      ReleaseMouse(#True)  
      CloseWindow(0)

Re: 3D light not working as expected

Posted: Wed Jun 07, 2023 4:55 pm
by pf shadoko
1) yeah, the 3d doc is imprecise and sometimes out of date
I suggested to Fred to redo it (with examples) but no answer...
lightdirection: it's badly explained: in 3D you often need a normalized vector (length =1), here it's not necessary

2) ogre's standard materials work with vertex lighting (a bit complicated if you're just starting out).
this is particularly noticeable when the vertices are widely spaced (as in your example)
you can solve this problem by using shaders
ex:
White = CreateShaderMaterial(#PB_Any, #PB_Material_PerpixelShader)
MaterialShaderTexture(White,TextureID(0),0,0,0)

Re: 3D light not working as expected

Posted: Wed Jun 07, 2023 7:44 pm
by Fred
pf shadoko wrote: Wed Jun 07, 2023 4:55 pm 1) yeah, the 3d doc is imprecise and sometimes out of date
I suggested to Fred to redo it (with examples) but no answer...
lightdirection: it's badly explained: in 3D you often need a normalized vector (length =1), here it's not necessary
The doc is available for edition by anyone, so if you want to redo the 3D part you can do it: https://github.com/fantaisie-software/p ... on/English

Re: 3D light not working as expected

Posted: Wed Jun 07, 2023 9:53 pm
by williamvanhoecke
Thanks for your effort pt shadoko, your proposel realy fixed my problem, however I have no clue on how or what did it.

What is CreateShaderMaterial() ? all the help-file says is 'Creates a new shader based material.' mmm... to be expected
Then CreateShader() says 'Creates a new shader using the specified vertex and fragment programs'. here also, I excepted so much, but WHAT does a shader do.
What does the parameter #PB_Material_PerpixelShader mean or #PB_Material_BumpShader or #PB_MaterialShader_CubicEnvBumpShader etc... etc...

Unlike all the powerbasic statements, which are ver good explained, the Engine3D statements seem much more complicated and NOT EXPLAINED at all.
With this poor help-file it is a lot of trial and error and it takes a very long time to a accomplish little.

Re: 3D light not working as expected

Posted: Wed Jun 14, 2023 11:23 am
by pf shadoko
@fred:
c'est un gros boulot, en échange j'aurais aimé, au minimum, que tu répondes à mes questions concernant la doc
de plus, la doc de mes fonctions 3D sont généralement retoquées, c'est souvent mérité, mais parfois ça veux plus rien dire (ex : genericjoint : transfromx/y/z, c'est quoi ce charabia ?)

@ williamvanhoecke:
there's an error in the doc
the CreateShader (shaderid) enumeration should be in
CreateShaderMaterial
for more info on PB shaders :
viewtopic.php?t=79409