void keyboard(unsigned char key, int x, int y) { switch (key) { case '-': sealevel -= 0.01; break; case '+': case '=': sealevel += 0.01; break; //more polygons case 'v': res = (res-1)*2 + 1; makemountain(); break; //less polygons case 'c': res = (res-1)/2 + 1; makemountain(); break; //speed up case 'x': speed += 0.003; break; //speed down case 'z': speed -= 0.03; if (speed <= 0.0) { speed = 0.0; } break; //translate the plane forward. case ' ': fly = !fly; if (fly) { speed = 0.005; } else speed = 0.0; break; //move vertically upward, like a helicopter. case 'w': vertical -= 0.01; break; //more vertically downward. case 's': vertical += 0.01; break; //exit case 'q': case 27: exit(0); break; } }
//initialize scene void init(void) { glClearColor(0.5, 0.5, 1.0, 0.0); /*glShadeModel (GL_FLAT); */ glEnable(GL_DEPTH_TEST); gluLookAt(0, 0, 0, 1, 0, 0, 0, 0, 1); sealevel = -.2; makemountain(); }
void keyboard(unsigned char key, int x, int y) { switch (key) { case '-': sealevel -= 0.01; break; case '+': case '=': sealevel += 0.01; break; case 'f': res = (res-1)*2 + 1; makemountain(); break; case 'c': res = (res-1)/2 + 1; makemountain(); break; case 27: exit(0); break; } }
void makemountain(int l,int t,int r,int b) { int cx=(l+r)/2; int cy=(t+b)/2; // int s=max(max(diff(cx,r),diff(cy,b)),max(diff(cx,l),diff(cy,t))); int s=max(diff(cx,l),diff(cy,t)); float nw=alt.pos[l][t]; float ne=alt.pos[r][t]; float se=alt.pos[r][b]; float sw=alt.pos[l][b]; float c=(nw+ne+sw+se)/4.0; alt.pos[cx][cy]=c; geol(l,cy,nw,sw,s); geol(r,cy,ne,se,s); geol(cx,t,nw,ne,s); geol(cx,b,sw,se,s); // alt.display(); if (s>0) { makemountain(l,t,cx,cy); makemountain(cx,t,r,cy); makemountain(l,cy,cx,b); makemountain(cx,cy,r,b); } }
void init(void) { GLfloat amb[] = {0.2,0.2,0.2}; GLfloat diff[] = {1.0,1.0,1.0}; GLfloat spec[] = {1.0,1.0,1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, amb); glLightfv(GL_LIGHT0, GL_DIFFUSE, diff); glLightfv(GL_LIGHT0, GL_SPECULAR, spec); glClearColor (0.5, 0.5, 1.0, 0.0); // sky glEnable(GL_DEPTH_TEST); sealevel = 0.0; makemountain(); }
void main(int argc,String *argv) { ArgParser a=ArgParser(argc,argv); size=a.intafter("-gs","grid size",20); float rotspeed=a.floatafter("-rs","rotation speed",pi/80.0); float elevang=a.floatafter("-ea","elevation angle",pi/7.0); float waterlevel=a.floatafter("-wl","water level",-0.2); bool funky=a.argexists("-f","funky"); bool groovy=a.argexists("-g","groovy"); a.done(); int scrwid=800; int scrhei=600; //printf("z\n"); allegrosetup(scrwid,scrhei); //printf("a\n"); PPsetup(scrwid,scrhei,4.0,4.0); //printf("b\n"); mypalette(0,myRGB(0,0,0)); mypalette(255,myRGB(0,255,0)); makepalette(&greypalette); alt=Map2d<float>(size,size,0.0); randomise(); drv=Map2d<float>(size,size,&myrnd); vel=Map2d<float>(size,size,0.0); vs=Map2d<V3d>(size,size); int time=0; dogeol(300); while (!keypressed()) { //printf("d\n"); time++; // Generate new mountain alt.pos[0][0]=drv.pos[0][0]; alt.pos[size-1][0]=drv.pos[size-1][0]; alt.pos[0][size-1]=drv.pos[0][size-1]; alt.pos[size-1][size-1]=drv.pos[size-1][size-1]; makemountain(0,0,size-1,size-1); // Render mountain PPclear(); Matrix ma=Matrix::rotation(V3d(0,1,0),rotspeed*(float)time); Matrix mb=Matrix::rotation(V3d(1,0,0),elevang); //printf("!"); Octree<Renderable*> o=Octree<Renderable*>(V3d::o,2.0,&getpos); for (int i=0;i<size;i++) for (int j=0;j<size;j++) { //printf(">"); V3d x=V3d((float)i/(float)size*2.0-1.0,alt.pos[i][j],(float)j/(float)size*2.0-1.0); // x.y=x.y*ramp((float)i/(float)size*2.0); x=x*V3d(1,2,1)-V3d(0,1,0); bool wat; if (x.y<waterlevel) { x.y=waterlevel; wat=true; } else wat=false; x=ma*x; x=mb*x; vs.pos[i][j]=x; if (i>0 && j>0) { o.add(new Triangle3d(x,vs.pos[i-1][j],vs.pos[i][j-1])); o.add(new Triangle3d(vs.pos[i-1][j],vs.pos[i][j-1],vs.pos[i-1][j-1])); } } Ori ori; Viewpoint v=Viewpoint(V3d(0,0,-4),ori); // Viewpoint v=Viewpoint(mb*(ma*V3d(0,0,-4)),mb*(ma*ori)); render(&o,&v); o.freedom(); // PPshow(); // alt.display(); // Change drivers dogeol(1); } }
void main(int argc,String *argv) { ArgParser a=ArgParser(argc,argv); size=a.intafter("-gs","grid size",20); float rotspeed=a.floatafter("-rs","rotation speed",pi/80.0); float elevang=a.floatafter("-ea","elevation angle",pi/7.0); float waterlevel=a.floatafter("-wl","water level",-0.2); a.done(); int scrwid=640; int scrhei=480; allegrosetup(scrwid,scrhei); PPsetup(scrwid,scrhei,4.0,4.0); mypalette(0,myRGB(0,0,0)); mypalette(255,myRGB(0,255,0)); alt=Map2d<float>(size,size,0.0); randomise(); drv=Map2d<float>(size,size,&myrnd); vel=Map2d<float>(size,size,0.0); vs=Map2d<V3d>(size,size); int time=0; dogeol(1000); while (!key[KEY_ESC]) { time++; // Generate new mountain alt.pos[0][0]=drv.pos[0][0]; alt.pos[size-1][0]=drv.pos[size-1][0]; alt.pos[0][size-1]=drv.pos[0][size-1]; alt.pos[size-1][size-1]=drv.pos[size-1][size-1]; makemountain(0,0,size-1,size-1); Matrix ma=Matrix::rotation(V3d(0,1,0),rotspeed*(float)time); Matrix mb=Matrix::rotation(V3d(1,0,0),elevang); // Render mountain PPclear(); for (int i=0;i<size;i++) for (int j=0;j<size;j++) { V3d x=V3d((float)i/(float)size*2.0-1.0,alt.pos[i][j],(float)j/(float)size*2.0-1.0); x.y=x.y*ramp((float)i/(float)size*2.0); x=x*V3d(1,2,1)-V3d(0,1,0); if (x.y<waterlevel) x.y=waterlevel; x=ma*x; x=mb*x; /* x=V3d::rotate(x,V3d(0,1,0),rotspeed*(float)time); x=V3d::rotate(x,V3d(1,0,0),elevang);*/ vs.pos[i][j]=x; if (i>0) PPplotline(x,vs.pos[i-1][j]); if (j>0) PPplotline(x,vs.pos[i][j-1]); } PPshow(); // alt.display(); // Change drivers dogeol(1); } }
// create() sets the elevations of the four grid corners to 0, generates // internal elevations remembering the highest point, then generates display // lists. void Landscape::create(double rug) { makemountain(); }