Manually create a mesh

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
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Manually create a mesh

Post by khatarat »

i want to create a mesh manually
irrlicht document is very poor and cant find anything about using classes of Engine.
after very searches i found that i should write something like this:

SMesh *m=new SMesh();
SMeshBuffer * meshbuf=new SMeshBuffer();
IMeshSceneNode *node;
video::S3DVertex* a = new video::S3DVertex(0,0,10, 1,1,0, video::SColor(255,0,255,255), 0, 1);
video::S3DVertex* b = new video::S3DVertex(10,0,-10, 1,0,0, video::SColor(255,255,0,255), 1, 1);
video::S3DVertex* c = new video::S3DVertex(0,20,0, 0,1,1, video::SColor(255,255,255,0), 1, 0);
video::S3DVertex* d = new video::S3DVertex(-10,0,-10, 0,0,1, video::SColor(255,0,255,0), 0, 0);



void* ver2[] = {a,b,c,d};
u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
m->addMeshBuffer(meshbuf);
meshbuf->drop();

meshbuf->append((const void*)&ver2, 4, (u16*)&indices, 12);
meshbuf->Vertices.reallocate(4);
meshbuf->Vertices.set_used(4);
meshbuf->Indices.set_used(12);
meshbuf->recalculateBoundingBox();


}
but it doesnt work.
when i call m_Scene->drawAll(); its never go to next line.
what is problem?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: Manually create a mesh

Post by greenya »

Please check 23.SMeshHandling example.
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Re: Manually create a mesh

Post by khatarat »

i road that example but its very complicated and not what i want.
i want to user CMeshBuffer.append to add several vertices at once . but when i use it the value of vertices and indices of buffer isnt what i add to beffer.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Manually create a mesh

Post by Mel »

Irrlicht uses a void* pointer so it doesn't have to rewrite each method for diferent vertex types and have diferent signatures, but what you are passing to the append method is a vector of pointers to vertices, not a vector of vertices, which is what is expected, the indices seem fine, though
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Re: Manually create a mesh

Post by khatarat »

can you explain more? i didnt understand that where is problem exactly.
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Manually create a mesh

Post by Mel »

The SMeshBuffer class inherits from the IMeshBuffer interface, which specifies an append method that uses a void* pointer so it doesn't have to specify a type of vertex.

This IMeshBuffer interface is then implemented in 3 subclasses: SMeshBuffer, for normal vertices, SMeshBufferLightMap for vertices with 2 sets of mapping coordinates and SMeshBufferTangents for meshes with tangents and binormal/bitangent vectors, which have 3 diferent vertex types, so the append method works a little bit diferent for them. One expects vertices of S3DVertex size, other expects the size of vertex of a S3DVertex2TCoords and the other, the size of a S3DVertexTangents, using a void* pointer unifies the whole interface, and makes it posible for all of them to work under the same scheme.

The problem is that you are passing a vector of pointers to vertices, not a vector of vertices to the append method. The engine can't force you to use a type of vertex with the void * pointer, but what you should pass to the append method is a S3DVertex*, The program crashes because you are reading out of the bounds of the vector you have defined for the vertices.

To solve that, instead of a void* pointer, use an array class, and get its pointer when passing the vertices to your mesh.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Re: Manually create a mesh

Post by khatarat »

thanks.
my problem has solved.
but why this explanations does not exist in document?

i have another question.
i have a mesh in each frame. how can i create an animation with this meshes?
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Manually create a mesh

Post by Mel »

there are many ways, you can manually animate the vertices, attach them to bones, it is up to you what to do
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Klunk
Posts: 264
Joined: Mon Jan 10, 2011 5:21 pm

Re: Manually create a mesh

Post by Klunk »

http://irrlicht.sourceforge.net/forum/v ... =9&t=45093 is an example of animated verts (which coincidentally are "manually" created) theres a link to a release build for window further down too.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Manually create a mesh

Post by hybrid »

khatarat wrote:thanks.
my problem has solved.
but why this explanations does not exist in document?
Basically because it's a beginner C++ problem, nothing related to Irrlicht.
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Re: Manually create a mesh

Post by khatarat »

i know that this void* is a way to create several functions with one signature but from where should i know that what is implimentations of each function in irrlicht library? i think the best way is to read all source codes so could use this library.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Manually create a mesh

Post by serengeor »

You can always open up the sources and look what's going inside for yourself :)
Working on game: Marrbles (Currently stopped).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Manually create a mesh

Post by hybrid »

Not sure which problem your void* question is related to. But here, there's absolutely no problem with the void interface, it's basically your Java-style of vertex creation which makes a problem. For C++, an array of vertices is simply not an array of dynamically allocated vertices, otherwise it would be explicitely mentioned.
khatarat
Posts: 18
Joined: Wed Dec 19, 2012 10:41 am

Re: Manually create a mesh

Post by khatarat »

just after i change S3DVertex * to S3DVertex my problem solved. because no where wrote that should not sent pointer and i just saw one sample code in internet that used pointers.so i sent pointer.
Post Reply