void RayTracer::renderScene(const SceneList& p_scene) { if (m_imageBuffer.empty()) { m_imageBuffer.resize(m_settings.resolutionWidth * m_settings.resolutionHeight); } int pixelIndex(0); Ray primaryRay(m_settings.eyePosition, Vector3(0, 0, 1)); const float halfWidth = m_settings.resolutionWidth / 2.0f; const float halfHeight = m_settings.resolutionHeight / 2.0f; for (int y = 0; y < m_settings.resolutionHeight; ++y) { for (int x = 0; x < m_settings.resolutionWidth; ++x) { // Compute direction of ray based on pixel position primaryRay.direction = Vector3(x - halfWidth, y - halfHeight, m_settings.depth); primaryRay.direction.normalize(); m_imageBuffer[pixelIndex] = trace(primaryRay, p_scene, 0); ++pixelIndex; } } }
Ray OrthographicCamera::computeRay(int x, int y, Screen* screen) { float nx = ((x+0.5) -((float)screen->getWidth()/2)) / ((float)screen->getWidth()/2); float ny = (((float)screen->getHeight()/2) - (y+0.5)) / ((float)screen->getHeight()/2); vec3 ray_direction = -m_w; vec3 origin = m_eye + ny*m_v + nx*m_u; Ray primaryRay(origin, glm::normalize(ray_direction)); return primaryRay; }
void render(const std::vector<Sphere> &spheres) { Colour *pixels = new Colour[CANVAS_WIDTH * CANVAS_HEIGHT], *pixel = pixels; for (int i = 0; i < CANVAS_WIDTH; ++i) { for (int j = 0; j < CANVAS_HEIGHT; ++j, ++pixel) { float x = i - (CANVAS_WIDTH / 2 - 1); float y = j - (CANVAS_HEIGHT / 2 - 1); Vector3D primaryRay(x, y, CANVAS_DEPTH); *pixel = trace(Vector3D(0, 0, 0), primaryRay.norm(), spheres, 0); } } std::ofstream ofs("test.ppm", std::ios::out | std::ios::binary); ofs << "P6\n" << CANVAS_WIDTH << " " << CANVAS_HEIGHT << "\n255\n"; for (unsigned i = 0; i < CANVAS_WIDTH * CANVAS_HEIGHT; ++i) { ofs << (unsigned char)(std::min(float(1), pixels[i].red) * 255) << (unsigned char)(std::min(float(1), pixels[i].green) * 255) << (unsigned char)(std::min(float(1), pixels[i].blue) * 255); } ofs.close(); delete[] pixels; }