Created
October 13, 2012 14:39
-
-
Save anonymous/3884855 to your computer and use it in GitHub Desktop.
ray cast function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void RayTracer::draw(const Scene &s, Screen& screen){ | |
int w = screen.getWidth(); | |
cout << "Width is: " << w << endl; | |
int h = screen.getHeight(); | |
double tanfov = tan(s.getEye().fov/2.0); | |
double d = boost::numeric::ublas::norm_2(s.getEye().eyepoint - s.getEye().lookat); | |
//cout << d << endl; | |
//cout << tanfov << endl; | |
//cout << "screen width : " << d * tanfov << endl; | |
double s_width = d *tanfov; | |
double s_height = (s_width * h) / w; | |
double p_width = (s_width * 2) / w; | |
double p_height = (s_height * 2) /h; | |
//cout << s_width << endl; | |
//cout << s_height << endl; | |
//cout << p_width << endl; | |
//cout << p_height << endl; | |
boost::numeric::ublas::vector<double> u = s.getEye().lookup - s.getEye().lookat; //vertical unit (up positive) | |
u /= boost::numeric::ublas::norm_2(u); | |
//cout << "U: " << u << endl; | |
boost::numeric::ublas::vector<double> v = cross((s.getEye().lookat - s.getEye().eyepoint), u) ; //horizontal unit (right positive) | |
v /= boost::numeric::ublas::norm_2(v); | |
//cout << "V: " << v << endl; | |
for(int i = 0;i<h; ++i){ | |
cout << "pix " << i << endl; | |
for(int j = 0;j<w;++j){ | |
//for each pixel | |
//cout << -s_width << " and " << j*p_width << " * " << v << endl; | |
boost::numeric::ublas::vector<double> aim = ((-s_width + (j*p_width)) * v) + ((s_height - (i*p_width)) * u); | |
boost::numeric::ublas::vector<double> direction = aim - s.getEye().eyepoint; | |
direction /= boost::numeric::ublas::norm_2(direction); | |
//cout << "Direction: " << direction << endl; | |
//exit(1); | |
Ray r(s.getEye().eyepoint, direction); | |
//cout << s.getColour(r).r; | |
screen.setPixel(i,j,s.getColour(r)); | |
} | |
//cout << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment