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
x 2
Contact:

Drawing a Flexible Object made of Vertices (FAST)

Post by Garibalde »

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
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Feanor16
Halfling
Posts: 46
Joined: Tue Feb 18, 2014 10:49 pm

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

Post by Feanor16 »

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"
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

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

Post by al2950 »

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!)
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

I am currently using 1.10.8 Ogre version are these samples available for this version? I am not using 2.1 yet.
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

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

Post by al2950 »

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. :(
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

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

Thanks
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

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

Post by al2950 »

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.
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

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

Post by paroj »

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
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

Thanks very much
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

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
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

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????
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

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

Post by al2950 »

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!
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

Can you point me to the manual object code samples i should look at.
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

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

Post by al2950 »

I would start by looking at how manual object manipulates hardwareVertexBufferes like here;
https://bitbucket.org/sinbad/ogre/src/a ... ct.cpp-618
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

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

Post by Garibalde »

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
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

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

Post by Lax »

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

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

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

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

Post by Garibalde »

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.
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

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

Post by Lax »

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

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

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

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

Post by Garibalde »

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();
			}
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Lax
Hobgoblin
Posts: 583
Joined: Mon Aug 06, 2007 12:53 pm
Location: Saarland, Germany
x 50

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

Post by Lax »

ok thanks!

I will take a look as soon as possible.

Regards
Lax

http://www.lukas-kalinowski.com/Homepage/?page_id=1631
Please support Second Earth Technic Base built of Lego bricks for Lego ideas: https://ideas.lego.com/projects/81b9bd1 ... b97b79be62

Post Reply