occasionally i experiment with xors3d, here is some notes:
xSaveMesh saves the mesh to .fbx format. we can convert
.fbx format to
.OBJ easily with this utility:
Autodesk FBX Converter 2013:
http://usa.autodesk.com/adsk/servlet/pc ... d=22694909
this enables us to print our graphics in purebasic with a 3D printer which use stl and obj formats as an input
let us make a test: save this code to the folder
\Xors3D-for-PB-master\Samples\Basic
and while running press
'S' key, the
knot.fbx is created in the
\Xors3D-for-PB-master\DLL folder
how to use Autodesk FBX Converter:
to view your file knot.fbx
Tools -> add fbx viewer
File -> choose your file
=======================
to convert the file to Obj or DXF or DAE collada
Tools -> add fbx converter
Add -> choose your fbx file
Destination Format -> Obj ,DXF, DAE collada
Convert
Code: Select all
SetCurrentDirectory("..\..\dll")
; Include header file
IncludeFile "..\..\xors3d.pbi"
xCreateLog()
; setup maximum supported AntiAlias Type
xSetAntiAliasType(xGetMaxAntiAlias())
; set application window caption
xAppTitle("Make a Mesh + Physics sample. W: wirerame. Space: stop rotation. arrows: move camera. S saves the mesh")
; initialize graphics mode
xSetEngineSetting("Splash::TilingTime", "0.0")
xSetEngineSetting("Splash::AfterTilingTime", "0.0")
xGraphics3D(800, 600, 32, #False, #True)
; hide mouse pointer
;xHidePointer()
; enable antialiasing
xAntiAlias(#True)
#CameraSpeed = 0.5
#NUM_BANDS = 16 ;number of points every circle (band) on the tube made from
#BAND_RAD = 2 ;tube thickness
Define.f KeyX, KeyY, MouseX, MouseY
Global rot = 1
Global rings
Global NbX = #NUM_BANDS
Global NbZ
Global tot
Structure vector3d
x.f
y.f
z.f
EndStructure
Global.l NUM_RINGS, NUM_BANDS
Global.f BAND_RAD, txu, txv
NUM_BANDS = 16
BAND_RAD = 3 ; tube thickness
Declare MakeTube(Rings.l, Bands.l, BandRadius.f)
Quit.b = #False
Procedure.f RandF(Min.f, Max.f, Resolution.i = 10000)
ProcedureReturn (Min + (Max - Min) * Random(Resolution) / Resolution)
EndProcedure
Global.l P1, P2, P3, P4,light, mesh, surf, cam, i, v1, v2, v3, brush, tri1, tot
Global.l light, mesh, surf, cam, i, brush, tri1, sphere
Global Texture, logoTexture, ground
xAntiAlias(#True)
light = xCreateLight(1)
mesh = xCreateMesh ()
xMeshPrimitiveType (mesh, 4)
surf = xCreateSurface(mesh, brush)
xSurfacePrimitiveType (surf, 4)
xRotateEntity(mesh, 90,0,0)
MakeTube (NUM_RINGS, NUM_BANDS, BAND_RAD)
Texture = xLoadTexture("..\media\textures\blue_marble.jpg")
xEntityTexture(mesh,Texture,0,0)
xPositionEntity(mesh, 0,30,0)
camera = xCreateCamera(0)
; // position camera
xPositionEntity(camera, 0, 60, -70, #False)
ground = xCreateCube()
xPointEntity(camera, ground, 0.0)
xScaleEntity(ground, 100, 1, 100, #False)
xEntityAddBoxShape(ground, 0.0, 0.0, 0.0, 0.0)
;loading logo from File
logoTexture = xLoadTexture("..\media\textures\logo.jpg")
; texture cube
xEntityTexture(ground, logoTexture)
; // texture ground box
xEntityTexture(ground, logoTexture, 0, 0)
light = xCreateLight(0)
xRotateEntity(light, 45, 0, 0, #False)
sphere = xCreateSphere(16, 0)
xScaleMesh(sphere,2,2,2)
xPositionEntity(sphere, -5,40,-5)
xEntityColor(sphere, 255, 0, 0)
xEntityAddSphereShape(sphere, 20.0, 0.0)
xEntityAddConcaveShape(mesh, 0)
xFlipMesh(mesh)
xEntityFX(mesh, 1|2|16) ; make the mesh texture brighter
xUpdateNormals(mesh)
xPointEntity(camera, mesh)
mv.f = 0.6
wireframe=0: turn = 1
;====================================================================
;xSaveMesh(mesh,"knot3")
; main program loop
While(Not (xKeyHit(#KEY_ESCAPE) Or xWinMessage("WM_CLOSE")))
; camera control
If xKeyDown(#KEY_Up)
xMoveEntity(camera, 0, 0, mv, #False)
EndIf
If xKeyDown(#KEY_Down)
xMoveEntity(camera, 0, 0, -mv, #False)
EndIf
If xKeyDown(#KEY_Left)
xMoveEntity(camera, -mv, 0, 0, #False)
EndIf
If xKeyDown(#KEY_Right)
xMoveEntity(camera, mv, 0, 0, #False)
EndIf
If xKeyHit(#xKEY_W)
If wireframe = 1
wireframe = 0
Else
wireframe = 1
EndIf
xWireframe(wireframe)
ElseIf xKeyHit(#xKEY_Space)
If turn = 1
turn = 0
Else
turn = 1
EndIf
EndIf
If xKeyHit(#xKEY_s)
xSaveMesh(mesh, "knot")
EndIf
xTurnEntity(mesh, 0, 0, turn/4)
;// RENDER scene
xUpdateWorld(1.0)
xRenderWorld(1.0, 0)
; // FPS & rendered triangles counters
xText(10, 10, "FPS: " + xGetFPS())
xText(10, 30, "Tri: " + Str(xCountTriangles(surf)))
xFlip()
Wend
;xReleaseGraphics()
Procedure vector_cross(*v1.vector3d, *v2.vector3d, *vout.vector3d)
*vout\x = (*v1\y * *v2\z) - (*v2\y * *v1\z)
*vout\y = (*v1\z * *v2\x) - (*v2\z * *v1\x)
*vout\z = (*v1\x * *v2\y) - (*v2\x * *v1\y)
EndProcedure
Procedure.f vector_magnitude(*v.vector3d)
mag.f
mag = Sqr(*v\x * *v\x + *v\y * *v\y + *v\z * *v\z)
If mag = 0:mag = 1:EndIf
ProcedureReturn mag
EndProcedure
Procedure vector_normalize (*v.vector3d)
mag.f
mag = vector_magnitude(*v)
*v\x = *v\x / mag
*v\y = *v\y / mag
*v\z = *v\z / mag
EndProcedure
Procedure vector_add (*v1.vector3d, *v2.vector3d, *vout.vector3d)
*vout\x = *v1\x + *v2\x
*vout\y = *v1\y + *v2\y
*vout\z = *v1\z + *v2\z
EndProcedure
Procedure vector_sub (*v1.vector3d, *v2.vector3d, *vout.vector3d)
*vout\x = *v1\x - *v2\x
*vout\y = *v1\y - *v2\y
*vout\z = *v1\z - *v2\z
EndProcedure
;main Procedure To calculate the tube
Procedure MakeTube (Rings.l , Bands.l ,BandRadius.f)
Protected.f x,y,z
Protected.l rr,gg,bb, i, RingsTotal, v1
Protected.f angle, radius, increm, upp, mag
angle = 0
Increm = 0.01
;--------------------------------
RingsTotal.l = 0 ; number of rings For the final mesh
Define next_point.vector3d
Define current_point.vector3d
Define T.vector3d
Define N.vector3d
Define B.vector3d
While angle <= 2*#PI - 0.1
RingsTotal + 1
angle + Increm
;center point
current_point\x = 0.1*(41 *Cos(angle) - 18 *Sin(angle) - 83 *Cos(2 *angle) - 83 *Sin(2 *angle) - 11 *Cos(3 *angle) + 57 *Sin(3 *angle))
current_point\y = 0.1*(36 *Cos(angle) + 27 *Sin(angle) - 113 *Cos(2 *angle) + 30 *Sin(2 *angle) + 11 *Cos(3 *angle) - 57 *Sin(3 *angle))
current_point\z = 0.1*(45 *Sin(angle) - 30 *Cos(2 *angle) + 113 *Sin(2 *angle) - 11 *Cos(3 *angle) + 27 *Sin(3 *angle))
;radius = a1 + b1*angle'+0.1
angle = angle + increm
;upp = upp + 0.2
;Next Point (For Frenet square Method)
next_point\x = 0.1*(41 *Cos(angle) - 18 *Sin(angle) - 83 *Cos(2 *angle) - 83 *Sin(2 *angle) - 11 *Cos(3 *angle) + 57 *Sin(3 *angle))
next_point\y = 0.1*(36 *Cos(angle) + 27 *Sin(angle) - 113 *Cos(2 *angle) + 30 *Sin(2 *angle) + 11 *Cos(3 *angle) - 57 *Sin(3 *angle))
next_point\z = 0.1*(45 *Sin(angle) - 30 *Cos(2 *angle) + 113 *Sin(2 *angle) - 11 *Cos(3 *angle) + 27 *Sin(3 *angle))
;angle = angle + increm
;upp = upp + 0.2
;T = P' - P
vector_sub(next_point, current_point, T)
;N = P' + P
vector_add(next_point, current_point, N)
;B = T x N
vector_cross(T, N, B)
;N = B x T
vector_cross(B, T, N)
;Normalize vectors Or Else it won't work
vector_normalize(B)
vector_normalize(N)
j.l = 0
For j = 0 To bands
new_point_x.f
new_point_y.f
;rotate around the current point using normal rotation makes bands
new_point_x = Sin(j * 2*#PI / bands) * BAND_RAD
new_point_y = Cos(j * 2*#PI / bands) * BAND_RAD
;this is the coords of our point along the curve
x = N\x * new_point_x + B\x * new_point_y + current_point\x
y = N\y * new_point_x + B\y * new_point_y + current_point\y
z = N\z * new_point_x + B\z * new_point_y + current_point\z
;rr=RND(0,255):gg=RND(0,255):bb=RND(0,255)
v1 = xAddVertex(surf, x, y, z, txu, txv,0)
;xVertexColor(surf, v1, rr, gg, bb, 1)
txv = txv + 1/bands
Next
;rr=Rnd(0,255):gg=Rnd(0,255):bb=Rnd(0,255)
txv = 0
txu = txu + 1/bands
Wend
c.l = 0
d.l = 0
Protected.l P1, P2, P3, P4
For d=0 To RingsTotal-2
For c=0 To NUM_BANDS-1
P1=c+(d*(NUM_BANDS+1))
P2=P1+1
P3=c+(d+1)*(NUM_BANDS+1)
P4=P3+1
tri1 = xAddTriangle(surf, P3, P2, P1)
tri1 = xAddTriangle(surf, P2, P3, P4)
tot + 1
Next
Next
EndProcedure
regarding the mesh deformation, we only need this function:
xVertexCoords(surface, vertex index, x,y,z) to change the position of any vertex.
press the arrow keys to change the upper triangle vertex positions:
Code: Select all
SetCurrentDirectory("..\..\dll")
; Include header file
IncludeFile "..\..\xors3d.pbi"
xCreateLog()
; setup maximum supported AntiAlias Type
xSetAntiAliasType(xGetMaxAntiAlias())
; set application window caption
xAppTitle("Example Xors3D: Press Arrow Keys to deform the triangle in real time (changing upper vertex position")
; initialize graphics mode
xSetEngineSetting("Splash::TilingTime", "0.0")
xSetEngineSetting("Splash::AfterTilingTime", "0.0")
xGraphics3D(800, 600, 32, #False, #True)
; hide mouse pointer
;xHidePointer()
; enable antialiasing
xAntiAlias(#True)
Global.l light, mesh, surf, cam, i, v1, v2, v3, brush, tri1, tot
Global.f x,y,z,r,g,b
light = xCreateLight(1)
mesh = xCreateMesh ()
xMeshPrimitiveType (mesh, 4)
surf = xCreateSurface(mesh, brush)
xSurfacePrimitiveType (surf, 4)
tot+1
v1 = xAddVertex(surf, -50, -50, 0, 0, 1)
xVertexColor(surf, v1, 0, 0, 255, 1)
v2 = xAddVertex(surf, 50, -50, 0, 0, 1)
xVertexColor(surf, v2, 0, 255, 0, 1)
v3 = xAddVertex(surf, 0, 50, 0, 0, 1)
xVertexColor(surf, v3, 255, 0, 0, 1)
tri1 = xAddTriangle(surf, v1, v2, v3)
;creating the camera
cam = xCreateCamera()
xPositionEntity(mesh, 0,0,400)
xPositionEntity(cam, 0, 0, 0)
;xPointEntity(cam, mesh) ; let the camera look always to the mesh
xFlipMesh(mesh)
xEntityFX(mesh, 1|2|18)
xUpdateNormals(mesh)
y.f=50 : x.f = 0
While(Not (xKeyHit(#KEY_ESCAPE) Or xWinMessage("WM_CLOSE")))
If xKeyDown(#KEY_Up)
y+0.5
xVertexCoords(surf, 2, x,y,0)
ElseIf xKeyDown(#KEY_Down)
y-0.5
xVertexCoords(surf, 2, x,y,0)
ElseIf xKeyDown(#KEY_Left)
x-0.5
xVertexCoords(surf, 2, x,y,0)
ElseIf xKeyDown(#KEY_right)
x+0.5
xVertexCoords(surf, 2, x,y,0)
EndIf
;xTurnEntity(mesh, 0, 1, 0)
xRenderWorld()
xText(10, 10, "FPS: " + xGetFPS())
xText(10, 30, "Tri: " + Str(xCountTriangles(surf)))
xFlip()
Wend
End
to add vertices and triangles in the run time: downlod these 2 codes, the first one add triangles and the second add spheres. save them to \Xors3D-for-PB-master\Samples\Basic
http://www.mediafire.com/file/tu3vo8848 ... angles.rar
better to run all the xors3d example in non unicode mode with PB 5.44 32/bit
and your computer should have directX 9 installed. or install it from :
http://download.microsoft.com/download/ ... redist.exe