//void lightCalculation(){ float rayCalculation(float levelDetail){ /* Default closest interection to 3000*/ float closestIntersection = 3000; int currentSphere = -1; int i; for(i = 0; i < numberOfSpheres; i++){ //if Ray and Sphere intersect if((intersectRaySphere(&ray, &spheres[i], &closestIntersection)) == 1){ currentSphere = i; } } //if not intersection found return if(currentSphere == -1){ return levelDetail; } //Finding the scaler value of the closestInterestion and the ray directions Vector scaled = vectorScalar(closestIntersection, &ray.direction); //Starting at the new location just found Vector newStart = vectorAddition(&ray.start, &scaled); //Find the normal for this new vector at the point of intersection Vector normal = vectorSubtraction(&newStart, &spheres[currentSphere].pos); float temp = vectorDotProduct(&normal, &normal); //if there's no dot product of the normal vector return if(temp == 0){ return levelDetail; } temp = (1.0 / sqrtf(temp)); //Setting the noram vector normal = vectorScalar(temp, &normal); //calucting the lights and shadows levelDetail = lightCalculation(levelDetail, currentSphere, &newStart, &normal); // The reflected ray start and direction ray.start = newStart; float reflect = 2 * vectorDotProduct(&ray.direction, &normal); Vector tmp = vectorScalar(reflect, &normal); ray.direction = vectorSubtraction(&ray.direction, &tmp); return levelDetail; }
/* Calculating the lights and shadows*/ float lightCalculation(float levelDetail, int currentSphere, Vector *newStart, Vector *normal){ // Determine the current material int getMaterialIndexValue = (spheres + currentSphere)->material; Material currentMaterial = materials[getMaterialIndexValue]; //Find the value of the light at the current position int j; for( j = 0; j < numberOfLights; j++){ Light currentLight = lights[j]; Vector distance = vectorSubtraction(¤tLight.pos, newStart); //If the dot product of the normal and the distance is less than or equal to zero //coutinue if(vectorDotProduct(normal, &distance) <= 0) { continue; } float closestIntersection; //setting the new closesInterection point closestIntersection = sqrtf(vectorDotProduct(&distance,&distance)); if(closestIntersection <= 0) { continue; } Ray lightRay; lightRay.start = *newStart; lightRay.direction = vectorScalar((1/closestIntersection), &distance); // Calculate shadows int shadow = 0; int k; for (k = 0; k < numberOfSpheres; ++k){ if (intersectRaySphere(&lightRay, &spheres[k], &closestIntersection) == 1){ shadow = 0; break; } } if (shadow != 1){ //Calculating the lambert value float lambert = vectorDotProduct(&lightRay.direction, normal) * levelDetail; //Processing the Lambert Diffusion LambertDiffusion(lambert, currentLight, currentMaterial); } } levelDetail *= currentMaterial.reflection; return levelDetail; }
void testVectors(){ vector<double> test1(10,8.0); vector<double> abc(10,7.); vector<double> res(10); cout<<"vecadd test1+abc "<<endl; vectorAdd(&test1,&abc,&res); vectorPrint(&res); cout<<"vecsub res-abc "<<endl; vectorSub(&res,&abc,&res); vectorPrint(&res); cout<<"vecscalar 3 "<<endl; vectorScalar(&res,3.); vectorPrint(&res); cout<<"vecscalar 3 "<<endl; vectorScalar(&res,3.,&test1); vectorPrint(&test1); cout<<"vecvec test1 abc "<<endl; cout<<vectorVector(&test1,&abc)<<endl; vector<map< int,double> > testmat(2);//=new vector<map< int,double> >(n); for (int i=0;i<2;++i){ testmat[i];//=new map< int,double> (); } testmat[0][0]= 1; testmat[0][1]= 1; testmat[1][0]= 1; //testmat[1][1]= 1; vector<double> vec(2,8.0); vector<double> ret(2); matrixVector(&testmat,&vec,&ret); vectorPrint(&ret); }