public class sphere{ double[] center; double R; material m; double[] toCenter; static double[] e=new double[] {0,0,1};//simplifying by assuming the eye is infinitely remote public sphere(){ center=new double[] {0,0,-5}; R=0.8; m=new material(); } public sphere(double r, material ms, double x, double y, double z){ center=new double[3]; center[0]=x; center[1]=y; center[2]=z; m = new material(); m.copy(ms); R=r; } public double intersect(double[] v, double[] w){ double A=dotP(w,w); toCenter=diff(v,center); double B=2*dotP(toCenter,w); double C=dotP(toCenter,toCenter) - R*R; double discr=B*B-4*A*C; if (discr<0) return 10500; if (discr==0) return -B/(2*A); if (discr>0) return (-B+Math.sqrt(discr))/(2*A); return 10500; } public double[] shade(light[] lights, double[] v, double[] w, double t){ double[] res = new double[] {0,0,0}; double[] currPt=new double[3]; for (int i=0; i<3; ++i){ currPt[i]=v[i]+t*w[i]; res[i]=m.ambient[i]; } double[] normV=diff(currPt, center); normV=normalize(normV); if (lights!=null){ for (int i=0; i0) res[j]+=lights[i].rgb[j]*m.diffuse[j]*vald;// diffuse double[] tmp=new double[3]; for (int k=0; k<3; ++k) tmp[k]=2*vald*normV[k]; double vals=dotP(diff(tmp,lights[i].dir),e); if (vals>0) res[j]+=lights[i].rgb[j]*m.specular[j]*Math.pow(vals,m.p); } } } return res; } public double dotP(double[] vs, double[] ws){ double res=0; int n=vs.length; for (int i=0; i