example 2
int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;
float flying = 0;
float[][] terrain;
void setup() {
size(600, 600, P3D);
cols = w / scl;
rows = h/ scl;
terrain = new float[cols][rows];
}
void draw() {
flying -= 0.1;
float yoff = flying;
for (int y = 0; y < rows; y++) {
float xoff = 0;
for (int x = 0; x < cols; x++) {
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
xoff += 0.2;
}
yoff += 0.2;
}
background(0);
fill(255);
text(frameRate,100,10);
stroke(255);
fill(120);
translate(width/2, height/2+50);
rotateX(PI/3);
translate(-w/2, -h/2);
for (int y = 0; y < rows-1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++) {
vertex(x*scl, y*scl, terrain[x][y]);
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
//rect(x*scl, y*scl, scl, scl);
}
endShape();
}
}
example 3
//int cols, rows;
int scl = 6;
int w = 2000;
int h = 1600;
int cols = w / scl, rows = h/ scl;
float flying = 0;
float flyingx = 0;
float[][] terrain;
boolean floored = true;
boolean mouse = true;
int floor = -20;
int ceiling = 100;
float sclx = 0.9;
float scly = 0.9;
float scl4 = 0.5;
float staticx = 0.055;
float staticy = 0.039;
float [] offsetx = new float[cols];
float [] offsety = new float[rows];
void setup() {
size(600, 600, P3D);
terrain = new float[cols][rows];
for(int i=0;i<cols;i++){
offsetx[i] = flying + sclx;
}
for(int i=0;i<rows;i++){
offsetx[i] = flyingx + scly;
}
};
void grid (){
stroke(255,255);
line(0,height/2,width,height/2);
line(width/2,0,width/2,height);
}
void Terrain(){
float mx = map(mouseX,0,width,-sclx,sclx);
float my = map(mouseY,0,height,-scly,scly);
float mx2 = map(mouseX,0,height,-sclx/4,sclx/4);
float my2 = map(mouseY,0,width,-scly/4,scly/4);
if(mouse){
flying += my;
flyingx += mx;
}else{
flying += 0.001;
flyingx += 0.001;
}
text(mx2, 90,10);
text(my2,90,30);
float yoff = flying;
for (int y = 0; y < rows; y++) {
float xoff = flyingx;
for (int x = 0; x < cols; x++) {
terrain[x][y] = map(noise(xoff, yoff), 0, 1, -ceiling, ceiling);
if(mouse){
xoff += mx;
}else{
xoff = sclx;
}
//text(xoff,60,10+10*x);
//xoff += offsetx[x];
}
//text(yoff,60,10+10*y);
if(mouse){
yoff += my;
}else{
yoff = scly;
}
//yoff += offsety[y];
}
//stroke(255);
noStroke();
noFill();
translate(width/2, height/2+50);
rotateX(1.1);
translate(-w/2, -h/2);
for (int y = 0; y < rows-1; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++) {
//rect(x*scl, y*scl, scl, scl);
float col = map(terrain[x][y],0,scl/2,0,40);
//float r =
fill(col,col,col,col);
//if(terrain[x][y]< floor && terrain[x][y]< floor - 20){
// fill(139,69,19);
// //fill(0,0,255,255);
//}
//if(terrain[x][y]< floor && terrain[x][y]> floor - 20){
// //fill(139,69,19,col+20);
// fill(0,0,255,255);
//}
if(col<40){
fill(139,69,19,150);
//fill(0,0,255,255);
}
if(terrain[x][y]< floor){
//fill(139,69,19,col+20);
fill(0,0,255,255);
}
if(terrain[x][y]> floor && terrain[x][y]< ceiling -70){
fill(0,255,0,col+70);
}
if(terrain[x][y]> ceiling -70 && terrain[x][y]< ceiling -40){
//fill(139,69,19,col+20);
fill(120,col);
}
if(terrain[x][y]> ceiling -40){
fill(255,col);
}
if(floored){
if(terrain[x][y]> floor){
vertex(x*scl, y*scl, terrain[x][y]);
}
else{
vertex(x*scl, y*scl, floor);
}
if(terrain[x][y+1]>floor){
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
}
else{
vertex(x*scl, (y+1)*scl, floor);
}}
else{
vertex(x*scl, y*scl, terrain[x][y]);
vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
}
}
endShape(OPEN);
}
};
void draw() {
background(0);
fill(255);
text(frameRate,10,10);
//fill(120);
grid();
Terrain();
};
void mouseDragged(){
}