Author Topic: Building a Terrain Object3D from scratch  (Read 3291 times)

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Building a Terrain Object3D from scratch
« on: October 18, 2008, 04:59:08 pm »
I have a couple more questions related to building a terrain Object3D.

My first question is why does building an Object3D increase its vertice count?  Building the following square Object3D increases its vertice count from 4 to 12 ???
Code: [Select]
        Object3D terrain = new Object3D( 2 );
        SimpleVector bottomLeft = new SimpleVector( 0, 0, 0 );
        SimpleVector bottomRight = new SimpleVector( 5, 0, 0 );
        SimpleVector topRight = new SimpleVector( 5, 0, 5 );
        SimpleVector topLeft = new SimpleVector( 0, 0, 5 );
       
        int triangle1 = terrain.addTriangle( topLeft, 0, 0,
                                             bottomLeft, 0, 1,
                                             bottomRight, 1, 1 );
        int triangle2 = terrain.addTriangle( bottomRight, 1, 1,
                                             topRight, 1, 0,
                                             topLeft, 0, 0 );

        System.out.println( "Unique Vertice count before build: " + terrain.getMesh().getUniqueVertexCount() );
        System.out.println( "    Triangles: " + terrain.getMesh().getTriangleCount() );
        terrain.build();
        System.out.println( "Unique Vertice count after build: " + terrain.getMesh().getUniqueVertexCount() );
        System.out.println( "    Triangles: " + terrain.getMesh().getTriangleCount() );
Strangely, the number of triangles remains 2 even though the number of vertices triples.  What is causing this, and how do I prevent it from happening?

My second question is what is the best way to determine a polyID given a triangle number.  I don't suppose they are the same, are they?

My last question is about OcTree's.  The JavaDoc states that the best maxPoly value to use for an OcTree depends on the object and its usage.  Unfortunately, I don't know enough to know what the best value to use for my situation might be.  What would you recommend for terrain objects that are 32X32 grids with two triangles per square?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Building a Terrain Object3D from scratch
« Reply #1 on: October 18, 2008, 10:04:45 pm »
Strangely, the number of triangles remains 2 even though the number of vertices triples.  What is causing this, and how do I prevent it from happening?
You can't...and you shouldn't. build() always increases the vertex count by 8. Those are a vertices for the object's bounding box that are stored in the same structure. That's all.

My second question is what is the best way to determine a polyID given a triangle number.  I don't suppose they are the same, are they?
If you are just adding triangles and are not building triangle strips afterwards on that object, the polyID should be in the same order in which you've added the polygons. Apart from that, there is no good way to find out which polygon gets which id.

My last question is about OcTree's.  The JavaDoc states that the best maxPoly value to use for an OcTree depends on the object and its usage.  Unfortunately, I don't know enough to know what the best value to use for my situation might be.  What would you recommend for terrain objects that are 32X32 grids with two triangles per square?
That's pretty hard to tell. Just give it a try and see what performs best in your case. Don't forget to enable the OcTree for collision detection or otherwise, only the rendering will benefit from it.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Building a Terrain Object3D from scratch
« Reply #2 on: October 18, 2008, 10:52:26 pm »
You can't...and you shouldn't. build() always increases the vertex count by 8. Those are a vertices for the object's bounding box that are stored in the same structure. That's all.
I just finished some testing before I read your post, and discovered it was always 8 more, not 3 times as many.  I hadn't thought of the bounding box (DUH!).  Yeh, that is pretty obvious now. :-[

If you are just adding triangles and are not building triangle strips afterwards on that object, the polyID should be in the same order in which you've added the polygons. Apart from that, there is no good way to find out which polygon gets which id.
Ok, that should be simple enough to work with.  I ought to be able to get an offest value like this (as long as all Object3Ds are created on the same thread):
Code: [Select]
int polyIdOffset = terrain.getPolygonManager().getMaxPolygonID() - terrain.getMesh().getTriangleCount();Then just add that number to a triangle number to get its polyID.

That's pretty hard to tell. Just give it a try and see what performs best in your case. Don't forget to enable the OcTree for collision detection or otherwise, only the rendering will benefit from it.
Ok, I suppose I will make a FPS application, start with maxPoly = 2, then randomly try higher values and see what works best.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Building a Terrain Object3D from scratch
« Reply #3 on: October 19, 2008, 02:04:50 am »
I'm having a little trouble getting calcMinDistance to work.  Not sure if I made my OcTree incorrectly or something.

The code I used to generate the terrain Object3D:
Code: [Select]
    public void createTerrain()
    {
        int x, y;
        terrain = new Object3D( 2048 );
       
        // Create the terrain's vertices:
        SimpleVector[][] vertices = new SimpleVector[33][33];
        for( y = 0; y < 33; y++ )
        {
            for( x = 0; x < 33; x++ )
            {
                vertices[x][y] = new SimpleVector( x * 5, 0, y * 5 );
            }
        }
       
        // Create the terrain's polys, set their UV coordinates,
        // and set their initial texture to grey:
        for( y = 0; y < 32; y++ )
        {
            for( x = 0; x < 32; x++ )
            {
                terrain.addTriangle( vertices[x][y+1], 0, 0,
                          vertices[x][y], 0, 1,
                          vertices[x+1][y], 1, 1,
                          TextureManager.getInstance().getTextureID( "Grey" ) );
                terrain.addTriangle( vertices[x+1][y], 1, 1,
                          vertices[x+1][y+1], 1, 0,
                          vertices[x][y+1], 0, 0,
                          TextureManager.getInstance().getTextureID( "Grey" ) );
            }
        }
       
        terrain.build();
       
        OcTree ocTree = new OcTree( terrain.getMesh(), 2048,
                                    OcTree.MODE_OPTIMIZED );
        ocTree.setCollisionUse( OcTree.COLLISION_USE );
        terrain.setOcTree( ocTree );
    }

The camera orientation:
Code: [Select]
        camera = world.getCamera();
        camera.setPosition( -80, -160, -80 );
        camera.lookAt( new SimpleVector( 80, 0, 80 ) );

And the calcMinDistance part:
Code: [Select]
        SimpleVector position = new SimpleVector( camera.getPosition() );
        SimpleVector direction = new SimpleVector( camera.getDirection() ).normalize();
        float distance = world.calcMinDistance( position, direction, 10000 );
       
        if( distance == Object3D.COLLISION_NONE )
            outputString = "No Collision";
        else
            outputString = "Collision - Distance = " + distance;

I also posted a link to the actual applet:
http://www.paulscode.com/source/calcMinDistanceTest/
« Last Edit: October 19, 2008, 04:39:36 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Building a Terrain Object3D from scratch
« Reply #4 on: October 19, 2008, 06:25:37 am »
I'm not so sure the problem is related to how I created the terrain any more.  I've tried loading a 3DS instead.  The camera is looking directly at the object, but plugging the camera's position and direction vectors into calcMinDistance still returns Object3D.COLLISION_NONE.  I must somehow be using the method incorrectly or something.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Building a Terrain Object3D from scratch
« Reply #5 on: October 19, 2008, 06:31:47 am »
OMG, I am dumb!  I forgot to make the terrain object a collision listener!!  Ok, it works great now, ROFL!