Displaying Newton's ConvexHull in Irrlicht

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Displaying Newton's ConvexHull in Irrlicht

Post by Auradrummer »

Hello guys,

I suffered with a code here, and I think that is very useful for those who is using Irrlicht together with Newton Dynamics physics engine. This code allows you to pass a ConvexHull object to Irrlicht and see it without need to initialize any graphics interface by Newton. This way we keep Irrlicht for all graphics.

Code: Select all

ISceneNode* renderConvexHull(NewtonCollision* convexHull, video::SColor color, ISceneManager* smgr)
{
	NewtonMesh* mesh;
	SMesh* irrMesh;
	SMeshBuffer* buffer;
	dInt32 m_vertexCount;
	dInt32 m_indexCount;
	GLfloat *m_uv;
	GLfloat *m_vertex;
	GLfloat *m_normal;
	GLushort* m_indexes;
	int i = 0;
	int x = 0;
	int handle;
	
	mesh = NewtonMeshCreateFromCollision(convexHull);

	// getting vertexes position

  	m_vertexCount = NewtonMeshGetVertexCount (mesh);
	
	m_vertex = (GLfloat*) malloc (3 * m_vertexCount * sizeof (GLfloat)); 
	m_normal = (GLfloat*) malloc (3 * m_vertexCount * sizeof (GLfloat)); 
	m_uv = (GLfloat*) malloc (2 * m_vertexCount * sizeof (GLfloat)); 
	memset (m_uv, 0, 2 * m_vertexCount * sizeof (GLfloat));
		
	NewtonMeshGetVertexStreams (mesh,
		3 * sizeof (GLfloat), (dFloat*) m_vertex,
		3 * sizeof (GLfloat), (dFloat*) m_normal,
		2 * sizeof (GLfloat), (dFloat*) m_uv);

	// Getting face indexes.

	handle = NewtonMeshFirstMaterial(mesh);
	
	m_indexes = (GLushort *) malloc (m_indexCount * sizeof (GLushort )); 
	m_indexCount = NewtonMeshMaterialGetIndexCount (mesh, handle);
	
	NewtonMeshMaterialGetIndexStreamShort (mesh, handle, (short int*)m_indexes);

	// Creating SMesh
	
	buffer = new SMeshBuffer();

   	buffer->Indices.set_used(m_indexCount);
   	for (i=0; i<m_indexCount; ++i)
		buffer->Indices[i] = m_indexes[i];
	
	buffer->Vertices.set_used(m_vertexCount);
	for (i=0; i < m_vertexCount * 3; i+=3)
	{  
		buffer->Vertices[x]  = video::S3DVertex(m_vertex[i], m_vertex[i+1], m_vertex[i+2],1,1,1,color, 1, 1);
		x++;
	}
  	
  	irrMesh = new SMesh();
   	irrMesh->addMeshBuffer(buffer);

   	ISceneNode * collmesh = smgr->addMeshSceneNode(irrMesh);
	collmesh -> setMaterialFlag(EMF_LIGHTING,false);
	collmesh -> setMaterialFlag(EMF_WIREFRAME, true);

	NewtonMeshDestroy(mesh);
	return (collmesh);
}
That's it, I think this can be useful for more people :) !
Professional Software Developer and Amateur Game Designer ;-)
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Post by agi_shi »

Useful indeed, but only available in Newton Archimedia (not 1.53).
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

there is a better way to do this.
just use the dofor allbodies command and then draw lines for each newton object.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Post by agi_shi »

Sudi wrote:there is a better way to do this.
just use the dofor allbodies command and then draw lines for each newton object.
Not necessarily. The above code creates an actual mesh from a Newton collision object. Your suggestion doesn't create a mesh, it only draws a wireframe over-view.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

agi_shi wrote:
Sudi wrote:there is a better way to do this.
just use the dofor allbodies command and then draw lines for each newton object.
Not necessarily. The above code creates an actual mesh from a Newton collision object. Your suggestion doesn't create a mesh, it only draws a wireframe over-view.
It would appear that Auradrummer wants to see a ConvexHull object without the need to initialize the graphics interface Newton has.
Auradrummer wrote: This code allows you to pass a ConvexHull object to Irrlicht and see it without need to initialize any graphics interface by Newton.
I fail to see how wireframe wouldn't suffice, and also this method creates a second copy of the data basically just for use with Irrlicht. So really this is a waste of memory, and a waste of a scene node.

A wireframe overview doesn't suffer from any of these problems, and seeing as it's probably only used for debugging purposes, then I don't see speed entering into the equation.
TheQuestion = 2B || !2B
Post Reply