Skip to content

Instantly share code, notes, and snippets.

Created October 13, 2012 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3884855 to your computer and use it in GitHub Desktop.
Save anonymous/3884855 to your computer and use it in GitHub Desktop.
ray cast function
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