Im trying to divide my 1km x 1km terrain sectors into 16x16 smaller sub sectors so I can better place grass and trees. I understand the concept of a quad tree, dividing sectors into quarters untill you can safely rule out all the children. However, I dont understand how you 'write' it.
Dont laugh, but I wrote it out long hand to try and better understand it. The following code cuts up one of my 1km square sectors into 16x16 sub sectors (storing the results in sector[][]).
//=============
//CULL SECTOR()
//=============
//determin visible sub sectors
void cullSector( float xPos, float zPos ) {
//---
//Two
//---
for (int x2 = 0; x2 < 2; x2++) {
for (int z2 = 0; z2 < 2; z2++) {
float size = 500.0f/2.0f;
float xoff = xPos+size+(x2*500.0f);
float zoff = zPos+size+(z2*500.0f);
if ( isBoxInFrustum(xoff,0.0f,zoff,size,1000.0f,size) ) {
//----
//Four
//----
for (int x4 = 0; x4 < 2; x4++) {
for (int z4 = 0; z4 < 2; z4++) {
size = 250.0f/2.0f;
xoff = xPos+size+(x2*500.0f)+(x4*250.0f);
zoff = zPos+size+(z2*500.0f)+(z4*250.0f);
if ( isBoxInFrustum(xoff,0.0f,zoff,size,1000.0f,size) ) {
//-----
//Eight
//-----
for (int x8 = 0; x8 < 2; x8++) {
for (int z8 = 0; z8 < 2; z8++) {
size = 125.0f/2.0f;
xoff = xPos+size+(x2*500.0f)+(x4*250.0f)+(x8*125.0f);
zoff = zPos+size+(z2*500.0f)+(z4*250.0f)+(z8*125.0f);
if ( isBoxInFrustum(xoff,0.0f,zoff,size,1000.0f,size) ) {
//-------
//Sixteen
//-------
for (int x16 = 0; x16 < 2; x16++) {
for (int z16 = 0; z16 < 2; z16++) {
size = 62.5f/2.0f;
xoff = xPos+size+(x2*500.0f)+(x4*250.0f)+(x8*125.0f)+(x16*62.5f);
zoff = zPos+size+(z2*500.0f)+(z4*250.0f)+(z8*125.0f)+(z16*62.5f);
if ( isBoxInFrustum(xoff,0.0f,zoff,size,1000.0f,size) ) {
int xstart = (x2*8)+(x4*4)+(x8*2)+(x16);
int zstart = (z2*8)+(z4*4)+(z8*2)+(z16);
sector[xstart][zstart] = true;
}
else {
int xstart = (x2*8)+(x4*4)+(x8*2)+(x16);
int zstart = (z2*8)+(z4*4)+(z8*2)+(z16);
sector[xstart][zstart] = false;
}
}} //next x16, next z16
}
else {
//all children can be hidden
int xstart = (x2*8)+(x4*4)+(x8*2); int xend = xstart + 2
int zstart = (z2*8)+(z4*4)+(z8*2); int zend = zstart + 2
for ( int x = xstart; x < xend; x++ ) {
for ( int z = zstart; z < zend; z++ ) {
sector[x][z] = false;
}} //next x, next z
}
}} //next x8, next z8
}
else {
//all children can be hidden
int xstart = (x2*8)+(x4*4); int xend = xstart + 4
int zstart = (z2*8)+(z4*4); int zend = zstart + 4
for ( int x = xstart; x < xend; x++ ) {
for ( int z = zstart; z < zend; z++ ) {
sector[x][z] = false;
}} //next x, next z
}
}} //next x4, next z4
}
else {
//all children can be hidden
int xstart = (x2*8); int xend = xstart + 8
int zstart = (z2*8); int zend = zstart + 8
for ( int x = xstart; x < xend; x++ ) {
for ( int z = zstart; z < zend; z++ ) {
sector[x][z] = false;
}} //next x, next z
}
}} //next x2, next z2
} //end cullSector()
After staring at the code for some time I just dont see how you can close those loops. I might want to subdivide into smaller sectors, and I sure as hell dont want to have to keep writing my code like that.
I know there is a better way to write it, I just cant see it.
Help!
All you need is zeal