[SOLVED] Mesh Creation

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[SOLVED] Mesh Creation

Postby hach-que » Thu Jan 08, 2009 1:18 am

I'm trying to get my irr::scene::IAnimatedMesh* from irrlicht into newton so that I can perform physics with it.

Code: Select all
NewtonCollision* collision;
collision = NewtonCreateTreeCollision(physworld, NULL);
NewtonTreeCollisionBeginBuild(collision);

// loop through, adding triangles.
mesh->grab();                                                    // Increases reference count on the IAnimatedMesh
singleframe = mesh->getMesh(0);                                  // Get the first frame (since we won't be doing collisions with an animated model; I don't think this is possible in Newton)
singleframe->grab();                                             // Increase the reference count on the first frame
for (int i = 0; i < singleframe->getMeshBufferCount(); i++)      // Loop through all the IMeshBuffers.  These each hold vertices.
{
   irr::scene::IMeshBuffer* meshbuffer;                          // Reserve memory.
   meshbuffer = singleframe->getMeshBuffer(i);                   // Get the IMeshBuffer
   meshbuffer->grab();                                           // Increase the reference count.
   for (int a = 0; a < meshbuffer->getIndexCount(); a++)         // Loop through the actual vertices.
   {
      irr::core::vector3df p1 = meshbuffer->getPosition(a);      // Get the position of the vertex.
      float newvertex[3];                                        // Reserve memory.
      newvertex[0] = p1.X;                                       // Store values
      newvertex[1] = p1.Y;
      newvertex[2] = p1.Z;
      NewtonTreeCollisionAddFace(                                // Add the face to newton.
         collision,                                              // -- to our collision
         1,                                                      // -- only 1 vertex to add
         newvertex,                                              // -- location of newvertex in memory
         sizeof(float)*3,                                        // -- size of newvertex
         1);                                                     // -- ID doesn't matter
   }
   meshbuffer->drop();                                           // Drop the reference count.
}
singleframe->drop();                                             // Drop the reference count.
mesh->drop();                                                    // Drop the reference count.
NewtonTreeCollisionEndBuild(collision, 1);                       // Finalize the collision and optimize.


That's my code. This causes the game to have a segmentation fault somewhere inside Newton.dll. Can anyone point out what I'm doing wrong?
Last edited by hach-que on Sun Jan 25, 2009 6:34 am, edited 2 times in total.
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Mesh Creation

Postby agi_shi » Thu Jan 08, 2009 5:12 pm

You can't add a face with less than 3 vertices. You're currently adding 1 by 1 vertices.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: Mesh Creation

Postby hach-que » Fri Jan 09, 2009 12:26 am

Oh, I need to add them in groups of 3... I'll see if that works.
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Mesh Creation

Postby JernejL » Fri Jan 09, 2009 9:33 am

hach-que wrote:Oh, I need to add them in groups of 3... I'll see if that works.


3 or more, quads work just as well.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Mesh Creation

Postby agi_shi » Fri Jan 09, 2009 5:14 pm

Remember, not just groups of three, but groups of three that form triangles ;) (or 4 for quads, etc.)
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: Mesh Creation

Postby hach-que » Fri Jan 09, 2009 9:36 pm

Well, at least it doesn't crash now. Unfortunately, even though I'm setting the mass and inertia, the callback is never made.

Any ideas why the callback isn't being called for a mesh? (maybe I have to call something extra?)
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Mesh Creation

Postby agi_shi » Fri Jan 09, 2009 11:20 pm

hach-que wrote:Well, at least it doesn't crash now. Unfortunately, even though I'm setting the mass and inertia, the callback is never made.

Any ideas why the callback isn't being called for a mesh? (maybe I have to call something extra?)

Which callback? Certain callbacks aren't called for static/tree collision objects.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: Mesh Creation

Postby Interface » Mon Jan 12, 2009 1:13 pm

Is there way for Newton to interpret triangle strips or triangle fans etc? Or should i always pass exact triangles/quads?
Interface
 
Posts: 4
Joined: Fri Jan 09, 2009 12:06 am

Re: Mesh Creation

Postby hach-que » Mon Jan 12, 2009 7:47 pm

agi_shi wrote:
hach-que wrote:Well, at least it doesn't crash now. Unfortunately, even though I'm setting the mass and inertia, the callback is never made.

Any ideas why the callback isn't being called for a mesh? (maybe I have to call something extra?)

Which callback? Certain callbacks aren't called for static/tree collision objects.


I've set my mesh to have mass and inertia so it isn't static, but how do I actually get it to make callbacks?
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Mesh Creation

Postby hach-que » Thu Jan 15, 2009 5:52 pm

EDIT: The mesh appears to be at 90 degrees to the model.

I tried to use the tutorial at http://irrlicht.sourceforge.net/tut_newton.html (Setting up the scene), and getting the code from their, but that raises a segmentation fault on NewtonTreeCollisionEndBuild (no errors on the other tree functions though).

Code: Select all
collision = NewtonCreateTreeCollision(physworld, NULL);
            NewtonTreeCollisionBeginBuild(collision);

            // loop through, adding triangles.
            mesh->grab();
            singleframe = mesh->getMesh(0);
            singleframe->grab();
            for (int i = 0; i < singleframe->getMeshBufferCount(); i++)
            {
               irr::scene::IMeshBuffer* meshbuffer;
               meshbuffer = singleframe->getMeshBuffer(i);
               meshbuffer->grab();
               irr::video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*)meshbuffer->getVertices();
               irr::u16* mb_indices  = meshbuffer->getIndices();
               for (int a = 0; a < meshbuffer->getIndexCount(); a+=3)
               {
                  v1i = mb_indices[a];
                  v2i = mb_indices[a+1];
                  v3i = mb_indices[a+2];
            
                  vArray[0] = mb_vertices[v1i].Pos.X;
                  vArray[1] = mb_vertices[v1i].Pos.Y;
                  vArray[2] = mb_vertices[v1i].Pos.Z;
                  vArray[3] = mb_vertices[v2i].Pos.X;
                  vArray[4] = mb_vertices[v2i].Pos.Y;
                  vArray[5] = mb_vertices[v2i].Pos.Z;
                  vArray[6] = mb_vertices[v3i].Pos.X;
                  vArray[7] = mb_vertices[v3i].Pos.Y;
                  vArray[8] = mb_vertices[v3i].Pos.Z;

                  NewtonTreeCollisionAddFace(collision, 3, (float*)vArray, 12, 1);
               }
               meshbuffer->drop();
            }
            singleframe->drop();
            mesh->drop();
            NewtonTreeCollisionEndBuild(collision, 0);
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Mesh Creation

Postby hach-que » Sat Jan 24, 2009 4:13 am

Okay, I'm not doing this for meshes anymore, but instead, I'm doing it for terrain. For some reason the segmentation fault is occurring inside the NewtonUpdate call.

This is the call stack I get:
Code: Select all
    Newton.dll!0035ee53()    
    Newton.dll!0035fb87()    
    Newton.dll!0035fb8e()    
    ntdll.dll!7c9268ad()    
    Newton.dll!0034097c()    
    Newton.dll!003602b0()    
    ntdll.dll!7c91056d()    
    Newton.dll!00373baf()    
    Newton.dll!0034533a()    
    Newton.dll!003453b6()    
    Newton.dll!0032fdda()    
    Newton.dll!0034892f()    
    Newton.dll!00348a46()    
    Newton.dll!00348b54()    
    Newton.dll!0034498f()    
    lua51.dll!10008a0b()    
    lua51.dll!10008a53()    
    lua51.dll!100168dd()    
    lua51.dll!10016b26()    
    lua51.dll!10001d62()    
>   Roket3D.exe!Luna<physics::RPhysicsManager>::createNew(lua_State * L=0x0012fcb8)  Line 306 + 0xd   C++
    Roket3D.exe!physics::RPhysicsManager::updateAll(lua_State * L=0x003a5ad8)  Line 32   C++
    Roket3D.exe!Luna<physics::RPhysicsManager>::function_dispatch(lua_State * L=0x003a5ad8)  Line 575 + 0x15   C++
    lua51.dll!100089ec()    
    lua51.dll!10017de4()    
    lua51.dll!10008a62()    
    lua51.dll!10002055()    
    lua51.dll!1000800a()    
    lua51.dll!10007ff8()    
    lua51.dll!10008bf1()    
    lua51.dll!100020b3()    
    Roket3D.exe!SDL_main(int argc=3, char * * argv=0x01026420)  Line 53 + 0x11   C++
    Roket3D.exe!_main()  + 0xd1   C
    Roket3D.exe!mainCRTStartup()  Line 398 + 0x11   C
    kernel32.dll!7c816d4f()    
    ntdll.dll!7c915b4f()    
    kernel32.dll!7c8399f3()    


And this is the code that I'm using (sphere and cube work fine, just the E_TYPE_TERRAIN isn't working):
Code: Select all
   void RSceneNode::setupPhysics(ePhysicsType type, NewtonWorld* physworld, irr::core::vector3df position, irr::scene::IMesh* mesh)
   {
      physType = type;
      irr::core::matrix4 posdesc;
      int cMeshBuffer, j;
      int v1i, v2i, v3i;
      irr::scene::IMesh* singleframe;
      NewtonCollision* collision;
      float vArray[9]; // vertex array (3*3 floats)
      int tmpCount = 0;
      switch (physType)
      {
         case E_TYPE_SPHERE:
            // -- SPHERE --
            // Get an Irrlicht matrix
            posdesc.setTranslation(irr::core::vector3df(0,0,0)); //self()->getPosition());
            posdesc.setRotationDegrees(self()->getRotation());
            //posdesc.setScale(self()->getScale());
            // Setup Roket Physics Body for sphere
            physBody = new physics::Roket_PhysicsBody(
                        NewtonCreateSphere(
                           physworld,
                           self()->getScale().X * NEWTON_SCALE,
                           self()->getScale().Y * NEWTON_SCALE,
                           self()->getScale().Z * NEWTON_SCALE,
                           posdesc.pointer()
                        ),
                        physworld,
                        E_TYPE_SPHERE,
                        0
                        );
            physBody->attachNode(this);
            physBody->setPosition(position);
            self()->setPosition(position);
            break;
         case E_TYPE_CUBE:
            // -- CUBE --
            // Get an Irrlicht matrix
            posdesc.setTranslation(irr::core::vector3df(0,0,0));
            posdesc.setRotationDegrees(self()->getRotation());
            //posdesc.setScale(self()->getScale());
            // Setup Roket Physics Body for sphere
            physBody = new physics::Roket_PhysicsBody(
                        NewtonCreateBox(
                           physworld,
                           self()->getScale().X * NEWTON_SCALE,
                           self()->getScale().Y * NEWTON_SCALE,
                           self()->getScale().Z * NEWTON_SCALE,
                           posdesc.pointer()
                        ),
                        physworld,
                        E_TYPE_CUBE,
                        0
                        );
            physBody->attachNode(this);
            physBody->setPosition(position);
            self()->setPosition(position);
            break;
         case E_TYPE_MESH:
            physType = E_TYPE_IGNORE;
            break;
         case E_TYPE_TERRAIN:
            // Setup Roket Physics Body for terrain
            posdesc.setTranslation(irr::core::vector3df(0,0,0));
            posdesc.setRotationDegrees(self()->getRotation());
            //posdesc.setScale(self()->getScale());
            // Setup Roket Physics Body for sphere
            collision = NewtonCreateTreeCollision(physworld, NULL);
            NewtonTreeCollisionBeginBuild(collision);

            // loop through, adding triangles.
            mesh->grab();
            singleframe = mesh;
            for (int i = 0; i < singleframe->getMeshBufferCount(); i++)
            {
               irr::scene::IMeshBuffer* meshbuffer;
               meshbuffer = singleframe->getMeshBuffer(i);
               meshbuffer->grab();
               irr::video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*)meshbuffer->getVertices();
               irr::u16* mb_indices  = meshbuffer->getIndices();
               for (int a = 0; a < meshbuffer->getIndexCount(); a+=3)
               {
                  irr::core::vector3df p1 = meshbuffer->getPosition(a);
                  irr::core::vector3df p2 = meshbuffer->getPosition(a+1);
                  irr::core::vector3df p3 = meshbuffer->getPosition(a+2);
                  float newvertex[3][3];
                  newvertex[0][0] = p1.X;
                  newvertex[0][1] = p1.Y;
                  newvertex[0][2] = p1.Z;
                  newvertex[1][0] = p2.X;
                  newvertex[1][1] = p2.Y;
                  newvertex[1][2] = p2.Z;
                  newvertex[2][0] = p3.X;
                  newvertex[2][1] = p3.Y;
                  newvertex[2][2] = p3.Z;
                  NewtonTreeCollisionAddFace(
                     collision,
                     3,
                     &newvertex[0][0],
                     sizeof(float)*9,
                     1);
               }
               meshbuffer->drop();
            }
            singleframe = 0;
            mesh->drop();
            NewtonTreeCollisionEndBuild(collision, 0);

            physBody = new physics::Roket_PhysicsBody(
                        collision,
                        physworld,
                        E_TYPE_TERRAIN,
                        0
                        );
            physBody->attachNode(this);
            physBody->setPosition(position);
            self()->setPosition(position);

            break;
         case E_TYPE_IGNORE:
         case E_TYPE_NONE:
         case E_TYPE_UNKNOWN:
            // Don't setup a Roket Physics Body
            break;
      }
   }
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: [UNSOLVED] Mesh Creation

Postby agi_shi » Sat Jan 24, 2009 9:36 am

Is this in Newton 2.0? I can guarantee you Newton's not the error here, tree collisions are a primary feature since way back. Ask on the Irrlicht forums if you're retrieving the triangles properly. And, no, Newton cannot use triangle strips or fans, it needs singular polygons. There is a heightfield collision, though.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: [UNSOLVED] Mesh Creation

Postby hach-que » Sat Jan 24, 2009 8:10 pm

This is Newton 2.0. I just found out that I wasn't adding any vertexes to the Newton collision, but I have fixed that now, and the engine now loads all 65536 triangles into the collision mesh.

However, it's still causing a segmentation fault. The call stack is the same as before. This is my new code:

Code: Select all
collision = NewtonCreateTreeCollision(physworld, NULL);
            NewtonTreeCollisionBeginBuild(collision);

            // loop through, adding triangles.
            mesh->grab();
            singleframe = mesh;
            totalbuffers = singleframe->getMeshBufferCount();
            for (int i = 0; i < totalbuffers; i++)
            {
               meshbuffer = singleframe->getMeshBuffer(i);
               meshbuffer->grab();
               mb_vertices = (irr::video::S3DVertex2TCoords*)meshbuffer->getVertices();
               //mb_indices = meshbuffer->getVertices();
               mb_vertices_total = meshbuffer->getVertexCount();
               for (int a = 0; a < mb_vertices_total-2; a++)
               {
                  for (int b = 0; b < 3; b++)
                  {
                     p[b] = meshbuffer->getPosition(a+b);
                     newvertex[b][0] = p[b].X;
                     newvertex[b][1] = p[b].Y;
                     newvertex[b][2] = p[b].Z;
                  }
                  NewtonTreeCollisionAddFace(
                     collision,
                     3,
                     &newvertex[0][0],
                     sizeof(float)*9,
                     1);
                  nonlua_debug("Adding triangle " << a << " of " << mb_vertices_total-2,LVL_INFO);
               }
               meshbuffer->drop();
            }
            singleframe = 0;
            mesh->drop();
            NewtonTreeCollisionEndBuild(collision, 0);
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: [UNSOLVED] Mesh Creation

Postby agi_shi » Sat Jan 24, 2009 8:14 pm

Check the docs, you're still doing it wrong: http://www.newtondynamics.com/wiki/inde ... ionAddFace

The strideInBytes parameter should be most commonly 12 (sizeof(float) * 3), not 36 (which is what you have via sizeof(float) * 9). It's the size of one vertex.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm

Re: [UNSOLVED] Mesh Creation

Postby hach-que » Sat Jan 24, 2009 8:20 pm

Okay, I've changed it to sizeof(float)*3, but it's still causing a segmentation fault (and at the same position, 0035ee53(), maybe Julio could take a look and see what's at that position?)
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 38 guests