Ejemplo n.º 1
0
void Camera::RenderPath(Scene &scn, int n) {
    RayTrace rayTrace(scn);
    
    for (int y = 0; y < _img.YRes; y++) {
        for (int x = 0; x < _img.XRes; x++) {
            
            // compute the primary ray
            Vector3 cy;
            cy.Cross(_worldMatrix.c, _worldMatrix.b);
            Vector3 cx = cy / pow(cy.Magnitude(), 2);
            cy.Cross(cx, _worldMatrix.c);
            
            float hfov = 2.f * atanf(_aspect * tanf(_verticalFOV / 2.f));
            float cw = 2.f * tanf(hfov/2.f);
            float ch = cw / _aspect;
            
            Ray ray;
            ray.Origin = _worldMatrix.d;
            
            ray.Direction =
            _worldMatrix.c + ((float)(x + 0.5f) / (float)_img.XRes - 0.5f) * cw * cx +
            ((float)(y + 0.5f)/(float)_img.YRes - 0.5f) * ch * cy;
            
            ray.type = Ray::PRIMARY;
            
            // shoot the primary ray
            Intersection hit;
            if (x > 124 && x <= 140 && y == _img.YRes - 495 ) {
//                Color white = Color::WHITE;
//                std::cout << "debug pixel" << std::endl;
//                _img.SetPixel(x, y, white.ToInt());
//                continue;
            }
            
            rayTrace.TraceRay(ray, hit);
            
            if (n != -1) {
                Color c;
                c.FromInt(_img.GetPixel(x, y));
                
                Color avg = Color::BLACK;
                avg.AddScaled(c, n - 1);
                avg.Add(hit.Shade);
                avg.Scale(1.0f / n);
                _img.SetPixel(x, y, avg.ToInt());
            }
            else _img.SetPixel(x, y, hit.Shade.ToInt());
        }
    }
    
}