Topic: How do MeshBuffers work?

Hi,

First of all thanks for your module!

I am trying to make meshes with code and more specifically by using MeshBuffers. I am trying to do it like this:

- Make an S3DVertex array and fill with vertices
- Make an short array and fill with indices
- Make a new sMeshBuffer and .append vertices and indices and call RecalculateBoundingBox
- Make a new sMesh and .addMeshBuffer(my sMeshBuffer)
- Make a new iMeshSceneNode with smgr.addMeshSceneNode(my sMesh)

But getIndexCount() and getVertexCount() on sMeshBuffer return zero, like does getMeshBufferCount() on sMesh. Nothing on screen.

I also tried with the interface versions (IMesh.getMeshBuffer(0).appendFromOther(my IMeshBuffer)) but no luck.

I also tried with fillIndicesPtr and fillS3DVertexVerticesPtrs but still no luck.

How should I do it?

Re: How do MeshBuffers work?

greetings smile  sounds like you are approaching it correctly.  do you have a quick sample?

Re: How do MeshBuffers work?

greetings smile  i think i found the problem with append.  please download the new mod and try your code.

Re: How do MeshBuffers work?

Woohoo, what a quick reply! smile

I'll test it later today when I get home.

Thanks!

Re: How do MeshBuffers work?

Still no work. I am new to Irrlich so I guess I just don't know how to create those structures or interfaces properly.

Here is my failing code:


SuperStrict
Framework irrlicht.core
Local device:IrrlichtDevice = IrrlichtDevice.create(EDT_DIRECT3D9, _DIMENSION2DI(640, 480), 16, False, False, False, Null)
Local driver:IVideoDriver = device.getVideoDriver()
Local smgr:ISceneManager = device.getSceneManager()
smgr.addCameraSceneNode(Null, _VECTOR3DF(0,10,-10), _VECTOR3DF(0,5,0))


Local ver:S3DVertex[] = New S3DVertex[3]
    Local uv:Vector2df = New Vector2df
        uv.set(0,0)
    ver[0] = New S3DVertex
    ver[0].setPos(_VECTOR3DF(0,5,10))
    ver[0].SetColor(_SCOLOR(255,255,0,0))
    ver[0].setNormal(_VECTOR3DF(0,1,0))
    ver[0].setTCoords(uv)
    
    ver[1] = New S3DVertex
    ver[1].setPos(_VECTOR3DF(10,5,10))
    ver[1].SetColor(_SCOLOR(255,255,0,255))
    ver[1].setNormal(_VECTOR3DF(0,1,0))
    ver[1].setTCoords(uv)
    
    ver[2] = New S3DVertex
    ver[2].setPos(_VECTOR3DF(10,0,0))
    ver[2].SetColor(_SCOLOR(255,255,0,0))
    ver[2].setNormal(_VECTOR3DF(0,1,0))
    ver[2].setTCoords(uv)

Local ind:Short[] = New Short[3]
    ind[0] = 0
    ind[1] = 1
    ind[2] = 2

Local mb:SMeshBuffer = New SMeshBuffer
    mb.append(ver,ind)
    mb.recalculateBoundingBox()

print mb.getIndexCount()
Print mb.getVertexCount()

Local me:SMesh = New SMesh
    me.addMeshBuffer(mb)

Local msc:IMeshSceneNode = smgr.addMeshSceneNode(me)


While(device.run())
    driver.beginScene(True, True, _SCOLOR(255,100,100,100))
    smgr.drawAll()
    driver.endScene()
Wend
device.drop()

Re: How do MeshBuffers work?

greetings smile  try changing:

New S3DVertex

to

S3DVertex.create()

since all types in the irrlicht mod are wrappers for C++ objects, for types that can be created on their own (ie S3DVertex) there is a .create() method instead of New.

7 (edited by irrigator 2009-03-03 15:05:40)

Re: How do MeshBuffers work?

S3DVertex has no create method, but there's a S3DVertexDefault which a create method that returns S3DVertex. Those other Sxxxx structures had a create() method of their own. Now the counts return something! Yippee! Nothing on screen yet, must be the data on my buffers. But in theory it's already working.

Thanks a ton for quick and good replies! Very good support! smile

Re: How do MeshBuffers work?

Here's a very simple sample of how to use MeshBuffers, just in case somebody else is trying to figure this out.

' basic Irrlich stuff for setting up the engine
    Local device:IrrlichtDevice = ..
    IrrlichtDevice.Create(EDT_OPENGL, _DIMENSION2DI(640, 480), 16, False)
    If Not device Return
    Local Driver:IVideoDriver = device.getVideoDriver()
    Local smgr:ISceneManager = device.getSceneManager()
    smgr.addCameraSceneNode(Null, _VECTOR3DF(0, 5, - 40), _VECTOR3DF(0, 10, 0))

' the vertex array
Local lVert:S3DVertex[] = New S3DVertex[4]
    ' setup the array
    lVert[0] = S3DVertexDefault.Create()
    lVert[1] = S3DVertexDefault.Create()
    lVert[2] = S3DVertexDefault.Create()
    lVert[3] = S3DVertexDefault.Create()
    
    ' only position, color and normals for each vertex
    ' we don't need texture coordinates this time        
    lVert[0].setPos(_VECTOR3DF(0, 0, 10))
    lVert[0].SetColor(_SCOLOR(255, 0, 255, 255))
    lVert[0].setNormal(_VECTOR3DF(1, 1, 0))
    
    lVert[1].setPos(_VECTOR3DF(10, 0, - 10))
    lVert[1].SetColor(_SCOLOR(255, 255, 0, 255))
    lVert[1].setNormal(_VECTOR3DF(1, 0, 0))
    
    lVert[2].setPos(_VECTOR3DF(0, 20, 0))
    lVert[2].SetColor(_SCOLOR(255, 255, 255, 0))
    lVert[2].setNormal(_VECTOR3DF(0, 1, 1))
    
    lVert[3].setPos(_VECTOR3DF(- 10, 0, - 10))
    lVert[3].SetColor(_SCOLOR(255, 0, 255, 0))
    lVert[3].setNormal(_VECTOR3DF(0, 0, 1))
    
' the index array - creates polygons from our vertex array
Local lInd:Short[] = New Short[9]
    lInd[0] = 1 ' equals lVert[1]
    lInd[1] = 2 ' equals lVert[2]
    lInd[2] = 0 ' equals lVert[0]...
    
    lInd[3] = 3
    lInd[4] = 2
    lInd[5] = 1

    lInd[6] = 0
    lInd[7] = 2
    lInd[8] = 3
      
' the mesh buffer
Local lSMB:SMeshBuffer = SMeshBuffer.Create()
    ' make it use our vertex and index buffers
    lSMB.append(lVert, lInd)
    lSMB.recalculateBoundingBox()
    
' the mesh
Local lM:SMesh = SMesh.Create()
    ' make it use our mesh buffer
    lM.addMeshBuffer(lSMB)
    lM.recalculateBoundingBox()
    ' turn off lightning so we can see the colors
    lM.setMaterialFlag(EMF_LIGHTING, False)
    
' the actual scene that connects the mesh to the scene
Local IMSN:IMeshSceneNode = smgr.addMeshSceneNode(lM)
    ' show debug stuff, that white wireframe cube
    IMSN.setDebugDataVisible(True)

Print "sizes of our buffers are:"
    Print lSMB.getIndexCount()
    Print lSMB.getVertexCount()
    Print lM.getMeshBufferCount()
 
' fancy eye candy - make our mesh rotate!
Local anim:ISceneNodeAnimator = ..
    smgr.createRotationAnimator(_VECTOR3DF(0.0, 0.8, 0.0))
    IMSN.addAnimator(anim)

    
' normal sample app logic follows
Local frames:Int=0

While(device.run())
    
    Driver.beginScene(True, True, _SCOLOR(0, 200, 100, 100))

    smgr.drawAll()

    driver.endScene()
    frames:+1
    
    If (frames=100)
        Local str:String = "Testi ["
        str :+ driver.getName()
        str :+ "] FPS: "
        str :+ driver.getFPS()

        device.setWindowCaption(str)
        frames=0
    EndIf
Wend

device.drop()