You are on page 1of 11

/*

* Generate Vector with user defined corners


*/
vector<vector<CHVector>>* CTerraformer::buildV3Vector(float fseedeft!"#
float fseedeft$own# float fseed%i&ht!"# float fseed%i&ht$own#
int iterations# float fbasiclen&th'(
/*
* Generate )$ Vector
*/
const int isi*e + com"ute,i*e(iterations'-
m.terrainvector/resi*e(isi*e'-
for (int i + 0- i < isi*e- i11'
(
m.terrainvector2i3/resi*e(isi*e'-
4
for (int i + 0- i < isi*e- i11'
(
for (int 5 + 0- 5< isi*e- 511'
(
m.terrainvector2i3253 + CHVector(((float'i'
* fbasiclen&th#((float'5' *fbasiclen&th# 6/0f'-
4
4
/*
* ,et user defined corners
*/
m.terrainvector203203/m.f* + fseedeft!"-
m.terrainvector2032isi*e 7 63/m.f* + fseed%i&ht!"-
m.terrainvector2isi*e 7 63203/m.f* + fseedeft$own-
m.terrainvector2isi*e 7 632isi*e 7 63/m.f* + fseed%i&ht$own-
return 8m.terrainvector-
4
void CTerraformer::com"ute$,9( float fvarian*# unsigned int random,eed'(
/*
* %un $iamond and ,:uare ,te" iterativl;
*/
int isi*e + m.terrainvector/si*e('-
int iste"si*e + isi*e 7 6-
int i&lobalsi*e + isi*e 7 6-
if (random,eed ++ <!'
(
init%andomGenerator('-
4
else
(
init%andomGenerator(random,eed'-
4
do
(
if (iste"si*e < )'(
/*
* 9rea to small
*/
break-
4
for (int i + 0- i < i&lobalsi*e- i 1+ iste"si*e'(
for (int 5 + 0- 5 < i&lobalsi*e- 5 1+ iste"si*e'(
s:uare,te"( fvarian*# i# 5# iste"si*e#
random,eed'-
4
4
for (int i + 0- i < i&lobalsi*e- i 1+ iste"si*e'(
for (int 5 + 0- 5 < i&lobalsi*e- 5 1+ iste"si*e'(
diamond,te"( fvarian*# i# 5# iste"si*e#
i&lobalsi*e# random,eed'-
4
4
fvarian* /+ )/0-
iste"si*e + iste"si*e / )-
4 while (true'-
4
/*
* Calculate =id"oints
*/
void CTerraformer::s:uare,te"(float fvarian*# int i># int i;# int iste"si*e#
unsigned int random,eed'(
/*
* %ead all Corners
*/
float ieft!" + m.terrainvector2i>32i;3/m.f*-
float ieft$own + m.terrainvector2i> 1 iste"si*e32i;3/m.f*-
float i%i&ht!" + m.terrainvector2i>32i; 1 iste"si*e3/m.f*-
float i%i&ht$own + m.terrainvector2i> 1 iste"si*e3
2i; 1 iste"si*e3/m.f*-
float randValue + (((float'rand('' / ((float'%9<$.=9?'' * () * @A'-
float noise + sinf(randValue'-
/*
* Calculate =id"oint 7 *7Value + corner avera&e 1 noise
*/
m.terrainvector2i> 1 (iste"si*e / )'32i; 1 (iste"si*e / )'3/m.f*
+ (ieft!" 1 ieft$own 1 i%i&ht!" 1 i%i&ht$own
/ B/0f 1 (noise * fvarian*'-
}
/*
* Calculate ed&e mid"oints
*/
void CTerraformer::diamond,te"(float fvarian*# int i># int i;# int iste"si*e#
int i&lobalsi*e# unsigned int random,eed'{
if (iste"si*e <+ 6'
{
return-
}
/*
* Get all Corners
*/
float ieft!" + m.terrainvector2i>32i;3/m.f*-
float ieft$own + m.terrainvector2i> 1 iste"si*e32i;3/m.f*-
float i%i&ht!" + m.terrainvector2i>32i; 1 iste"si*e3/m.f*-
float i%i&ht$own + m.terrainvector2i> 1 iste"si*e3
2i; 1 iste"si*e3/m.f*-
float i=id=id + m.terrainvector2i> 1 (iste"si*e / )'32i;
1 (iste"si*e / )'3/m.f*-
float * iTo"=id + 8m.terrainvector2i>32i; 1 (iste"si*e / )'3/m.f*-
float * ieft=id + 8m.terrainvector2i> 1 (iste"si*e / )'32i;3/m.f*-
float * i%i&ht=id + 8m.terrainvector2i> 1 (iste"si*e / )'3
2i; 1 iste"si*e3/m.f*-
float * iCot=id + 8m.terrainvector2i> 1 iste"si*e3
2i; 1 (iste"si*e / )'3/m.f*-
float noise# iD%# iD# iDT# iD$-
float randValue + 0/0f-
/*
* ChecE# whether the desired "oint has 3 or B nei&hbors
*/
int iDutsideeft + i; 7 (iste"si*e / )'-
if (iDutsideeft < 0' {
iDutsideeft + 0-
}
int iDutside%i&ht + i; 1 (6/Ff* iste"si*e'-
if (iDutside%i&ht > i&lobalsi*e' {
iDutside%i&ht + 0-
}
int iDutsideTo" + i> 7 (iste"si*e / )'-
if (iDutsideTo" < 0' {
iDutsideTo" + 0-
}
int iDutside$own + i> 1 (6/Ff* iste"si*e'-
if (iDutside$own > i&lobalsi*e'{
iDutside$own + 0-
}
/*
* Calculate @oints
*/
randValue + (((float'rand('' / ((float'%9<$.=9?'' * () * @A'-
if (GiDutsideeft'{
noise + sinf(randValue'-
*ieft=id + (ieft!" 1 ieft$own 1 i=id=id' / 3/0f
1 (noise * fvarian*'-
}
else{
noise + sinf(randValue'-
iD + m.terrainvector2i> 1 (iste"si*e / )'3
2iDutsideeft3/m.f*-
*ieft=id + (ieft!" 1 ieft$own 1 i=id=id 1 iD'
/ B/0f 1 (noise * fvarian*'-
}
randValue + (((float'rand('' / ((float'%9<$.=9?'' * () * @A'-
if (GiDutsideTo"'(
noise + sinf(randValue'-
*iTo"=id + (ieft!" 1 i%i&ht!" 1 i=id=id' / 3/0f
1 (noise * fvarian*'-
4
else{
noise + sinf(randValue'-
iDT + m.terrainvector2iDutsideTo"3
2i; 1 (iste"si*e / )'3/m.f*-
*iTo"=id + (ieft!" 1 i%i&ht!" 1 i=id=id 1 iDT'
/ B/0f 1 (noise * fvarian*'-
4
randValue + (((float'rand('' / ((float'%9<$.=9?'' * () * @A'-
if (GiDutside%i&ht'(
noise + sinf(randValue'-
*i%i&ht=id + (i%i&ht!" 1 i=id=id 1 i%i&ht$own' / 3/0f
1 (noise * fvarian*'-
4
else{
noise + sinf(randValue'-
iD% + m.terrainvector2i> 1 (iste"si*e / )'3
2iDutside%i&ht3/m.f*-
*i%i&ht=id + (i%i&ht!" 1 i=id=id 1 i%i&ht$own 1 iD%'
/ B/0f 1 (noise * fvarian*'-
4
randValue + (((float'rand('' / ((float'%9<$.=9?'' * () * @A'-
if (GiDutside$own'(
noise + sinf(randValue'-
*iCot=id + (ieft$own 1 i=id=id 1 i%i&ht$own' / 3/0f
1 (noise * fvarian*'-
4
else{
noise + sinf(randValue'-
iD$ + m.terrainvector2iDutside$own3
2i; 1 (iste"si*e / )'3/m.f*-
*iCot=id + (ieft$own 1 i=id=id 1 i%i&ht$own 1 iD$'
/ B/0f 1 (noise * fvarian*'-
4
4
/*
* $is"lace ? and H Values of all @oints sli&htl;
*/
void CTerraformer::dis"lace?H(int ibasiclen&th# unsigned int random,eed#
float frou&hness'(
float noise-
float randValue + 0/0f-
if (random,eed ++ <!'
(
init%andomGenerator('-
4
else
(
init%andomGenerator(random,eed'-
4
for (unsigned int i + 6- i < (m.terrainvector/si*e(' 7 6'- i 1+ 6'(
for (unsigned int 5 + 6- 5 < (m.terrainvector/si*e(' 7 6'- 5 1+ 6'(
randValue + (((float'rand('' / ((float'%9<$.=9?''
* () * @A'-
noise + sinf(randValue'-
m.terrainvector2i3253/m.f> 1+ (noise *(frou&hness
* ibasiclen&th''-
randValue + (((float'rand('' / ((float'%9<$.=9?''
* () * @A'-
noise + sinf(randValue'-
m.terrainvector2i3253/m.f; 1+ (noise *(frou&hness
* ibasiclen&th''-
4
4
4
bool CTerraformer::oadfrom@@=(void){
return oadfrom@@=('-
4
bool CTerraformer::oadfrom@@=(std::strin& filename'(
if (filename/com"are(Ino fileI' ++ 0'(
return false-
4
strin& row-
int iwidth + 0-
int ihei&ht + 0-
int icolorde"th + 0-
float fste"si*e + 0/0f-
bool isThumb + false# isHi&h + false-
int icounteri + 0-
int icounter5 + 0-
int ihi&hvalue + 0-
int iterations + 0-
unsigned char r&b233-
// ChecE Jormat
fstream file(filename# ios::in K ios::binar;'-
if (sizeof(file' ++ <!'
(
return false-
4
&etline(file# row'-
if (row/substr(0# )'/com"are(I@LI' ++ 6'(
return false-
4
&etline(file# row'-
iwidth + std::stoi(row'-
&etline(file# row'-
ihei&ht + std::stoi(row'-
&etline(file# row'-
row + row/substr(6'-
if (row/find(IHi&hI' G+ 76'
(
isHi&h + true-
4
else if (row/find(Iow7ThumbI' G+ 76'(
file/close('-
int "osition + filename/find(I/I'-
filename + filename/substr(0# "osition 7 6'-
return oadfrom@@=(filename'-
4
else if (row/find(IowI' ++ 76'(
return false-
4
&etline(file# row'-
row + row/substr(6'-
fste"si*e + atof(row/c.str(''-
&etline(file# row'-
icolorde"th + std::stoi(row'-
file/read(reinterpret_cast<char*>(8r&b'# sizeof(r&b''-
iterations + lo&((float'(iwidth 7 6'' / lo&()/0f'-
// %ead Color Anformations
while (Gfile/eof(''
(
file >> r&b203-
file >> r&b263-
file >> r&b2)3-
if (icounteri >+ iwidth'(
icounteri + 0-
icounter511-
4
if (isHi&h ++ true'
(
ihi&hvalue + (r&b203 * LFF3L' 1 (r&b263 * )FL' 1 r&b2)3-
m.terrainvector2icounteri32icounter53/m.f> + ihi&hvalue-
m.terrainvector2icounteri32icounter53/m.f; + ihi&hvalue-
m.terrainvector2icounteri32icounter53/m.f* + ihi&hvalue-
4
else {
m.terrainvector2icounteri32icounter53/m.f> + r&b203-
m.terrainvector2icounteri32icounter53/m.f; + r&b263-
m.terrainvector2icounteri32icounter53/m.f* + r&b2)3-
4
icounteri11-
4
file/close('-
return true-
4
CTrian&le,tri"* CTerraformer::&et,tri"(void){
return 8m.Terrain,tri"-
4
/*
* ,ave Vector in Gra;scale Ama&e
*/
void CTerraformer::,aveto@@=(std::strin& file"refi># char t'
(
char * filename + new char2file"refi>/len&th(' 1 )03-
strc";(filename# IterrainbilderMMI'-
strcat(filename# file"refi>/c.str(''-
strcat(filename# I/""mI'-
JAN *f + fo"en(filename# IwbI'-
int iwidth + m.terrainvector/si*e('-
int ihei&ht + m.terrainvector/si*e('-
values v3Values + &etV3N>trema('-
float fhi&hvalue + v3Values/fhi&hValue-
float flowValue + v3Values/flowValue-
float fnormali*edvalue-
float fratio-
float multi"liEator + 6/06f-
unsigned char tem"Char-
if (flowValue < 0/0f'
(
flowValue *+ 76/0f-
fhi&hvalue 1+ flowValue-
4
else {
fhi&hvalue 7+ flowValue-
flowValue *+ 76/0f-
4
if (t ++ OlO KK t ++ OO'(

fratio + (fhi&hvalue / )FL/0f'-
f"rintf(f# I@LMnPiMnPiMnQowMnQP60/60fMn)FFMnI# iwidth#
ihei&ht# fratio'-
fratio *+ multi"liEator-
for (int ; + m.terrainvector/si*e(' 7 6- ; >+ 0- ;77'(
for (int > + 0- ><m.terrainvector/si*e('- >11'
(
fnormali*edvalue + (m.terrainvector2;32>3/m.f*
1 flowValue'-
tem"Char + ((int'(fnormali*edvalue / fratio''
P )FL-
f"utc(tem"Char# f'- // 0 // )FF
f"utc(tem"Char# f'- // 0 // )FF
f"utc(tem"Char# f'- // 0 // )FF
4
4
4
else if (t ++ OhO KK t ++ OHO'(
//Create Thumbnail
strcat(filename# I/thumb/""mI'-
JAN *fthumb + fo"en(filename# IwbI'-
fratio + (fhi&hvalue / )FL/0f'-
f"rintf(fthumb# I@LMnPiMnPiMnQow7ThumbMnQP60/60fMn)FFMnI#
iwidth# ihei&ht# fratio'-
fratio *+ multi"liEator-
for (int ; + m.terrainvector/si*e(' 7 6- ; >+ 0- ;77'(
for (int > + 0- ><m.terrainvector/si*e('- >11'
(
fnormali*edvalue + (m.terrainvector2;32>3/m.f*
1 flowValue'-
tem"Char + ((int'(fnormali*edvalue / fratio''
P )FL-
f"utc(tem"Char# fthumb'- // 0 // )FF
f"utc(tem"Char# fthumb'- // 0 // )FF
f"utc(tem"Char# fthumb'- // 0 // )FF
4
4
fflush(fthumb'-
fclose(fthumb'-
// Create Hi&h7%es Ama&e
unsigned int tem"Ant# rest-
fratio + (fhi&hvalue / 6LRRR)6L/0f'-
f"rintf(f# I@LMnPiMnPiMnQHi&hMnQP60/60fMn)FFMnI# iwidth#
ihei&ht# fratio'-
fratio *+ multi"liEator-
for (int ; + m.terrainvector/si*e(' 7 6- ; >+ 0- ;77'(
for (int > + 0- ><m.terrainvector/si*e('- >11'
(
fnormali*edvalue + (m.terrainvector2;32>3/m.f*
1 flowValue'-
tem"Ant + (int'(fnormali*edvalue / fratio'-
rest + tem"Ant P LFF3L-
tem"Ant 7+ rest-
tem"Ant /+ LFF3L-
f"utc(tem"Ant# f'-
tem"Ant + rest-
rest + tem"Ant P )FL-
tem"Ant 7+ rest-
tem"Ant /+ )FL-
f"utc(tem"Ant# f'-
f"utc(rest# f'-
4
4
4
fflush(f'-
fclose(f'-
return-
4
/*
* Get H N>trema (min and ma> Value'
*/
values CTerraformer::&etV3N>trema(void){
int i&lobalsi*e + m.terrainvector/si*e(' 7 6-
float ihi&hvalue + m.terrainvector203203/m.f*-
float lowValue + m.terrainvector203203/m.f*-
values v3Values-
for (int i + 0- i <+ i&lobalsi*e- i 1+ 6'(
for (int 5 + 0- 5 <+ i&lobalsi*e- 5 1+ 6'(
if (m.terrainvector2i3253/m.f* >+ ihi&hvalue'
(
ihi&hvalue + m.terrainvector2i3253/m.f*-
4
else if (m.terrainvector2i3253/m.f* <+ lowValue'
(
lowValue + m.terrainvector2i3253/m.f*-
4
4
4
v3Values + ( ihi&hvalue# lowValue 4-
return v3Values-
4
vector<vector<CHVector>> Cterraformer::&etVeEtor('
(
return m.terrainvector-
4
/*
* =aEe a ,tri"
*/
void CTerraformer::calculate,tri"(void){
CVerte>* m.aTerrainVerte> + new CVerte>2m.terrainvector/si*e('
* m.terrainvector/si*e('3-
int count + 0-
int i5um"ertrue + 0-
int i5um"erfalse + 0-
int veEtorsi*e + m.terrainvector/si*e('-
bool bdirection + true-
float u# v-
float ste"si*e + m.terrainvector2032veEtorsi*e 7 63/m.f> / veEtorsi*e-
int modulo + floor(F/0J / ste"si*e'-
for (int i + 0- i < veEtorsi*e- i11'(
for (int 5 + 0- 5 < veEtorsi*e- 511'(
u + i P (modulo 1 6'-
v + 5 P (modulo 1 6'-

/*
* @osition# <ormal# Tan&ente# !# V
*/
m.aTerrainVerte>2count3/Anit(
CHVector(m.terrainvector2i3253/Get?('#
m.terrainvector2i3253/GetH('#
m.terrainvector2i3253/GetS('#
m.terrainvector2i3253/GetT(''#
CHVector(m.terrainvector2i3253/Get?('#
m.terrainvector2i3253/GetH('#
m.terrainvector2i3253/GetS('#
m.terrainvector2i3253/GetT(''/<ormal('#
CHVector(6# 6# 6# 0'#
u#
v'-
count11-
4
4
count + 0-
for (int i + 0- i < veEtorsi*e 7 6- i11'
(
for (int " + (i * veEtorsi*e'- " < ((i 1 6' * veEtorsi*e'- "11'(
i5um"ertrue + "-
m.Terrain,tri"/9ddVerte>(8m.aTerrainVerte>2"3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
m.Terrain,tri"/9ddVerte>(8m.aTerrainVerte>2"
1 veEtorsi*e3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
4
m.Terrain,tri"/9ddVerte>(8m.aTerrainVerte>2i5um"ertrue
1 veEtorsi*e3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
i11-
if (i < veEtorsi*e'(
for (int : + (i5um"ertrue 1 veEtorsi*e'- : >+
(i * veEtorsi*e'- :77'(
i5um"erfalse + :-
m.Terrain,tri"/9ddVerte>(8m.aTerrainVerte>2:3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
m.Terrain,tri"/9ddVerte>(
8m.aTerrainVerte>2: 1 veEtorsi*e3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
4
m.Terrain,tri"/9ddVerte>(
8m.aTerrainVerte>2i5um"erfalse 1 veEtorsi*e3'-
m.Terrain,tri"/9ddAnde>(count'-
count11-
4
4
m.Terrain,tri"/Anit('-
return-
4