|
Author Topic:   Wrapping Terrain
Oiler
Member
posted March 22, 2000 02:40 PM            
Hate to be the one to break the silence on coding becuase of yesterdays bad news but I need some help

I'm sick of flying over my terrain tile. How do I make it wrap so I continually fly over it.

Thanks
Oiler

IP:

mazy
New Member
posted March 23, 2000 03:58 PM            
i had to deal with exactly this problem. i used index function for accessing my arrays with height data and color and others.

it looks like:

code:

int index(int x, int y) { return (y << size_shift) + x; } // for accessing array members


-- that was only for very fast access (it was an inline function of a class)

code:

inline int land::indext(int x, int y)
{ // safe index - for calculating of normals
x %= size;
y %= size;
if (x < 0) x += size;
if (y < 0) y += size;
return index(x,y);
}


-- and that is function, which you can give any coordinates and it will recalculate them into my title. i used this function to generate my fractal title, so it assured that it will be possible to use this one title again and again ...

i will show you part of my land generation procedure ...

code:

void land::GenerateFractal(int x1, int y1, int x2, int y2, int level) // recursive fractal generator
{
if ((x2 - x1 >= 2) && (y2 - y1 >= 2))
{
// new center point
int xn = (x2 + x1) >> 1;
int yn = (y2 + y1) >> 1;
{ // for minimizing stack use (only input parameters and xn, yn will be saved to stack)
// heights in corners
int i = indext(x1,y1);
GLfloat h1 = height[i] != NO_HEIGHT ? height[i] : fract_init;
i = indext(x2,y1);
GLfloat h2 = height[i] != NO_HEIGHT ? height[i] : fract_init;
i = indext(x2,y2);
GLfloat h3 = height[i] != NO_HEIGHT ? height[i] : fract_init;
i = indext(x1,y2);
GLfloat h4 = height[i] != NO_HEIGHT ? height[i] : fract_init;

// height generation interval limiter
GLfloat max = fract_max/(1 << (level - 1));
//float max = level > 2 ? 0.8/(1 << level) : 0.5;

// new height in center
GLfloat hn;
height[indext(xn,yn)] = hn = GenerateHeight(h1 + h2 + h3 + h4, 4, max);

// new heights in surrouding cells
i = indext(xn,y1);
if (height[i] == NO_HEIGHT) height[i] = GenerateHeight(h1 + h2 + hn, 3, max);
i = indext(x2,yn);
if (height[i] == NO_HEIGHT) height[i] = GenerateHeight(h2 + h3 + hn, 3, max);
i = indext(xn,y2);
if (height[i] == NO_HEIGHT) height[i] = GenerateHeight(h3 + h4 + hn, 3, max);
i = indext(x1,yn);
if (height[i] == NO_HEIGHT) height[i] = GenerateHeight(h4 + h1 + hn, 3, max);

// for tile style - obsolete, tiling is now done by indext and some other tricks
//if (x1 == 0) height[indext(size_min_one,yn)] = height[index(x1,yn)];
//if (y1 == 0) height[indext(xn,size_min_one)] = height[index(xn,y1)];
}
// let's iterate; the right order is necessary
GenerateFractal(x1, y1, xn, yn, ++level);
GenerateFractal(xn, y1, x2, yn, level);
GenerateFractal(x1, yn, xn, y2, level);
GenerateFractal(xn, yn, x2, y2, level);
}
}

void land::Fractal() // create fractal surface
{
#ifdef DEBUG
cout << "land::Fractal() called" << endl;
#endif
height[index(0,0)] = fract_init;
// height[index(size_min_one,0)] = fract_init;
// height[index(size_min_one,size_min_one)] = fract_init;
// height[index(0,size_min_one)] = fract_init;
#ifdef WIN_PERFORMANCE
LARGE_INTEGER delta;
QueryPerformanceCounter(&delta);
#endif
GenerateFractal(0, 0, size, size, 1);
#ifdef WIN_PERFORMANCE
LARGE_INTEGER gamma;
QueryPerformanceCounter(&gamma);
cout << "land::Fractal() performance is " << int(gamma.QuadPart - delta.QuadPart) << " performace units" << endl;
#endif
}


i hope it can help ...

IP: