package Graphics; import java.awt.*; import java.lang.*; import java.util.*; import java.awt.image.*; public class Object { private static final int MAX_POLYGONS = 8192; private Vector colours[] = new Vector[MAX_POLYGONS]; private Vector current_colour,orig_colour; private MyPolygon polygons[] = new MyPolygon[MAX_POLYGONS]; private int nr_polygons = 0; private boolean is_a_sphere = false; private Vector center; private double radius; private double height; public int wireframe=6; public Vector objcenter=null; public Vector auto_objcenter=new Vector(0,0,0); Light light=null; Random m=new Random(); // animation parameters Vector[] path=new Vector[10]; int npaths=0; Matrix r_transform=new Matrix(); Matrix r_temp_transform=new Matrix(); Matrix anim_transform=new Matrix(); Vector[] rlocations=new Vector[10]; int[] rtype=new int[10]; double[] rangles=new double[10]; boolean rendered=false; //____for terrain double rfactor=0; public void setLight(Vector pos , Vector col) { light=new Light(pos,col); objcenter=new Vector(pos); } public void set_anim(int frameno, int totalframes) { //__Set translation Vector trans=new Vector(); if(npaths>1) { int fp=totalframes/(npaths-1); int i=frameno/fp; //___ determine current path no int j,k; if(i>=npaths) i=npaths-1; trans=path[i].vclone(); trans=trans.subtract(path[0]); Vector sub=new Vector(); if(i+11.0) ff=1.0; sub=sub.multiplied_by(ff); trans=trans.add(sub); } //add current path r_temp_transform=r_transform.matrix_multiply(r_temp_transform); if(frameno==1) r_temp_transform.set(r_transform); //___We are doing translation with rotatary path here Vector rtrans=r_temp_transform.vec_postmultiply(return_center()); //System.out.println(rtrans); rtrans=rtrans.subtract(return_center()); // System.out.println(rtrans); Matrix rtrans_matrix=new Matrix(); rtrans_matrix.translate(rtrans); //____multiply path translation anim_transform.translate(trans); // ___ multiply rotation translation anim_transform=rtrans_matrix.matrix_multiply(anim_transform); // if(light!=null) // anim_transform.display(); } public void rotate_x(Vector location,double angle) { Matrix t=new Matrix(); Matrix m=new Matrix(); Matrix r=new Matrix(); m.rotate_x(angle); r=new Matrix(m); t=m.matrix_multiply(t); t.rotate_xl(location,angle); int i=0; for(i=0;i0) { int t=1-turn; for(j=0;j0) { int t=1-turn; for(j=0;j0) { int t=1-turn; for(j=0;j0) { int t=1-turn; for(j=0;j<4;j++) { //_____add polygons to object MyPolygon p = new MyPolygon(); p.addPoint(center.add(grid[t][j])); p.addPoint(center.add(grid[t][(j+1)%cpoints])); p.addPoint(center.add(grid[turn][(j+1)%cpoints])); this.add_polygon(p,color); p = new MyPolygon(); p.addPoint(center.add(grid[t][j])); p.addPoint(center.add(grid[turn][(j+1)%cpoints])); p.addPoint(center.add(grid[turn][j])); this.add_polygon(p,color); } } turn=1-turn; } // for i //_________add apex polygons Vector apex=new Vector(center.add(new Vector(0,ht,0))); for(j=0;j<4;j++) { int t=1-turn; //_____add polygons to object MyPolygon p = new MyPolygon(); p.addPoint(center.add(grid[t][(j+1)%cpoints])); p.addPoint(apex); p.addPoint(center.add(grid[t][j])); this.add_polygon(p,color); } objcenter=center.add(new Vector(0,ht/2,0)).vclone(); System.out.println("Creating base"); turn=0; int t=0; //_____Now create base____ double ox,oz,bb,bl; bb=bt/wireframe; bl=lt/wireframe; oz=-lt/2; for(i=0;i0) { int t=1-turn; for(j=0;j<4;j++) { //_____add polygons to object MyPolygon p = new MyPolygon(); p.addPoint(center.add(grid[t][j])); p.addPoint(center.add(grid[t][(j+1)%cpoints])); p.addPoint(center.add(grid[turn][(j+1)%cpoints])); this.add_polygon(p,color); p = new MyPolygon(); p.addPoint(center.add(grid[t][j])); p.addPoint(center.add(grid[turn][(j+1)%cpoints])); p.addPoint(center.add(grid[turn][j])); this.add_polygon(p,color); } } turn=1-turn; } // for i objcenter=center.add(new Vector(0,0,0)).vclone(); System.out.println("Creating base"); turn=0; int t=0; //_____Now create base and top____ Vector apex=new Vector(0,ht,0); double ox,oz,bb,bl; bb=bt/wireframe; bl=lt/wireframe; oz=-lt/2; for(i=0;i0) make_rocky(points,0,wireframe,0,wireframe); for(i=0;i> 24 ) & 0xff; int red=(p[y*w+x] >> 16 ) & 0xff; int green=(p[y*w+x] >> 8 ) & 0xff; int blue=(p[y*w+x] >> 0 ) & 0xff; return new Color(red,green,blue); } public void create_mapterrain(double ox ,double oz,double bt,double lt,double ht,String im,Vector color) { int segments=wireframe; int cpoints=4; System.out.println("Image : " + im); System.out.flush(); Image image=Toolkit.getDefaultToolkit().getImage(im); // Toolkit.getDefaultToolkit().prepareImage(image,-1,-1,null); ImgObs is = new ImgObs(); int w=image.getWidth(is); int h=image.getHeight(is); if(w<=0 || h<=0) while(!is.avail) Thread.yield(); h=is.height; w=is.width; int[] pixels = new int[w*h]; PixelGrabber pg=new PixelGrabber(image,0,0,w,h,pixels,0,w); try { pg.grabPixels(); } catch(InterruptedException e) { } int min=w; if(h0) make_rocky(points,0,wireframe,0,wireframe); for(i=0;i1.0) tc=1; Vector r=new Vector(c.get_x(),tc,c.get_z()); return r; } public void drawTree(tree t,int r,double angle) { if (r<=0) return; MyPoint lp = new MyPoint(); MyPoint rp = new MyPoint(); MyPoint mp = new MyPoint(); double len = Math.pow(Math.pow(t.bottom.x-t.top.x,2)+Math.pow(t.bottom.y-t.top.y,2),0.5); double mAngle=0; double dx = t.top.x-t.bottom.x; double dy = t.top.y-t.bottom.y; double temp=0; if(dx==0) { dx++; } temp = Math.atan(Math.abs(dy/dx)); if((dx<0)&&(dy<0)) { mAngle=Math.PI+temp; } if((dx<0)&&(dy>0)) { mAngle=Math.PI-temp; } if((dx>0)&&(dy<0)) { mAngle=-temp; } if((dx>=0)&&(dy>=0)) { mAngle=temp; } mp.x=t.bottom.x*t.mBr + t.top.x*(1-t.mBr); mp.y=t.bottom.y*t.mBr + t.top.y*(1-t.mBr); double lBrLen = t.lBr*len; double rBrLen = t.rBr*len; lp.x = (mp.x + lBrLen*Math.cos(t.lAngle*Math.PI/180 + mAngle)); lp.y = (mp.y + lBrLen*Math.sin(t.lAngle*Math.PI/180+ mAngle)); rp.x = (mp.x + rBrLen*Math.cos(-(t.rAngle*Math.PI/180) + mAngle)); rp.y = (mp.y + rBrLen*Math.sin(-(t.rAngle*Math.PI/180)+ mAngle)); // g2d.drawLine(t.bottom,t.top); // g2d.drawLine(mp,lp); // g2d.drawLine(mp,rp); Matrix rotm=new Matrix(); rotm.rotate_y(angle); MyPolygon p = new MyPolygon(); p.addPoint(rotm.vec_postmultiply(new Vector(t.bottom.x,t.bottom.y,0D))); p.addPoint(rotm.vec_postmultiply(new Vector(t.top.x,t.top.y,0))); // p.addPoint(new Vector(t.bottom.x,t.bottom.y,0)); this.add_polygon(p,current_colour); p = new MyPolygon(); //double sgn = Math.random(); //double z_off = 1.5*lBrLen*Math.random()*(sgn-0.5)/Math.abs(sgn-0.5); p.addPoint(rotm.vec_postmultiply(new Vector(mp.x,mp.y,0))); p.addPoint(rotm.vec_postmultiply(new Vector(lp.x,lp.y,0))); // p.addPoint(new Vector(mp.x,mp.y,0)); this.add_polygon(p,current_colour); p = new MyPolygon(); //z_off = -1.5*rBrLen*Math.random()*(sgn-0.5)/Math.abs(sgn-0.5); p.addPoint(rotm.vec_postmultiply(new Vector(mp.x,mp.y,0))); p.addPoint(rotm.vec_postmultiply(new Vector(rp.x,rp.y,0))); // p.addPoint(new Vector(mp.x,mp.y,0)); this.add_polygon(p,current_colour); r--; tree tr=new tree(); tr.lAngle=t.lAngle; tr.rAngle=t.rAngle; tr.lBr=t.lBr; tr.rBr=t.rBr; tr.mBr=t.mBr; tr.top = lp; tr.bottom = mp; current_colour=rcolor(orig_colour); drawTree(tr,r,angle); tr.top = rp; tr.bottom = mp; current_colour=rcolor(orig_colour); drawTree(tr,r,angle); tr.top = t.top; tr.bottom = mp; current_colour=rcolor(orig_colour); drawTree(tr,r,angle); } public void draw(double langle,double rangle, double lbr,double rbr,double mbr,double top,int recd,int steps) { tre=new tree(); tre.lAngle=langle; tre.rAngle=rangle; tre.lBr=lbr; tre.rBr=rbr; tre.mBr=mbr; tre.top=new MyPoint(0,top); recDepth=recd; orig_colour=current_colour; for(int i=0;i0) ysign =dir.get_y()/Math.abs(dir.get_y()); double xsign=1; if(Math.abs(dir.get_x())>0) xsign =dir.get_x()/Math.abs(dir.get_x()); double zsign=1; if(Math.abs(dir.get_z())>0) zsign =dir.get_z()/Math.abs(dir.get_z()); rotate_x(l1,Math.acos(d1)); if(d>=0) rotate_z(l1,-Math.acos(Math.abs(dir.get_y())/d1)); } // end line } // class GraphicsObject class ImgObs implements ImageObserver { int height; int width; boolean avail=false; public boolean imageUpdate(Image img,int inf ,int x,int y,int w,int h) { if(inf>=ImageObserver.HEIGHT +ImageObserver.WIDTH) { height = h; width = w; avail = true; return false; } return true; } }