ItemPtr getClosestItem(Ray ray) { ItemPtr closestItem = NULL; real distance2 = 1e300; for(int i=0;i<itemNumber;i++) { Vector intersection = getIntersectPoint(&items[i], ray); real cur2 = length2(intersection); if(cur2 != 0 && cur2 < distance2) { distance2 = cur2; closestItem = &items[i]; } } return closestItem; }
Vector getPixel(Ray ray, Stack * stack, real magnitude, real refractiveIndex, int depth, ulong *seed) { ItemPtr item = getClosestItem(ray); if(item==NULL) { return ZERO; } Vector point = getIntersectPoint(item, ray) + ray.from; int specularRoughness = getSpecularRoughness(item, point); Vector normal = getNormal(item, ray, point); Vector diffuseColor = ZERO; Vector specularColor = ZERO; Ray reflection; reflection.ray = reflect(ray.ray, normal); reflection.from = point+(reflection.ray*.001f); if(length2(getDiffuse(item, point))>0 || length2(getSpecular(item, point))>0) { for(int i=0;i<lightNumber;i++) { ItemPtr light = &lights[i]; Vector lightVector = light->center-point; Vector lightPixel = fast_normalize(lightVector); real diffuseFactor = dot(normal, lightPixel); real specularFactor = dot(reflection.ray, lightPixel); if(diffuseFactor>0 || specularFactor>0) { int hitLight=0; Ray movedLightRay; movedLightRay.from=reflection.from; Vector right = getRight(lightPixel); Vector up = getUp(lightPixel, right); for(int i=0;i<SHADOW_RUNS;i++) { //TODO: this can be much faster methinks real r = random(seed)*2-1; real u = random(seed)*sqrt(1-r*r); movedLightRay.ray = fast_normalize(lightVector+right*r*light->radius+up*u*light->radius); ItemPtr closestItem = getClosestItem(movedLightRay); if(closestItem!=NULL && closestItem->type==LIGHT) { hitLight++; } } if(hitLight > 0) { real lightValue = ((real)hitLight) / SHADOW_RUNS; if(diffuseFactor>0) { diffuseFactor *= lightValue; diffuseColor = diffuseColor+light->light*diffuseFactor; } if(specularFactor>0) { specularFactor = lightValue * pow(specularFactor, specularRoughness); specularColor = specularColor+light->light*specularFactor; } } } } } Vector color = (diffuseColor*getDiffuse(item, point)+specularColor*getSpecular(item, point)) * magnitude; if(depth > 0) { Ray refraction; if(getRefraction(item, point)) { refraction.ray = refract(ray.ray, normal, item, refractiveIndex); refraction.from = point+(refraction.ray*.001f); if(length2(refraction.ray)>0) { push(stack, depth-1, refraction, magnitude, getRefraction(item, point)); } } if(getReflection(item, point) > 0) { push(stack, depth-1, reflection, magnitude * getReflection(item, point), refractiveIndex); } } return color; }
int main(int argc, char *argv[]) { /* printf("Argument count: %d\n", argc); for (int i = 0; i < argc; i++) { printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]); for (char *j=argv[i]; *j!='\0'; j++) { printf("Another way to print argument vector values: " "%c at %p memory\n", *j, j); } } */ struct Line line1; double m,c; m = -1.00; c = 6.00; setLine(&line1,m,c); printf("Line1 has slope: %f and y-intercept: %f\n",line1.slope,line1.yintercept); struct Line line2; m = 1.00; c = 6.00; setLine(&line2,m,c); printf("Line2 has slope: %f and y-intercept: %f\n",line2.slope,line2.yintercept); struct Point p1; double xval,yval; xval = 2; yval = 1; setPoint(&p1,xval,yval); printf("Point1 has x: %f and y: %f\n",p1.x,p1.y); struct Point p2; xval = 2; yval = 1; setPoint(&p2,xval,yval); printf("Point2 has x: %f and y: %f\n",p2.x,p2.y); m = getSlope(p1,p2); printf("Slope of Line joining p1 and p2: %f\n",m); p2 = getIntersectPoint(line1,line2); printf("Intersection Point of Line1 and Line2 has x: %f and y: %f\n",p2.x,p2.y); struct Square sq1; double xl,xr,yt,yb; xl = 2.00; xr = 4.00; yt = 4.00; yb = 2.00; setSquare(&sq1, xl, xr, yt, yb); printf("Sqaure1 has xleft: %f, xright: %f and ytop: %f, ybottom: %f\n",sq1.xleft,sq1.xright,sq1.ytop,sq1.ybottom); struct Point sqCentre1; sqCentre1 = getSquareCentre(sq1); printf("Centre Point of Square1 has x: %f and y: %f\n",sqCentre1.x,sqCentre1.y); struct Square sq2; xl = 4.00; xr = 6.00; yt = 6.00; yb = 4.00; setSquare(&sq2, xl, xr, yt, yb); printf("Sqaure2 has xleft: %f, xright: %f and ytop: %f, ybottom: %f\n",sq2.xleft,sq2.xright,sq2.ytop,sq2.ybottom); struct Point sqCentre2; sqCentre2 = getSquareCentre(sq2); printf("Centre Point of Square2 has x: %f and y: %f\n",sqCentre2.x,sqCentre2.y); m = getSlope(sqCentre1,sqCentre2); printf("Slope of Line joining Square 1 Centre and Square 2 Centre: %f\n",m); return 0; }