Vertex Rendering::shade(Ray intersect, Scene scene, Vertex viewerDirection) { // intersect is a ray with origin at the point of intersect, // and direction of the normal of the intersected polygon Vertex shade (0,0,0); vector<Vertex> lights = scene.getDirectionalLights(); for (int a=0; a<lights.size(); a++) { // see if that light is blocked, if so, it's shadow Vertex light = Vertex(lights[a].get(0), lights[a].get(1), lights[a].get(2)); Vertex color = Vertex(lights[a].get(3), lights[a].get(4), lights[a].get(5)); light = light.scale(-1); if (!isShadowed(intersect.getOrigin(), light, scene, false)) { light = light.normalize(); float gradient = max(0.0f, light.dot(intersect.getDirection())); float specular = intersect.getDirection().reflect(light.scale(-1)).dot(viewerDirection.scale(-1).normalize()); specular = pow(max(0.0f, specular),specularConst); shade = shade.add(color.scale(gradient+specular)); } } vector<Vertex> plights = scene.getPointLights(); for (int a=0; a<plights.size(); a++) { Vertex light = Vertex(plights[a].get(0), plights[a].get(1), plights[a].get(2)); Vertex color = Vertex(plights[a].get(3), plights[a].get(4), plights[a].get(5)); light = light.sub(intersect.getOrigin()); if (!isShadowed(intersect.getOrigin(), light, scene, true)) { light = light.normalize(); float gradient = max(0.0f, light.dot(intersect.getDirection())); float specular = intersect.getDirection().reflect(light.scale(-1)).dot(viewerDirection.scale(-1).normalize()); specular = pow(max(0.0f, specular),10); shade = shade.add(color.scale(gradient+specular)); } } return shade; }
/* * Calculates a Catmull Clark Edgepoint. Overloading the original Faces version to accept QFaces. */ Vertex GeometryOps::getEdgePoint(Edge* edg,QFace*fptr,int iFaces,Edge * eptr,int i){ Vertex edgePoint = Vertex(); GeometryOps::twoQFace touchingFaces; touchingFaces = getOtherFace(*edg,fptr,iFaces); // WAS PASSING THE SIZE OF EDGE ARRAY -.- edgePoint.add(*edg->getVertexA()); edgePoint.add(*edg->getVertexB()); //edgePoint.add(edg->getEdgeMidPoint()); edgePoint.add(touchingFaces.faceOne.getCentroid()); edgePoint.add(touchingFaces.faceTwo.getCentroid()); //cout << "EdgePoint Added" << vertexToString(edgePoint)<<endl; edgePoint.div(4.0f); //edgePoint.div(3.0f); //cout << "Edgepoint: \t" << vertexToString(edgePoint)<<"\n"<<endl; return edgePoint; }
/* * Calculates a Catmull Clark Edgepoint. */ Vertex GeometryOps::getEdgePoint(Edge* edg,Face*fptr,int i,Edge * eptr,int j){ Vertex edgePoint = Vertex(); GeometryOps::twoFace touchingFaces; touchingFaces = getOtherFace(*edg,fptr,i); edgePoint.add(*edg->getVertexA()); edgePoint.add(*edg->getVertexB()); //edgePoint.add(edg->getEdgeMidPoint()); edgePoint.add(touchingFaces.faceOne.getCentroid()); edgePoint.add(touchingFaces.faceTwo.getCentroid()); //cout << "EdgePoint Added" << vertexToString(edgePoint)<<endl; edgePoint.div(4.0f); //edgePoint.div(3.0f); //cout << "Edgepoint: \t" << vertexToString(edgePoint)<<"\n"<<endl; return edgePoint; }
Vertex Rendering::raytrace(Ray ray, Scene scene, int numReflections) { vector<Model*> models = scene.getModels(); vector<Sphere> spheres = scene.getSpheres(); //ray.getOrigin().print(); //ray.getDirection().print(); //cout << endl; float lowestT = 100000; Vertex color (0,0,0); // for every model, check if intersect. if so, shade it as the pixel's color for (int a=0; a<models.size(); a++) { Ray intersect = models[a]->intersect(ray); // if intersect, then shade if (!intersect.getDirection().isNull()) { float t = models[a]->intersect_t(ray); if (t < lowestT) { lowestT = t; color = Vertex(0,0,0); color = color.add(shade(intersect, scene, ray.getDirection()).scale(1-reflectConst)); color = color.add(reflect(intersect, ray.getDirection(), scene, numReflections).scale(reflectConst)); color = color.add(Vertex(ambientConst,ambientConst,ambientConst)); } } } for (int a=0; a<spheres.size(); a++) { Ray intersect = spheres[a].intersect(ray); if (!intersect.getDirection().isNull()) { float t = spheres[a].intersect_t(ray); if (t < lowestT) { lowestT = t; color = Vertex(0,0,0); color = color.add(shade(intersect, scene, ray.getDirection()).scale(1-reflectConst)); color = color.add(reflect(intersect, ray.getDirection(), scene, numReflections).scale(reflectConst)); color = color.add(Vertex(ambientConst,ambientConst,ambientConst)); } } } return color; }