How to modify existing mesh?

Everything related to 3D programming
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

How to modify existing mesh?

Post by mrw »

Hi,
I have a problem understanding how to modify a previously created mesh.
When I read the documentation I´m just getting confused. It says that using CreateMesh() you can set a mesh to be either Static or Dynamic and only Dynamic can be updated. So far so good.
But the example "SetMeshData.pb" changes the mesh on a Static mesh using SetMeshData() so why does this work? Is this command just an exception to the Static/Dynamic rule?
And how do I create a dynamic mesh? Whatever I chose with the CreateMesh() command seems to be overwritten by the FinishMesh() command which converts it to either a Static(#True) or Dynamic(#False). So what´s the point of having that option with the CreateMesh() command?
Also whenever I use FinishMesh(#False) my application doesn´t display the created mesh at all and when I close the application it just crashes instead of just exiting. This tells me that there are differences in capabilities between Static and Dynamic meshes, but what?

So how should I best create and modify a mesh in realtime? I want to be able to change vertexcoordinates, normals, UVs, adding/removing vertices and triangles. Basically everything.

Another issue is how I get the specific vertex or triangle I want to change. All the commands seems to loop through from the beginning and this is a problem if I want to have each vertex and triangle set as an indexnumber.

Imagine I create a 3D tool like Lightwave and 3DStudioMax. I need to control the mesh completely. Selecting a vertex or triangle and changing all values on that.
How would I accomplish that?

//Andreas..
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Re: How to modify existing mesh?

Post by mrw »

Thanks for your reply,

For UpdateMesh() I assume the mesh need to be Dynamic? My first problem with that is that I can´t create a dynamic mesh. I thought I only needed to change the flags in CreateMesh() and FinishMesh() but when I do that I don´t get anything visible. What could be the reasons for that? Is there anything that´s not supported when using a dynamic mesh?

But if I get that to work, as an example, how would I select triangle 17 and remove it?

Or if SetMeshData() is better, how would I do the above using that command?


//Andreas..
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Re: How to modify existing mesh?

Post by mrw »

I use v5.42.
But I found out why dynamic meshes wasn´t displayed. I use CreateEntity() to create lots of entitys using this same mesh, and in the documentation for CreateEntity() it says that dynamic meshes are not supported.
This in itself is really weird since how else should I use a dynamic mesh??

But I will experiment with SetMeshData() as you suggest and this command apparently CAN alter a static mesh, even if the documentation says otherwise.
Seems like a good idea to alter the mesh in the array and then set all adjustments in one go.

Thanks for your help Alexi!
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Re: How to modify existing mesh?

Post by mrw »

Hi,
Sorry to bump an old thread, but since I started it and the problem is still there, I thought, what the heck ;)
I haven´t had the time to explore SetMeshData(), until now. so that´s the reason for the long reply.

But what I have come up with is that I cannot use SetMeshData() for the simple reason that it cannot add vertices to the mesh. I can only alter vertices that exist in the mesh.
The same goes for altering polygons. Plus that is is even more confusing since the array that SetMeshData() is using can differ depending on the type of polygons. That is understandable, but it makes ít very confusing when using the command.

So I guess I´m left with either recreating the mesh from scratch using CreateMesh() or trying to alter it using UpdateMesh().
I assume that it takes longer to recreate it, why else would there be a UpdateMesh() command.

And so comes my current problem. How do I use UpdateMesh() to adjust(coordinates, normals, UVs), remove, add vertices and polygons to a mesh?
All my attempts have failed, with either a crash or weird results. I really need a simple complete working example of using UpdateMesh() to its fullest.

It doesn´t help when the documentation states this:
UpdateMesh(): The mesh has to be created with the #PB_Mesh_Dynamic flag.
CreateEntity(): Dynamic meshes are not supported (meshes created with the #PB_Mesh_Dynamic flag).

How on earth am I supposed to use lots of 3D objects, and be able to modify the meshes, without using them as Entitys?

Can anyone help me get my head straight around this?


Thanks
//Andreas..
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: How to modify existing mesh?

Post by Samuel »

For an example of UpdateMesh() take a look at MeshManualFlag.pb in Purebasic's 3D Demo examples.

From what I understand you have to completely recreate the mesh if you use UpdateMesh(). You can't just update random points like SetMeshData() can.
It may pay to setup a system where you use SetMeshData() for updating and UpdateMesh() for adding/deleting the data.
mrw
User
User
Posts: 76
Joined: Wed Dec 31, 2014 1:01 pm

Re: How to modify existing mesh?

Post by mrw »

Hi,
I examined the MeshManualFlag.pb example and the technique it uses is to create a Node instead of an Entity.
One disadvantage with that is that the command WorldShadows() only works with Entities, so no more "real" shadows. Or is there a way to get the same thing when using a Node?
Then again, if I use the command BuildMeshShadowVolume() on the mesh the program crashes when it ends. My guess is that that command doesn´t work on Dynamic meshes.

There are probably more disadvantages by not using Entities that I haven´t discovered yet ;)

I have also discovered that its too slow to use UpdateMesh() and redo the whole mesh(atleast when reaching max vertices) from scratch every frame. My guess is that SetMeshData() would be fast enough for this.
But I don´t think I need to do this every frame, although it would be nice.
Post Reply