ImageRef Scene::rasterize(CameraRef camera) { ImageRef image = new Image(camera->width(), camera->height()); for (unsigned int y = 0; y < camera->height(); y++) { for (unsigned int x = 0; x < camera->width(); x++) { float xPos = (float)x / (float) camera->width() - 0.5f; float yPos = (float)y / (float) camera->height() - 0.5f; Ray viewRay = camera->viewRay(xPos, yPos); HitInfo hitInfo; if (m_rootGroup->hit(viewRay, &hitInfo)) { image->setPixelColor(x, y, hitInfo.material->color()); } } } return image; }
void Scene::raytrace(CameraRef camera, Rect rect, RenderOption option, ImageRef image) { ASSERT(image->width() == rect.size.width && image->height() == rect.size.height); for (unsigned int y = 0; y < rect.size.height; y++) { for (unsigned int x = 0; x < rect.size.width; x++) { float xPos = (float)(x + rect.origin.x) / (float) camera->width() - 0.5f; float yPos = (float)(y + rect.origin.y) / (float) camera->height() - 0.5f; Ray viewRay = camera->viewRay(xPos, yPos); HitInfo hitInfo; Color color; if (traceRay(viewRay, option, 1.0f, 0, &color, &hitInfo)) { image->setPixelColor(x, y, color); } else { // set Background ccolor //image->setPixelColor(x, y, hitInfo.material->color()); } } } }