Drawing a Flexible Object made of Vertices (FAST)

Problems building or running the engine, queries about how to use features etc.
Post Reply
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Wed Aug 30, 2017 3:22 pm

HI

I am looking to see the fastest possible way to draw a soft object made of vertices in OGRE.

I have an physics base model of an object that will calculate the vertex position of my VISUAL object vertices
every frame. I would like to draw this object to OGRE.

I am currently using a ManualObject that I create and draw the element and destroy each iteration. I think this
is a slow process. I am looking to see if there is a faster way to do this. If you have examples to provide that would
be great too.

Thanks
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Feanor16
Halfling
Posts: 44
Joined: Tue Feb 18, 2014 10:49 pm

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Feanor16 » Thu Aug 31, 2017 9:13 am

ManualObject is outdated in Ogre 2.1 but you can use
Ogre::MeshPtr then create a Mesh mannually with getSingleton().createManual(const String& name, const String& groupName, ManualResourceLoader* loader = 0)
then create a VertexDeclaration to make your vertex point per point with indices

but that is a static build. To have something dynamic, Check out the samples "CustomRenderable" and "DynamicGeometry"
0 x

al2950
OGRE Expert User
OGRE Expert User
Posts: 1029
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by al2950 » Thu Aug 31, 2017 11:00 am

as Feanor16 said check out the "CustomRenderable" and "DynamicGeometry".

Do not use ManualObject even for static meshes! It was put in to help people port projects from 1.x, but it does not make sense in 2.1 and it is lacking a lot of features and is slooooooooooooooooooooow! Happy to help if you have any questions regarding the 2 sample projects. It may take a few attempts to get your head around it, but when you do it is very easy, (I would say it is easier than ManualObject.. or at least easier to follow for me anyway!)
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Thu Aug 31, 2017 6:28 pm

I am currently using 1.10.8 Ogre version are these samples available for this version? I am not using 2.1 yet.
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

al2950
OGRE Expert User
OGRE Expert User
Posts: 1029
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by al2950 » Thu Aug 31, 2017 7:56 pm

Sorry i jumped to the conclusion you were using 2.1.

Its been a while since i looked at ogre 1.x, however I had to do something similar and i used ManualObject. I believe manual object has a 'dynamic' mode, and you can reserve the max number of vertices you are going to use and then you can update it each frame.

ill try and dig out some old code if you get stuck.

FYI, it still might not be as fast as you require, so you may need to deal with hardware buffers which is not pleasant in 1.x. :(
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Sun Sep 03, 2017 1:28 am

If you have a sample code for this that would be greatly appreciated.

Thanks
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

al2950
OGRE Expert User
OGRE Expert User
Posts: 1029
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by al2950 » Sun Sep 03, 2017 12:11 pm

Something like this;

on construction:

Code: Select all

        m_manualObject = m_sceneMgr->createManualObject();
        m_manualObject->setDynamic(true);
        m_manualObject->estimateVertexCount(totalPossibleVertices);
        m_manualObject->estimateIndexCount(totalPossibleIndices);

        //put some initial data in
        m_manualObject->begin(m_material, Ogre::RenderOperation::OT_TRIANGLE_LIST);
        //fill in data ....
        m_manualObject->end();
on update:

Code: Select all

	//reset boundung box before update
	m_manualObject->setBoundingBox(Ogre::AxisAlignedBox::EXTENT_FINITE);

	//begin update of vertex buffers
	m_manualObject->beginUpdate(0);
       //fill in data just like you would normally ....
	m_manualObject->end();

        //dont forget to update the bounding box of the parent scene node.
	m_parentNode->_updateBounds();
I think that should work! But again its not the most efficient way of doing things, but should be better than creating a new manual object every frame.
0 x

paroj
OGRE Team Member
OGRE Team Member
Posts: 307
Joined: Sun Mar 30, 2014 2:51 pm
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by paroj » Sun Sep 03, 2017 12:21 pm

the suggestion by al2950 is quite good already.

If your data is stored consecutively, you might save a memcpy by writing to the HardwareVertexBuffers directly:
http://www.ogre3d.org/tikiwiki/Generating+A+Mesh
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Mon Sep 04, 2017 2:48 pm

Thanks very much
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Wed Sep 13, 2017 9:17 pm

I could use some direction here Please :)

Code: Select all

void MyTestApp::createColourCube()
{
	/// Create the mesh via the MeshManager
	Ogre::MeshPtr msh = Ogre::MeshManager::getSingleton().createManual("ColourCube", "General");

	/// Create one submesh
	Ogre::SubMesh* sub = msh->createSubMesh();

	const float sqrt13 = 0.577350269f; /* sqrt(1/3) */

									   /// Define the vertices (8 vertices, each have 3 floats for position and 3 for normal)
	const size_t nVertices = 8;
	const size_t vbufCount = 3 * 2 * nVertices;
	float vertices[vbufCount] = {
		-10.0,10.0,-10.0,        //0 position
		-sqrt13,sqrt13,-sqrt13,     //0 normal
		10.0,10.0,-10.0,         //1 position
		sqrt13,sqrt13,-sqrt13,      //1 normal
		10.0,-10.0,-10.0,        //2 position
		sqrt13,-sqrt13,-sqrt13,     //2 normal
		-10.0,-10.0,-10.0,       //3 position
		-sqrt13,-sqrt13,-sqrt13,    //3 normal
		-10.0,10.0,10.0,         //4 position
		-sqrt13,sqrt13,sqrt13,      //4 normal
		10.0,10.0,10.0,          //5 position
		sqrt13,sqrt13,sqrt13,       //5 normal
		10.0,-10.0,10.0,         //6 position
		sqrt13,-sqrt13,sqrt13,      //6 normal
		-10.0,-10.0,10.0,        //7 position
		-sqrt13,-sqrt13,sqrt13,     //7 normal
	};

	Ogre::RenderSystem* rs = Ogre::Root::getSingleton().getRenderSystem();
	Ogre::RGBA colours[nVertices];
	Ogre::RGBA *pColour = colours;
	// Use render system to convert colour value since colour packing varies
	rs->convertColourValue(Ogre::ColourValue(1.0, 0.0, 0.0), pColour++); //0 colour
	rs->convertColourValue(Ogre::ColourValue(1.0, 1.0, 0.0), pColour++); //1 colour
	rs->convertColourValue(Ogre::ColourValue(0.0, 1.0, 0.0), pColour++); //2 colour
	rs->convertColourValue(Ogre::ColourValue(0.0, 0.0, 0.0), pColour++); //3 colour
	rs->convertColourValue(Ogre::ColourValue(1.0, 0.0, 1.0), pColour++); //4 colour
	rs->convertColourValue(Ogre::ColourValue(1.0, 1.0, 1.0), pColour++); //5 colour
	rs->convertColourValue(Ogre::ColourValue(0.0, 1.0, 1.0), pColour++); //6 colour
	rs->convertColourValue(Ogre::ColourValue(0.0, 0.0, 1.0), pColour++); //7 colour

																   /// Define 12 triangles (two triangles per cube face)
																   /// The values in this table refer to vertices in the above table
	const size_t ibufCount = 36;
	unsigned short faces[ibufCount] = {
		0,2,3,
		0,1,2,
		1,6,2,
		1,5,6,
		4,6,5,
		4,7,6,
		0,7,4,
		0,3,7,
		0,5,1,
		0,4,5,
		2,7,3,
		2,6,7
	};

	/// Create vertex data structure for 8 vertices shared between submeshes
	msh->sharedVertexData = new Ogre::VertexData();
	msh->sharedVertexData->vertexCount = nVertices;

	/// Create declaration (memory format) of vertex data
	Ogre::VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration;
	size_t offset = 0;
	// 1st buffer
	decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
	offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
	decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
	offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
	/// Allocate vertex buffer of the requested number of vertices (vertexCount) 
	/// and bytes per vertex (offset)
	Ogre::HardwareVertexBufferSharedPtr vbuf =
		Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
			offset, msh->sharedVertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
	/// Upload the vertex data to the card
	vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true);

	/// Set vertex buffer binding so buffer 0 is bound to our vertex buffer
	Ogre::VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding;
	bind->setBinding(0, vbuf);

	// 2nd buffer
	offset = 0;
	decl->addElement(1, offset, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
	offset += Ogre::VertexElement::getTypeSize(Ogre::VET_COLOUR);
	/// Allocate vertex buffer of the requested number of vertices (vertexCount) 
	/// and bytes per vertex (offset)
	vbuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
		offset, msh->sharedVertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
	/// Upload the vertex data to the card
	vbuf->writeData(0, vbuf->getSizeInBytes(), colours, true);

	/// Set vertex buffer binding so buffer 1 is bound to our colour buffer
	bind->setBinding(1, vbuf);

	/// Allocate index buffer of the requested number of vertices (ibufCount) 
	Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().
		createIndexBuffer(
			Ogre::HardwareIndexBuffer::IT_16BIT,
			ibufCount,
			Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);

	/// Upload the index data to the card
	ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true);

	/// Set parameters of the submesh
	sub->useSharedVertices = true;
	sub->indexData->indexBuffer = ibuf;
	sub->indexData->indexCount = ibufCount;
	sub->indexData->indexStart = 0;

	/// Set bounding information (for culling)
	msh->_setBounds(Ogre::AxisAlignedBox(-10, -10, -10, 10, 10, 10));
	msh->_setBoundingSphereRadius(Ogre::Math::Sqrt(3 * 10 * 10));

	/// Notify -Mesh object that it has been loaded
	msh->load();
}
Above code is from the wiki provided above. I have used it as follows:

In my project during setup I do the following (I can see the static cube drawn).

Code: Select all

        createColourCube();

	mEntity = mScnMgr->createEntity("cc", "ColourCube");
	mEntity->setMaterialName("Examples/BeachStones");
	mSceneNode = mScnMgr->getRootSceneNode()->createChildSceneNode();
	mSceneNode->setPosition(0, 0, 100);
	mSceneNode->attachObject(mEntity);
I changed the vertexdata and did a write buffer in frameRenderingQueued() as below.
So I assume to see a change in shape at least from the initial 10x10x10 to 25x25x25 after this
writedata executes... But there is no change in shape the cube is still 10x10x10.

Code: Select all

	const float sqrt13 = 0.577350269f;
	float vertices[48] = {
		-25.0,25.0,-25.0,        //0 position
		-sqrt13,sqrt13,-sqrt13,     //0 normal
		25.0,25.0,-25.0,         //1 position
		sqrt13,sqrt13,-sqrt13,      //1 normal
		25.0,-25.0,-25.0,        //2 position
		sqrt13,-sqrt13,-sqrt13,     //2 normal
		-25.0,-25.0,-25.0,       //3 position
		-sqrt13,-sqrt13,-sqrt13,    //3 normal
		-25.0,25.0,25.0,         //4 position
		-sqrt13,sqrt13,sqrt13,      //4 normal
		25.0,25.0,25.0,          //5 position
		sqrt13,sqrt13,sqrt13,       //5 normal
		25.0,-25.0,25.0,         //6 position
		sqrt13,-sqrt13,sqrt13,      //6 normal
		-25.0,-25.0,25.0,        //7 position
		-sqrt13,-sqrt13,sqrt13,     //7 normal
	};

	mvbuf->writeData(0, mvbuf->getSizeInBytes(), vertices, true);
What am I doing wrong to update the cube to 25x25x25 as the vertexdata in the frameRenderingQueued() shows..
Do I have to explicitly call render? if so how do I do that?

Thanks
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Fri Sep 15, 2017 10:24 pm

I am stuck.. I think I have tried everything I can to update the vertices of the cube after it is first created.
I see the 10x10x10 cube in the render window.... but it never becomes 25x25x25 cube after the frameRenderingQueued()
its like it never gets the new data or does not update the draw on the object.

What am I doing wrong????
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

al2950
OGRE Expert User
OGRE Expert User
Posts: 1029
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by al2950 » Mon Sep 18, 2017 10:10 pm

Sorry I never got my hands dirty with ogre 1.x hardwareVertexBuffers. I would suggest looking through the manual object code to get some ideas!
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Tue Sep 19, 2017 3:45 am

Can you point me to the manual object code samples i should look at.
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

al2950
OGRE Expert User
OGRE Expert User
Posts: 1029
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by al2950 » Tue Sep 19, 2017 9:39 am

I would start by looking at how manual object manipulates hardwareVertexBufferes like here;
https://bitbucket.org/sinbad/ogre/src/a ... ct.cpp-618
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Tue Sep 19, 2017 4:00 pm

Is it worth switching to 2.1? I am not great at Ogre. So is it stable enough for rendering objects.
My use is for rendering flexible structures with changing vertices positions. Also have static mesh also.

I would like textures and all the shaders. I assume 2.1 is in development so has bugs and issues that
I would not be able to resolve myself.

Do you advice in my case to switch to 2.1 is a benefit?

Thanks
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Lax
Goblin
Posts: 263
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Lax » Sat Oct 28, 2017 12:00 pm

Hi Garibalde,
Is it worth switching to 2.1?
I think yes, in any case! Since there are a lot of new functionalities and its using next-gen technologies like physically based shading and is much more performant etc.
I switched to Ogre2.1 and it was a longer process, but I'm happy I did. Nevertheless you can still use entities, manual objects from the v1 namespace. I do, because in Ogre 2.1 there is no Ogre::Entity anymore but instead Ogre::Item, but I think Ogre::Item is still in development process. So I'm using Ogre::v1::Entity.

But back to your main questions here. Did you manage to create your Mesh at runtime for soft-bodies?

Because I started to integrate for OgreNewt3.0 soft bodies technique and I'm also facing issues in changing the mesh at runtime.
I'm really not good at the whole vertex topics...

In Ogre2.1 you can use the VAO manager, which is much more performant as the old technology.

Regards
Lax
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Tue Oct 31, 2017 12:49 pm

Yes I was able to use the createmanualobject() function and set the position, uv etc..
Then during update call beginUdpate(). As posted above. Seems to work fine so far for me.
I have used any materials and stuff yet... I am working on that now. so its pretty plain.

If you like me to post the code I could. Let me know.
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Lax
Goblin
Posts: 263
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Lax » Tue Oct 31, 2017 5:49 pm

Hi Garibalde,
If you like me to post the code I could. Let me know.
Yes please. At the moment I struggle with newton physics, as the softbody functionality is broken. But when the author will fix it. I'm still unsure if the mesh re-generation will work correctly. So having any help would be appreciated.

Regards
Lax
0 x

Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
Contact:

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde » Tue Nov 07, 2017 2:48 pm

Here it is.

I call CreateManObj() the first time to create the object.
then when there is a change in state I call UpdateManObj()

Code: Select all

			void OgreVisualModel::CreateManObj(	Ogre::ManualObject *ogreObject,
												const std::string &materialName,
												const ResizableExtVector<Coord>& positions,
												const ResizableExtVector<Coord>& normals,
												const ResizableExtVector<TexCoord>& textCoords,
												const ResizableExtVector<Triangle>& triangles,
												const ResizableExtVector<Quad>& quads) const
			{
				const bool hasTexCoords = !(textCoords.empty());

				mManObj->begin(materialName, Ogre::RenderOperation::OT_TRIANGLE_LIST);

				ResizableExtVector<Coord>::const_iterator itp = positions.begin();
				ResizableExtVector<Coord>::const_iterator itn = normals.begin();
				ResizableExtVector<TexCoord>::const_iterator ittex = textCoords.begin();
				ResizableExtVector<Quad>::const_iterator itq = quads.begin();
				ResizableExtVector<Triangle>::const_iterator itt = triangles.begin();

				for (int i=0; i < positions.size(); i++)
				{ 
					mManObj->position(itp->elems[0], itp->elems[1], itp->elems[2]);
					mManObj->normal(itn->elems[0], itn->elems[1], itn->elems[2]);
					if (hasTexCoords)
						mManObj->textureCoord(ittex->elems[0], ittex->elems[1]);

					itp++; itn++; ittex++;
				}

				for (int k = 0; k < triangles.size(); k++)
				{
					mManObj->triangle(itt->elems[0], itt->elems[1], itt->elems[2]);
					itt++;
				}

				for (int j = 0; j < quads.size(); j++)
				{
					mManObj->quad(itq->elems[0], itq->elems[1], itq->elems[2], itq->elems[3]);
					itq++;
				}

				mManObj->end();
			}

			void OgreVisualModel::UpdateManObj(	const ResizableExtVector<Coord>& positions,
												const ResizableExtVector<Coord>& normals,
												const ResizableExtVector<TexCoord>& textCoords,
												const ResizableExtVector<Triangle>& triangles,
												const ResizableExtVector<Quad>& quads) const
			{
				const bool hasTexCoords = !(textCoords.empty());

				mManObj->beginUpdate(0);

				ResizableExtVector<Coord>::const_iterator itp = positions.begin();
				ResizableExtVector<Coord>::const_iterator itn = normals.begin();
				ResizableExtVector<TexCoord>::const_iterator ittex = textCoords.begin();
				ResizableExtVector<Quad>::const_iterator itq = quads.begin();
				ResizableExtVector<Triangle>::const_iterator itt = triangles.begin();

				for (int i = 0; i < positions.size(); i++)
				{
					mManObj->position(itp->elems[0], itp->elems[1], itp->elems[2]);
					mManObj->normal(itn->elems[0], itn->elems[1], itn->elems[2]);
					if (hasTexCoords)
						mManObj->textureCoord(ittex->elems[0], ittex->elems[1]);

					itp++; itn++; ittex++;
				}

				for (int k = 0; k < triangles.size(); k++)
				{
					mManObj->triangle(itt->elems[0], itt->elems[1], itt->elems[2]);
					itt++;
				}

				for (int j = 0; j < quads.size(); j++)
				{
					mManObj->quad(itq->elems[0], itq->elems[1], itq->elems[2], itq->elems[3]);
					itq++;
				}

				mManObj->end();
			}
0 x
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com

Lax
Goblin
Posts: 263
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany

Re: Drawing a Flexible Object made of Vertices (FAST)

Post by Lax » Sat Nov 11, 2017 2:56 pm

ok thanks!

I will take a look as soon as possible.

Regards
Lax
0 x

Post Reply