public class spherebis{ 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 spherebis(){ center=new double[] {0,0,-5}; R=0.8; m=new material(); } public spherebis(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[] shade(sphere[] obj, 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 & tTmp<9995){ shadowed=1; break; } } if (shadowed==1){ continue; } for (int j=0;j<3;j++){ //double vald=lights[i].dir[0]*normV[0]+lights[i].dir[1]*normV[1]+lights[i].dir[2]*normV[2]; double vald=dotP(lights[i].dir, normV); if (vald>0) 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