PDA

View Full Version : Mesh creation in irrlicht



primo
30-10-2017, 10:52
trying to mimic example 15 of IrrlichtWrapper for freebasic , look the wrapper here http://www.frankdodd.screaming.net/IrrlichtWrapper/IrrlichtWrapperPortal.htm
my problem is : can't connect all the pyramid vertices with lines, i am aware that arrays in thinbasic begins with 1, i have changed all the vertices and indices to begin with 1.
to try the example download the wrapper for thinbasic from here http://www.thinbasic.com/community/showthread.php?8034-Irrlicht-module-unofficial-preview-3&p=83724&viewfull=1#post83724 look example irrlicht-tb-largo3a-all.zip and save the code below in it. attached also the freebasic code
note that in freebasic there is:
DIM indices(0 to 17) as ushort
i have tried in TB UInt16, long, dword, word
to let it display only the vertices (as big smooth points) uncomment:
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_POINTCLOUD, 1 )
and comment:
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_WIREFRAME, 1 )


#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "Irrlicht_lw1c.inc"
Uses "console"
'' global variables
Dim xx,yy,zz As Long
' irrlicht objects
Dim PyramidNode As irr_node

Dim Pyramid As irr_mesh
Dim OurCamera As irr_camera

' -----------------------------------------------------------------------------
' start the irrlicht interface
IrrStart( %IRR_EDT_OPENGL, 800, 600, %IRR_BITS_PER_PIXEL_32, %IRR_WINDOWED, _ '200
%IRR_SHADOWS, %IRR_IGNORE_EVENTS, %IRR_VERTICAL_SYNC_ON )

' send the window caption
IrrSetWindowCaption( Ucode$("Example 15: Create a custom mesh"))

Dim verts(5) As IRR_VERT
Dim indices(18) As DWord 'UInt16

' -----------------------------------------------------------------------------
' set up five vertices to define the points of a pyramid. the vertices have
' many properties that need to be set up to properly define the structure
verts(1).x = -10 : verts(1).y = 0 : verts(1).z = -10
verts(2).x = -10 : verts(2).y = 0 : verts(2).z = 10
verts(3).x = 10 : verts(3).y = 0 : verts(3).z = 10
verts(4).x = 10 : verts(4).y = 0 : verts(4).z = -10
verts(5).x = 0 : verts(5).y = 20:verts(5).z = 0

' each of the vertices can be assigned a colour to tint the texture, in this
' case we use white
verts(1).vcolor = &H000000FF
verts(2).vcolor = &H000000FF
verts(3).vcolor = &H00FF0000
verts(4).vcolor = &H00007D00
verts(5).vcolor = &H00007D00

' -----------------------------------------------------------------------------
' create the faces, this is an array of indices referencing the vectors they
' are collected into groups of three each defining a triangle in the mesh
indices(1) = 1 : indices(2) = 2 : indices(3) = 5
indices(4) = 2 : indices(5) = 3 : indices(6) = 5
indices(7) = 3 : indices(8) = 4 : indices(9) = 5
indices(10) = 4 : indices(11) = 1 : indices(12) = 5
indices(13) = 3 : indices(14) = 2 : indices(15) = 1
indices(16) = 1 : indices(17) = 4 : indices(18) = 3

' create the mesh from the array of vertices and indices
Pyramid = IrrCreateMesh( "TestMesh", 5, verts(1), 18, indices(1))
PyramidNode = IrrAddMeshToScene( Pyramid )
' switch off lighting effects on this node
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_LIGHTING, %IRR_OFF )
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_WIREFRAME, 1 )
'IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_POINTCLOUD, 1 )
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_BACK_FACE_CULLING, 0 )

IrrSetNodeScale( PyramidNode, 0.5, 0.5, 0.5 )
IrrSetNodePosition( PyramidNode, -8,-3,0)

' add a camera into the scene
OurCamera = IrrAddCamera( 0,25,-29, 0,0,0 )
'IrrSetNodePosition( OurCamera, 30,20,25)
IrrSetCameraTarget(OurCamera, 0,0,0)
'IrrHideMouse


IrrAddFlyCircleAnimator(PyramidNode,0,8,0,11.5,-0.001)

' -----------------------------------------------------------------------------
' while the irrlicht environment is still running

While IrrRunning
glEnable(%GL_POINT_SMOOTH)
glPointSize(10)
glLineWidth(5)
yy=yy+1:xx=xx+0:zz=zz+0

'IrrSetNodeRotation( PyramidNode, xx, yy, zz )

' begin the scene, erasing the canvas with sky-blue before rendering
IrrBeginScene( 240, 255, 255 )
' draw the scene
IrrDrawScene

' end drawing the scene and render it
IrrEndScene
Wend
' -----------------------------------------------------------------------------
' Stop the irrlicht engine and release resources
IrrStop


the freebasic code:

'' ----------------------------------------------------------------------------
'' Irrlicht Wrapper for Imperative Languages - Freebasic Examples
'' Frank Dodd (2006)
'' ----------------------------------------------------------------------------
'' Example 15 : Custom Mesh
'' This example creates a pyramid mesh that is set up ready to be textured
'' it then adds the mesh as a new node and applies a material to it
'' ----------------------------------------------------------------------------

'' ////////////////////////////////////////////////////////////////////////////
'' Includes for extension libraries
#include "IrrlichtWrapper.bi"

'' ////////////////////////////////////////////////////////////////////////////
'' global variables

' irrlicht objects
DIM Mesh as irr_mesh
DIM MeshTexture as irr_texture
DIM SceneNode as irr_node
DIM OurCamera as irr_camera


'' ////////////////////////////////////////////////////////////////////////////
'' GDB debugger main() function

' -----------------------------------------------------------------------------
' start the irrlicht interface
IrrStart( IRR_EDT_OPENGL, 400, 400, IRR_BITS_PER_PIXEL_32, _
IRR_WINDOWED, IRR_SHADOWS, IRR_IGNORE_EVENTS, IRR_VERTICAL_SYNC_ON )

' set the window caption
IrrSetWindowCaption( "Example 15: Create a custom mesh" )

' a mesh is created from an array of types called vertices that define a point
' in space. and an array of indices to these vertices that are grouped into
' threes to create triangles that form the mesh
DIM verts(0 to 4) as IRR_VERT
DIM indices(0 to 17) as ushort

' -----------------------------------------------------------------------------
' set up five vertices to define the points of a pyramid. the vertices have
' many properties that need to be set up to properly define the structure
verts(0).x = -10 : verts(0).y = 0 : verts(0).z = -10
verts(1).x = -10 : verts(1).y = 0 : verts(1).z = 10
verts(2).x = 10 : verts(2).y = 0 : verts(2).z = 10
verts(3).x = 10 : verts(3).y = 0 : verts(3).z = -10
verts(4).x = 0 : verts(4).y = 20 : verts(4).z = 0

' the co-ordinates across a texture run from 0 to 1 so we place each of the
' vertices on this texture plane to appear as if the pyramid was painted from
' its bottom up
verts(0).texture_x = 0 : verts(0).texture_y = 0
verts(1).texture_x = 0 : verts(1).texture_y = 1
verts(2).texture_x = 1 : verts(2).texture_y = 1
verts(3).texture_x = 1 : verts(3).texture_y = 0
verts(4).texture_x = 0.5 : verts(4).texture_y = 0.5

' each of the vertices can be assigned a colour to tint the texture, in this
' case we use white
verts(0).vcolor = &h00FFFFFF
verts(1).vcolor = &h00FFFFFF
verts(2).vcolor = &h00FFFFFF
verts(3).vcolor = &h00FFFFFF
verts(4).vcolor = &h00FFFFFF

' -----------------------------------------------------------------------------
' create the faces, this is an array of indices referencing the vectors they
' are collected into groups of three each defining a triangle in the mesh
indices(0) = 0 : indices(1) = 1 : indices(2) = 4
indices(3) = 1 : indices(4) = 2 : indices(5) = 4
indices(6) = 2 : indices(7) = 3 : indices(8) = 4
indices(9) = 3 : indices(10) = 0 : indices(11) = 4
indices(12) = 2 : indices(13) = 1 : indices(14) = 0
indices(15) = 0 : indices(16) = 3 : indices(17) = 2

' create the mesh from the array of vertices and indices
Mesh = IrrCreateMesh( "TestMesh", 5, verts(0), 18, indices(0))

' load texture resource for texturing the nodes
MeshTexture = IrrGetTexture( "./media/texture.jpg" )

' add the mesh to the scene a couple of times
SceneNode = IrrAddMeshToScene( Mesh )

' apply a material to the node
IrrSetNodeMaterialTexture( SceneNode, MeshTexture, 0 )

' switch off lighting effects on this node
IrrSetNodeMaterialFlag( SceneNode, IRR_EMF_LIGHTING, IRR_OFF )

' add a camera into the scene and resposition it to look at the pyramid
OurCamera = IrrAddFPSCamera( IRR_NO_OBJECT, 100.0f, 0.1f )
IrrSetNodePosition( OurCamera, 30,50,25)
IrrSetCameraTarget(OurCamera, 0,0,0)
IrrHideMouse


' -----------------------------------------------------------------------------
' while the irrlicht environment is still running
WHILE IrrRunning
' begin the scene, erasing the canvas with sky-blue before rendering
IrrBeginScene( 64, 96, 96 )

' draw the scene
IrrDrawScene

' end drawing the scene and render it
IrrEndScene
WEND

' -----------------------------------------------------------------------------
' Stop the irrlicht engine and release resources
IrrStop

primo
30-10-2017, 18:24
now it is solved: index(1) = 0 : it means index 1 refer to vertex 0 which is defined by verts(1).x = -10 : verts(1).y = 0 : verts(1).z = -10
( yes it is confusing )
so
indices(1) = 0 : indices(2) = 1 : indices(3) = 2
will make a triangle between vertices 0, 1, 2

#Include "%app_includepath%\thinbasic_gl.inc"
#Include "%app_includepath%\thinbasic_glu.inc"
#Include "Irrlicht_lw1c.inc"
Uses "console"
'' global variables
Dim xx,yy,zz As Long
' irrlicht objects
Dim PyramidNode As irr_node

Dim Pyramid As irr_mesh
Dim OurCamera As irr_camera

' -----------------------------------------------------------------------------
' start the irrlicht interface
IrrStart( %IRR_EDT_OPENGL, 800, 600, %IRR_BITS_PER_PIXEL_32, %IRR_WINDOWED, _ '200
%IRR_SHADOWS, %IRR_IGNORE_EVENTS, %IRR_VERTICAL_SYNC_ON )

' send the window caption
IrrSetWindowCaption( Ucode$("Example 15: Create a custom mesh"))

Dim verts(5) As IRR_VERT
Dim indices(21) As UInt16

' -----------------------------------------------------------------------------
' set up five vertices to define the points of a pyramid. the vertices have
' many properties that need to be set up to properly define the structure
verts(1).x = -10 : verts(1).y = 0 : verts(1).z = -10
verts(2).x = -10 : verts(2).y = 0 : verts(2).z = 10
verts(3).x = 10 : verts(3).y = 0 : verts(3).z = 10
verts(4).x = 10 : verts(4).y = 0 : verts(4).z = -10
verts(5).x = 0 : verts(5).y = 20:verts(5).z = 0

' each of the vertices can be assigned a colour to tint the texture, in this
' case we use white
verts(1).vcolor = &H000000FF
verts(2).vcolor = &H000000FF
verts(3).vcolor = &H00FF0000
verts(4).vcolor = &H00007D00
verts(5).vcolor = &H00007D00

' -----------------------------------------------------------------------------
' create the faces, this is an array of indices referencing the vectors they
' are collected into groups of three each defining a triangle in the mesh

indices(1) = 0 : indices(2) = 1 : indices(3) = 2
indices(4) = 2 : indices(5) = 3 : indices(6) = 0
indices(7) = 0 : indices(8) = 1 : indices(9) = 4
indices(10) = 1 : indices(11) = 4 : indices(12) = 2
indices(13) = 2 : indices(14) = 4 : indices(15) = 3
indices(16) = 3 : indices(17) = 4 : indices(18) = 0

' create the mesh from the array of vertices and indices
Pyramid = IrrCreateMesh( "TestMesh", 5, verts(1).x, 18, indices(1))
PyramidNode = IrrAddMeshToScene( Pyramid )
' switch off lighting effects on this node
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_LIGHTING, %IRR_OFF )
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_WIREFRAME, 1 )
'IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_POINTCLOUD, 1 )
IrrSetNodeMaterialFlag( PyramidNode, %IRR_EMF_BACK_FACE_CULLING, 0 )

IrrSetNodeScale( PyramidNode, 0.5, 0.5, 0.5 )
IrrSetNodePosition( PyramidNode, -8,-3,0)

' add a camera into the scene
OurCamera = IrrAddCamera( 0,25,-29, 0,0,0 )
'IrrSetNodePosition( OurCamera, 30,20,25)
IrrSetCameraTarget(OurCamera, 0,0,0)
'IrrHideMouse


IrrAddFlyCircleAnimator(PyramidNode,0,8,0,11.5,-0.001)

' -----------------------------------------------------------------------------
' while the irrlicht environment is still running

While IrrRunning
glEnable(%GL_POINT_SMOOTH)
glPointSize(10)
glLineWidth(5)
yy=yy+1:xx=xx+0:zz=zz+0

IrrSetNodeRotation( PyramidNode, xx, yy, zz )

' begin the scene, erasing the canvas with sky-blue before rendering
IrrBeginScene( 240, 255, 255 )
' draw the scene
IrrDrawScene

' end drawing the scene and render it
IrrEndScene
Wend
' -----------------------------------------------------------------------------
' Stop the irrlicht engine and release resources
IrrStop


Edit: note that in the include file Irrlicht_lw1c.inc Declare Function IrrCreateMesh Lib "IrrlichtWrapper.dll" Alias "IrrCreateMesh" (ByVal mesh_name As String Ptr, ByVal vertex_count As LONG, ByRef vertices As IRR_VERT, ByVal indices_count As LONG, ByRef indices As Long) As irr_mesh

but we we dim indices in thinbasic as UInt16
and this is what is defined in freebasic:
DIM indices(0 to 17) as ushort

primo
10-11-2017, 16:27
in all previous irrlicht examples we can't use the keys and the only way to exit the window is to press ALT-F4. the reason is that the wrapper include file Irrlicht_lw1c.inc have 2 remarks in this line :
'Declare Function IrrReadKeyEvent Lib "IrrlichtWrapper.dll" Alias "IrrReadKeyEvent" () As IRR_KEY_EVENT 'Ptr
so remove these remarks and we can use the keys again
also we should add %IRR_CAPTURE_EVENTS to the IrrStart parameters

it is said that we can't create more than 65535 different vertices
look: Too many vertices for 16bit index type:
http://irrlicht.sourceforge.net/forum/viewtopic.php?t=43886
but they suggest to use a compound mesh

also changed
IrrCreateMesh .....ByRef indices As Long) As irr_mesh
to
ByRef indices As word) As irr_mesh
because the freebasic counter part is
IrrCreateMesh .....byref indices as ushort) as irr_mesh
so in thinbasic we write something like this:
Dim indices(30000) As Word(or UInt16)
i have edited the include file to reflect these changes.

i will not study irrlicht more , my eyes vision does not allow me to follow all these complex functions. who wants more, just study the examples in the freebasic irrlicht wrapper, also this forum https://www.freebasic.net/forum/viewtopic.php?f=14&t=3584 is a wealth of info
unfortunately the wrapper developer stop to work on it, and the forum itself freeze at 2013 with no more posts.

note the IrrAddFPSCamera allow you to navigate with arrow keys and mouse
press ESC to exit, mouse right click will trigger msgbox
download all the package (example , textures, models, inc file, irrlicht dll and wrapper dll, the irrlich dll is downloaded from the distribution version 1.7.3 http://irrlicht.sourceforge.net/?page_id=10

https://www.mediafire.com/file/lciw9i3tlh1atdx/thinbasic_irrlicht.rar

to add keys support to the Largo examples posted in thinbasic forum just use the attached inc file, all the virtue is for the people who contributed to this project


9763