// Include Name : World creator // Version : 1.0a // Author : Spider // Contact : spider@bahnhof.se /* Notes in no order at all: Made by spider (spider@bahnhof.se) Fixed a bug that caused a crash when writing.(Thanks to Mark Gordon and Chris Young) inspired by height_fields output is cornered on origo, and extends between <0,0,0> and <1,1,1> (the HF, ya know) All vector calls in the macros should use , since that is how I coded it. (Y in this case is in a 2D plane, that's why :-) the world output will kill the arrays in memory, because of efficience in memory use. Noise is the initzialiser, a good thing to use. If you don't want any noise, use 0.0 finalize before creating any outputs. This will bring it to useable data. N is a value that should(must) be defined before including. it is the size of the world. */ /* Macro headers : //Make a string from a vector. debug macro. #macro v2str(vVector) //return the vector at the point vPos. //This will return the WHOLE vector, #macro point(vPos) //This macro will change the world array into 0..1 #macro finalize() //Add a linear change //vPos is in the range <0..1,0,0..1> //fMax is inn the range -1..1 #macro change_lin(vPos, fMax) //Add a logarithmic change. //vPos is in the range <0..1,0,0..1> //fMax is inn the range -1..1 #macro change_log(vPos, fMax) //Create an output union of boxes. in <0,0,0>,<1,0..1,1> //the tWhiteBox and tBlackBox are the textures for black and white ( or any other colour) squares. #macro OutBoxes(tWhiteBox, tBlackBox) //Create a output mesh between <0,0,0>,<1,0..1,1> (default texture is gradient y) #macro OutMesh() //Write the box union to a file. //Will be writtend directly, to use, do either: //object {#include "Filename" scale ... } //or add a #declare line in the written file. //tWhiteBox and tBlackBox must be declared textures before useage. Sorry :-/ #macro WriteBoxes(sFilename) //See WriteBoxes info. #macro WriteMesh(sFileName) */ //Make a string from a vector. debug macro. #macro v2str(vVector) concat("<", str(vVector.x,-1,10), ",", str(vVector.y,-1,10), ",", str(vVector.z,-1,10),">") #end #version 3.1; //used internally to reset variables #macro wMinMax() #declare wMin = +100000000; #declare wMax = -100000000; #end //Ditto #macro tMinMax() #declare tMin = +100000000; #declare tMax = -100000000; #end #ifndef(N) #error "declare N as a value before including this file\n" #end #ifdef(UsingGround) #error "Only include this file once.(ground.inc)" #else #declare UsingGround = 1; #end //init the world grid. #declare maxX = N; #declare maxY = N; #undef N #declare triangles = 0; #declare Rseed = seed(256); #declare world = array[maxX+1][maxY+1] #declare tworld = array[maxX+1][maxY+1] wMinMax() tMinMax() #statistics concat("World Size: ", str(maxX,-3,0), " x ", str(maxY,-3,0), "\n") #statistics concat(str(maxX*maxY,-3,0)," points will be stored\n\n") //return the vector at the point vPos. //This will return the WHOLE vector, #macro point(vPos) #ifndef(world) #error "output is created." #end #local X = vPos.x*maxX; #local Y = vPos.z*maxY; #if(X=0) #local X = 1; #end #if(Y=0) #local Y = 1; #end #local H = world[X][Y]; () #end //Get the height value. #macro height(vPos) #ifndef(world) #error "output is created." #end #local X = vPos.x*maxX; #local Y = vPos.z*maxY; #if(X=0) #local X = 1; #end #if(Y=0) #local Y = 1; #end (world[X][Y]) #end //Normalize every number with the current values in tMin and tMax //This is used inside the macros, don't bother calling it in other cases. #macro normAndAdd(fMaxNum) #debug "Integrating with world...\n" #debug concat("...Relevance: ", str(fMaxNum,-3,4), "\n") wMinMax() #debug concat("...first Min is: ", str(tMin,6,6), "\n") #debug concat("...first Max is: ", str(tMax,6,6), "\n") #declare tMax = tMax - tMin; #debug concat("...fixed Max is: ", str(tMax,6,6), "\n") #local iM = fMaxNum/tMax; #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare H = (tworld[X][Y]-tMin)* iM; #declare world[X][Y] = world[X][Y] + H; #if(wMaxworld[X][Y]) #declare wMin = world[X][Y]; #end #local X = X - 1; #end #local Y = Y - 1; #end #debug "...integrated\n---\n" #end //This macro will change the world array into 0..1, assumes that wMin and wMax are correct. #macro finalize() #declare wMax = wMax - wMin; #debug "Finalizing world\n" #statistics concat("...fixed world max is ", str(wMax,6,6), "\n") #statistics concat("...world min is ", str(wMin,6,6), "\n") #local iM = 1/wMax; #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare world[X][Y] = (world[X][Y]-wMin)* iM; #local X = X - 1; #end #local Y = Y - 1; #end #debug "...finalized\n---\n" #end //Add a linear change //vPos is in the range <0..1,0,0..1> //fMax is inn the range -1..1 #macro change_lin(vPos, fMax) tMinMax() #debug concat("\nCalculating LIN position ", v2str(vPos), " into tworld...\n") #local pos = vPos*((z*maxY)+(x*maxX)+(0*y)); #debug concat("...position: ",v2str(pos), "\n") #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare H = vlength(-pos); #declare tworld[X][Y] = -H; #if(tMaxtworld[X][Y]) #declare tMin = tworld[X][Y]; #end #local X = X - 1; #end #local Y = Y - 1; #end #debug "...complete\n" normAndAdd(fMax) #end //Add a logarithmic change. //vPos is in the range <0..1,0,0..1> //fMax is inn the range -1..1 #macro change_log(vPos, fMax) tMinMax() #local fact = exp(1); #debug concat("\nCalculating LOG position ", v2str(vPos), " into tworld...\n") #local pos = vPos*((z*maxY)+(x*maxX)+(0*y)); #debug concat("...position: ",v2str(pos), "\n") #local maxXY = maxX*maxY; #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare H = vlength(-pos); // #declare H = fact-log((H+2)/4); #declare H = fact-log((H+2)/maxXY); #declare H = H * 100; #declare tworld[X][Y] = H; #if(tMaxtworld[X][Y]) #declare tMin = tworld[X][Y]; #end #local X = X - 1; #end #local Y = Y - 1; #end #debug "...complete\n" normAndAdd(fMax) #end //Initzialise and add noise. #macro noise(fMax) wMinMax() #debug concat("\nCalculating noise...\n", "...Noise: ", str(fMax,-3,6), "\n") #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare world[X][Y] = rand(Rseed)*fMax; #if(wMaxworld[X][Y]) #declare wMin = world[X][Y]; #end #local X = X - 1; #end #local Y = Y - 1; #end #debug "...complete\n---\n" #end //Lets add some points. //this doesn't work. Sorry folks, do this instead. /* #declare I = 5; #declare sSeed = seed(150); #while(I>0) change_lin(, 2*rand(sSeed)-1) #declare I = I -1; #end */ #macro random(sSeed, iPoints) tMinMax() #debug "Creating random landscape...\n" #local pos = array[iPoints+1] #local aH = array[iPoints+1] #debug "...randomizing points\n" #local I = iPoints; #while(I>0) #local pos[I] = 0 + ; #local aH[I] = (rand(sSeed)*2)-1; //is it Positive or negative(hill or hole)? will be in the range -1..+1 #debug concat("...: <", str(pos[I].x,-3,6),",", str(pos[I].z,-3,6),"> @ ", str(aH[I],-3,6),",", "\n") #local I = I - 1; #end #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) #declare H = 0; #local I = iPoints; #while(I>0) #declare H = H + (aH[I]*vlength(-pos[I])); #local I = I - 1; #end #declare tworld[X][Y] = -H; #if(tMaxtworld[X][Y]) #declare tMin = tworld[X][Y]; #end #local X = X - 1; #end #local Y = Y - 1; #end #debug "...complete\n---\n" normAndAdd(1) #end //Create an output union of boxes. in <0,0,0>,<1,0..1,1> //the tWhiteBox and tBlackBox are the textures for black and white ( or any other colour) squares. #macro OutBoxes(tWhiteBox, tBlackBox) #ifdef(tworld) #undef tworld #end #debug "\nCreating output union of boxes...\n" #local s = 0.4999999; union { #local Y = maxY; #while (Y>0) #local X = maxX; #while(X>0) box { , #if(mod(X+Y,2)) texture{tWhiteBox} #else texture{tBlackBox} #end } #local X = X - 1; #end #local Y = Y - 1; #end #ifdef(world) #undef world #end #debug "...Scaling to fit\n" scale <1/maxX,1,1/maxY> } #debug "...done\n" #end //Create a output mesh between <0,0,0>,<1,0..1,1> #macro OutMesh() #ifdef(tworld) #undef tworld #end #debug "\nCreating output mesh...\n" #debug concat("...",str(((maxX-1)*(maxY-1))*2,-3,0)," triangles will be used\n") #local s = 0.4999999; mesh { #local Y = maxY-1; #while (Y>0) #local X = maxX; #while(X>1) #declare triangles = triangles + 2; triangle { , , } triangle { , , } #local X = X - 1; #end #local Y = Y - 1; #end #ifdef(world) #undef world #end #debug "...moving to origo\n" translate <-(1/maxX/2),0,-(1/maxY)/2> // pigment { gradient y colour_map { [0 rgb 0 ] [1 rgb 1 ] } } #debug "...Scaling to fit\n" scale <1/maxX,1,1/maxY> } #debug "...done\n" #end //Write the box union to a file. //Will be writtend directly, to use, do either: //object {#include "Filename" scale ... } //or add a #declare line in the written file. //tWhiteBox and tBlackBox must be declared textures before useage. Sorry :-/ #macro WriteBoxes(sFilename) #fopen Out sFilename write #debug "\nCreating output file with union of boxes\n" #local s = 0.4999999; #write(Out, "union{\n") #local Y = maxY; #while (Y>0) #debug concat("\n: ",str(Y,0,0)," ") #local X = maxX; #while(X>0) // #debug concat(str(X,0,0)," ") #write (Out, "box{", v2str(),",",v2str( )) #write (Out, " texture{") //Start texture #if(mod(X+Y,2))//The texture #write (Out, "tWhiteBox") #else #write (Out, "tBlackBox") #end #write (Out, "}}\n")//End texture and end box. #local X = X - 1; #end #local Y = Y - 1; #end #write (Out, "scale ", v2str(<1/maxX,1,1/maxX>), "\n" ) #write (Out, "}\n") #fclose Out #end //See WriteBoxes info. #macro WriteMesh(sFileName) #fopen Out sFilename write #debug "\nCreating output file with mesh...\n" #local s = 0.4999999999999999999999; #write(Out,"mesh{\n") #local Y = maxY-1; #while (Y>0) #local X = maxX; #while(X>1) #write (Out, "triangle{\n", v2str(),"," v2str(),"," v2str(),"\n}\n" ) #write (Out, "triangle{\n", v2str(),"," v2str(),"," v2str(),"\n}\n" ) #local X = X - 1; #end #local Y = Y - 1; #end // #write (Out, "pigment{gradient y colour_map{[0 rgb 0][1 rgb 1]}}\n") #write (Out, "scale", v2str(<1/maxX,1,1/maxY>),"\n") #write (Out, "}\n") #fclose Out #end