Optimizing mesh creation

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Optimizing mesh creation

Postby hach-que » Mon Jan 26, 2009 8:13 am

I've got my terrain loading from Irrlicht into Newton. Problem is that a 256x256 grid results in 345600 faces being added to Newton. This (as you can imagine) takes a considerable amount of time to perform, not to mention the additional time when NewtonTreeCollisionEndBuild is called to actually put it into the collision format.

I want to know if there's anyway to optimize the speed of this (so that terrain loading takes under half a second, rather than 3 minutes). Has anyone done this before, and if so, how did you optimize it?
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby Julio Jerez » Mon Jan 26, 2009 9:30 am

newton have a heygh field terrain, int will load in teh blink of an eye. and fopr 256 x 256 map it will be way unt 256k of memory
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimizing mesh creation

Postby hach-que » Mon Jan 26, 2009 5:16 pm

The problem is, later on it won't just be a height map. The terrain format will support caves and extrusions not possible with a height map.

EDIT: I can't find the height map functions in the Wiki. Can someone point me to them?
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby JernejL » Mon Jan 26, 2009 5:53 pm

You can serialize trimeshes and load them so they are created instantly.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Optimizing mesh creation

Postby hach-que » Tue Jan 27, 2009 7:04 pm

Do I have to create serialize and deserialize callbacks? Or if I provide NULL is there some sort of inbuilt function that will handle it?
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby Stucuk » Wed Jan 28, 2009 2:11 pm

if you looked at userheightfieldcollision.cpp you would find that the demos used NewtonCreateUserMeshCollision to create the actual collision.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Optimizing mesh creation

Postby Julio Jerez » Wed Jan 28, 2009 4:10 pm

I think it is easier for him if he uses this instead:
NewtonCreateHeightFieldCollision

it loads in teh blink of and eye, and no code had to be written
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimizing mesh creation

Postby hach-que » Wed Jan 28, 2009 5:28 pm

There doesn't seem to be a wiki article on that function Julio, so I have no idea how to use it ;)

This is a line from the examples:

NewtonCreateHeightFieldCollision (nWorld, width, height, 0, elevations, attibutes, CELL_SIZE, ELEVATION_SCALE_INV);

I think I've been able to figure out how elevations is made, but I just wanted to check. Is it just an single array of height values?

I don't understand what CELL_SIZE and ELEVATION_SCALE_INV mean though.
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby Julio Jerez » Wed Jan 28, 2009 5:55 pm

NewtonCreateHeightFieldCollision (nWorld, width, height, 0, elevations, attibutes, CELL_SIZE, ELEVATION_SCALE_INV);

the elevations is an array of shorts
width and high are bitmap dimetions of the array
atribute is an array of bytes to enconde terrain properties like materials.

the 0, is diagionals 0, or 1 (you enable debig display so tat you see the layout of t eh digonal
CELL_SIZE, is the size in word unit tha convert eacg pixel cell to mesh cell,
ELEVATION_SCALE_INV is the scale the that convert an elvation to convert it to world units.

you use a collision offset matrix, or a body matrix to place it in any orienation you want.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimizing mesh creation

Postby hach-que » Wed Jan 28, 2009 7:32 pm

How do I arrange the array of shorts? I tried arrangement by X and arrangement by Z, but neither are an exact copy of the terrain. The testing spheres hit stuff in midair and go through the visible terrain as well.

Code: Select all
width = abs(terrain->getBoundingBox().MinEdge.X - terrain->getBoundingBox().MaxEdge.X);
            height = abs(terrain->getBoundingBox().MinEdge.Z - terrain->getBoundingBox().MaxEdge.Z);

            scalex = terrain->getScale().X;
            scalez = terrain->getScale().Z;

            elevations = (unsigned short*) malloc (width * height * sizeof (unsigned short));
            attibutes = (char*) malloc (width * width * sizeof (char));
            memset (attibutes, 0, width * height * sizeof (char));

            cout << "Heightmap information: " << endl
               << "  Width: " << width << endl
               << "  Height: " << height << endl
               << "  Cellsize X: " << scalex << endl
               << "  Cellsize Z: " << scalez << endl
               << "  Cells X: " << width / scalex << endl
               << "  Cells Z: " << height / scalez << endl;

            a = 0;
            for (int x = 0; x < height / scalez; x++ )
            {
               for (int z = 0; z < width / scalex; z++ )
               {
                  elevations[a] = terrain->getHeight(x * scalex,z * scalez);
                  a++;
               }
            }

            cout << "Heightmap is: " << endl;
            for (int b = 0; b < a; b++)
            {
               cout << elevations[b] << ",";
            }
            cout << endl;

            collision = NewtonCreateHeightFieldCollision (physworld, width / scalex, height / scalez, 0, elevations, attibutes, scalex, 1);
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby Julio Jerez » Wed Jan 28, 2009 9:57 pm

Code: Select all
width = abs(terrain->getBoundingBox().MinEdge.X - terrain->getBoundingBox().MaxEdge.X);
height = abs(terrain->getBoundingBox().MinEdge.Z - terrain->getBoundingBox().MaxEdge.Z);

// grid must be regule size scalez == scalex
scalez = terrain->getScale().Z;

elevations = (unsigned short*) malloc (width * height * sizeof (unsigned short));
attibutes = (char*) malloc (width * width * sizeof (char));
memset (attibutes, 0, width * height * sizeof (char));

a = 0;
for (int x = 0; x < height; x++ )
{
   for (int z = 0; z < width; z++ )
   {
      elevations[a] = terrain->getHeight(x, z);
      a++;
   }
}

// you need teh hight scale that is
// how many meter a unit is
// elevation goes frem -37766 tp 37637
// say scaleY  1 / 1000
//that mean thet higest mountant is 37 meters
collision = NewtonCreateHeightFieldCollision (physworld, width, height, 0, elevations, attibutes, scalez, scaleY);


then you need to position it with a the body matrix, therefore the x, or z is irrelevant because you can rotate the terrain to match the anything you want.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimizing mesh creation

Postby hach-que » Thu Jan 29, 2009 12:56 am

I'm a little confused as to what 1 meter is in Newton. Is it 1 pixel? I know that 1 Irrlicht unit = 1 Newton unit. So how many Newton units is 1 meter?
a meter is that a meter.

:P You accidentally edited my post.

I do no undertand whey you find thos so hard, there is a demo that create a collision just like that. It is quiet simple
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby Julio Jerez » Thu Jan 29, 2009 1:17 am

upps, sorry abopu that
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Optimizing mesh creation

Postby hach-que » Thu Jan 29, 2009 2:14 am

I'm still getting the scale wrong (hey, it's 32 degrees Celsius here :P). The maximum value from getHeight is 256, so I put (256 / 37637) as the scale value, however all the balls drop onto a flat place.
User avatar
hach-que
 
Posts: 27
Joined: Fri Jun 13, 2008 2:11 am

Re: Optimizing mesh creation

Postby agi_shi » Thu Jan 29, 2009 8:30 am

Your units depend on your terrain/etc. size. If you terrain is supposed to be 1x1 kilometers, but it goes 1000 units in each directions, then that means that it's using meters. Therefore, you will want both Irrlicht and Newton to use meters (there is no "setting," just properly place your camera in Irrlicht and use the correct data in Newton).

Seriously, though, observe and play with the demo height field. See what changes what, etc.
agi_shi
 
Posts: 263
Joined: Fri Aug 17, 2007 6:54 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 19 guests

cron