Page 1 of 1

Camera Orientation

Posted: Fri Dec 25, 2015 7:09 am
by S.T.
Question:
After a camera is created and camera Z direction is set using CameraLookAt function, then how do you set the direction of the X axis of the cameral relative to the World coordinate system? I want to make X parallel to a known vector but CameraLookAt does not take more than coordinates of the point that the camera is looking at. How do you do this?

Thanks

Re: Camera Orientation

Posted: Sun Dec 27, 2015 2:29 pm
by applePi
i hope you mean camera direction !!, if this is the case then may be this talk useful since i know nothing about the Quaternions (look FetchOrientation.pb example).
the zero of the world coordinates (0,0,0) is inside the room in the example below, it is like the zero of longitudes in Greenwich city. the camera move outside the screen is positive, while toward the screen is negative ,.
the manual said the CameraDirection is The direction vector (usually values between -1.0 and 1.0)
in the example below we position the camera at (-10,0,10) (the end scene camera is just a grand watcher to what happened and not related to this talk):

Code: Select all

MoveCamera(0,-10,0,10,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
then we get its direction:

Code: Select all

vecX.f=CameraDirectionX(0)
vecY.f=CameraDirectionY(0)
vecZ.f=CameraDirectionZ(0)
we position a small sphere at the position of the camera (-10,0,10) as a flag
the vector here (vecX, vecY, vecZ) : its (practical) meaning is: it is the amount of x,y,z displacement the camera will move one unit in the space, look at the picture below, the big red point represent the (vecX, vecY, vecZ) displacement , if you uncommented line 64 Debug vecX:Debug vecY:Debug vecZ you will get 0.7071068, 0, -0.7071068, if the camera displaced by these values it will move one unit. the proof from the Pythagoras : 0.707^2 + 0.707^2 = 1 . the coordinates of the red point is (CameraX+vecX, CameraY+vecY, CameraZ+vecZ). in this way we are able to draw a line from the camera to that point. and to amplify the line we multiply the VecX, VecY, VecZ by 20
Image

not that infinite number of lines can have the vector (vx,vy,vz) and all are looking toward the same direction but not the same point.

Code: Select all

Macro Text3D(No, Texte, Color, Alignment)
  CreateText3D(No, Texte)
  Text3DColor(No, Color)
  Text3DAlignment(No, Alignment)
EndMacro

Declare Room()
Declare CreateMatrix()
Declare DrawCoordinates()

Global.s text

#CameraSpeed = 1

Enumeration
  
  #mesh
  #entity
  #tex
  #light
  #camera
  #wait

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

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

;OpenWindow(0,0,0,ScreenX,ScreenY,"3D Camera research",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindow(0,0,0,ScreenX,ScreenY,"3D Camera research, .... change camera view using keys 1/2")
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)
CreateMaterial(100, LoadTexture(100, "Wood.jpg"))


Room() ; call the room building


;Create Light
CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)

MoveCamera(0,-10,0,10,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)

vecX.f=CameraDirectionX(0)
vecY.f=CameraDirectionY(0)
vecZ.f=CameraDirectionZ(0)
;Debug vecX:Debug vecY:Debug vecZ
CreateSphere(2000,0.5); move the sphere to the camera position (-10,0,10)
CreateEntity(2000, MeshID(2000),  MaterialID(100) , -10,0,10)

;draw a line from the camera position to the vector*20
CreateLine3D(200, -10,0,10, RGB(0,255,0), -10+vecX*20, 0+vecY*20, 10+vecZ*20, RGB(0,255,0))


AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "geebee2.bmp"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)


CreateMatrix()

temp=1
DrawCoordinates() ; draw X,Y,Z lines

MoveCamera(0,-10,11,53,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX.f = #CameraSpeed
        Else
          KeyX.f = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY.f = #CameraSpeed
        Else
          KeyY.f = 0
        EndIf
        
      If KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
        temp ! 1
      EndIf
      
      If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  
      EndIf 
      
      If KeyboardReleased(#PB_Key_1);change camera view from the top of the room
        MoveCamera(0,  0, 60, 0.000001,#PB_Absolute)
        CameraLookAt(0,  0, 0, 0)
      ElseIf KeyboardReleased(#PB_Key_2) ;;change camera view as usual
        MoveCamera(0,-10,11,53,#PB_Absolute)
        CameraLookAt(0, 0, 0, 0)
      EndIf
      
  roty - temp : ;roty-temp: roty-temp
  
  RotateEntity(#entity, rotx+0, roty/2+0, rotz+90,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      
  RenderWorld()
  
  FlipBuffers()
  

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.f a, b, x, y, z, t
  Protected n.l

a.f = 0.2 :b.f=0.8 : n=20: t.f


While t <= 2*#PI
          ;parametric equations for the Toroidal spiral
          x=(a*Sin(n*t)+b)*Cos(t)
          y=(a*Sin(n*t)+b)*Sin(t)
          z=a*Cos(n*t)

                          
        MeshVertexPosition(x, y, z)
        MeshVertexColor(RGB(0,255,0))
      
    t + 0.01
  Wend
  ;************************************************************
  

EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static) ; #PB_Mesh_LineStrip to joint the points with lines
  ;CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static) ; to draw just points
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,-4,0)
  
  ScaleEntity(#entity,4,4,4)
  
EndProcedure

Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,200))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure


Procedure DrawCoordinates()
CreateLine3D(100, -100, 0, 0, RGB(0,255,0), 100, 0, 0, RGB(0,255,0))
CreateLine3D(101, 0, -100, 0, RGB(0,0,255), 0, 100, 0, RGB(0,0,255))
CreateLine3D(102, 0, 0, 100, RGB(255,0,0), 0, 0, -100, RGB(255,0,0))

Text3D(100, "X", RGBA(255, 20, 250, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(100, 5, 5, 0) 
MoveText3D(100, 17, 0, 0)

    
Text3D(101, "Y", RGBA(0, 255, 255, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(101, 5, 5, 0) 
MoveText3D(101, 0, 15, 0)

Text3D(102, "Z", RGBA(255, 0, 0, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(102, 5, 5, 0) 
MoveText3D(102, 0, 0, 60)
EndProcedure



      

Re: Camera Orientation

Posted: Fri Jan 01, 2016 8:27 pm
by S.T.
Thanks ApplePi,
I tried your code, compiler could not find line 26 function, GetSystemMetrics().
I see that PueBasic help for CameraDirectionX() says:

Return value

Returns the 'x' direction vector of the camera. This value is always between -1.0 and 1.0.

However a vector cannot be specified by a number, which is just a scaler. A vector must be defined by its 3 components. I think PureBasic help needs to fill the missing information for vector definition.

Am I missing something? Any comment?

Re: Camera Orientation

Posted: Sat Jan 02, 2016 11:23 am
by applePi
Hi S.T, i think it is an API function since there is an underscore as its suffix, usually i use previously available code from other users and relics from their code still there, and also using api functions will not run on linux. sorry for that, the code corrected.
However a vector cannot be specified by a number, which is just a scaler. A vector must be defined by its 3 components.
i think in purebasic and may be other programming languages are using the practical way to deal with vectors , and my approach described above are trying to decipher the subject and it seems to me successfull, as an example my approach to normals and tangents http://purebasic.fr/english/viewtopic.php?f=36&t=62257 was successful to position the spikes over sphere, and over a knot surface perpendicularly using my deciphering of the normals vector.
in concise as i said the vector defined here from the point of view of how to plot it in practice and not as it is described in math books but it is the essence of what is said in math books which are abstract. so when we say the vector (vx, vy, vz) it means the displacement needed in x,y,z directions to move any point one unit only to the target point labeled as (vx, vy, vz)

Code: Select all

Macro Text3D(No, Texte, Color, Alignment)
  CreateText3D(No, Texte)
  Text3DColor(No, Color)
  Text3DAlignment(No, Alignment)
EndMacro

Declare Room()
Declare CreateMatrix()
Declare DrawCoordinates()

Global.s text

#CameraSpeed = 1

Enumeration
  
  #mesh
  #entity
  #tex
  #light
  #camera
  #wait

EndEnumeration

;ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
;ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

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

;OpenWindow(0,0,0,ScreenX,ScreenY,"3D Camera research",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
;OpenWindow(0,0,0,ScreenX,ScreenY,"3D Camera research, .... change camera view using keys 1/2")
;OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "3D Camera research, .... change camera view using keys 1/2")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0, #PB_Screen_SmartSynchronization)
  
  
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)
CreateMaterial(100, LoadTexture(100, "Wood.jpg"))


Room() ; call the room building


;Create Light
CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(0,0,0,100,100)

MoveCamera(0,-10,0,10,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)

vecX.f=CameraDirectionX(0)
vecY.f=CameraDirectionY(0)
vecZ.f=CameraDirectionZ(0)
;Debug vecX:Debug vecY:Debug vecZ
CreateSphere(2000,0.5); move the sphere to the camera position (-10,0,10)
CreateEntity(2000, MeshID(2000),  MaterialID(100) , -10,0,10)

;draw a line from the camera position to the vector*20
CreateLine3D(200, -10,0,10, RGB(0,255,0), -10+vecX*20, 0+vecY*20, 10+vecZ*20, RGB(0,255,0))


AmbientColor(RGB(255,255,255))
CreateMaterial(#tex, LoadTexture(#tex, "geebee2.bmp"))
MaterialCullingMode(#tex, #PB_Material_NoCulling)


CreateMatrix()

temp=1
DrawCoordinates() ; draw X,Y,Z lines

MoveCamera(0,-10,11,53,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX.f = #CameraSpeed
        Else
          KeyX.f = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY.f = #CameraSpeed
        Else
          KeyY.f = 0
        EndIf
        
      If KeyboardReleased(#PB_Key_Space) ;press key to toggle the rotation of the object
        temp ! 1
      EndIf
      
      If KeyboardReleased(#PB_Key_H) ;press H to toggle Hide / UnHide of the room
        HideFlag ! 1
        For i=100 To 104
          HideEntity(i, HideFlag)
        Next  
      EndIf 
      
      If KeyboardReleased(#PB_Key_1);change camera view from the top of the room
        MoveCamera(0,  0, 60, 0.000001,#PB_Absolute)
        CameraLookAt(0,  0, 0, 0)
      ElseIf KeyboardReleased(#PB_Key_2) ;;change camera view as usual
        MoveCamera(0,-10,11,53,#PB_Absolute)
        CameraLookAt(0, 0, 0, 0)
      EndIf
      
  roty - temp : ;roty-temp: roty-temp
  
  RotateEntity(#entity, rotx+0, roty/2+0, rotz+90,#PB_Absolute)
  
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      
  RenderWorld()
  
  FlipBuffers()
  

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawMatrix()
  Protected.f a, b, x, y, z, t
  Protected n.l

a.f = 0.2 :b.f=0.8 : n=20: t.f


While t <= 2*#PI
          ;parametric equations for the Toroidal spiral
          x=(a*Sin(n*t)+b)*Cos(t)
          y=(a*Sin(n*t)+b)*Sin(t)
          z=a*Cos(n*t)

                          
        MeshVertexPosition(x, y, z)
        MeshVertexColor(RGB(0,255,0))
      
    t + 0.01
  Wend
  ;************************************************************
  

EndProcedure  

Procedure CreateMatrix()
  
  CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static) ; #PB_Mesh_LineStrip to joint the points with lines
  ;CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static) ; to draw just points
  DrawMatrix()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(#tex))
  CreateEntity(#entity, MeshID(0), MaterialID(#tex),0,-4,0)
  
  ScaleEntity(#entity,4,4,4)
  
EndProcedure

Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
  
  
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,200))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure


Procedure DrawCoordinates()
CreateLine3D(100, -100, 0, 0, RGB(0,255,0), 100, 0, 0, RGB(0,255,0))
CreateLine3D(101, 0, -100, 0, RGB(0,0,255), 0, 100, 0, RGB(0,0,255))
CreateLine3D(102, 0, 0, 100, RGB(255,0,0), 0, 0, -100, RGB(255,0,0))

Text3D(100, "X", RGBA(255, 20, 250, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(100, 5, 5, 0) 
MoveText3D(100, 17, 0, 0)

    
Text3D(101, "Y", RGBA(0, 255, 255, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(101, 5, 5, 0) 
MoveText3D(101, 0, 15, 0)

Text3D(102, "Z", RGBA(255, 0, 0, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered)  
ScaleText3D(102, 5, 5, 0) 
MoveText3D(102, 0, 0, 60)
EndProcedure

Re: Camera Orientation

Posted: Sat Jan 02, 2016 3:26 pm
by S.T.
Thanks applePi,
I could see the result of your code in Windows, it was nice. I understand how you use mouse movement to rotate the camera. This works for cases that you don't need to know exact orientation of the camera. User can play with mouse and look at the scene. However it is part of capabilities of 3D programming to be able to control not only position but also orientation of the camera. I requested this to be added in the documentation also a function that allows to set the orientation. Rotation about 3 main axes is not very practical as predicting the result is difficult. You want a control like when you take a real camera, you can look at the subject and if needed rotate the camera with respect to your eyes! Let us expect more!

Thanks

Re: Camera Orientation

Posted: Mon Jan 25, 2016 3:41 am
by S.T.
I do not see any action or response to my request for fixing the error that I mentioned here. I need to position a camera at a precise location and that is possible in concept in 3D programming. Yes, I mentioned the example posted was "nice" but that still cannot position a camera correctly and with control. That style does not eliminate the need for the suggested corrections.

Re: Camera Orientation

Posted: Fri Jan 29, 2016 11:24 pm
by mpz
Hi S.T.,

i think the problem is nobody understand what you want. I hope i understand it an can explain what you must do. You have a 3d world with x,y and z coords. In this world the coords are fixed. It is not possible to "move a vector" to the x direction of a world or "camera". What you can make is to calculate the 2d vector in a 3d room. Then you can move an turn the camera in the direction of the vector.

First step find 2 x 3D points of the vector. Then you have the camera point and the lookat camera point. You can turn the z - axis of the camera if you need. For the movement along the vector you need to calculate the angles of the camera. You see 3d is only mathematic ;) The next step it the movement of the camera, and this show you the code of applePi.

Ho to calculate a vector in the 3d room shows you the following side:
http://www.intmath.com/vectors/7-vector ... -space.php

A 3d engine can help you with the movement , but the mathematic is important for special cases...

Greetings Michael

Re: Camera Orientation

Posted: Sat Jan 30, 2016 2:47 am
by S.T.
Hi Michael,
3D visualization is all about working with different coordinate systems. You have a World coordinate system and you place other entities in this World using the local coordinate system of that entity. Camera is one entity and its orientation must be defined with respect to the World. There must be commands capable of fully defining a local coordinate system in the World. There is no non-mathematical way. Any correct way is based on vector analysis. What I was saying is that I did not find enough commands in PureBasic to define all three components of the local coordinate system of a camera and the ones that appear to be intended for this purpose are not defined correctly.
CameraDirectionX(0) is a vector not a number, how it is defined with just one value? A vector has a direction in space. How can you define the direction of the X axis of a camera with just one number? These are basic flaws that should be fixed.
Thanks for spending time and sending me your thoughts.

Re: Camera Orientation

Posted: Sat Jan 30, 2016 9:02 am
by Samuel
S.T. wrote: CameraDirectionX(0) is a vector not a number, how it is defined with just one value? A vector has a direction in space. How can you define the direction of the X axis of a camera with just one number? These are basic flaws that should be fixed.
Well, I haven't used any CameraDirection commands in a while, but the only thing it should be returning is a normalized value between -1 and 1. Anything else would be wrong in my opinion.

For example let's say you got the directions and stored them in a couple of variables.
Like so.
CamDirX = CameraDirectionX(#Camera)
CamDirY = CameraDirectionY(#Camera)
CamDirZ = CameraDirectionZ(#Camera)

Now let's say those three values equaled the following.
CamDirX = 0
CamDirY = -0.707
CamDirZ = 0.707
There ya go it's reporting that our camera is looking downwards on a 45 degree slope on the Y and Z axis.

With that said pretty much all 3D engines have these four basic camera controls. With these commands you should be able to move your camera anywhere in 3D space and have it look down any direction.
MoveCamera which allows you to change the camera's position.
CameraDirection which allows you to change the camera's directional vector.
RotateCamera which allows you to change your camera's direction based on a set of degrees.
CameraLookAt which changes the camera's direction to look at a specific point.

What more could you want?



EDIT:
S.T. wrote: CameraDirectionX(0) is a vector not a number.
I see where you might have gotten confused. CameraDirectionX just returns the X portion of the vector. You need to use CameraDirectionY and CameraDirectionZ (like in the little example I showed above) in order to get the complete vector.

Re: Camera Orientation

Posted: Sat Jan 30, 2016 5:55 pm
by mpz
Hi S.T.,

it is not so much complicate. We get the Webside with

http://www.intmath.com/vectors/7-vector ... -space.php

Here you have the vector P(2,3,5). That mean you have

P0(0,0,0) and P1(2,3,5)

Now you can use

MoveCamera(#Kamera,2,3,5,#PB_Absolute)
CameraLookAt(#Kamera, 0, 0, 0)

for the camera and the movement in the vector direction is

MoveCamera (#Kamera,x,y,z) ; The relative movement

if you want to have the camera in another place of the world you can also use the CameraDirection() comand

you need the normal of the vector and a point in the world

Code: Select all

    Structure Vector3
      x.f
      y.f
      z.f
    EndStructure

    Procedure Normalize(*V.Vector3)
      Define.f magSq, oneOverMag
     
      magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
      If magsq > 0
        oneOverMag = 1.0 / Sqr(magSq)
        *V\x * oneOverMag
        *V\y * oneOverMag
        *V\z * oneOverMag
      EndIf
    EndProcedure

CamVec.Vector3 ; My camera vector
CamVec\x = 2
CamVec\y = 3
CamVec\z = 5

; CamVec\x = -2 ; other direction
; CamVec\y = -3 ; other direction
; CamVec\z = -5 ; other direction

MoveCamera(#Kamera,23,5,8,#PB_Absolute) ; a absolute point in the world
CameraDirection(#Kamera, CamVec\x, CamVec\y, CamVec\z)
and with

Code: Select all

MoveCamera  (#Kamera,x,y,z) ; The relative movement
can you move the camera.

Here a little example (thank applePi for the first demo code)

greetings Michael

Code: Select all

Macro Text3D(No, Texte, Color, Alignment)
  CreateText3D(No, Texte)
  Text3DColor(No, Color)
  Text3DAlignment(No, Alignment)
EndMacro

Declare DrawCoordinates()

Global.s text

#CameraSpeed = 1
#Moveandlook = 1

Enumeration
  
  #Kamera
  #mesh
  #tex
  #light
  #camera
  #wait

EndEnumeration


    Structure Vector3
      x.f
      y.f
      z.f
    EndStructure

    Procedure Normalize(*V.Vector3)
      Define.f magSq, oneOverMag
     
      magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
      If magsq > 0
        oneOverMag = 1.0 / Sqr(magSq)
        *V\x * oneOverMag
        *V\y * oneOverMag
        *V\z * oneOverMag
      EndIf
    EndProcedure

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

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "3D Camera research, .... change camera view using keys 1/2")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0, #PB_Screen_SmartSynchronization)
 
 
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/fonts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)
CreateMaterial(100, LoadTexture(100, "Wood.jpg"))


;Room() ; call the room building


;Create Light
CreateLight(0,RGB(245,245,205),19,13,0)

;Create Camera
CreateCamera(#Kamera,0,0,100,100)
CameraFOV(#Kamera, 45)

If #Moveandlook = 1

  MoveCamera(#Kamera,2,3,5,#PB_Absolute)
  CameraLookAt(#Kamera, 0, 0, 0)

Else
  
  MoveCamera(#Kamera,2,3,5,#PB_Absolute)
  CamVec.Vector3
  CamVec\x = -2
  CamVec\y = -3
  CamVec\z = -5
  Normalize(CamVec)
  CameraDirection(#Kamera, CamVec\x, CamVec\y, CamVec\z)
  
EndIf  

AmbientColor(RGB(255,255,255))

temp=1
DrawCoordinates() ; draw X,Y,Z lines

Repeat
  WindowEvent()
 
  ShowCursor_(0)
   
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
     
        If KeyboardPushed(#PB_Key_Left)
          KeyX.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX.f = #CameraSpeed
        Else
          KeyX.f = 0
        EndIf
       
        If KeyboardPushed(#PB_Key_Up)
          KeyY.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY.f = #CameraSpeed
        Else
          KeyY.f = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_PageUp)
          KeyZ.f = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_PageDown)
          KeyZ.f = #CameraSpeed
        Else
          KeyZ.f = 0
        EndIf
        
        
        MoveCamera  (#Kamera,Keyx,KeyZ,Keyy)

  RenderWorld()
 
  FlipBuffers()
 

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow


Procedure Room()
  ;Create Red Wall
CreateTexture(1,32,32)
  StartDrawing(TextureOutput(1))
    Box(0,0,32,32,RGB(205,0,0))
  StopDrawing()
  CreateMaterial(1,TextureID(1))
  MaterialCullingMode(1, #PB_Material_NoCulling)
 
 
CreateCube(100,1)
CreateEntity(100,MeshID(100),MaterialID(1),-19,0,0)
ScaleEntity(100,1,33,32)

;Create Green Wall
CreateTexture(2,32,32)
  StartDrawing(TextureOutput(2))
    Box(0,0,32,32,RGB(0,205,0))
  StopDrawing()
  CreateMaterial(2,TextureID(2))
  MaterialCullingMode(2, #PB_Material_NoCulling)

CreateCube(101,1)
CreateEntity(101,MeshID(101),MaterialID(2),19,0,0)
ScaleEntity(101,1,33,32)

;Create Back Wall
CreateTexture(3,32,32)
  StartDrawing(TextureOutput(3))
    Box(0,0,32,32,RGB(250,250,250))
  StopDrawing()
  CreateMaterial(3,TextureID(3))
  MaterialCullingMode(3, #PB_Material_NoCulling)
CreateCube(102,1)
CreateEntity(102,MeshID(102),MaterialID(3),0,0,-16)
ScaleEntity(102,36.5,32,1)


;Create Floor
CreateTexture(5,32,32)
  StartDrawing(TextureOutput(5))
    Box(0,0,32,32,RGB(250,250,200))
  StopDrawing()
  CreateMaterial(5,TextureID(5))
  MaterialCullingMode(5, #PB_Material_NoCulling)
CreateCube(104,1)
CreateEntity(104,MeshID(104),MaterialID(5),0,-16,0)
ScaleEntity(104,36.5,1,32)

EndProcedure


Procedure DrawCoordinates()
CreateLine3D(100, -100, 0, 0, RGB(0,255,0), 100, 0, 0, RGB(0,255,0))
CreateLine3D(101, 0, -100, 0, RGB(0,0,255), 0, 100, 0, RGB(0,0,255))
CreateLine3D(102, 0, 0, 100, RGB(255,0,0), 0, 0, -100, RGB(255,0,0))

Text3D(100, "X", RGBA(255, 20, 250, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered) 
ScaleText3D(100, 5, 5, 0)
MoveText3D(100, 17, 0, 0)

   
Text3D(101, "Y", RGBA(0, 255, 255, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered) 
ScaleText3D(101, 5, 5, 0)
MoveText3D(101, 0, 15, 0)

Text3D(102, "Z", RGBA(255, 0, 0, 255), #PB_Text3D_HorizontallyCentered | #PB_Text3D_VerticallyCentered) 
ScaleText3D(102, 5, 5, 0)
MoveText3D(102, 0, 0, 60)
EndProcedure