[2.2] Dynamic mesh editing

Problems building or running the engine, queries about how to use features etc.
Post Reply
ori30ffs
Gnoblar
Posts: 11
Joined: Tue Sep 18, 2018 6:38 am

[2.2] Dynamic mesh editing

Post by ori30ffs »

Ogre Version: :2.2.5:
Operating System: :Win10:
Render System: :Directx11:

Hello guys,
I'm working on porting my app from 1.12 to 2.2.5.
So, For several reason I need to edit my mesh at request.

For 1.12.x this code works fine.

Code: Select all

			Ogre::SubMesh* pSubMesh = mEntityMesh->getSubMesh(0);
			mEntityMesh->_setBounds(Ogre::AxisAlignedBox(mMin, mMax));
			const Ogre::VertexElement* VertexEle_POS = pSubMesh->vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);

			Ogre::HardwareVertexBufferSharedPtr VertexBufPOS = pSubMesh->vertexData->vertexBufferBinding->getBuffer(VertexEle_POS->getSource());
			unsigned char* VertexPtrPOS = static_cast<unsigned char*>(VertexBufPOS->lock(Ogre::HardwareBuffer::LockOptions::HBL_NORMAL));
			int VertSizePOS = VertexBufPOS->getVertexSize();

			float * pElementPOS = NULL;

			int nMaxVert = pSubMesh->vertexData->vertexCount;

			int count = 0;
			for (int nVert = 0; nVert < nMaxVert; nVert++)
			{
				VertexEle_POS->baseVertexPointerToElement(VertexPtrPOS, &pElementPOS);
				pElementPOS[0] = mVertex[count].mPosition.x;
				pElementPOS[1] = mVertex[count].mPosition.y;
				pElementPOS[2] = mVertex[count].mPosition.z;
				count++;
				VertexPtrPOS += VertSizePOS;
			}

			VertexBufPOS->unlock();
Now I have little bit different pipeline.
First of all, I have v1-Mesh as input data.

I need to load v1-Mesh -> convert to v2-Mesh -> edit

I found 2 ways to convert it to v2-Mesh:

Code: Select all

Ogre::MeshPtr meshV2 = Ogre::MeshManager::getSingleton().createByImportingV1(name, group, meshV1.get(), false, false, true);
that is what I need, but I cannot get inner data from it

Image

But when I use it:

Code: Select all

	Ogre::MeshPtr meshV2 = Ogre::MeshManager::getSingleton().createManual(name, group);
        meshV2->importV1(meshV1.get(), false, false, true);
Image

and the code which I use for editing:

Code: Select all

            Ogre::SubMesh* subMesh = mesh->getSubMesh(0);
            
            Ogre::VertexArrayObjectArray vaos = subMesh->mVao[0];

            if (!vaos.empty())
            {
                const Ogre::VertexBufferPackedVec& vertexBuffers = vaos[0]->getVertexBuffers();
                Ogre::IndexBufferPacked* indexBuffer = vaos[0]->getIndexBuffer();
                
                auto elems = vertexBuffers[0]->getVertexElements();
                auto count = vertexBuffers[0]->getNumElements();
                AsyncTicketPtr asyncTicket = vertexBuffers[0]->readRequest(0, vertexBuffers[0]->getNumElements());
                const uint8* vertexData = static_cast<const uint8*>(asyncTicket->map());
                void* data = malloc(vertexBuffers[0]->getTotalSizeBytes());
                memcpy(data, vertexData, vertexBuffers[0]->getTotalSizeBytes());
                asyncTicket->unmap();
                struct Vertices {
                    float px, py, pz;
                    float nx, ny, nz;
                    float texx, texy;
                };
                //some temp manipulation with data
                float* start = reinterpret_cast<float*>(data);
                float px = *start++;
                float py = *start++;
                float pz = *start++;

                py = 44;
                vertexBuffers[0]->upload(data, 0, vertexBuffers[0]->getNumElements());

            }
So, now I have 2 questions.
1. Why I cannot get inner data with createByImportingV1?
2. What is the best way to edit my mesh?

P.S. The final goal is hiding a part of model by mesh indices.
Best Regards
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: [2.2] Dynamic mesh editing

Post by dark_sylinc »

Hi!
ori30ffs wrote: Thu Apr 01, 2021 4:22 pm 1. Why I cannot get inner data with createByImportingV1?
Call meshV2->load() after createByImportingV1.
ori30ffs wrote: Thu Apr 01, 2021 4:22 pm 2. What is the best way to edit my mesh?
Btw you never freed data, which was leaking.

The code you posted is already close to the optimal way. There is something you can do if you want to squeeze the last bit of performance:

If we see the code of BufferInterface::upload, for BT_DEFAULT meshes it just memcpys the pointer you passed to a StagingBuffer. If you work on a StagingBuffer directly you can get rid of that memcpy. But the code will be more verbose and lower level. This is from BufferInterface::upload:

Code: Select all

StagingBuffer *stagingBuffer =
    vaoManager->getStagingBuffer( vertexBuffers[0]->getTotalSizeBytes(), true );

AsyncTicketPtr asyncTicket = vertexBuffers[0]->readRequest( 0, vertexBuffers[0]->getNumElements() );

float const *srcData = reinterpret_cast<float const *>( asyncTicket->map() );
float *dstData = reinterpret_cast<float *>( stagingBuffer->map( elementCount * bytesPerElement ) );

memcpy( dstData, srcData, vertexBuffers[0]->getTotalSizeBytes() );

*dstData[47] = 12.0f;  // Change it.

// Copy data from Staging to real buffer (GPU -> GPU)
stagingBuffer->unmap( StagingBuffer::Destination( vertexBuffers[0],
                                                  vertexBuffers[0]->getTotalSizeBytes(), 0,
                                                  vertexBuffers[0]->getTotalSizeBytes() ) );
stagingBuffer->removeReferenceCount();
asyncTicket->unmap();
The code is more direct (assuming the vertex buffer is BT_DEFAULT) and does not handle updating the shadowCopy (you could do it by hand if you const cast bufferPacked->getShadowCopy, although the const cast feels a bit dirty). This alternate code allows you to get rid of one memcpy, which can be a big deal if the mesh is very big.

If maximum performance isn't a concern then consider your original code which is friendlier and can handle any API detail changes in the future.
ori30ffs
Gnoblar
Posts: 11
Joined: Tue Sep 18, 2018 6:38 am

Re: [2.2] Dynamic mesh editing

Post by ori30ffs »

Thank you so much! Now it's more clear for me!
Post Reply