3D Coordinate System

Everything related to 3D programming
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

3D Coordinate System

Post by S.T. »

I am trying to use pure basic for 3D visualization in engineering. I learned from the help that coordinate system in PureBasic is a right handed coordinate system. Then I did a simple test:

CreateLine3D(#PB_Any, 0, 0, 0, RGB(255,0,0),100, 0, 0, RGB(255,0,0)) ; X axis
CreateLine3D(#PB_Any, 0, 0, 0, RGB(0,255,0), 0, 100, 0, RGB(0,255,0)) ; Y axis
CreateLine3D(#PB_Any, 0, 0, 0, RGB(0,0,255), 0,0, 100, RGB(0,0,255)) ; Z axis

I pasted these lines into one of the 3D examples that come with PureBasic and saw the result so it can be easily reproduced.
these lines should draw a red line for X, a green line for Y and a blue line for Z axis. However the result is not a "right-hand-rule" system. Z line is appearing in the reverse direction. Where am I making the mistake? Can anybody help on this?

Thanks
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: 3D Coordinate System

Post by STARGÅTE »

I don't know.

If I insert you code lines into an 3D example, I get a correct result:
red is X and to the left direction.
green is Y and to the top direction.
blue is Z and to the front direction.
That's right handed.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

Re: 3D Coordinate System

Post by S.T. »

STARGÅTE wrote:I don't know.

If I insert you code lines into an 3D example, I get a correct result:
red is X and to the left direction.
green is Y and to the top direction.
blue is Z and to the front direction.
That's right handed.
For a right handed system Z should point towards yourself when X is to the right and Y is pointing to top of the page.
Z is in the reverse direction if pointing to the front.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: 3D Coordinate System

Post by STARGÅTE »

S.T. wrote:
STARGÅTE wrote:I don't know.

If I insert you code lines into an 3D example, I get a correct result:
red is X and to the left direction.
green is Y and to the top direction.
blue is Z and to the front direction.
That's right handed.
For a right handed system Z should point towards yourself when X is to the right and Y is pointing to top of the page.
Z is in the reverse direction if pointing to the front.
Yes, and the blue line is point towards yourself.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: 3D Coordinate System

Post by Comtois »

Look at CreateLine3D.pb example

red is X and to the RIGHT direction.
green is Y and to the top direction.
blue is Z and to the front direction.
Please correct my english
http://purebasic.developpez.com/
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

Re: 3D Coordinate System

Post by S.T. »

STARGÅTE wrote:
S.T. wrote:
STARGÅTE wrote:I don't know.

If I insert you code lines into an 3D example, I get a correct result:
red is X and to the left direction.
green is Y and to the top direction.
blue is Z and to the front direction.
That's right handed.
For a right handed system Z should point towards yourself when X is to the right and Y is pointing to top of the page.
Z is in the reverse direction if pointing to the front.
Yes, and the blue line is point towards yourself.
No it is not! It is pointing to the front as you said just above! In addition to PureBasic help you may find the right hand rule in many math books. This is most probably a mismatch between OGRE and PureBasic help.
Last edited by S.T. on Mon Dec 21, 2015 10:10 pm, edited 1 time in total.
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

Re: 3D Coordinate System

Post by S.T. »

Comtois wrote:Look at CreateLine3D.pb example

red is X and to the RIGHT direction.
green is Y and to the top direction.
blue is Z and to the front direction.

Thanks for pointing to the CeateLins3D example. It shows the same thing and Z is to the front but this is not a coordinate system defined by right hand rule in PureBasic help and other math references.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: 3D Coordinate System

Post by STARGÅTE »

Output of the example:
(X, Y, Z)
Image
I don't see a mistake.
It look equal to other right hand definitions.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: 3D Coordinate System

Post by Comtois »

S.T. wrote:Thanks for pointing to the CeateLins3D example. It shows the same thing and Z is to the front but this is not a coordinate system defined by right hand rule in PureBasic help and other math references.
Example show this :
http://www.purebasic.com/documentation/ ... index.html
Please correct my english
http://purebasic.developpez.com/
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

Re: 3D Coordinate System

Post by S.T. »

I see this on a mac laptop:

Image

Which is different with what you get on Windows. What you got is correct but not what I get on mac.
The color of lines are different from colors of the arrows except for green. In fact if you turn off the arrows you see a left-handed coordinate system.
S.T.
User
User
Posts: 16
Joined: Mon Dec 21, 2015 7:43 pm

Re: 3D Coordinate System

Post by S.T. »

Thank you all who helped to track down this bug. I think Fantasia will need to take care of this for mac.
I installed PureBasic as a windows version on my mac as I have also Windows installed and I got what you got.

Now I need to report this as a bug which I don't know how?

Wish a great happy new year for all!
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3D Coordinate System

Post by applePi »

i have noticed this with CreateLine3D long time ago when we compile the code with option opengl
compiler -> compiler options -> Library subsystem : opengl
it produce the same S.T picture. since in mac and linux the opengl is the default
so in mac if you want to draw red (within CreateLine3D function) use rgb(0,0,255)
or use another approach is to draw a line by plotting 2 points with a parameter #PB_Mesh_LineList and it will give the same color whether in windows or mac
these thick lines works only in opengl compiler option, the function which define the thickness is: glLineWidth_(10) ; this is an opengl only function
Image
note the camera position, change it from MoveCamera(#camera, 5, 10, 20, #PB_Absolute)
to MoveCamera(#camera, 5, 10, -20, #PB_Absolute)
and naturally the red X will be in the left if we fixed the camera eye on 0,0,0

Code: Select all

Define.f KeyX, KeyY, MouseX, MouseY
#camera = 0 

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), " ... move/ rotate the cam with arrow keys and mouse", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
;Initialize environment
InitEngine3D()
InitSprite()

OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)

InitKeyboard()
InitMouse()

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
;Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
;Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
;Parse3DScripts()
  
  CreateCamera(#camera, 0, 0, 100, 100)
  MoveCamera(#camera, 5, 10, 20, #PB_Absolute)
  ;CameraFOV(#camera, 70)
  CameraLookAt(#camera,0,0,0)  
      
  CreateMesh(0, #PB_Mesh_LineList , #PB_Mesh_Static)
  ;note above the #PB_Mesh_LineList so a line will be drawn between every pair of points
     
    MeshVertexPosition(0, 0, 0) 
    MeshVertexColor(RGB(255,0,0)) ; color of the above point
    MeshVertexPosition(10, 0, 0)
    MeshVertexColor(RGB(255,0,0))
        
    
    MeshVertexPosition(0, 0, 0)
    MeshVertexColor(RGB(0,255,0)) 
    MeshVertexPosition(0, 10, 0) 
    MeshVertexColor(RGB(0,255,0))
    
    
    MeshVertexPosition(0, 0, 0)
    MeshVertexColor(RGB(0,0,255)) 
    MeshVertexPosition(0, 0, 10) 
    MeshVertexColor(RGB(0,0,255))
    
    glLineWidth_(10) ; this is an opengl only function
    
    FinishMesh(#True)
        
    ;imagine the entity (the 3 lines is painted with white color and then we paint the lines in specific colores
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    SetMeshMaterial(0, MaterialID(0))
    
    CreateEntity(0,MeshID(0),MaterialID(0) ) ; the 3 lines
    
    
    Repeat
      Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.5
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.5
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.5
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.5
        Else
          KeyY = 0
        EndIf
        
        
  
       EndIf
       
        
      RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
           
      RenderWorld()
  
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
    End
or with Screen3DRequester

Code: Select all

IncludeFile #PB_Compiler_Home + "Examples/3D/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
#camera = 0
  
If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
   ; Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
   ; Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
   
  
   
  InitSprite()
  InitKeyboard()
  InitMouse()
  
    
  If Screen3DRequester()
    
    CreateCamera(#camera, 0, 0, 100, 100)
  MoveCamera(#camera, 5, 10, 20, #PB_Absolute)
  ;CameraFOV(#camera, 70)
  CameraLookAt(#camera,0,0,0)  
    
    CreateMesh(0, #PB_Mesh_LineList , #PB_Mesh_Static)
 
    x.f = 0
    y.f = 0
    z.f = 0
     
    MeshVertexPosition(0, 0, 0) 
    MeshVertexColor(RGB(255,0,0))
    MeshVertexPosition(10, 0, 0)
    MeshVertexColor(RGB(255,0,0))
        
    
    MeshVertexPosition(0, 0, 0)
    MeshVertexColor(RGB(0,255,0)) 
    MeshVertexPosition(0, 10, 0) 
    MeshVertexColor(RGB(0,255,0))
    
    
    MeshVertexPosition(0, 0, 0)
    MeshVertexColor(RGB(0,0,255)) 
    MeshVertexPosition(0, 0, 10) 
    MeshVertexColor(RGB(0,0,255))
    
    glLineWidth_(10) ; this is an opengl function
    

    FinishMesh(#True)
    
    ;imagine the entity (the 3 lines is painted with white color and then we paint the lines in specific colores
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    SetMeshMaterial(0, MaterialID(0))
    
    CreateEntity(0,MeshID(0),MaterialID(0) )
    
    
    Repeat
      Screen3DEvents()
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -0.5
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 0.5
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -0.5
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 0.5
        Else
          KeyY = 0
        EndIf
        
        
  
       EndIf
       
        
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
   
      ;RotateEntity(0, 0.5,1,0.5,#PB_Relative)
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
    
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
  
End
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: 3D Coordinate System

Post by Samuel »

@Comtois
Is it possible the lines are somehow using the BGR format on macs? If the actual coordinates are swapped then this wouldn't be the problem, but if they are OK then I'd assume an opposite color format is being used.
I've run into this problem before when using OpenGL directly so I thought I'd throw it out there as a possibility.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: 3D Coordinate System

Post by Samuel »

@S.T.
Could you give this example a try. The cube should be blue, but I'm wondering if it will be red on your mac.

Code: Select all

Global RollAxisX.f
Global RollAxisY.f
Global RollAxisZ.f

Global RotateSpeedX.f = 1.0
Global RotateSpeedY.f
Global RotateSpeedZ.f = 1.0

Global ZoomFactor.f = 1.0 ; Distance of the camera. Negative value = zoom back

Procedure DrawCube(Gadget)
  SetGadgetAttribute(Gadget, #PB_OpenGL_SetContext, #True)
  
  glPushMatrix_()                  ; Save the original Matrix coordinates
  glMatrixMode_(#GL_MODELVIEW)

  glTranslatef_(0, 0, ZoomFactor)  ;  move it forward a bit

  glRotatef_ (RollAxisX, 1.0, 0, 0) ; rotate around X axis
  glRotatef_ (RollAxisY, 0, 1.0, 0) ; rotate around Y axis
  glRotatef_ (RollAxisZ, 0, 0, 1.0) ; rotate around Z axis
 
  RollAxisX + RotateSpeedX 
  RollAxisY + RotateSpeedY 
  RollAxisZ + RotateSpeedZ 

  ; clear framebuffer And depth-buffer

  glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

  glDisable_(#GL_LIGHTING)
  glBegin_  (#GL_QUADS)
  
  glColor3f_  (0.0,0.0,1.0)
  
  glNormal3f_ (0,0,1.0)
  glVertex3f_ (0.5,0.5,0.5)            
  glVertex3f_ (-0.5,0.5,0.5)
  glVertex3f_ (-0.5,-0.5,0.5)
  glVertex3f_ (0.5,-0.5,0.5) 

  glNormal3f_ (0,0,-1.0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (-0.5,0.5,-0.5)
  glVertex3f_ (0.5,0.5,-0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  
  glNormal3f_ (   0, 1.0,   0)
  glVertex3f_ ( 0.5, 0.5, 0.5)
  glVertex3f_ ( 0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)

  glNormal3f_ (0,-1.0,0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (-0.5,-0.5,0.5)

  glNormal3f_ (1.0,0,0)
  glVertex3f_ (0.5,0.5,0.5)
  glVertex3f_ (0.5,-0.5,0.5)
  glVertex3f_ (0.5,-0.5,-0.5)
  glVertex3f_ (0.5,0.5,-0.5)

  glNormal3f_ (-1.0,   0,   0)
  glVertex3f_ (-0.5,-0.5,-0.5)
  glVertex3f_ (-0.5,-0.5, 0.5)
  glVertex3f_ (-0.5, 0.5, 0.5)
  glVertex3f_ (-0.5, 0.5,-0.5)
  
  glEnd_()

  glPopMatrix_()
  glFinish_()

  SetGadgetAttribute(Gadget, #PB_OpenGL_FlipBuffers, #True)
EndProcedure


Procedure SetupGL()
    
  glMatrixMode_(#GL_PROJECTION)
  gluPerspective_(30.0, 200/200, 1.0, 10.0) 
  
  ; position viewer
  glMatrixMode_(#GL_MODELVIEW)
  
  glTranslatef_(0, 0, -5.0)
  
  glEnable_(#GL_DEPTH_TEST)   ; Enabled, it slowdown a lot the rendering. It's to be sure than the
                              ; rendered objects are inside the z-buffer.
  
  glEnable_(#GL_CULL_FACE)    ; This will enhance the rendering speed as all the back face will be
                              ; ignored. This works only with CLOSED objects like a cube... Singles
                              ; planes surfaces will be visibles only on one side.
    
  glShadeModel_(#GL_SMOOTH)
EndProcedure

Procedure HandleError (Result, Text$)
  If Result = 0
    MessageRequester("Error", Text$, 0)
    End
  EndIf
EndProcedure

OpenWindow(0, 0, 0, 600, 400, "OpenGL Gadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

OpenGLGadget(0, 0, 0, 600, 400)
SetupGL()

AddWindowTimer(0, 1, 16) ; about 60 fps

Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Timer
      If EventTimer() = 1
        DrawCube(0)
      EndIf
  EndSelect
  
Until Event = #PB_Event_CloseWindow
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: 3D Coordinate System

Post by Comtois »

Samuel wrote:@Comtois
Is it possible the lines are somehow using the BGR format on macs? If the actual coordinates are swapped then this wouldn't be the problem, but if they are OK then I'd assume an opposite color format is being used.
I've run into this problem before when using OpenGL directly so I thought I'd throw it out there as a possibility.
I dont have mac to test, but i get the bug with opengl subsystem. Should be fixed for next release (if opengl used i swap color)
Please correct my english
http://purebasic.developpez.com/
Post Reply