convert opengl to irrlicht(about physx)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

convert opengl to irrlicht(about physx)

Post by lizhigang34 »

I am learning physx.I need create a cloth.I found it very hard to learn physx,here is the code of MyCloth written by OpenGL without any comment.I didn't learn OpenGL before.How to convert OpenGL code to Irrlicht.
/////// define some Nx argument //////
class MyCloth {
NxMeshData mReceiveBuffers;
NxScene *mScene;
NxCloth *mCloth;
NxClothMesh *mClothMesh;

GLfloat *mTexCoords;
NxU32 mNumTexCoords;
GLuint mTexId;
}
//this function written for draw the cloth

Code: Select all


void MyCloth::draw(bool shadows)
{
	NxU32 numVertices = *mReceiveBuffers.numVerticesPtr; 
	NxU32 numTriangles = *mReceiveBuffers.numIndicesPtr / 3;

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, mReceiveBuffers.verticesPosBegin);
	glNormalPointer(GL_FLOAT, 0, mReceiveBuffers.verticesNormalBegin);

	if (mTexId) {
		updateTextureCoordinates();
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glTexCoordPointer(2, GL_FLOAT, 0, mTexCoords);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, mTexId);
		glColor4f(1.0f, 1.0f, 1.0f,1.0f);
	}

	glDrawElements(GL_TRIANGLES, 3*numTriangles, GL_UNSIGNED_INT, mReceiveBuffers.indicesBegin);

	if (mTexId) {
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glDisable(GL_TEXTURE_2D);
	}

	if (shadows) {
		const static float ShadowMat[]={ 1,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,1 };
		glPushMatrix();
		glMultMatrixf(ShadowMat);
		glDisable(GL_LIGHTING);
		glColor4f(0.05f, 0.1f, 0.15f,1.0f);
		glDrawElements(GL_TRIANGLES, 3*numTriangles, GL_UNSIGNED_INT, mReceiveBuffers.indicesBegin);
		glColor4f(1.0f, 1.0f, 1.0f,1.0f);
		glEnable(GL_LIGHTING);
		glPopMatrix();
	}

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
}
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I've done this in IrrPhysx, If you don't want to use IrrPhysx you can still look at the source for how I've got the cloth stuff done, it ain't particularly pretty though so might be a bit hard to follow!
Image Image Image
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

Thanks JP..I've seen your code,It's not fit for me.you got the cloth stuff from mesh,I run the cloth example,when I set the triangle scale of cloth bigger than sphere,the sphere will fly through the cloth without collision.I want to control the number and scale of triangle of the cloth,I don't want to create mesh use other software.
I read the demo of PhysX,I got the mesh data of cloth such as vertices position,texture coordinate.I don't know how to draw a cloth with these data use Irrlicht.
This is why I pose the question how to convert OpenGL to Irrlicht,How to rewrite the function MyCloth::draw().
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Well the reason the sphere goes through the cloth is because Physx cloth only supports vertex collision, so objects will only collide with the vertices of the cloth, not the triangles, so large triangles will allow objects through.

If you don't want to use a mesh created in a modelling program you can create a flat 'hill plane' mesh with the smgr which creates a grid mesh and allows you to specify the sizes of the cells of the grid and how many cells there are to make up the grid.
Image Image Image
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

Thanks,,
I'll have a try. :wink:
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

Hi,JP.
I am reading your code conscientiously.I try to divide part function that's useful for me from your lib.
and I have some questions..

1. about render cloth
Now,I can render the cloth,but can't render the debug data.strangely,except cloth,I can render other NxActor debug data,such as cube , sphere, and ground plane.what's more,the effect of render cloth is bad.I don't use CCloth::update() and CCloth::updateTextureCoordinates(),It's also render the cloth.as following image.
Image
2. Cut the cloth
I want simulate cutting the cloth,which function should I do?tearVertex()?
If you need cut the cloth,how will you do?
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

What amaze me is how you attach PhysX to Irrlicht.
I mean one hand is collision detecting of object and the other is render object with irrlicht.How do you achieve that?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

If you want to cut the cloth then yes you need to tear the necessary vertices.

If you're not calling CCloth update functions then how can you expect it to work correctly?

I'm not sure what your above post is saying about rendering and collision detection...
Image Image Image
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

JP wrote:If you want to cut the cloth then yes you need to tear the necessary vertices.

If you're not calling CCloth update functions then how can you expect it to work correctly?

I'm not sure what your above post is saying about rendering and collision detection...
I'm sorry for my poor English. :cry:
I don't know how to use tearVertex(),I use raycast() find the right vertex for the first parameter, but,what is the the normal of the split plane?

When do you call CCloth::update()?I don't find the code that you call it exactly.Is it called in class CPhysxManager?

I mean the PhysX handles collision detecting while the Irrlicht draw the object,when a cloth teared,how you update the cloth sceneNode?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

the cloth scene node shares the same buffers as the physx cloth so that when physx updates how the cloth mesh should be the cloth scene node has the same buffer so knows as well and just renders it accordingly.

I'm not sure how important the normal of the split plane is... If you look at the IrrPhysx source for where that's defined you can probably see what I used and maybe use the same..?

Off the top of my head I don't know where CCloth::update is called... Presumably it must be called somewhere so just find where the CCloth instance is held and find out when that variable has gets update called on it?
Image Image Image
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

Thanks JP.
The IrrPhysX is written by you,Don't you know when called CCloth::update().I think I must be wrong in here,so cann't draw debug data of the cloth,and the effect of render of the cloth is bad.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Yes i wrote the code but that was quite a few months ago and I don't have the code to hand to check where it is, it'll be just as fast for you to find it yourself ;)
Image Image Image
lizhigang34
Posts: 49
Joined: Sun Dec 21, 2008 4:11 pm

Post by lizhigang34 »

Hi,JP
Do you know how to construct a S3DVertex from NxMeshData??
I mean the NxMeshData stores vertex positions in verticesPosBegin,stores vertex normals in verticesNormalBegin,and stores vertex indices in indicesBegin,I want to use IVideoDriver::drawIndexedTriangleList() to draw a cloth.But,I only draw the part of the cloth.

My code as following,where is my wrong do you think?

Code: Select all

NxVec3* pPos = (NxVec3*)mReceiveBuffers.verticesPosBegin; //顶点位置
	NxVec3* pNormal = (NxVec3*)mReceiveBuffers.verticesNormalBegin;//顶点法线
	NxU32 maxVertices = mReceiveBuffers.maxVertices; //最大顶点数量
	u16* Indices = (u16*)mReceiveBuffers.indicesBegin; //索引

	NxU32* NumVertices = mReceiveBuffers.numVerticesPtr;
	NxU32* NumIndices = mReceiveBuffers.numIndicesPtr;

	int num = (int)*NumVertices;
	S3DVertex* pVertices = new S3DVertex[num];
	for (int i = 0; i < num; i++)
	{
		pVertices[i].Pos = core::vector3df(pPos[i].x, pPos[i].y, pPos[i].z);
		pVertices[i].Normal = core::vector3df(pNormal[i].x, pNormal[i].y, pNormal[i].z);
		pVertices[i].Color = SColor(255, 255, 255, 0);		
	}
	driver->drawIndexedTriangleList(pVertices, num, Indices, *NumIndices / 3);
	delete pVertices;
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Looks fine to me at first glance... Maybe someone else can chip in their 2 cents....

So you just get part of the cloth being rendered and not all of it?
Image Image Image
Post Reply