Page 2 of 2

Re: 3d point and line graphs in PB

Posted: Fri Jul 20, 2018 3:29 pm
by applePi
because the DK_PETER texture is designed as a square with color green and he put over it another smaller square with color black so in result the whole texture appears as having edges. and when combining a transparency we see it as lines only
the texture coordinates used by "MeshVertexTextureCoordinate(u, v) for every vertex and designed for the square, but it does not does display the Lines over the pyramid triangles with the current u,v texture coordinates but it display it on the Base since it is square.
note that the texture coordinates are going like this over a picture:
(0,0) ...... (1,0)
.
.
.
(0,1) ...... (1,1)

(in reverse of the usual Cartesian coordinates)

note that in the data the item Data.f 0.0,0.5,0.0 is for the pyramid apix
you can even make extremely thick lines by changing line

Code: Select all

RoundBox(24, 24, 976, 976, 10, 10, $0)
with

Code: Select all

RoundBox(100, 100, 800, 800, 10, 10, RGB(0,0,0))
the data section from Front to bottom: (what is changed is only the u,v coordinates, it is one way, other approaches may be better)

Code: Select all

;-Front
  Data.f 0.5,-0.5,0.5   ; position
  Data.l $FFFFFF        ; color
  Data.f 0,0           ; UVCoordinate
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2         ; Face
  
  ;-Back
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2
  
  ;-Left
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2
  
  ;-Right
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f 0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2

Re: 3d point and line graphs in PB

Posted: Fri Jul 20, 2018 9:42 pm
by Comtois
If you dont need texture, you can use PB_Mesh_LineStrip or #PB_Mesh_LineList

https://www.purebasic.com/documentation ... l2.pb.html

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ; Create a pyramid, manually
    ;
    
    Color1 = RGB(255, 0, 0)
    Color2 = RGB(0, 255, 0)
    Color3 = RGB(0, 0, 255)
    CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
    
    ;Base
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.5,-0.5,0.5) 
    MeshVertexColor(Color1)
    
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexColor(Color2)
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexColor(Color2)
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexColor(Color2)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexColor(Color3)
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexColor(Color3)
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexColor(Color3)
    
    
    FinishMesh(#False)
    
    ;- Material
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
    SetMeshMaterial(0, MaterialID(0))
    CreateNode(0)
    AttachNodeObject(0, MeshID(0))
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 2, #PB_Absolute)
    
    CreateLight(0, RGB(255,255,255), 300, 600, -100)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      ExamineMouse()
      
      ExamineKeyboard()
      
      RotateNode(0, 1, 1, 1, #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

Re: 3d point and line graphs in PB

Posted: Sun Jul 22, 2018 4:12 pm
by applePi
at last i have found the coordinates of a triangular pyramid when we position its Base center at 0,0,0 . its top can be positioned any where vertically
i forgot the coordinates geometry, but this triangle gives me a clue
https://math.stackexchange.com/question ... -equilater

to be sure the pyramid rotates around its base center without error look at it (the camera) from above by uncommenting line 26
to see it as textured and not as a wire comment line 18
the purpose of the small sphere is to mark the center of the scene

Code: Select all

InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  
  ; get screen size
  ExamineDesktops()
  DesktopW = DesktopWidth(0)
  DesktopH = DesktopHeight(0)
  
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "triangular pyramid")
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
       
  CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
  MaterialCullingMode(0, #PB_Material_NoCulling)
  MaterialShadingMode(0, #PB_Material_Wireframe)


  ;DisableMaterialLighting(0, #True)
    
  
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 0, 3, 10, #PB_Absolute)
  ;MoveCamera(0, 0, 10, 0.01, #PB_Absolute)
  CameraLookAt(0,0,0,0)
  CameraBackColor(0, RGB(155, 155, 255))
  CreateLight(0, RGB(255,255,255),0,50,0)
  LightLookAt(0, 0,0,0)
 
  CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Static)
  SetMeshMaterial(0, MaterialID(0))
  
  MeshVertexPosition(-2, 0, 1)
  ;MeshVertexColor(RGB(255,255,255)) 
  MeshVertexTextureCoordinate(0,0)
  
  MeshVertexPosition(2, 0, 1)
  ;MeshVertexColor(RGB(255,255,255)) 
  MeshVertexTextureCoordinate(0,1)
  
  MeshVertexPosition(0, 0, -2)
  ;MeshVertexColor(RGB(255,255,255)) 
  MeshVertexTextureCoordinate(1,1)
  
  MeshVertexPosition(0, 2, 0) ; the top vertex ; change 2 to any value
  ;MeshVertexColor(RGB(0,255,0))
  MeshVertexTextureCoordinate(1,0)
  
  MeshFace(0,1,2)
  MeshFace(0,1,3)
  MeshFace(1,2,3)
  MeshFace(2,0,3)
  FinishMesh(#True)
  
  
  CreateEntity(0, MeshID(0), #PB_Material_None )
    
  CreateSphere(1,0.1)
  CreateEntity(1, MeshID(1), #PB_Material_None, 0,0,0)
  ;http://eusebeia.dyndns.org/4d/tetrahedron
  ;https://math.stackexchange.com/questions/755312/show-that-two-points-from-four-are-at-a-distance-leq-sqrt3-in-an-equilater
  Repeat
    Repeat
      Event = WindowEvent()
    Until Event = 0
    ExamineKeyboard()
    RotateEntity(0, 0, 1, 0, #PB_Relative)
    
    RenderWorld()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  
  

Re: 3d point and line graphs in PB

Posted: Sat Aug 04, 2018 12:16 pm
by doctornash
Thanks applePi. I've had a go at creating meshes with Paul Bourke's published co-ordinates for a variety of polygons, and they've worked out well. In case anyone else finds them handy:
http://paulbourke.net/geometry/platonic/
http://paulbourke.net/geometry/spacefill/
Another question: How to swap the mesh of one object with another 'on the fly'? So, as an object is rotating, the user selects another and then the chosen one replaces the current one in the window. Not sure from where to do a call to a procedure containing the CreateMesh() definition for the new object.

Re: 3d point and line graphs in PB

Posted: Sat Aug 04, 2018 7:41 pm
by doctornash
Just resolved:
How to swap the mesh of one object with another 'on the fly'?
By simply calling a Procedure like the following upon change of a GadgetState. Was getting a blank in the image window previously as had not attached the mainnode to the newly created mesh :oops:

Code: Select all

Procedure Tetrahedron()
  
 Protected a.f = 0.5
 
    Color1 = RGB(255, 0, 0)

    CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
    
    MeshVertexPosition(a,a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,a,-a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a,a, -a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(a,a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(a,a, a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,a,-a)
    MeshVertexColor(Color1)
    
    FinishMesh(#False) 
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
    SetMeshMaterial(0, MaterialID(0))
    mainnode = CreateNode(#PB_Any,0, 0, 0)
    AttachNodeObject(mainnode, MeshID(0))

EndProcedure  

Re: 3d point and line graphs in PB

Posted: Sun Aug 05, 2018 5:11 am
by applePi
doctornash:
How to swap the mesh of one object with another 'on the fly'?
use copyMesh
copyMesh(0,1) copy 0 to 1
to swap 2 meshes we need a third mesh as a mediator
to select object with a mouse look example PointPick.pb from PB examples.
here is a cube, cylinder , sphere
we want to swap (cube, cylinder) every time we press a space key:

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

OpenWindow(0, 0, 0, 800, 600, "press 'Space' to swap cube with cylinder", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()
    
    
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 3, 15)
CameraLookAt(0, 0,0,0)
CreateLight(1, RGB(220,20,120), 0,100,20)


CreateCube(0, 1)
CreateEntity(0, MeshID(0), #PB_Material_None, -4, 0, 0)

CreateCylinder(1, 1, 1)
CreateEntity(1, MeshID(1), #PB_Material_None, 0, 0, 0)

CreateSphere(2, 1) 
CreateEntity(2, MeshID(2), #PB_Material_None, 5, 0, 0)

Repeat
    Repeat
    event = WindowEvent()
  Until event = 0
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Space)
   CopyMesh(0, 2) ; storing mesh 0 in 2
   CreateEntity(2, MeshID(2), #PB_Material_None, 5, 0, 0)
   
   CopyMesh(1, 0) ; copy mesh 1 to 0
   CreateEntity(0, MeshID(0), #PB_Material_None, -4, 0, 0)
   
   CopyMesh(2, 1) ; copy mesh 2 to 1
   CreateEntity(1, MeshID(1), #PB_Material_None, 0, 0, 0)
  EndIf

  RotateEntity(0, 0,1,1, #PB_Relative)
  RotateEntity(1, 0,-1,-1, #PB_Relative)
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)
about platonic objects look:
viewtopic.php?f=36&t=58273&p=437581#p437736

Re: 3d point and line graphs in PB

Posted: Wed Sep 05, 2018 7:31 am
by doctornash
Thanks so much applePi, Comtois and DK_Peter for your invaluable assistance with 3D polygon programming (in which I had no experience prior)!
It helped me turn into reality the idea of generating a changing, in-sync sound waveform from the motion of the vertices of a 3D object :D
The app code and a link to a video showcasing it, is posted at this Announcement thread:
viewtopic.php?f=14&t=71341