December 04, 2020, 11:14:20 AM

Author Topic: [bb] 3D line function the sequel by Blitzplotter [ 1+ years ago ]  (Read 721 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 3D line function the sequel
Author : Blitzplotter
Posted : 1+ years ago

Description : Demonstrates the power of Yasha's 3D line code with an array that can be modified by changing the xgap, ygap & zgap parameters.

Thanks to Yasha for posting the 3D line function in the first place, great little slice of code.


Code :
Code: BlitzBasic
  1. ;Global appheight=768
  2. ;Global appwidth=1024
  3. ;Global appdepth=32
  4.  
  5. Global appheight=800
  6. Global appwidth=800
  7. Global appdepth=32
  8.  
  9. AppTitle "Blitzplotter, plotting Balls using an array with a little help from Yasha";,"Are you sure you want to quit?"
  10.  
  11. Local SC_FPS=60 ;Desired framerate
  12. Local rtime=Floor(1000.0/SC_FPS)
  13. Local ctime,limited=True
  14. Local FPScount,FPStime,FPSframes
  15.  
  16. Graphics3D appwidth,appheight,appdepth,6
  17. SetBuffer BackBuffer()
  18.  
  19. ;Create an empty scene
  20. Local centrecam=CreatePivot()
  21. PositionEntity centrecam,0,25,0
  22. Local camera=CreateCamera(centrecam)
  23. PositionEntity camera,0,20,-50,1
  24.  
  25. Local sun=CreateLight()
  26. PositionEntity sun,-100,400,0
  27. PointEntity sun,centrecam
  28.  
  29. Local ground=CreateMesh(),tiles=CreateSurface(ground)
  30. Local v1=AddVertex(tiles,-125,0,150),v2=AddVertex(tiles,125,0,150),v3=AddVertex(tiles,125,0,-100)
  31. AddTriangle(tiles,v1,v2,v3):v2=AddVertex(tiles,-125,0,-100):AddTriangle(tiles,v1,v3,v2)
  32. EntityColor ground,0,0,255
  33. Local block=CreateCube():ScaleMesh block,20,5,20
  34.  
  35. Local linemesh=CreateMesh(camera),linesurf=CreateSurface(linemesh)      ;The surface to use to draw the lines - single surface is always faster
  36. ;Adjust the surface's colour here, or add vertex colours to the drawing function as necessary
  37.  
  38. Local marker1=CreateSphere():PositionEntity marker1,-5,15,-5:EntityAlpha marker1,0.3    ;Mark the ends of the line in 3D space
  39. Local marker2=CreateSphere():PositionEntity marker2,5,25,5:EntityAlpha marker2,0.3
  40.  
  41. ;my marker
  42.  
  43. Local marker3=CreateSphere():PositionEntity marker3,5,35,15:EntityAlpha marker3,0.3
  44.  
  45.  
  46. ;#####      3d point creation array     #####
  47. ;#####                                  #####
  48.  
  49. MAXBALLS#=100
  50.  
  51. ;array to hold a few 3d points
  52. Dim ay(100,2)
  53.  
  54. ;how many balls do you want plotted
  55. Global extrapoints=80
  56.  
  57. If extrapoints>MAXBALLS
  58.         Print"The array is only 100 big... please reduce your balls"
  59.         Print"Press any key"
  60.         WaitKey
  61.         End
  62. EndIf
  63.  
  64.  
  65. ;populate First 10 co-ords of array
  66. xGap=2   ;distance in the xplane between points
  67. yGap=3   ;distance in the yplane between points
  68. zGap=2   ;distance in the zplane between points
  69.  
  70. slope_in_zed=1  ;1 adjusts co-ords in the z plane, 0 doesn't
  71.  
  72. ;populate array with 3d points for a zig zag effect
  73. For x = 1 To extrapoints
  74.         ay(x,0) = x*xGap;  x co-ords
  75.        
  76.        
  77.         If x Mod 2 = 1; y co-ords up and down
  78.                 ay(x,1) = x * -yGap
  79.         Else
  80.                 ay(x,1) = x * yGap
  81.         EndIf
  82.        
  83.         If slope_in_zed=1
  84.                 If x Mod 2 = 1; z co-ords up and down
  85.                         ay(x,2) = x * -zGap
  86.                 Else
  87.                         ay(x,2) = x * zGap
  88.                 EndIf
  89.         Else
  90.                 ay(x,2) = x*zGap; constant z co-ords
  91.         EndIf
  92.  
  93.         ;print out the co-ordinates
  94. ;       Print "x: "+ay(x,0)+" y: "+ay(x,1)+" z: "+ay(x,2)
  95. ;      
  96. ;       Print" "
  97.        
  98. Next
  99.  
  100. ;Print" Press any key...."
  101. Print " "
  102. Print " "
  103. Print " "
  104. ;WaitKey
  105.  
  106. ;#####                             #####
  107. ;#####  end point creation         #####
  108. ;#####                             #####
  109.  
  110.  
  111.  
  112. Dim marker(4)
  113.  
  114. ;this plots the spheres from the points within the array
  115.  
  116. For cz = 1 To extrapoints
  117.        
  118.         Local marker5=CreateSphere():PositionEntity marker5,5+ay(cz,0),35+ay(cz,1),15+ay(cz,2):EntityAlpha marker5,0.3
  119.        
  120. Next
  121.  
  122.  
  123.  
  124. While Not KeyDown(1)
  125.         ctime=MilliSecs()
  126.        
  127.         MoveEntity camera,0,KeyDown(200)-KeyDown(208),KeyDown(30)-KeyDown(44)
  128.         TurnEntity centrecam,0,KeyDown(203)-KeyDown(205),0
  129.         PointEntity camera,centrecam
  130.         RenderWorld
  131.        
  132.         ClearSurface linesurf   ;If you redraw each frame as with standard drawing commands
  133.         Draw3DLine(camera,linesurf,-5,15,-5,5,25,5,4)
  134.        
  135.         ;introducing the third point
  136.         Draw3DLine(camera,linesurf,5,25,5,5,35,15,4)
  137.        
  138.         linethick=2
  139.        
  140.         ;;iterating through the array of co-ordinates:-
  141.         For ct = 1 To extrapoints
  142.                
  143.                 ;prototype new Draw 3D Linw which will march through a pre-defined set of points within an array
  144.                 Draw3DLine(camera,linesurf,5+ay(ct,0),35+ay(ct,1),15+ay(ct,2),5+ay(ct+1,0),35+ay(ct+1,1),15+ay(ct+1,2),linethick)
  145.                
  146.         Next
  147.        
  148.        
  149.         If MilliSecs()-FPStime=>1000 Then FPScount=FPSframes:FPSframes=0:FPStime=MilliSecs():Else FPSframes=FPSframes+1
  150.         Text 0,30,"FPS: "+FPScount
  151.         Text 0,60,"Arrow keys to turn camera, A and Z to zoom"
  152.        
  153.         Delay (rtime-(MilliSecs()-ctime))-(limited+1)           ;Free spare CPU time
  154.         Flip limited
  155. Wend
  156.  
  157. End
  158.  
  159.  
  160. Function Draw3DLine(camera,surf,x1#,y1#,z1#,x2#,y2#,z2#,thickness#=1,entity=0)  ;If entity is not 0, points refer to an entity's local space
  161.         TFormPoint x1,y1,z1,entity,camera
  162.         x1=TFormedX()
  163.         y1=TFormedY()
  164.         z1=TFormedZ()
  165.         Local d1#=Sqr(x1*x1+y1*y1+z1*z1)/GraphicsWidth()
  166.         TFormPoint x2,y2,z2,entity,camera
  167.         x2=TFormedX()
  168.         y2=TFormedY()
  169.         z2=TFormedZ()
  170.         Local d2#=Sqr(x2*x2+y2*y2+z2*z2)/GraphicsWidth()
  171.        
  172.         Local theta#=ATan2(y2*(d1/d2)-y1,x2*(d1/d2)-x1)
  173.         Local xTForm#=Cos(theta)*thickness
  174.         Local yTForm#=Sin(theta)*thickness
  175.        
  176.         Local V0=AddVertex(surf,x1+yTForm*d1,y1-xTForm*d1,z1)
  177.         Local V1=AddVertex(surf,x1-yTForm*d1,y1+xTForm*d1,z1)
  178.         Local V2=AddVertex(surf,x2-yTForm*d2,y2+xTForm*d2,z2)
  179.         Local V3=AddVertex(surf,x2+yTForm*d2,y2-xTForm*d2,z2)
  180.        
  181.         AddTriangle(surf,V0,V1,V2)
  182.         AddTriangle(surf,V2,V3,V0)
  183. End Function


Comments :


Blitzplotter(Posted 1+ years ago)

 added a 2nd and third graph, the definition of the plotted points is within the lines_4.bb and lines_5.bb files. Make sure lines_4.bb and lines_5.bb is in the same directory as the 'main' piece of code that includes the other two:-
Code: [Select]
; now with three lines....

;Global appheight=768
;Global appwidth=1024
;Global appdepth=32

Global appheight=800
Global appwidth=800
Global appdepth=32

AppTitle "Blitzplotter, plotting Balls using an array with a little help from Yasha";,"Are you sure you want to quit?"

Local SC_FPS=60 ;Desired framerate
Local rtime=Floor(1000.0/SC_FPS)
Local ctime,limited=True
Local FPScount,FPStime,FPSframes

Graphics3D appwidth,appheight,appdepth,6
SetBuffer BackBuffer()

;Create an empty scene
Local centrecam=CreatePivot()
PositionEntity centrecam,0,25,0
Local camera=CreateCamera(centrecam)
PositionEntity camera,0,20,-50,1

Local sun=CreateLight()
PositionEntity sun,-100,400,0
PointEntity sun,centrecam

Local ground=CreateMesh(),tiles=CreateSurface(ground)
Local v1=AddVertex(tiles,-125,0,150),v2=AddVertex(tiles,125,0,150),v3=AddVertex(tiles,125,0,-100)
AddTriangle(tiles,v1,v2,v3):v2=AddVertex(tiles,-125,0,-100):AddTriangle(tiles,v1,v3,v2)
EntityColor ground,0,0,255
Local block=CreateCube():ScaleMesh block,20,5,20

Local linemesh=CreateMesh(camera),linesurf=CreateSurface(linemesh) ;The surface to use to draw the lines - single surface is always faster
;Adjust the surface's colour here, or add vertex colours to the drawing function as necessary

Local marker1=CreateSphere():PositionEntity marker1,-5,15,-5:EntityAlpha marker1,0.3 ;Mark the ends of the line in 3D space
Local marker2=CreateSphere():PositionEntity marker2,5,25,5:EntityAlpha marker2,0.3

;my marker

Local marker3=CreateSphere():PositionEntity marker3,5,35,15:EntityAlpha marker3,0.3

skysize=700


;sky
;Global sky = CreateCube(camera_pivot)
;FlipMesh sky
;ScaleEntity sky,skysize,skysize,skysize
;PositionEntity sky, 0,0,0
;sky_tex = LoadTexture("sky.bmp")
;EntityTexture sky,sky_tex


;#####      3d point creation array     #####
;#####                                  #####

MAXBALLS#=100

;array to hold a few 3d points
Dim ay(100,2)
Dim ay2(100,2)
Dim ay3(100,2)

Include "lines_4.bb"; populating the 2nd array with co-ords
Include "lines_5.bb"; populating the 2nd array with co-ords


;how many balls do you want plotted
Global extrapoints=60

If extrapoints>MAXBALLS
Print"The array is only 100 big... please reduce your balls"
Print"Press any key"
WaitKey
End
EndIf


;populate First 10 co-ords of array
xGap=2   ;distance in the xplane between points
yGap=1 ;distance in the yplane between points
zGap=2   ;distance in the zplane between points

slope_in_zed=0  ;1 adjusts co-ords in the z plane, 0 doesn't

;populate array with 3d points for a zig zag effect
For x = 1 To extrapoints
ay(x,0) = x*xGap;  x co-ords


If x Mod 2 = 1; y co-ords up and down
ay(x,1) = x * -yGap
Else
ay(x,1) = x * yGap
EndIf

If slope_in_zed=1
If x Mod 2 = 1; z co-ords up and down
ay(x,2) = x * -zGap
Else
ay(x,2) = x * zGap
EndIf
Else
ay(x,2) = x*zGap; constant z co-ords
EndIf

;print out the co-ordinates
; Print "x: "+ay(x,0)+" y: "+ay(x,1)+" z: "+ay(x,2)
;
; Print" "

Next

;Print" Press any key...."
Print " "
Print " "
Print " "
;WaitKey

;#####                             #####
;#####  end point creation         #####
;#####                             #####



Dim marker(4)

;this plots the spheres from the points within the array

For cz = 1 To extrapoints

Local marker5=CreateSphere():PositionEntity marker5,5+ay(cz,0),35+ay(cz,1),15+ay(cz,2):EntityAlpha marker5,0.3

Next

For cz = 1 To extrapoints

Local marker6=CreateSphere():PositionEntity marker6,5+ay2(cz,0),35+ay2(cz,1),15+ay2(cz,2):EntityAlpha marker6,0.3

Next

For cz = 1 To extrapoints

Local marker7=CreateSphere():PositionEntity marker7,5+ay3(cz,0),35+ay3(cz,1),15+ay3(cz,2):EntityAlpha marker7,0.3

Next



While Not KeyDown(1)
ctime=MilliSecs()

MoveEntity camera,0,KeyDown(200)-KeyDown(208),KeyDown(30)-KeyDown(44)
TurnEntity centrecam,0,KeyDown(203)-KeyDown(205),0
PointEntity camera,centrecam
RenderWorld

ClearSurface linesurf ;If you redraw each frame as with standard drawing commands
Draw3DLine(camera,linesurf,-5,15,-5,5,25,5,4)

;introducing the third point
Draw3DLine(camera,linesurf,5,25,5,5,35,15,4)

linethick=2

;;iterating through the array of co-ordinates:-
For ct = 1 To extrapoints

;prototype new Draw 3D Linw which will march through a pre-defined set of points within an array
Draw3DLine(camera,linesurf,5+ay(ct,0),35+ay(ct,1),15+ay(ct,2),5+ay(ct+1,0),35+ay(ct+1,1),15+ay(ct+1,2),linethick)

Draw3DLine(camera,linesurf,5+ay2(ct,0),35+ay2(ct,1),15+ay2(ct,2),5+ay2(ct+1,0),35+ay2(ct+1,1),15+ay2(ct+1,2),linethick)

Draw3DLine(camera,linesurf,5+ay3(ct,0),35+ay3(ct,1),15+ay3(ct,2),5+ay3(ct+1,0),35+ay3(ct+1,1),15+ay3(ct+1,2),linethick)

Next


If MilliSecs()-FPStime=>1000 Then FPScount=FPSframes:FPSframes=0:FPStime=MilliSecs():Else FPSframes=FPSframes+1
Text 0,30,"FPS: "+FPScount
Text 0,60,"Arrow keys to turn camera, A and Z to zoom"

Delay (rtime-(MilliSecs()-ctime))-(limited+1) ;Free spare CPU time
Flip limited
Wend

End


Function Draw3DLine(camera,surf,x1#,y1#,z1#,x2#,y2#,z2#,thickness#=1,entity=0) ;If entity is not 0, points refer to an entity's local space
TFormPoint x1,y1,z1,entity,camera
x1=TFormedX()
y1=TFormedY()
z1=TFormedZ()
Local d1#=Sqr(x1*x1+y1*y1+z1*z1)/GraphicsWidth()
TFormPoint x2,y2,z2,entity,camera
x2=TFormedX()
y2=TFormedY()
z2=TFormedZ()
Local d2#=Sqr(x2*x2+y2*y2+z2*z2)/GraphicsWidth()

Local theta#=ATan2(y2*(d1/d2)-y1,x2*(d1/d2)-x1)
Local xTForm#=Cos(theta)*thickness
Local yTForm#=Sin(theta)*thickness

Local V0=AddVertex(surf,x1+yTForm*d1,y1-xTForm*d1,z1)
Local V1=AddVertex(surf,x1-yTForm*d1,y1+xTForm*d1,z1)
Local V2=AddVertex(surf,x2-yTForm*d2,y2+xTForm*d2,z2)
Local V3=AddVertex(surf,x2+yTForm*d2,y2-xTForm*d2,z2)

AddTriangle(surf,V0,V1,V2)
AddTriangle(surf,V2,V3,V0)
End Function
Code: [Select]
;lines4.bb

;how many balls do you want plotted
Global extrapointz=60

If extrapointz>MAXBALLS
Print"The array2 is only 100 big... please reduce your balls"
Print"Press any key"
WaitKey
End
EndIf


;populate First 10 co-ords of array2
xGap=4   ;distance in the xplane between points
yGap=6 ;distance in the yplane between points
zGap=8   ;distance in the zplane between points

slope_in_zed=0  ;1 adjusts co-ords in the z plane, 0 doesn't

;populate array2 with 3d points for a zig zag effect
For x = 1 To extrapointz
ay2(x,0) = x*xGap;  x co-ords


If x Mod 2 = 1; y co-ords up and down
ay2(x,1) = x * -yGap
Else
ay2(x,1) = x * yGap
EndIf

If slope_in_zed=1
If x Mod 2 = 1; z co-ords up and down
ay2(x,2) = x * -zGap
Else
ay2(x,2) = x * zGap
EndIf
Else
ay2(x,2) = x*zGap; constant z co-ords
EndIf

;print out the co-ordinates
Print "array 2 x: "+ay2(x,0)+" y: "+ay2(x,1)+" z: "+ay2(x,2)
;
; Print" "

Next
Code: [Select]
;lines_5.bb

;how many balls do you want plotted
Global extrapointa=60

If extrapointa>MAXBALLS
Print"The array3 is only 100 big... please reduce your balls"
Print"Press any key"
WaitKey
End
EndIf


;populate First 10 co-ords of array3
xGap=6 ;distance in the xplane between points
yGap=13 ;distance in the yplane between points
zGap=19  ;distance in the zplane between points

slope_in_zed=0  ;1 adjusts co-ords in the z plane, 0 doesn't

;populate array3 with 3d points for a zig zag effect
For x = 1 To extrapointa
ay3(x,0) = x*xGap;  x co-ords


If x Mod 2 = 1; y co-ords up and down
ay3(x,1) = x * -yGap
Else
ay3(x,1) = x * yGap
EndIf

If slope_in_zed=1
If x Mod 2 = 1; z co-ords up and down
ay3(x,2) = x * -zGap
Else
ay3(x,2) = x * zGap
EndIf
Else
ay3(x,2) = x*zGap; constant z co-ords
EndIf

;print out the co-ordinates
Print "array 3 x: "+ay3(x,0)+" y: "+ay3(x,1)+" z: "+ay3(x,2)
;
; Print" "

Next


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal