#declare ImageWidth = 320; //160; #declare ImageHeight = 240; //120; #declare MaxRecLev = 5; // Maximum recursion level #declare AmbientLight = <0.2, 0.2, 0.2>; #declare BGColor = <0, 0, 0>; // Sphere information. // Values are: // Center, , Color , #declare Coord = array[2][4] { {<-1.2, 0.00, 4.00>, <1.0, 0.0, 0>, <0.00, 0.0, 0.20>, <40, 0.8, 0>} {< 1.2, 0.00, 4.00>, <1.0, 0.0, 0>, <0.00, 0.0, 0.20>, <40, 0.8, 64>} } // Light source directions and colors: #declare LVect = array[3][2] { {<-1, 0, -0.5>, <0.8, 0.4, 0.1>} {< 1, 1, -0.5>, <1.0, 1.0, 1.0>} {< 0, 1, 0.0>, <0.1, 0.2, 0.5>} } //================================================================== // Raytracing calculations: //================================================================== #declare MaxDist = 1e5; #declare ObjAmnt = dimension_size (Coord, 1); #declare LightAmnt = dimension_size (LVect, 1); #declare Ind = 0; #while(Ind < LightAmnt) #declare LVect[Ind][0] = vnormalize (LVect[Ind][0]); #declare Ind = Ind+1; #end #macro calcRaySphereIntersection (P, D, sphereInd) #local V = P - Coord[sphereInd][0]; // Ray starting point - center of sphere #local R = Coord[sphereInd][1].x; // Radius of sphere #local DV = vdot (D, V); #local D2 = vdot (D, D); #local SQ = DV*DV - D2*(vdot (V, V) - R*R); #if(SQ < 0) #local Result = -1; #else #local SQ = sqrt (SQ); #local T1 = (-DV+SQ)/D2; #local T2 = (-DV-SQ)/D2; #local Result = (T10 & T0) #local Shadowed = true; #local Ind2 = ObjAmnt; #end #local Ind2 = Ind2 + 1; #end #if(!Shadowed) // Diffuse: #local Factor = vdot (Normal, L); // cosine between two vectors #if (Factor > 0) #local Pixel = Pixel + LVect[Ind][1] * Coord[closest][2] * Factor; #end //################################################################################################## // Specular: #local Factor = vdot (vnormalize (Refl), L); // cosine between two vectors #if (Factor > 0) // Light color phong size phong amount #local Pixel = Pixel + LVect[Ind][1] * pow (Factor, Coord[closest][3].x) * Coord[closest][3].y; #end //################################################################################################## #end #local Ind = Ind + 1; #end // Reflection: #if(recLev < MaxRecLev & Coord[closest][1].y > 0) #local Pixel = Pixel + Trace(IP, Refl, recLev+1)*Coord[closest][1].y; #end #end Pixel #end #debug "Rendering...\n\n" #declare Image = array[ImageWidth][ImageHeight] #declare IndY = 0; #while (IndY < ImageHeight) #declare CoordY = IndY/(ImageHeight-1)*2-1; #declare IndX = 0; #while (IndX < ImageWidth) #declare CoordX = (IndX/(ImageWidth-1)-0.5)*2*ImageWidth/ImageHeight; #declare Image[IndX][IndY] = Trace (-z*3, , 1); // Trace call #declare IndX = IndX+1; #end #declare IndY = IndY+1; #debug concat("\rDone ", str(100*IndY/ImageHeight, 0, 1), "% (line ",str(IndY,0,0)," out of ",str(ImageHeight,0,0),")") #end #debug "\n" //================================================================== // Image creation (colored mesh): //================================================================== #default { finish { ambient 1 } } #debug "Creating colored mesh to show image...\n" mesh2 { vertex_vectors { ImageWidth*ImageHeight*2, #declare IndY = 0; #while (IndY < ImageHeight) #declare IndX = 0; #while (IndX < ImageWidth) <(IndX/(ImageWidth-1)-.5)*ImageWidth/ImageHeight*2, IndY/(ImageHeight-1)*2-1, 0>, <((IndX+.5)/(ImageWidth-1)-.5)*ImageWidth/ImageHeight*2, (IndY+.5)/(ImageHeight-1)*2-1, 0> #declare IndX = IndX+1; #end #declare IndY = IndY+1; #end } texture_list { ImageWidth*ImageHeight*2, #declare IndY = 0; #while (IndY < ImageHeight) #declare IndX = 0; #while (IndX < ImageWidth) texture { pigment { rgb Image[IndX][IndY] } } #if (IndX < ImageWidth-1 & IndY < ImageHeight-1) texture { pigment { rgb (Image[IndX][IndY]+Image[IndX+1][IndY]+ Image[IndX][IndY+1]+Image[IndX+1][IndY+1])/4 } } #else texture { pigment { rgb 0 } } #end #declare IndX = IndX+1; #end #declare IndY = IndY+1; #end } face_indices { (ImageWidth-1)*(ImageHeight-1)*4, #declare IndY = 0; #while (IndY < ImageHeight-1) #declare IndX = 0; #while (IndX < ImageWidth-1) , IndX*2+ IndY *(ImageWidth*2), IndX*2+2+IndY *(ImageWidth*2), IndX*2+1+IndY *(ImageWidth*2), , IndX*2+ IndY *(ImageWidth*2), IndX*2+ (IndY+1)*(ImageWidth*2), IndX*2+1+IndY *(ImageWidth*2), , IndX*2+ (IndY+1)*(ImageWidth*2), IndX*2+2+(IndY+1)*(ImageWidth*2), IndX*2+1+IndY *(ImageWidth*2), , IndX*2+2+IndY *(ImageWidth*2), IndX*2+2+(IndY+1)*(ImageWidth*2), IndX*2+1+IndY *(ImageWidth*2) #declare IndX = IndX+1; #end #declare IndY = IndY+1; #end } } camera { orthographic location -z*2 look_at 0 }