the reference by T4r4ntul4 contains many things about 3d points.
we can plot data easily with purebasic 3d engine .
but i think there is a need for a 2 pages introduction in the help docs to describe how to plot a 3d point because this is what 96% of the users wants from the Graphics, the other 4% is the games / software developers , and how in the world the user will know that he should first create a mesh before plotting a 3d point !!.
here is my viewpoint:
1- to be able to draw a 3d point or line in PB we need to prepare a 3d environment for plotting , or say a place in the memory, imagine this env. a 3d room, or even a body, undefined object yet, waiting for us to run our color brush over it, we call it here "mesh" , we should create it with
CreateMesh(...) before we plot points/lines , CreateMesh(...) has many parameters to suit different graphics options.
such as
CreateMesh(#mesh, #PB_Mesh_PointList, #PB_Mesh_Static) :
#PB_Mesh_PointList is the most usable option since it means we want to plot any collection of unrelated points. we can create several instances from the same mesh by using CreateEntity(...) and every entity (or say instance) have different material, or positions ...etc. in this case we must finalize the mesh with
FinishMesh(#True). else we use
FinishMesh(#False) as used in the the official example
MeshManual2.pb in PureBasic\Examples\3D folder .
MeshManual2.pb contains everything needed, but to study it i suggest to isolate the several codes it contains and to experiment with it alone. as an example here is i isolate plotting the points only and i have finalized the mesh and created an entity. compare with the original example
note that we set the mesh material to
white, imagine this like a white blackboard we want to draw color over it with the following brush :
MeshVertexPosition(x, y, z) ; position of the point
MeshVertexColor(RGB(?,?,?)) ; color of the point
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - MeshManual
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
#CameraSpeed = 1
#scale = 3
Define.f KeyX, KeyY, MouseX, MouseY
If InitEngine3D()
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()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)
OpenWindow(0, 0, 0, DesktopW, DesktopH, "Just Points ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
;- Material
CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)
;- Mesh Stars
CreateMesh(1, #PB_Mesh_PointList, #PB_Mesh_Static)
For i = 0 To 100000
MeshVertexPosition(Random(200)-100, Random(200)-100, Random(200)-100)
MeshVertexColor(RGB(Random(255,1),Random(255,1),Random(255,1)))
Next i
FinishMesh(#True)
SetMeshMaterial(1, MaterialID(0))
CreateEntity(1, MeshID(1), MaterialID(0))
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 40, 150, #PB_Absolute)
CameraFOV(0, 40)
;CameraLookAt(0, NodeX(Grid), NodeY(Grid), NodeZ(Grid))
CameraBackColor(0, RGB(0, 0, 40))
;-Light
CreateLight(0, RGB(255,255,255), -10, 60, 10)
AmbientColor(RGB(90, 90, 90))
Repeat
Event = WindowEvent()
ExamineKeyboard()
RotateEntity(1, 0.1, 0.1, 0.1, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
End
2- suppose you want to plot the point as a tiny rose
save the following lines to
rose.material file together with the following code and the
rose.png (it has Alpha channel and it is referred to in the "rose.material".
http://s28.postimg.org/6k98je5n1/rose.png

what we will see is the mandelbrot set made from roses:

i know little about material scripts, and in fact the first time i know about it is from Samuel codes about materials. before that i was scaring from the word "script" and still.
rose.material :
Code: Select all
material PointsSprites
{
technique
{
pass
{
cull_hardware none
cull_software none
scene_blend alpha_blend
lighting on
diffuse 1 1 1 1
ambient 1 1 1 1
specular 1 1 1 1
emissive 1 1 1 1
point_sprites on
point_size 20
//point_size_min 2.0
//point_size_max 256.0
depth_write off
depth_check on
depth_func less_equal
//point_size_attenuation on 1.0 0.0 3.5
texture_unit
{
filtering anisotropic
max_anisotropy 16
texture rose.png
}
}
}
}
mandelbrot set from roses:
Code: Select all
Enumeration
#MESH
#LIGHT
#CAMERA_ONE
EndEnumeration
Global.f dx, dy, x, y, z
Global wd, ht, i, count, n, iter
Global.f w, leng, tx, ty, tz, tem
Global.f cr, ci, cj, ck, wk, inc, distance
Global mand, zval
Global.f angle
Define.f red, green, blue
;mand = 0 is Julia Set
;mand = 1 is Mandelbrot 3D
mand = 1
;zval = 1 shows entire set
;zval = 0 cuts set in half
;zval = 0 is an interesting effect
zval = 1
iter = 5
inc = 5
;#quat = 1
zval = 1
iter = 5
inc = 5
Procedure.f RandF(Min.f, Max.f, Resolution.i = 10000)
ProcedureReturn (Min + (Max - Min) * Random(Resolution) / Resolution)
EndProcedure
Quit.b = #False
ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "mandelbrot set from roses", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
InitKeyboard()
SetFrameRate(60)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Parse3DScripts()
CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))
CreateCamera(#CAMERA_ONE, 0, 0, 100, 100)
MoveCamera(#CAMERA_ONE, 0, 2, 7)
CameraLookAt(#CAMERA_ONE, 0, 1, 0)
RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf
CreateMesh(1, #PB_Mesh_PointList, #PB_Mesh_Dynamic )
GetScriptMaterial(0, "PointsSprites")
SetMeshMaterial(1, MaterialID(0))
DisableMaterialLighting(0, #True)
Global Stars = CreateNode(#PB_Any)
AttachNodeObject(Stars, MeshID(1))
Procedure.f calcleng( x.f, y.f, z.f)
w.f: kr.f: ki.f: kj.f: kk.f
w = wk
n = 0
If mand = 1 ;full Mandelbrot set
kr = x
ki = y
kj = z
kk = 0
Else ;else draw Julia Set
kr = cr
ki = ci
kj = cj
kk = ck
EndIf
While n < iter
tem = x+x
x = x*x-y*y-z*z-w*w+kr
y = tem*y + ki
z = tem*z + kj
w = tem*w + kk
n+1
distance = x*x+y*y+z*z+w*w
If distance > 4
n = iter
EndIf
Wend
;Return distance
ProcedureReturn distance
EndProcedure
Procedure calcit()
zz.f
foo.l
iterations = 10000
count = 0
If zval = 0
zz = 2.0
Else
zz = 4.0
EndIf
For foo = 0 To iterations
;x.f = RandF(0, 1)
;y.f = RandF(0, 1)
x.f = RandF(-2, 2)
y.f = RandF(-2, 2)
z.f = zz*RandF(0, 1) -2.0
;calls the quaternion calculation
leng.f = calcleng(x,y,z)
If leng < 4
MeshVertexPosition(x, y, z)
EndIf
Next
FinishMesh(#False)
EndProcedure
calcit() ; calling the mandel 3D or julia generator function
;Main loop
Repeat
Event = WindowEvent()
RotateNode(Stars, 0, 1, 0, #PB_Relative)
RenderWorld()
FlipBuffers()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape)
Quit = #True
EndIf
Until Quit = #True Or Event = #PB_Event_CloseWindow
regarding lines, in addition to the
CreateLine3D(...) function (look CreateLine3d.pb official example) , we can also create mesh like this:
CreateMesh(#mesh, #PB_Mesh_LineList , #PB_Mesh_Static)
and after that when we plot 2 consecutive points it will be connected by a line automatically
look example MeshManual2.pb also this thread:
http://purebasic.fr/english/viewtopic.php?f=36&t=61049
EDIT:
here is how to plot Big points
suppose you want a point size
3
save this as bigpoint.material in the the same folder as the following code, since the
Add3DArchive(".", #PB_3DArchive_FileSystem) instruct the engine to look at the code folder for textures, scripts, models, etc. and here
GetScriptMaterial(5, "def_Points") search for *.material text files for sentence
material def_Points (we can use any other name such as material Big_Points)
look that the text file bigpoint.material have a line
point_size 3
so every :
MeshVertexPosition(x, y, z)
MeshVertexColor(RGB(0,0,255))
will plot a big blue point. i should not forgot that Parse3DScripts() are essential to add.
note that we can zoom on/out rotate camera with mouse/keyboard around the Graphics to have the desirable view
bigPoints.material:
Code: Select all
material def_Points
{
receive_shadows off
technique
{
pass
{
point_size 3
lighting off
diffuse vertexcolour
}
}
}
sine plotting code
Code: Select all
Declare DrawBigPoints()
#CameraSpeed = 1
Enumeration
#mesh
#entity
#tex
#light
#camera
EndEnumeration
ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)
InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0,0,0,ScreenX,ScreenY,"3D curves Plot",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
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)
Parse3DScripts()
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,0,23,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(155,155,100))
;-Mesh
CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
GetScriptMaterial(5, "def_Points")
SetMeshMaterial(0, MaterialID(5))
DisableMaterialLighting(5, #True)
DrawBigPoints()
FinishMesh(#True)
SetMeshMaterial(0, MaterialID(5))
CreateEntity(0, MeshID(0), MaterialID(5),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 = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
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 DrawBigPoints()
Protected.l a, b, Nb
Protected.w P1, P2, P3, P4
Protected.f x, z
NbX=600
NbZ=600
NbY=600
;xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
xMin.f = -2*#PI : yMin.f = -2*#PI: zMin.f = -3 : xMax.f = 2*#PI: yMax = 2*#PI : zMax = 3
;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
range = xMax - xMin
step1.f = range / NbX
x.f = xMin: z.f = 0 : y.f = yMin
For a=0 To NbX
;y.f = x * x
y.f = Sin(x)
MeshVertexPosition(x, y, z)
;MeshVertexPosition(x*3, y*3, z) ;to amplify values
MeshVertexColor(RGB(0,0,255))
x.f + step1
Next a
EndProcedure