#include #include #include #include #include class camera { //need variables for the position, lookat direction, the aspect ratio and FOV private: int height; int width; float fov; vec3 up; public: vec3 position; vec3 lookat; int totalRays; // void setCamera(vec3 pos, vec3 look, int w, int h, float fo) { position = pos; lookat = look; fov = fo; height = h; width = w; totalRays = h*w; up.x = 0; up.y = 1; up.z = 0; } void setCamera(vec3 pos, vec3 look, int w, int h, vec3 upVec, float fo) { position = pos; lookat = look; fov = fo; height = h; width = w; totalRays = h*w; up.x = upVec.x; up.y = upVec.y; up.z = upVec.z; } vec3 calculateRay(int i, int j) { double radians = 3.14*(fov/180.0); double theta = radians/2.0f; double y = sin(theta); double x = cos(theta); //calculate the width and height of the virtual image plane which is 1 unit away from the camera's origin point // double W = 2.0*(y/x); // double H = W*((double)height/(double)width); double H = 2.0*(y/x); double W = H*((double)width/(double)height); //now find the 3D points in space corresponding to the pixels on the image plane //the image plane starts distance unit 1 from the origin down the view ray lookat.normalize(); vec3 imageCenter(position.x + lookat.x, position.y + lookat.y, position.z + lookat.z); //now get the horizontal direction along the width of our virtual image up.normalize(); vec3 horizontalDirection = lookat.crossProduct(up); horizontalDirection.normalize(); //now get the vertical direction for our virtual image plane vec3 verticalDirection = lookat.crossProduct(horizontalDirection); verticalDirection.normalize(); //now get the upper left point of our virtual image plane vec3 upperLeft( imageCenter.x + horizontalDirection.x*(W/2.0) + verticalDirection.x*(H/2.0), imageCenter.y + horizontalDirection.y*(W/2.0) + verticalDirection.y*(H/2.0) , imageCenter.z + horizontalDirection.z*(W/2.0) + verticalDirection.z*(H/2.0) ); double xDisp = -W/(double)width; double yDisp = -H/(double)height; vec3 point( upperLeft.x + horizontalDirection.x*xDisp*i + verticalDirection.x*yDisp*j, upperLeft.y + horizontalDirection.y*xDisp*i + verticalDirection.y*yDisp*j, upperLeft.z + horizontalDirection.z*xDisp*i + verticalDirection.z*yDisp*j ); //get the direction from the eye point into the scene vec3 viewVec(point.x - position.x, point.y - position.y, point.z - position.z); viewVec.normalize(); return viewVec; } vec3 getPosition() { return position; } };