Still no joy and I've changed everything to double floats just in case. Now that I've ignored what the editor is showing but it compiles and runs.
This is the DBPro code :
autocam off
sync on
sync rate 60
` load the height map
`noise=find free image() : load image "terrain.png",noise
noise = create_noise_map(64,100);
obj=create_terrain(noise)
img=create_texture(1024,320)
texture object obj,img
global light as integer
light=find free light()
make light light
set ambient light 20
set light range light,1000
set spot light light,10,30
set object specular obj,0xffffff
set object specular power obj,5
`set bump mapping on obj,img
set object smoothing obj,60
position camera 100,100,100
point camera 200,0,200
while escapekey() = 0
control_camera()
paste image noise,0,0
sync
endwhile
end
function create_noise_map(size,seed)
PN_init(0.5, 8.0, 0.9999, 6, seed)
bm = find free bitmap()
create bitmap bm, size, size
lock pixels
for x = 0 to size - 1
for y = 0 to size - 1
sx# = x : sx# = sx#` / size
sy# = y : sy# = sy#` / size
h# = PN_getheight( sx#, sy# ) * 127.0 + 128.0
col = h#
ink rgb(col,col,col),0xffffff
dot x,y
next
next
unlock pixels
img = find free image()
get image img,0,0,size-1,size-1
delete bitmap bm
endfunction img
function control_camera()
mx#=mousemovex():mx#=mx#/4.0
my#=mousemovey():my#=my#/4.0
move camera upkey()-downkey()*2
move camera right rightkey()-leftkey()*2
xr#=wrapvalue(camera angle x() + my#)
yr#=wrapvalue(camera angle y() + mx#)
rotate camera xr#,yr#,camera angle z()
rotate light light,xr#,yr#,camera angle z()
position light light,camera position x(),camera position y(),camera position z()
endfunction
#constant size_x 20
#constant size_y 20
function create_terrain(noise)
w=image width(noise)
h=image height(noise)
bm=find free bitmap()
create bitmap bm,w,h
paste image noise,0,0
` vertex count
vc=(w+1)*(h+1)
` face count
fc=w*h*2
` UV multipliers
us#=1.0 / w
vs#=1.0 / h
` object id
obj=find free object()
make object new obj,vc,fc*3
lock vertexdata for limb obj,0
` vertex width
nw=w+1
for y=0 to h
for x=0 to w
` get height map data
hgt=point(x,y) : hgt=hgt and 255
` set vertex position
set vertexdata position y*nw+x, size_x*x, hgt, size_y*y
` set vertex uv
set vertexdata uv y*nw+x, us#*x, vs#*y
next
next
` create the faces
for y=0 to h-1
for x=0 to w-1
` face index offset
fi=(y*nw+x)*6
` first triangle
set indexdata fi+0,y*nw+x
set indexdata fi+2,(y+1)*nw+x+1
set indexdata fi+1,(y+1)*nw+x
` second triangle
set indexdata fi+3,y*nw+x
set indexdata fi+5,y*nw+x+1
set indexdata fi+4,(y+1)*nw+x+1
next
next
unlock vertexdata
delete bitmap bm
set object normals obj
calculate object bounds obj
set object cull obj,1
color object obj,0xff0000
endfunction obj
function create_texture(size, seed)
randomize seed
bm = find free bitmap()
create bitmap bm, size, size
red_range = rnd(256)
green_range = rnd(256)
blue_range = rnd(256)
select rnd(2)
case 0
red_range = red_range / 2
endcase
case 1
green_range = green_range / 2
endcase
case 2
blue_range = blue_range / 2
endcase
endselect
red_half = red_range / 2
green_half = green_range / 2
blue_half = blue_range / 2
lock pixels
red_value = rnd( 255 )
green_value = rnd( 255 )
blue_value = rnd( 255 )
for x = 0 to size - 1
for y = 0 to size - 1
red_new = red_value + ( rnd( red_range ) - red_half )
if red_new < 0
red_new = 0
else
if red_new > 255
red_new = 255
endif
endif
green_new = green_value + ( rnd( green_range ) - green_half )
if green_new < 0
green_new = 0
else
if green_new > 255
green_new = 255
endif
endif
blue_new = blue_value + ( rnd( blue_range ) - blue_half )
if blue_new < 0
blue_new = 0
else
if blue_new > 255
blue_new = 255
endif
endif
ink rgb( red_new, green_new, blue_new ),0
dot x, y
next
next
unlock pixels
img = find free image()
get image img, 0,0, size-1, size-1
delete bitmap bm
endfunction img
` %%%%%%%%%%%%%%%%%%%%%%%%%%%%
` %% PERLIN NOISE functions %%
` %%%%%%%%%%%%%%%%%%%%%%%%%%%%
` I will admit that I don't fully understand how this works but I managed to get
` the general idea.
` (credits go to http://stackoverflow.com/questions/4753055/perlin-noise-generation-for-terrain)
` (which is from what I've converted this from)
function PN_init(_persistance as double float, _frequency as double float, _amplitude as double float, _octaves as integer, _randomseed as integer)
global PN_persistance as double float
global PN_frequency as double float
global PN_amplitude as double float
global PN_octaves as integer
global PN_randomseed as integer
PN_persistance = _persistance
PN_frequency = _frequency
PN_amplitude = _amplitude
PN_octaves = _octaves
PN_randomseed = 2.0 + _randomseed * _randomseed
endfunction
function PN_getheight( x as double float, y as double float )
result as double float
result = PN_amplitude * PN_total( x, y )
endfunction result
function PN_total( i as double float, j as double float )
` properties of one octave (changing each loop)
t as double float
_amplitude as double float
freq as double float
t = 0.0
_amplitude = 1
freq = PN_frequency
for k=0 to PN_octaves-1
t = t + PN_getvalue( j * freq + PN_randomseed, i * freq + PN_randomseed ) * _amplitude
_amplitude = _amplitude * PN_persistance
freq = freq * 2.0
next
endfunction t
function PN_getvalue( x as double float, y as double float )
xint as integer
yint as integer
xint = x
yint = y
xfrac as double float
yfrac as double float
xfrac = x - xint
yfrac = y - yint
` noise values
n01 as double float : n02 as double float : n03 as double float : n04 as double float
n05 as double float : n06 as double float : n07 as double float : n08 as double float
n09 as double float
n12 as double float : n14 as double float : n16 as double float
n23 as double float : n24 as double float : n28 as double float
n34 as double float
n01 = PN_noise( xint - 1, yint - 1 )
n02 = PN_noise( xint + 1, yint - 1 )
n03 = PN_noise( xint - 1, yint + 1 )
n04 = PN_noise( xint + 1, yiny + 1 )
n05 = PN_noise( xint - 1, yint )
n06 = PN_noise( xint + 1, yint )
n07 = PN_noise( xint, yint - 1 )
n08 = PN_noise( xint, yint + 1 )
n09 = PN_noise( xint, yint )
n12 = PN_noise( xint + 2, yint - 1 )
n14 = PN_noise( xint + 2, yint + 1 )
n16 = PN_noise( xint + 2, yint )
n23 = PN_noise( xint - 1, yint + 2 )
n24 = PN_noise( xint + 1, yint + 2 )
n28 = PN_noise( xint, yint + 2 )
n34 = PN_noise( xint + 2, yint + 2 )
` fint the noise values of the four corners
x0y0 as double float : x1y0 as double float
x0y1 as double float : x1y1 as double float
x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09)
x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06)
x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08)
x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04)
` interpolate between those values according to the x and y fractions
v1 as double float : v2 as double float : fin as double float
v1 = PN_interpolate(x0y0, x1y0, Xfrac) ` interpolate in x direction (y)
v2 = PN_interpolate(x0y1, x1y1, Xfrac) ` interpolate in x direction (y+1)
fin = PN_interpolate(v1, v2, Yfrac) `interpolate in y direction
endfunction fin
function PN_interpolate( x as double float, y as double float, a as double float )
nega as double float : negasqr as double float : fac1 as double float
asqr as double float : fac2 as double float : result as double float
nega = 1.0 - a
negasqr = nega * nega
fac1 = ( 3.0 * negasqr ) - ( 2.0 * ( negasqr * nega ) )
asqr = a * a
fac2 = ( 3.0 * asqr ) - ( 2.0 * ( asqr * a ) )
result = ( x * fac1 ) + ( y * fac2 )
endfunction result
function PN_noise( x as integer, y as integer )
n as integer
n = x + y * 57
n = ( n << 13 ) ^ n
t as integer
t = (n * (n * n * 15731 + 789221) + 1376312589) and 0x7fffffff
result as double float
result = t
result = 1.0 - result * 0.931322574615478515625e-9
endfunction result
And this is the C++ code :
#include "main.h"
PerlinNoise::PerlinNoise()
{
persistence = 0;
frequency = 0;
amplitude = 0;
octaves = 0;
randomseed = 0;
}
PerlinNoise::PerlinNoise(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
{
persistence = _persistence;
frequency = _frequency;
amplitude = _amplitude;
octaves = _octaves;
randomseed = 2 + _randomseed * _randomseed;
}
void PerlinNoise::Set(double _persistence, double _frequency, double _amplitude, int _octaves, int _randomseed)
{
persistence = _persistence;
frequency = _frequency;
amplitude = _amplitude;
octaves = _octaves;
randomseed = 2 + _randomseed * _randomseed;
}
double PerlinNoise::GetHeight(double x, double y) const
{
return amplitude * Total(x, y);
}
double PerlinNoise::Total(double i, double j) const
{
//properties of one octave (changing each loop)
double t = 0.0f;
double _amplitude = 1;
double freq = frequency;
for(int k = 0; k < octaves; k++)
{
t += GetValue(j * freq + randomseed, i * freq + randomseed) * _amplitude;
_amplitude *= persistence;
freq *= 2;
}
return t;
}
double PerlinNoise::GetValue(double x, double y) const
{
int Xint = (int)x;
int Yint = (int)y;
double Xfrac = x - Xint;
double Yfrac = y - Yint;
//noise values
double n01 = Noise(Xint-1, Yint-1);
double n02 = Noise(Xint+1, Yint-1);
double n03 = Noise(Xint-1, Yint+1);
double n04 = Noise(Xint+1, Yint+1);
double n05 = Noise(Xint-1, Yint);
double n06 = Noise(Xint+1, Yint);
double n07 = Noise(Xint, Yint-1);
double n08 = Noise(Xint, Yint+1);
double n09 = Noise(Xint, Yint);
double n12 = Noise(Xint+2, Yint-1);
double n14 = Noise(Xint+2, Yint+1);
double n16 = Noise(Xint+2, Yint);
double n23 = Noise(Xint-1, Yint+2);
double n24 = Noise(Xint+1, Yint+2);
double n28 = Noise(Xint, Yint+2);
double n34 = Noise(Xint+2, Yint+2);
//find the noise values of the four corners
double x0y0 = 0.0625*(n01+n02+n03+n04) + 0.125*(n05+n06+n07+n08) + 0.25*(n09);
double x1y0 = 0.0625*(n07+n12+n08+n14) + 0.125*(n09+n16+n02+n04) + 0.25*(n06);
double x0y1 = 0.0625*(n05+n06+n23+n24) + 0.125*(n03+n04+n09+n28) + 0.25*(n08);
double x1y1 = 0.0625*(n09+n16+n28+n34) + 0.125*(n08+n14+n06+n24) + 0.25*(n04);
//interpolate between those values according to the x and y fractions
double v1 = Interpolate(x0y0, x1y0, Xfrac); //interpolate in x direction (y)
double v2 = Interpolate(x0y1, x1y1, Xfrac); //interpolate in x direction (y+1)
double fin = Interpolate(v1, v2, Yfrac); //interpolate in y direction
return fin;
}
double PerlinNoise::Interpolate(double x, double y, double a) const
{
double negA = 1.0 - a;
double negASqr = negA * negA;
double fac1 = 3.0 * (negASqr) - 2.0 * (negASqr * negA);
double aSqr = a * a;
double fac2 = 3.0 * aSqr - 2.0 * (aSqr * a);
return x * fac1 + y * fac2; //add the weighted factors
}
double PerlinNoise::Noise(int x, int y) const
{
int n = x + y * 57;
n = (n << 13) ^ n;
int t = (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff;
return 1.0 - double(t) * 0.931322574615478515625e-9;/// 1073741824.0);
}
I've had both of them sat side by side trying to find out what is wrong with the DBPro version...
Thanks JackDawson, I fixed that typo but still no joy. The screen shot above is what it should be producing. I've also set some of the values to the Perlin Noise generation code to create different noise maps. Nothing...
I'm very determined...
Warning! May contain Nuts!