Building a Terrain Object3D from scratch

Started by paulscode, October 18, 2008, 04:59:08 PM

Previous topic - Next topic

paulscode

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 ???
        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?

EgonOlsen

Quote from: paulscode on October 18, 2008, 04:59:08 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.

Quote from: paulscode on October 18, 2008, 04:59:08 PM
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.

Quote from: paulscode on October 18, 2008, 04:59:08 PM
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.

paulscode

Quote from: EgonOlsen on October 18, 2008, 10:04:45 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. :-[

Quote from: EgonOlsen on October 18, 2008, 10:04:45 PM
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):
int polyIdOffset = terrain.getPolygonManager().getMaxPolygonID() - terrain.getMesh().getTriangleCount();
Then just add that number to a triangle number to get its polyID.

Quote from: EgonOlsen on October 18, 2008, 10:04:45 PM
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.

paulscode

#3
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:
    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:
        camera = world.getCamera();
        camera.setPosition( -80, -160, -80 );
        camera.lookAt( new SimpleVector( 80, 0, 80 ) );


And the calcMinDistance part:
        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/

paulscode

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.

paulscode

OMG, I am dumb!  I forgot to make the terrain object a collision listener!!  Ok, it works great now, ROFL!