void drawit(int i) { int k, j; double tempx[points[i]], tempy[points[i]]; double plane_x[points[i]], plane_y[points[i]], plane_z[points[i]]; double mod = (HEIGHT / 2) / tan(halfangle * (M_PI / 180)); for (k = 0; k < polys[i]; k++) { j = 0; while (j < shapes[i][k]) { // printf("%d", shapeorder[i][k][j]); if (fabs(z[i][shapeorder[i][k][j]]) > 10e-7) { //Checks if z == 0 tempx[j] = mod * (x[i][shapeorder[i][k][j]] / z[i][shapeorder[i][k][j]]) + (WIDTH / 2); tempy[j] = mod * (y[i][shapeorder[i][k][j]] / z[i][shapeorder[i][k][j]]) + (HEIGHT / 2); plane_x[j] = x[i][shapeorder[i][k][j]]; plane_y[j] = y[i][shapeorder[i][k][j]]; plane_z[j] = z[i][shapeorder[i][k][j]]; j++; } else { j++; } } if (check_if_behind(plane_x, plane_y, plane_z, j) == 1) { G_rgb(1, 0, .3); G_polygon(tempx, tempy, j); } } }
int main() // this tests clipping of polygon to convex window //prefix -p == polygon; prefix -w == window { int pn, wn ; double pt[2], u, v, q ; double px[100] = { 70, 460, 400} ; double py[100] = { 350, 25, 550} ; pn = 3 ; double wx[100] = { 100, 600, 550, 150} ; double wy[100] = { 150, 200, 450, 500} ; wn = 4 ; srand48(100) ; G_init_graphics (700, 700) ; G_rgb (0, 0, 0) ; G_clear() ; G_rgb (1, 0, 0) ; G_polygon(wx, wy, wn) ; G_rgb (0, 0, 1) ; G_polygon(px, py, pn) ; q = G_wait_key() ; pn = Clip_Polygon_Against_Convex_Window (px, py, pn, wx, wy, wn) ; G_rgb (1, 1, 0) ; for (int i = 0; i < pn; i++) { G_fill_circle(px[i], py[i], 2); printf("x:%.2lf, y:%.2lf\n", px[i], py[i]); } G_fill_polygon(px, py, pn) ; q = G_wait_key() ; }
int main() { double j ; char q ; G_init_graphics(601, 601) ; // the original design was for a 400x400 // window and the object is centered on 200,200 // so we recenter it and make it larger translate(-200, -200) ; scale(2.0) ; translate(300, 300) ; j = 0 ; while (j < 100) { G_rgb(0,0,0) ; G_clear() ; G_rgb(0, 0.5, 1) ; G_fill_polygon(x,y,n) ; G_rgb(1, 1, 0) ; G_polygon(x,y,n) ; // q = G_wait_key() ; G_display_image() ; usleep(25000) ; // microseconds translate(-300, -300) ; scale(0.95) ; rotate(4) ; translate(300, 300) ; j=j+1 ; } G_rgb(1,0,0) ; G_fill_circle(300,300, 50) ; q = G_wait_key() ; }
void display(int pnum, int sign, int specpow, double diffusepro, double ambient, double lightx, double lighty, double lightz){ int i, j, k, m; G_rgb(0,0,0); G_clear(); double halfAngle = 30*M_PI/180; G_clear(1,1,1); if(sign == 0){ sign = -1; }else{ sign=1; } int n=0; for(n=0; n<plsize; n++){ double xtemp[100]; double ytemp[100]; double ztemp[100]; for(j=0; j<poly_sizes[polyLoc[n].objnum][polyLoc[n].polynum];j++){ xtemp[j] = x[polyLoc[n].objnum][polygons[polyLoc[n].objnum][polyLoc[n].polynum][j]]; ytemp[j] = y[polyLoc[n].objnum][polygons[polyLoc[n].objnum][polyLoc[n].polynum][j]]; ztemp[j] = z[polyLoc[n].objnum][polygons[polyLoc[n].objnum][polyLoc[n].polynum][j]]; } int tempsize = poly_sizes[polyLoc[n].objnum][polyLoc[n].polynum]; double perpendicular[3], first[3], second[3], eye[3], light[3],reflection[3]; tempsize = Clip_Polygon_Against_Window(xtemp,ytemp,ztemp, poly_sizes[polyLoc[n].objnum][polyLoc[n].polynum]); if(tempsize < 1)continue; first[0] = xtemp[0]-xtemp[1]; first[1] = ytemp[0]-ytemp[1]; first[2] = ztemp[0]-ztemp[1]; second[0] = xtemp[0]-xtemp[2]; second[1] = ytemp[0]-ytemp[2]; second[2] = ztemp[0]-ztemp[2]; eye[0] = 0 - xtemp[0] ; eye[1] = 0 - ytemp[0] ; eye[2] = 0 - ztemp[0] ; light[0]= lightx - xtemp[0] ; light[1]= lighty - ytemp[0] ; light[2]= lightz - ztemp[0] ; double xfinal[tempsize], yfinal[tempsize]; for(j=0; j<tempsize;j++){ xfinal[j] = xtemp[j]/ztemp[j]; yfinal[j] = ytemp[j]/ztemp[j]; xfinal[j] = (xfinal[j] * (300/tan(halfAngle))) + 300; yfinal[j] = (yfinal[j] * (300/tan(halfAngle))) + 300; } D3d_x_product(perpendicular,first,second); double length = sqrt(perpendicular[0]*perpendicular[0] + perpendicular[1]*perpendicular[1] + perpendicular[2]*perpendicular[2]); perpendicular[0] = perpendicular[0]/length; perpendicular[1] = perpendicular[1]/length; perpendicular[2] = perpendicular[2]/length; double lightLength = sqrt(light[0]*light[0] + light[1]*light[1] + light[2]*light[2]); light[0] = light[0]/lightLength; light[1] = light[1]/lightLength; light[2] = light[2]/lightLength; double eyelength = sqrt(eye[0]*eye[0] + eye[1]*eye[1] + eye[2]*eye[2]); eye[0] = eye[0]/eyelength; eye[1] = eye[1]/eyelength; eye[2] = eye[2]/eyelength; double pl = dot_product(perpendicular, light); if(pl < 0){ perpendicular[0] = -1 * perpendicular[0]; perpendicular[1] = -1 * perpendicular[1]; perpendicular[2] = -1 * perpendicular[2]; pl = dot_product(perpendicular, light); } double pe = dot_product(perpendicular, eye); double intensity; if (pe < 0) { intensity = ambient ; goto JJJ ; } double diffuse = diffusepro * pl; reflection[0] = ((2 * pl) * perpendicular[0]) - light[0]; reflection[1] = ((2 * pl) * perpendicular[1]) - light[1]; reflection[2] = ((2 * pl) * perpendicular[2]) - light[2]; double specular; specular = (1 - ambient - diffusepro) * pow((dot_product(eye, reflection)),specpow); intensity = ambient + diffuse + specular; JJJ : ; double redt, greent, bluet; if(intensity <= ambient + diffusepro){ double f = intensity/ (ambient + diffusepro); redt = f*red[polyLoc[n].objnum]; greent = f* green[polyLoc[n].objnum]; bluet = f*blue[polyLoc[n].objnum]; } else{ double I[3]; I[0] = 1-red[polyLoc[n].objnum]; I[1] = 1-green[polyLoc[n].objnum]; I[2] = 1-blue[polyLoc[n].objnum]; double f = (intensity - ambient - diffusepro)/ (1 - ambient - diffusepro); redt = f*I[0] + red[polyLoc[n].objnum]; greent = f*I[1] + green[polyLoc[n].objnum]; bluet = f*I[2] + blue[polyLoc[n].objnum]; } G_rgb(redt,greent,bluet); G_fill_polygon(xfinal, yfinal, tempsize); G_rgb(0,0,0); G_polygon(xfinal, yfinal, tempsize); } }
int main() { double swidth, sheight ; double lowleftx, lowlefty, width, height ; double x[10],y[10] ; double numpoints,q, x0,y0,x1,y1 ; double p[2] ; // must do this before you do 'almost' any other // graphical tasks swidth = 400 ; sheight = 400 ; G_init_graphics (swidth, sheight) ; // draw a point, a line, some rectangles, some triangles G_rgb(1, 0, 0) ; // red G_point(200, 380) ; G_rgb(0, 1, 0) ; // green // G_line (0, 0, 400, 400) ; // won't show if part is off screen G_line (0, 0, 399, 399) ; G_rgb(0, 0, 1) ; // blue lowleftx = 200 ; lowlefty = 50 ; width = 10 ; height = 30 ; G_rectangle (lowleftx, lowlefty, width, height) ; lowleftx = 250 ; G_fill_rectangle (lowleftx, lowlefty, width, height) ; G_rgb(1, 1, 0) ; // yellow G_triangle (10, 300, 40,300, 60,250) ; G_fill_triangle (10,100, 40,100, 60,150) ; G_rgb(1, 0.5, 0) ; // orange G_circle (100, 300, 75) ; G_fill_circle (370, 200, 50) ; // prints text in your graphics window G_rgb(0, 0, 0) ; // black G_draw_string ("hello",300,100) ; // draw a polygon x[0] = 100 ; y[0] = 100 ; x[1] = 100 ; y[1] = 300 ; x[2] = 300 ; y[2] = 300 ; x[3] = 300 ; y[3] = 100 ; x[4] = 200 ; y[4] = 175 ; numpoints = 5 ; G_polygon (x,y,numpoints) ; q = G_wait_key() ; // pause to look ...any key to continue G_rgb (0.4, 0.2, 0.1) ; // brown G_fill_polygon (x,y,numpoints) ; G_rgb (0.5, 0.8, 0.4) ;// what color is this? G_wait_click(p) ; // wait for a mouse click x0 = p[0] ; y0 = p[1] ; // extract coordinates G_fill_rectangle (x0-2, y0-2, 4,4) ;// mark the clicked point G_wait_click(p) ; x1 = p[0] ; y1 = p[1] ; G_fill_rectangle (x1-2, y1-2, 4,4) ; G_rgb (0.5, 0.5, 0.5) ; // a grey G_line (x0,y0, x1,y1) ; q = G_wait_key() ; // pause again before exit G_save_image_to_file ("t01c.xwd") ; G_close() ; // terminate graphics...probably not fatal if forgotten }
int main() { int swidth, sheight ; double lowleftx, lowlefty, width, height ; double x[10],y[10] ; double numpoints,q, x0,y0,x1,y1 ; double p[2] ; int i ; // must do this before you do 'almost' any other // graphical tasks swidth = 400 ; sheight = 500 ; G_init_graphics (swidth, sheight) ; // draw a point, a line, some rectangles, some triangles G_rgb(1, 0, 0) ; // red G_point(200, 380) ; G_fill_rectangle (300,400, 50, 20) ; G_rgb(0, 1, 0) ; // green // G_line (0, 0, 400, 400) ; // won't show if part is off screen G_line (0, 0, 399, 399) ; G_fill_triangle(50, 400, 100,400, 150,475) ; G_rgb(0, 0, 1) ; // blue lowleftx = 200 ; lowlefty = 50 ; width = 10 ; height = 30 ; G_rectangle (lowleftx, lowlefty, width, height) ; lowleftx = 250 ; G_fill_rectangle (lowleftx, lowlefty, width, height) ; G_rgb(1, 1, 0) ; // yellow G_triangle (10, 300, 40,300, 60,250) ; G_fill_triangle (10,100, 40,100, 60,150) ; G_rgb(1, 0.5, 0) ; // orange G_circle (100, 300, 75) ; G_fill_circle (370, 200, 50) ; // prints text in your graphics window G_rgb(0, 0, 0) ; // black G_draw_string ("hello",300,100) ; // draw a polygon x[0] = 100 ; y[0] = 100 ; x[1] = 100 ; y[1] = 300 ; x[2] = 300 ; y[2] = 300 ; x[3] = 300 ; y[3] = 100 ; x[4] = 200 ; y[4] = 175 ; numpoints = 5 ; G_polygon (x,y,numpoints) ; G_rgb (0.4, 0.2, 0.1) ; // brown G_fill_polygon (x,y,numpoints) ; G_rgb (0.5, 0.8, 0.4) ;// what color is this? int xc,yc ; int pixel ; int rgbI[3] ; double rgb[3] ; G_wait_click(p) ; xc = p[0] ; yc = p[1] ; while (yc > 20) { printf("%d %d\n",xc,yc) ; pixel = G_get_pixel(xc,yc) ; G_convert_pixel_to_rgbI(pixel, rgbI) ; G_convert_rgbI_to_rgb(rgbI, rgb) ; printf("pixel = %x\n",pixel) ; printf("%3d %3d %3d\n",rgbI[0],rgbI[1],rgbI[2]) ; printf("%lf %lf %lf\n",rgb[0],rgb[1],rgb[2]) ; printf("\n") ; G_wait_click(p) ; xc = p[0] ; yc = p[1] ; } }