/****************************************************************************************/ /* For constructing 3D worlds. */ /* */ /* Compile with: */ /* cc gen_landscape3_low.c -lm -o gen_landscape /* */ /****************************************************************************************/ #include #include #include #include FILE *ofile; float *landalt; /* Assumes objects stored as: /* type_name ... instance_name type_name x y z xAxisAngle yAxisAngle zAxisAngle ... Where x,y,z is initial positon. Ie. In def_obj, color can be anywhere or (almost) nowhere. Color r,g,b and t, where t is transparency (1=solid, 0=clear, 0.5=translucent, etc.). */ float RCONST = 1.0/2147483647.0; /* set up the rand normalizing constant */ float duration=200.0; int vertcnt=0; float vrtmem[3][3]; float old_color[4]={-1.0,0.0,0.0,0.0}; int color_compare( float *new, float *old ) { int j; if ((new[0]==old[0]) && (new[1]==old[1]) && (new[2]==old[2]) && (new[3]==old[3])) return 1; else { old[0] = new[0]; old[1] = new[1]; old[2] = new[2]; old[3] = new[3]; return 0; } } void sendvertex( float *colorv, float x, float y, float z ) { int i; float n1[3], n2[3], dotpr, vert[3]; vert[0] = x; vert[1] = y; vert[2] = z; for (i=0; i<3; i++) vrtmem[vertcnt][i] = vert[i]; if (vertcnt==2) { fprintf(ofile,""); if (!color_compare(colorv,old_color)) fprintf(ofile,"", colorv[0], colorv[1], colorv[2], colorv[3] ); for (vertcnt=0; vertcnt<3; vertcnt++) fprintf(ofile,"", vrtmem[vertcnt][0], vrtmem[vertcnt][1], vrtmem[vertcnt][2] ); fprintf(ofile,"\n"); vertcnt = 0; } else vertcnt++; } float landcolor[4], watercolor[4]; void cavg( float *c1, float cA, float cB, float *c4 ) { int i; float *c2, *c3; if (cA>0.0) c2 = landcolor; else c2 = watercolor; if (cB>0.0) c3 = landcolor; else c3 = watercolor; for (i=0; i<3; i++) c4[i] = 0.3333 * (c1[i] + c2[i] + c3[i]); } void subdivide( float x, float dx, float z, float dz, float *color1, float *color2, float *color3, float *color4, float alt1, float alt3, float alt2, float alt4, int N ) { float *cmat; int i, j, k, m; /* Original dimension, N=2. One extra point (5-extra in 2D), N=3. Assume N = 4 or 5. Etc.. */ cmat = (float *)malloc( (N+1) * (N+1) * 5 * sizeof(float) ); for (i=0; iy2) y = y1; else y = y2; if (xmax-x < 300.0) y3 = 350.0 - (xmax-x-300.0) * (350.0/600); else if (x-xmin < 300.0) y3 = 350.0 - (x-xmin-300.0) * (350.0/600); if (zmax-z < 300.0) y4 = 350.0 - (zmax-z-300.0) * (350.0/600); else if (z-zmin < 300.0) y4 = 350.0 - (z-zmin-300.0) * (350.0/600); if (y3>y4) y5 = y3; else y5 = y4; if (y5>0.0) y = y5; y = y + (50.0 + y) * ((float)random() * RCONST); return y; } void Make_Land( float xmin, float xmax, int N, float zmin, float zmax, int M ) { float x, y, z, dx, dz, *colorV, *colmap, ftmp; int i, j; /* Make terrain elevation map. */ landalt = (float *)malloc( (N+2) * (M+2) * sizeof(float) ); if (landalt==0) {printf("Insufficient memory.\n"); exit(0);} y = 0.0; dx = (xmax-xmin)/(float)N; dz = (zmax-zmin)/(float)M; i = 0; for (x=xmin; x<=xmax+0.5*dx; x=x+dx) { j = 0; for (z=zmin; z<=zmax+0.5*dz; z=z+dz) { landalt[i*(M+1)+j] = horizon_cusp( x, z, xmin, xmax, zmin, zmax ); j++; } i++; } /* Smooth. */ for (i=1; i 0.6) j++; else i++; if (i>=2) colmap[(i-2)*(M+1)+j+2] = 0.0; if (i>=1) colmap[(i-1)*(M+1)+j+1] = 0.0; colmap[i*(M+1)+j] = 0.0; if (j>=1) colmap[(i+1)*(M+1)+j-1] = 0.0; if (j>=2) colmap[(i+1)*(M+1)+j-2] = 0.0; if ((j>=2) && (i+1<15*N/32)) colmap[(i+2)*(M+1)+j-2] = 0.0; /* if (j>=3) colmap[(i+1)*(M+1)+j-3] = 0.0; if (j>=4) colmap[(i+1)*(M+1)+j-4] = 0.0; if (j>=5) colmap[(i+1)*(M+1)+j-5] = 0.0; */ } watercolor[0] = 0.0; watercolor[1] = 0.05; watercolor[2] = 0.3; watercolor[3] = 0.7; fprintf(ofile," Land\n"); i = 0; for (x=xmin; x0.0) || (colmap[(i+1)*(M+1)+j+1]>0.0) || (colmap[(i+1)*(M+1)+j]>0.0) || (colmap[i*(M+1)+j+1]>0.0)) && ((colmap[i*(M+1)+j]==0.0) || (colmap[(i+1)*(M+1)+j+1]==0.0) || (colmap[(i+1)*(M+1)+j]==0.0) || (colmap[i*(M+1)+j+1]==0.0)) ) { /* Lake edge. Subdivide for fineness. */ if (colmap[i*(M+1)+j]==0.0) { landalt[i*(M+1)+j] = 0.0; c1 = watercolor; } else c1 = landcolor; if (colmap[(i+1)*(M+1)+j+1]==0.0) { landalt[(i+1)*(M+1)+j+1] = 0.0; c4 = watercolor; } else c4 = landcolor; if (colmap[(i+1)*(M+1)+j]==0.0) { landalt[(i+1)*(M+1)+j] = 0; c2 = watercolor; } else c2 = landcolor; if (colmap[i*(M+1)+j+1]==0.0) { landalt[i*(M+1)+j+1] = 0.0; c3 = watercolor; } else c3 = landcolor; subdivide( x, dx, z, dz, c1, c2, c3, c4, landalt[i*(M+1)+j], landalt[(i+1)*(M+1)+j], landalt[i*(M+1)+j+1], landalt[(i+1)*(M+1)+j+1], 3 ); } else { if (colmap[i*(M+1)+j]>0.0) { colorV = landcolor; colorV[0] = colmap[i*(M+1)+j]; y = landalt[i*(M+1)+j]; } else { y = 0.0; colorV = watercolor; } if (y>0) { sendvertex( colorV, x, y, z ); if (colmap[(i+1)*(M+1)+j+1]>0.0) y = landalt[(i+1)*(M+1)+j+1]; else y = 0.0; sendvertex( colorV, x+dx, y, z+dz ); if (colmap[(i+1)*(M+1)+j]>0.0) y = landalt[(i+1)*(M+1)+j]; else y = 0.0; sendvertex( colorV, x+dx, y, z ); if (colmap[i*(M+1)+j]>0.0) { y = landalt[i*(M+1)+j]; } else { y = 0.0; } sendvertex( colorV, x, y, z ); if (colmap[i*(M+1)+j+1]>0.0) y = landalt[i*(M+1)+j+1]; else y = 0.0; sendvertex( colorV, x, y, z+dz ); if (colmap[(i+1)*(M+1)+j+1]>0.0) y = landalt[(i+1)*(M+1)+j+1]; else y = 0.0; sendvertex( colorV, x+dx, y, z+dz ); } } j++; } i++; } watercolor[0] = 0.0; watercolor[1] = 0.1; watercolor[2] = 0.4; watercolor[3] = 1.0; sendvertex( watercolor, xmin, -0.01, zmin ); sendvertex( watercolor, 0.0, -0.01, 0.0 ); sendvertex( watercolor, 0.0, -0.01, zmin ); sendvertex( watercolor, xmin, -0.01, zmin ); sendvertex( watercolor, xmin, -0.01, 0.0 ); sendvertex( watercolor, 0.0, -0.01, 0.0 ); fprintf(ofile,"\n"); fprintf(ofile,"\n"); free(landalt); free(colmap); } void Make_Sky( float xmin, float xmax, float zmin, float zmax, float ymin, float ymax, float r, float g, float b ) { float x, y, z, dx, dz, colorV[4]; fprintf(ofile,"Sky\n"); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmax ); fprintf(ofile,"\n"); fprintf(ofile,"\n"); } int building_cnt=0; void Building( float width, float depth, float height, float r, float g, float b, float posx, float posz ) { float x, y, z, dx, dz, xmin, ymin, zmin, xmax, zmax, ymax, colorV[4]; fprintf(ofile," BuildingT%d\n", building_cnt); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; xmin = posx; xmax = posx + width; ymin = 0.0; ymax = height; zmin = posz; zmax = posz + depth; /* front */ sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); /* back */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); /* right */ sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmin ); /* left */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymax, zmax ); /* top */ sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmax ); fprintf(ofile,"\n"); fprintf(ofile,"\n", building_cnt, building_cnt++); } int tank_cnt=0; void Tank( float length, float width, float height, float r, float g, float b, float posx, float posz, float ang ) { float x, y, z, dx, dz, xmin, ymin, zmin, xmax, zmax, ymax, colorV[4], xmaxtop, xmintop; int i; if (tank_cnt==0) { fprintf(ofile," TankT\n"); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; xmin = 0.0; ymin = 50.0; zmin = 0.0; xmax = length; zmax = width; ymax = ymin + 0.9 * height; xmaxtop = 0.9 * length; xmintop = 0.1 * length; /* front */ colorV[0] = 0.005; colorV[1] = 0.05; colorV[2] = 0.01; colorV[3] = 1.0; sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmaxtop, ymax, zmax ); sendvertex( colorV, xmintop, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmaxtop, ymax, zmax ); /* back */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmaxtop, ymax, zmin ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmintop, ymax, zmin ); sendvertex( colorV, xmaxtop, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; /* right */ sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmaxtop, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmaxtop, ymax, zmin ); sendvertex( colorV, xmaxtop, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmin ); /* left */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmintop, ymax, zmax ); sendvertex( colorV, xmintop, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmintop, ymax, zmax ); /* top */ sendvertex( colorV, xmintop, ymax, zmin ); sendvertex( colorV, xmaxtop, ymax, zmax ); sendvertex( colorV, xmaxtop, ymax, zmin ); sendvertex( colorV, xmintop, ymax, zmin ); sendvertex( colorV, xmintop, ymax, zmax ); sendvertex( colorV, xmaxtop, ymax, zmax ); xmin = 0.25 * length; ymin = ymin + height; zmin = 0.2 * width; xmax = 0.75 * length; ymax = ymin + 0.5 * height; zmax = 0.8 * width; /* Turret. */ /* front */ sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmax ); /* back */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); /* right */ sendvertex( colorV, xmax, ymin, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymin, zmin ); /* left */ sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymin, zmax ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin ); sendvertex( colorV, xmin, ymax, zmax ); /* top */ sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmax ); xmin = 0.75 * length; ymin = ymin; zmin = 0.4 * width; xmax = 1.8 * length; ymax = ymax - 0.1 * height; zmax = 0.6 * width; /* Muzzel */ for (i=0; i<3; i++) colorV[i] = 0.2 * colorV[i]; sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymax, zmax ); for (i=0; i<3; i++) colorV[i] = (1.0/0.2) * colorV[i]; fprintf(ofile,"\n"); } fprintf(ofile,"\n", tank_cnt++, posx, posz, ang); } int ship_cnt=0; void Ship( float width, float depth, float height, float r, float g, float b, float posx, float posz, float ang ) { float x, y, z, dx, dz, xmin, ymin, zmin, xmax, zmax, ymax, colorV[4]; int i; fprintf(ofile," ShipT%d\n", ship_cnt); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; xmin = 0.0; xmax = width; ymin = 0.0; ymax = height; zmin = 0.0; zmax = depth; /* Starboard-side */ sendvertex( colorV, xmin+0.05*width, ymin, zmax-0.1*depth ); sendvertex( colorV, xmax, ymin, zmax-0.5*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); sendvertex( colorV, xmin, 0.7*ymax, zmax ); sendvertex( colorV, xmin+0.05*width, ymin, zmax-0.1*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); /* Port-side */ sendvertex( colorV, xmin+0.05*width, ymin, zmin+0.1*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmin+0.1*depth ); sendvertex( colorV, xmax, ymin, zmin+0.5*depth ); sendvertex( colorV, xmin, 0.7*ymax, zmin ); sendvertex( colorV, xmax, 0.85*ymax, zmin+0.1*depth ); sendvertex( colorV, xmin+0.05*width, ymin, zmin+0.1*depth ); /* Bow */ for (i=0; i<3; i++) colorV[i] = 0.8 * colorV[i]; sendvertex( colorV, xmax, ymin, zmin+0.5*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmin+0.1*depth ); sendvertex( colorV, xmax+0.3*width, ymax, zmin+ 0.5*depth ); sendvertex( colorV, xmax, ymin, zmax-0.5*depth ); sendvertex( colorV, xmax+0.3*width, ymax, zmin+ 0.5*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); for (i=0; i<3; i++) colorV[i] = (1.0/0.8) * colorV[i]; /* Stern transom. */ sendvertex( colorV, xmin+0.05*width, ymin, zmin+0.1*depth ); sendvertex( colorV, xmin+0.05*width, ymin, zmax-0.1*depth ); sendvertex( colorV, xmin, 0.7*ymax, zmax ); sendvertex( colorV, xmin, 0.7*ymax, zmin ); sendvertex( colorV, xmin+0.05*width, ymin, zmin+0.1*depth ); sendvertex( colorV, xmin, 0.7*ymax, zmax ); /* Top deck. */ sendvertex( colorV, xmin, 0.7*ymax, zmin ); sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmin+0.1*depth ); sendvertex( colorV, xmin, 0.7*ymax, zmin ); sendvertex( colorV, xmin, 0.7*ymax, zmax ); sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); sendvertex( colorV, xmax, 0.85*ymax, zmin+0.1*depth ); /* Fore deck. */ sendvertex( colorV, xmax, 0.85*ymax, zmax-0.1*depth ); sendvertex( colorV, xmax+0.3*width, ymax, zmin + 0.5 * depth ); for (i=0; i<3; i++) colorV[i] = 1.5 * colorV[i]; /* Superstructure. */ sendvertex( colorV, xmin+0.3*width, 0.75*ymax, zmax-0.15*depth ); sendvertex( colorV, xmax-0.3*width, 0.85*ymax, zmax-0.15*depth ); sendvertex( colorV, xmax-0.5*width, 0.9*ymax+0.55*height, zmax-0.5*depth ); sendvertex( colorV, xmin+0.3*width, 0.75*ymax, zmin+0.15*depth ); sendvertex( colorV, xmax-0.5*width, 0.9*ymax+0.55*height, zmin+0.5*depth ); sendvertex( colorV, xmax-0.3*width, 0.85*ymax, zmin+0.15*depth ); sendvertex( colorV, xmin+0.3*width, 0.75*ymax, zmax-0.15*depth ); sendvertex( colorV, xmax-0.5*width, 0.9*ymax+0.55*height, zmax-0.5*depth ); sendvertex( colorV, xmin+0.3*width, 0.75*ymax, zmin+0.15*depth ); sendvertex( colorV, xmax-0.3*width, 0.75*ymax, zmax-0.15*depth ); sendvertex( colorV, xmax-0.3*width, 0.85*ymax, zmin+0.15*depth ); sendvertex( colorV, xmax-0.5*width, 0.9*ymax+0.55*height, zmax-0.5*depth ); for (i=0; i<3; i++) colorV[i] = (1.0/1.5) * colorV[i]; fprintf(ofile,"\n"); fprintf(ofile,"\n", ship_cnt, ship_cnt++, posx, posz, ang); } int jet_cnt=0; void Jet( float width, float length, float height, float altitude, float r, float g, float b, float posx, float posz, float ang ) { float x, y, z, dx, dz, xmin, ymin, zmin, xmax, zmax, ymax, colorV[4]; if (jet_cnt==0) { fprintf(ofile," JetT\n", jet_cnt ); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; xmin = 0.0; xmax = length; ymin = 0.0; ymax = height; zmin = 0.0; zmax = width; /* Delta Wing */ sendvertex( colorV, xmin, ymin, zmin+0.5*width ); sendvertex( colorV, xmax, ymin, zmax ); sendvertex( colorV, xmax, ymin, zmin ); colorV[0] = 0.1*r; colorV[1] = 0.1*g; colorV[2] = 0.1*b; colorV[3] = 1.0; sendvertex( colorV, xmin, ymin-0.5, zmin+0.5*width ); sendvertex( colorV, xmax, ymin-5.5, zmin+0.5*width ); sendvertex( colorV, xmax, ymin-0.5, zmax ); sendvertex( colorV, xmin, ymin-0.5, zmin+0.5*width ); sendvertex( colorV, xmax, ymin-0.5, zmin ); sendvertex( colorV, xmax, ymin-5.5, zmin+0.5*width ); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; /* Rudder */ sendvertex( colorV, xmin, ymin, zmin+0.5*width ); sendvertex( colorV, xmax, 0.5 * ymax, zmin+0.5*width ); sendvertex( colorV, xmax, ymin, zmin+0.45*width ); sendvertex( colorV, xmin, ymin, zmin+0.5*width ); sendvertex( colorV, xmax, ymin, zmin+0.55*width ); sendvertex( colorV, xmax, 0.5 * ymax, zmin+0.5*width ); sendvertex( colorV, 0.5 * xmax, ymin, zmin+0.5*width ); sendvertex( colorV, xmax, ymax, zmin+0.5*width ); sendvertex( colorV, xmax, ymin, zmin+0.45*width ); sendvertex( colorV, 0.5 * xmax, ymin, zmin+0.5*width ); sendvertex( colorV, xmax, ymin, zmin+0.55*width ); sendvertex( colorV, xmax, ymax, zmin+0.5*width ); fprintf(ofile,"\n"); } fprintf(ofile,"\n", jet_cnt++, posx, altitude, posz, ang); } int helo_cnt=0; void Helo( float width, float length, float height, float altitude, float r, float g, float b, float posx, float posz, float ang ) { float x, y, z, dx, dz, xmin, ymin, zmin, xmax, zmax, ymax, colorV[4]; if (helo_cnt==0) { fprintf(ofile," HeloT\n", helo_cnt); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; xmin = 0.0; xmax = length; ymin = 0.0; ymax = height; zmin = -0.5*width; zmax = 0.5*width; /* Top */ sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymax, zmin ); /* Left Side */ sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, 0.95*xmax, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymin, zmin+0.5*width ); /* Right side */ sendvertex( colorV, xmin, ymin, zmax-0.5*width ); sendvertex( colorV, 0.95*xmax, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin-0.2*length, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin-0.2*length, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymin, zmax-0.5*width ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin-0.2*length, ymin+0.5*height, zmin+0.5*width ); sendvertex( colorV, xmin, ymax, zmin ); sendvertex( colorV, xmin, ymin, zmin+0.5*width ); fprintf(ofile,"\n"); } fprintf(ofile,"\n", helo_cnt, posx, altitude, posz, ang); ymax = 1.2 * height; xmin = - 0.8 * length; xmax = 0.8 * length; zmin = - 0.05 * length; zmax = 0.05 * length; if (helo_cnt==0) { fprintf(ofile," Blade\n"); sendvertex( colorV, xmin, ymax+0.03*height, zmin ); sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmax, ymax+0.03*height, zmin ); sendvertex( colorV, xmin, ymax+0.03*height, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmax, ymax+0.03*height, zmin ); ymax = ymax - 0.01 * height; sendvertex( colorV, xmax, ymax, zmax ); sendvertex( colorV, xmin, ymax-0.03*height, zmin ); sendvertex( colorV, xmax, ymax-0.03*height, zmin ); sendvertex( colorV, xmin, ymax, zmax ); sendvertex( colorV, xmin, ymax-0.03*height, zmin ); sendvertex( colorV, xmax, ymax-0.03*height, zmin ); fprintf(ofile,"\n"); } fprintf(ofile,"\n", helo_cnt++, posx, altitude, posz, ang); fprintf(ofile,"\n", helo_cnt-1, duration, 360 * duration); } int road_cnt=0; void Road( float width, float length, float xpos, float zpos, float angle, float altitude, float r, float g, float b ) { float colorV[4]; fprintf(ofile," RoadT%d\n", road_cnt ); colorV[0] = r; colorV[1] = g; colorV[2] = b; colorV[3] = 1.0; /* top */ sendvertex( colorV, 0.0, altitude, 0.0 ); sendvertex( colorV, length, altitude, width ); sendvertex( colorV, length, altitude, 0.0 ); sendvertex( colorV, 0.0, altitude, 0.0 ); sendvertex( colorV, 0.0, altitude, width ); sendvertex( colorV, length, altitude, width ); fprintf(ofile,"\n"); fprintf(ofile,"\n", road_cnt, road_cnt++, xpos, zpos, angle); } /********************************************************************************************************/ /* OrbitCenter - This routine calculates an object's center of orbit, given it's initial position, */ /* a radius of orbit, and a second position on (the tangent) of it's trajectory. */ /* Also, of course, a switch to find the 'port' (-1) or 'starboard' (+1) side center. */ /* Basically, this calculates the x-y coord of a point 90-degrees to the right or left of the object at */ /* the distance of the radius. Result returned in first two params. */ /********************************************************************************************************/ void orbitcenter( float *centx, float *centy, float radius, int side, float x1, float y1, float x2, float y2 ) { float dx, dy, h; dx = x1 - x2; dy = y1 - y2; h = radius / sqrt(dx*dx + dy*dy); *centx = x1 + (float)side * dy * h; *centy = y1 - (float)side * dx * h; } /********************************************************************************************************/ /* Center_Orbit - This routine calculates an object's center of orbit, given it's initial position, */ /* and angle of orientation, assumed as the angle of the tangent of orbit at that point. */ /* Also, of course, a switch to find the 'port' (-1) or 'starboard' (+1) side center. */ /* Basically, this calculates the x-y coord of a point 90-degrees to the right or left of the object at */ /* the distance of the radius. Launches orbit commands. */ /********************************************************************************************************/ void center_orbit( float radius, int side, float x, float z, float ang) { float centx, centy; centx = x - radius * (float)side * sin(-3.14159 * ang/180.0); centy = z + radius * (float)side * cos(-3.14159 * ang/180.0); fprintf(ofile,"\n", tank_cnt-1, centx, 50.0, centy, (float)side * 20.0 * duration ); fprintf(ofile,"\n", tank_cnt-1, - (float)side * 20.0 * duration + ang); } /*================================================================*/ int main( int argc, char **argv ) { int i, side=1; /* initialize the random generator seed: */ srandom( 123 ); /*---------------------------------------------------------------*/ /* Describe any object models. Object ID's should begin at "1". */ /*---------------------------------------------------------------*/ ofile = fopen("geom.dat", "w"); fprintf(ofile,"\n \n"); fprintf(ofile," \n \n\n"); Make_Land( -5000.0, 5000.0, 30, -5000.0, 5000.0, 30 ); /* Make_Sky( -5101.0, 5101.0, -5101.0, 5101.0, 50.0, 2000.0, 0.25, 0.25, 0.9 ); */ /* Make city */ { float x, z, w, d, h, c, tint1, tint2; for (i=0; i<70; i++) { x = 1700.0 * ((float)random() * RCONST) - 450.0; z = 1700.0 * ((float)random() * RCONST) - 450.0; w = 40.0 + 80.0 * ((float)random() * RCONST); d = 40.0 + 80.0 * ((float)random() * RCONST); h = 50.0 + 160.0 * ((float)random() * RCONST); c = ((float)random() * RCONST); tint1 = 0.5 * c * ((float)random() * RCONST); tint2 = 0.25 * c * ((float)random() * RCONST); if (h>50.0) Building( w, d, h, 0.8 * c-tint1, c-0.75*tint2, c, x, z ); } for (i=0; i<15; i++) { x = 500.0 * ((float)random() * RCONST) + 1000.0; z = 500.0 * ((float)random() * RCONST) - 2050.0; w = 120.0 * ((float)random() * RCONST); d = 80.0 * ((float)random() * RCONST); h = 100.0 * ((float)random() * RCONST); c = ((float)random() * RCONST); tint1 = 0.5 * c * ((float)random() * RCONST); tint2 = 0.5 * c * ((float)random() * RCONST); if (h>50.0) Building( w, d, h, c-tint1, c-0.75*tint2, c, x, z ); } } /* Road( width, length, posx, posz, angle, altitude, r, g, b ) */ Road( 65.0, 1000.0, -1300.0, 700, -45.0, 40.0, 0.7, 0.95, 1.0 ); Road( 2.0, 1000.0, -1320.0, 720, -45.0, 45.0, 1.0, 1.0, 0.5 ); Road( 55.0, 500.0, -1150.0, 1200, 20.0, 40.0, 0.6, 0.9, 0.95 ); Road( 35.0, 1800.0, 800.0, 0.0, 70.0, 35.0, 0.5, 0.85, 0.9 ); Road( 30.0, 1600.0, -1700.0, -510.0, -15.0, 40.0, 0.5, 0.85, 0.9 ); Road( 30.0, 1525.0, -3200.0, -250.0, 10.0, 45.0, 0.5, 0.85, 0.9 ); Road( 30.0, 1100.0, -150.0, 100.0, 0.0, 40.0, 0.5, 0.85, 0.9 ); Road( 30.0, 800.0, 300.0, 450.0, 90.0, 40.0, 0.5, 0.85, 0.9 ); Ship( 200.0, 50.0, 40.0, 0.3, 0.5, 0.45, -1000.0, -1000.0, 0.0 ); Ship( 100.0, 25.0, 20.0, 0.15, 0.25, 0.2, -2000.0, -1800.0, -45.0 ); Ship( 100.0, 25.0, 20.0, 0.15, 0.25, 0.2, -2500.0, -2200.0, 25.0 ); Ship( 100.0, 25.0, 20.0, 0.15, 0.25, 0.2, -2800.0, -2400.0, -40.0 ); /* Tank( length, width, height, r, g, b, posx, posz, ang ) */ Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -800.0, -2300.0, -270.0 ); center_orbit( 200.0, side, -800.0, -2300.0, -270.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -460.0, -2280.0, 50.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -200.0, -2400.0, 100.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -20.0, -2300.0, -30.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, 100.0, -2600.0, -60.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, 20.0, -2500.0, 30.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, 200.0, -2700.0, 10.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -180.0, -2900.0, 300.0 ); Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, -120.0, -3100.0, -80.0 ); for (i=0; i<15; i++) { float x, z, ang; x = - 1400.0 * ((float)random() * RCONST) - 1600.0; z = 2000.0 * ((float)random() * RCONST) + 1200.0; ang = 360.0 * ((float)random() * RCONST); if (ang>200.0) Tank( 40.0, 25.0, 12.0, 0.04, 0.2, 0.12, x, z, ang ); else Tank( 40.0, 25.0, 12.0, 0.2, 0.4, 0.3, x, z, ang ); center_orbit( 200.0, side, x, z, ang); side = -side; } /* Jet( width, length, height, altitude, r, g, b, posx, posz, ang ); */ Jet( 40.0, 80.0, 25.0, 50.0, 0.5, 0.9, 0.85, -1200.0, 700.0, 60.0 ); Jet( 40.0, 80.0, 25.0, 50.0, 0.5, 0.9, 0.85, -1125.0, 800.0, 60.0 ); Jet( 40.0, 80.0, 25.0, 50.0, 0.5, 0.9, 0.85, -1050.0, 900.0, 60.0 ); for (i=0; i<8; i++) { float centx, centz; int side; Jet( 40.0, 60.0, 20.0, 350.0 + (float)i + 50.0, 0.5, 0.9, 0.85, (float)i * 220.0, (float)i * 120.0, -30.0 ); if (i % 2 == 0) side = -1; else side = 1; orbitcenter( ¢x, ¢z, 2000.0, side, (float)i * 220.0, (float)i * 120.0, (float)(i-1) * 220.0, (float)(i-1) * 120.0 ); fprintf(ofile,"\n", jet_cnt-1, centx, 350.0 + (float)i + 50.0, centz, (float)side * 1800.0 ); fprintf(ofile,"\n", jet_cnt-1, - (float)side * 1800.0 - 30.0); } Helo( 40.0, 80.0, 25.0, 180.0, 0.0, 0.015, 0.005, -1000.0, -300.0, -30.0 ); Helo( 40.0, 80.0, 25.0, 250.0, 0.0, 0.1, 0.015, -1200.0, -1200.0, 60.0 ); fprintf(ofile,"\n"); fprintf(ofile,"\n"); fclose(ofile); printf("Wrote 'geom.dat'\n"); return 0; }