Archived

This topic is now archived and is closed to further replies.

Arch@on

Calculating the offset(heightmap) for each node in quadtree.

Recommended Posts

I have had problems with calculating correctly the offsets for each node in a quadtree. I''m using vertex arrays in OpenGL I have taken the top down approach: _____ | | | _____ | | | _____ My nodes are always square and the minium node size which is also the patchwidth is 64. My total map length is 1024. And every node needs the offset because I calculate the node height from it. With the offset I adjust the vertices memory address in the rendering process after I have culled the nonvisible vertices away. glColorPointer(3, GL_FLOAT, sizeof(Vector3f),currentData->colors+node->offset); glVertexPointer(3, GL_FLOAT, sizeof(Vector3f), currentData->vertices+node->offset); glDrawElements(GL_TRIANGLE_STRIP, currentData->numIndices, GL_UNSIGNED_INT, currentData->indices);


int Node::getOffset(HeightMap* mesh, float width, int nodeID){
    int newOffset = 0;
    
    switch(nodeID){
        case TOP_LEFT:
                newOffset = offset;
            break;
            
        case TOP_RIGHT:
                newOffset = offset + (int)width;
            break;
            
        case BOTTOM_LEFT:
                newOffset = offset + mesh->mapSize;
            break;
        
        case BOTTOM_RIGHT:
                newOffset = offset + mesh->mapSize + (int)width;
            break;
    
    }

    return offset;
}
Offset is parent offset. Right now when I look at my code I everything seem somewhat logical. Using offset is really good because it will yield in small node sizes, but are there any disadvatanges using it?

Share this post


Link to post
Share on other sites
In general, is it better to have your triangle mesh represented in single array of vertices or as multiple arrays per node. I'm still struggling with the most optimal data store for all the triangles in the scene.

Also how do you mark& recognize your overlapping triangles?

[edited by - Captain Goatse on July 28, 2003 3:15:56 PM]

Share this post


Link to post
Share on other sites
if you know how your octree is going to unfold, then you know exactially what all the bounds (max/min)of each node should be.
Try this: hard code your bounds by hand. that will really give you a good idea as to what numbers should be generated by your offset, and what numbers are being generated.

Here's my version of your code. (I wrote this for a book i was pitching about a year ago) your main box should be the bounding box of your entire map. this function does the rest when passed in which box you want to create.




BBOX CalculateNewBBox(BBOX aabb, int id)
{
// I created this function which takes an enum ID to see which node's center

// we need to calculate. Once we find that we need to subdivide a node we find

// the centers of each of the 8 new nodes. This is what that function does.

// We just tell it which node we want.


BBOX retbox;
// Switch on the ID to see which subdivided node we are finding the center

switch(id)
{

case 0://Left Back:


// Calculate the center of this new node

retbox.center.Set(aabb.center.x - (aabb.center.x - aabb.min.x)/2,
aabb.center.y,
aabb.center.z + ( aabb.max.z - aabb.center.z)/2);

//since this is a quad, our y values stay the same

retbox.max.y=aabb.max.y;
retbox.min.y=aabb.min.y;

retbox.max.x=aabb.center.x;
retbox.min.x=aabb.min.x;

retbox.max.z=aabb.max.z;
retbox.min.z=aabb.center.z;

break;

case 1://Right Back:


// Calculate the center of this new node

retbox.center.Set(aabb.center.x + (aabb.center.x - aabb.min.x)/2,
aabb.center.y,
aabb.center.z + ( aabb.max.z - aabb.center.z)/2);

//since this is a quad, our y values stay the same

retbox.max.y=aabb.max.y;
retbox.min.y=aabb.min.y;

retbox.max.x=aabb.max.x;
retbox.min.x=aabb.center.x;

retbox.max.z=aabb.max.z;
retbox.min.z=aabb.center.z;

break;

case 2://Left Front:

// Calculate the center of this new node

retbox.center.Set(aabb.center.x - (aabb.center.x - aabb.min.x)/2,
aabb.center.y,
aabb.center.z - (aabb.center.z - aabb.min.z)/2);

//since this is a quad, our y values stay the same

retbox.max.y=aabb.max.y;
retbox.min.y=aabb.min.y;

retbox.max.x=aabb.center.x;
retbox.min.x=aabb.min.x;

retbox.max.z=aabb.center.z;
retbox.min.z=aabb.min.z;

break;

case 3://Right Front:

// Calculate the center of this new node

retbox.center.Set(aabb.center.x + (aabb.center.x - aabb.min.x)/2,
aabb.center.y,
aabb.center.z - ( aabb.max.z - aabb.center.z)/2);

//since this is a quad, our y values stay the same

retbox.max.y=aabb.max.y;
retbox.min.y=aabb.min.y;

retbox.max.x=aabb.max.x;
retbox.min.x=aabb.center.x;

retbox.max.z=aabb.center.z;
retbox.min.z=aabb.min.z;

break;
}

// Return the new node center

return retbox;
}



~Main

[EDIT] Although I just realized you're talking about oct-trees. The above code is written for a 3d quad tree, although the concept itself is quite easy to grasp. Simply modify the Y value based upon if you're at the top or bottom.

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave

[edited by - duhroach on July 28, 2003 12:11:44 AM]

Share this post


Link to post
Share on other sites