moved render function into camera

This commit is contained in:
Moritz Gmeiner 2023-12-27 17:54:48 +01:00
commit 30f24adf2a
3 changed files with 43 additions and 37 deletions

View file

@ -1,7 +1,11 @@
#pragma once
#include <iostream>
#include "image.h"
#include "ray.h"
#include "raytracer.h"
#include "renderobject.h"
#include "vec3.h"
class Camera {
@ -27,6 +31,19 @@ class Camera {
constexpr Vec3 centre() const { return centre_; }
constexpr void Render(Image& img, const RenderObject& world) const {
for (u32 j = 0; j < img.height(); j++) {
std::clog << "\rWriting line " << j << " of " << img.height() << std::flush;
for (u32 i = 0; i < img.width(); i++) {
auto ray = CastRay(i, j);
img[i, j] = Cast(ray, world);
}
}
}
private:
constexpr Vec3 Image2World(u32 i, u32 j) const {
return pixel_00_ + i * d_u_pixel_ + j * d_v_pixel_;
}
@ -39,7 +56,6 @@ class Camera {
return Ray{centre(), ray_direction};
}
private:
Vec3 centre_;
// position of pixel 0, 0

View file

@ -2,40 +2,13 @@
#include <memory>
#include "camera.h"
#include "colour.h"
#include "image.h"
#include "interval.h"
#include "ray.h"
#include "raytracer.h"
#include "renderobject.h"
#include "renderobjectlist.h"
#include "sphere.h"
#include "vec3.h"
Colour RayColour(const Ray& ray, const RenderObject& world) {
auto hit_record = world.hit(ray, Interval::kPositive);
// auto hit_record = world.hit(ray, Interval{0.505, kInf});
if (hit_record.has_value()) {
// assert(hit_record.front_face);
Vec3 n = hit_record->normal;
if (!hit_record->front_face) {
// return Colour{0.0, 0.0, 0.0};
return Colour::kBlack;
}
return Colour{0.5 * (n + Vec3{1, 1, 1})};
}
auto unit_dir = ray.direction().normed();
double a = 0.5 * (unit_dir.y() + 1.0);
return (1.0 - a) * Colour{1.0, 1.0, 1.0} + a * Colour{0.5, 0.7, 1.0};
}
int main(int /* argc */, char* /* argv */[]) {
// image
@ -66,15 +39,7 @@ int main(int /* argc */, char* /* argv */[]) {
// render
for (u32 j = 0; j < img.height(); j++) {
std::clog << "\rWriting line " << j << " of " << img.height() << std::flush;
for (u32 i = 0; i < img.width(); i++) {
auto ray = camera.CastRay(i, j);
img[i, j] = RayColour(ray, world);
}
}
camera.Render(img, world);
std::cout << img;

View file

@ -3,6 +3,7 @@
#include <memory>
#include <optional>
#include "colour.h"
#include "interval.h"
#include "ray.h"
#include "vec3.h"
@ -30,3 +31,27 @@ class RenderObject {
};
using SharedRenderObject = std::shared_ptr<RenderObject>;
constexpr Colour Cast(const Ray& ray, const RenderObject& world) {
auto hit_record = world.hit(ray, Interval::kPositive);
// auto hit_record = world.hit(ray, Interval{0.505, kInf});
if (hit_record.has_value()) {
// assert(hit_record.front_face);
Vec3 n = hit_record->normal;
if (!hit_record->front_face) {
// return Colour{0.0, 0.0, 0.0};
return Colour::kBlack;
}
return Colour{0.5 * (n + Vec3{1, 1, 1})};
}
auto unit_dir = ray.direction().normed();
double a = 0.5 * (unit_dir.y() + 1.0);
return (1.0 - a) * Colour{1.0, 1.0, 1.0} + a * Colour{0.5, 0.7, 1.0};
}