Thank you DK_PETER for the nice example, and for Kwai chang caine for the beautiful graphics.
@Keya, the following cube made from Lines Lists, it is extracted from example
MeshManual2.pb to draw a cube from lines, the example haven't created entity but attached the mesh to a node to display and rotate.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - MeshManual
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
#CameraSpeed = 1
#scale = 3
IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"
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()
If Screen3DRequester()
;- Material
CreateMaterial(0, LoadTexture(0, "White.jpg"))
DisableMaterialLighting(0, #True)
;- Mesh Box (using MeshIndex)
CreateMesh(5, #PB_Mesh_LineList, #PB_Mesh_Static)
; Define vertex position of index 0..7
MeshVertexPosition(-10, -10, -10) ; vertex 0
MeshVertexPosition(-10, -10, 10) ; vertex 1
MeshVertexPosition( 10, -10, 10) ; vertex 2
MeshVertexPosition( 10, -10, -10) ; vertex 3
MeshVertexPosition(-10, 10, -10) ; vertex 4
MeshVertexPosition(-10, 10, 10) ; vertex 5
MeshVertexPosition( 10, 10, 10) ; vertex 6
MeshVertexPosition( 10, 10, -10) ; vertex 7
; Define usage of vertices by referring To the indexes, 12 Lines
MeshIndex(0): MeshIndex(1)
MeshIndex(1): MeshIndex(2)
MeshIndex(2): MeshIndex(3)
MeshIndex(0): MeshIndex(3)
MeshIndex(4): MeshIndex(5)
MeshIndex(5): MeshIndex(6)
MeshIndex(6): MeshIndex(7)
MeshIndex(4): MeshIndex(7)
MeshIndex(0): MeshIndex(4)
MeshIndex(1): MeshIndex(5)
MeshIndex(2): MeshIndex(6)
MeshIndex(3): MeshIndex(7)
FinishMesh(#False)
SetMeshMaterial(5, MaterialID(0)) ;
Box2 = CreateNode(#PB_Any, 0, -30, 0)
AttachNodeObject(Box2, MeshID(5))
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 10, 100, #PB_Absolute)
CameraFOV(0, 40)
CameraLookAt(0, 0, -20, 0)
CameraBackColor(0, RGB(0, 0, 40))
;-Light
CreateLight(0, RGB(255,255,255), -10, 60, 10)
AmbientColor(RGB(90, 90, 90))
glLineWidth_(2) ;affect the lines width if Library subsystem = opengl
Repeat
Screen3DEvents()
ExamineKeyboard()
RotateNode(Box2, 0, 0.3, 0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End
CreateMesh(5, #PB_Mesh_LineList, #PB_Mesh_Static)
note the #PB_Mesh_LineList
after describing the cube points by 8 vertices with MeshVertexPosition(x,y,z)
we use:
Code: Select all
; Define usage of vertices by referring To the indexes
; Define usage of vertices by referring To the indexes, 12 Lines
MeshIndex(0): MeshIndex(1)
MeshIndex(1): MeshIndex(2)
MeshIndex(2): MeshIndex(3)
MeshIndex(0): MeshIndex(3)
.... etc
this means we instruct the 3D engine to:
1- connect vertex 0 to vertex 1 by a line
2- connect vertex 1 to vertex 2 by a line
3- connect vertex 2 to vertex 3 by a line
4- connect vertex 0 to vertex 3 by a line
5- an so on ... it is not obligatory to connect points like this , any 2 points can be connected to make a chaotic shape and you can use thousands of points to make big mesh.
note
#PB_Mesh_LineStrip is different, test it, it will consume fewer points since it goes continuously from point a to point z.
we can't cover this cube with a texture, it should be made from triangles, and this is possible if we choose #PB_Mesh_TriangleList when we create the Mesh and in this case we must use triples of MeshIndex to weave triangles
the book
PureBasic - A Beginners Guide Chapter 11 talk about 3D, even its examples are old but its essence still alive.
the glLineWidth_(2) line 78 have an effect only if we choose Library subsystem as OpenGL, to draw thick lines, it is also affecting every Mesh drawn as wireframe