Page 1 of 1

SubMesh examples

Posted: Thu Oct 18, 2018 3:49 am
by rrl
I'm trying to create several separate meshes in 2.1 that together make a whole mesh, but display them separately. There used to be a sample in 2.1 that used SubMesh's and IIRC, had a function named createStaticMesh, but I can no longer find this. Any know where that might have gone. I've grep'd the entire Sample tree but can't find it.

Re: SubMesh examples

Posted: Thu Oct 18, 2018 6:42 am
by dark_sylinc

Re: SubMesh examples

Posted: Sat Oct 20, 2018 4:04 am
by rrl
In this sample, DynamicGeometryGameState::createScene01() calls createStaticMesh multiple times where upon each call, it calls createManual(...).

Are the MeshPtr's mStaticMesh and mPartialMesh (within DynamicGeometryGameState::createScene01) examples of two separate meshes, and not two sub meshes of the same mesh? Each call of createManual is a separate mesh, correct?

Just trying to understand when to use a submesh. I have several models and want to show/hide each wireframe, have each wireframe model appear in different colors, etc...

Re: SubMesh examples

Posted: Sat Oct 20, 2018 4:38 am
by dark_sylinc
rrl wrote: Sat Oct 20, 2018 4:04 am Are the MeshPtr's mStaticMesh and mPartialMesh (within DynamicGeometryGameState::createScene01) examples of two separate meshes, and not two sub meshes of the same mesh? Each call of createManual is a separate mesh, correct?
That's correct.
rrl wrote: Sat Oct 20, 2018 4:04 am Just trying to understand when to use a submesh. I have several models and want to show/hide each wireframe, have each wireframe model appear in different colors, etc...
Ogre has the following hierarchy:
  • Mesh
    • SubMesh 0
    • SubMesh 1
    • ...
    • SubMesh N
And likewise instantiation is done through Items:
  • Item (instantiates Mesh)
    • SubItem 0 (instantiates SubMesh 0)
    • SubItem 1 (instantiates SubMesh 1)
    • ...
    • SubItem N (instantiates SubMesh N)
Mesh contains information such as AABB that encloses all of its Submeshes, list of LOD distances, references the associated skeleton.
Submeshes contains the actual geometry and a material. Each SubMesh can only have one material.

Items contain position/orientation/scale information, AABB for frustum culling, can toggle themselves visible or not (via setVisible). SubItems contain material information, and reference the SubMesh.

SubItems cannot normally be turned on/off at will, Items can. BUT which Renderables will be rendered is determined by MovableObject::mRenderables which is a public member.
You could theoretically manipulate MovableObject::mRenderables from an Item to hide a particular SubItem/SubMesh. However this is probably a bit hacky and thus I suggest you restore mRenderables back to how it was before destroying the Item.
I have several models and want to show/hide each wireframe, have each wireframe model appear in different colors, etc...
If it's the same exact model just with different material, then I suggest you create multiple Items based on the same Mesh, and change the materials of these Items. Items can change their material. The one in the Mesh is just "the default one", but it is not immutable.

Re: SubMesh examples

Posted: Mon Aug 30, 2021 10:48 am
by dcavallini
It can be useful to novices.
This is an example of a mesh formed by two submeshes with different colors with NO useSharedVertices.

Code: Select all

//first submesh
float vertices[32] = {
        -100, -100, 0,  // pos
        0,0,1,          // normal
        0,1,            // texcoord
        100, -100, 0,
        0,0,1,
        1,1,
        100,  100, 0,
        0,0,1,
        1,0,
        -100,  100, 0 ,
        0,0,1,
        0,0
    };
// second submesh
   float vertices1[32] = {
        -100, 0, -100,  // pos
        0,1,0,          // normal
        0,1,            // texcoord
        100, 0, -100,
        0,1,0,
        1,1,
        100,  0, 100,
        0,1,0,
        1,0,
        -100,  0, 100 ,
        0,1,0,
        0,0
    };
  uint16 faces[6] = {0,1,2,
                       0,2,3 };

    uint16 faces1[6] = {0,1,2,
                       0,2,3 };


    MeshPtr mainMesh = MeshManager::getSingleton().createManual("mainMash", RGN_DEFAULT);


// set bounds
    mainMesh->_setBounds(AxisAlignedBox({-100,-100,-100}, {100,100,100}));

    SubMesh* sub1 = mainMesh->createSubMesh();
    sub1->vertexData= new VertexData();
    VertexDeclaration* decl = sub1->vertexData->vertexDeclaration;
    VertexBufferBinding* bind = sub1->vertexData->vertexBufferBinding;
    size_t offset = 0;
    offset += decl->addElement(0, offset, VET_FLOAT3, VES_POSITION).getSize();
    offset += decl->addElement(0, offset, VET_FLOAT3, VES_NORMAL).getSize();
    offset += decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0).getSize();
    HardwareVertexBufferSharedPtr vbuf =
            HardwareBufferManager::getSingleton().createVertexBuffer(offset, 4, HBU_GPU_ONLY);
    vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true);
    bind->setBinding(0, vbuf);

    HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().createIndexBuffer(
                HardwareIndexBuffer::IT_16BIT, 6, HBU_GPU_ONLY);
    ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true);


    sub1->useSharedVertices = false;
    sub1->indexData->indexBuffer = ibuf;
    sub1->indexData->indexCount = 6;
    sub1->indexData->indexStart = 0;


    sub1->vertexData->vertexCount = 4;
    sub1->vertexData->vertexStart = 0;
    sub1->setMaterialName("color1");


//****

    SubMesh* sub2 = mainMesh->createSubMesh();
    sub2->vertexData= new VertexData();

    VertexDeclaration* decl1 = sub2->vertexData->vertexDeclaration;
    VertexBufferBinding* bind1 = sub2->vertexData->vertexBufferBinding;
    size_t offset1 = 0;
    offset1 += decl1->addElement(0, offset1, VET_FLOAT3, VES_POSITION).getSize();
    offset1 += decl1->addElement(0, offset1, VET_FLOAT3, VES_NORMAL).getSize();
    offset1 += decl1->addElement(0, offset1, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0).getSize();


    HardwareVertexBufferSharedPtr vbuf1 =
            HardwareBufferManager::getSingleton().createVertexBuffer(offset1, 4, HBU_GPU_ONLY);
    vbuf1->writeData(0, vbuf1->getSizeInBytes(), vertices1, true);
    bind1->setBinding(0, vbuf1);

    HardwareIndexBufferSharedPtr ibuf1 = HardwareBufferManager::getSingleton().createIndexBuffer(
                HardwareIndexBuffer::IT_16BIT, 6, HBU_GPU_ONLY);
    ibuf1->writeData(0, ibuf1->getSizeInBytes(), faces1, true);


    sub2->useSharedVertices = false;
    sub2->indexData->indexBuffer = ibuf1;
    sub2->indexData->indexCount = 6;
    sub2->indexData->indexStart = 0;


    sub2->vertexData->vertexCount = 4;
    sub2->vertexData->vertexStart = 0;
    sub2->setMaterialName("color2");

// create an entity
    Entity* meshEntity1 = scnMgr->createEntity("mainMash");
    int size=meshEntity1->getSubEntities().size();

  //   meshEntity1->setMaterialName("Materiale blu");

// attach object
    Ogre::SceneNode* node1 = scnMgr->getRootSceneNode()->createChildSceneNode();
    node1->attachObject(meshEntity1);
User with more experienced can confirm the correctness
Image

Re: SubMesh examples

Posted: Mon Aug 30, 2021 5:17 pm
by dark_sylinc
dcavallini wrote: Mon Aug 30, 2021 10:48 am It can be useful to novices.
This is an example of a mesh formed by two submeshes with different colors with NO useSharedVertices.

User with more experienced can confirm the correctness
Image
That's a v1 example, the post was about v2 examples. v1 meshes can be converted to v2 though (see Samples/2.0/ApiUsage/V2Mesh)