This commit is contained in:
K Tyl 2020-06-03 01:56:59 +01:00
parent c330c8b760
commit 0a30234b26
2 changed files with 51 additions and 6 deletions

View File

@ -1,23 +1,43 @@
#include "colour.h"
#include "vec3.h"
#include "ray.h"
#include <iostream>
const int WIDTH = 256;
const int HEIGHT = 256;
const double ASPECT_RATIO = 16.0 / 9.0;
const int WIDTH = 384;
const int HEIGHT = static_cast<int>(WIDTH / ASPECT_RATIO);
colour rayColour(const ray& r)
{
vec3 unitDirection = unitVector(r.direction());
double t = 0.5 * (unitDirection.y() + 1.0);
return (1.0-t) * colour(1.0,1.0,1.0) + t*colour(0.5, 0.7, 1.0);
}
int main()
{
std::cout << "P3" << std::endl;
std::cout << WIDTH << ' ' << HEIGHT << "\n255\n";
auto viewportHeight = 2.0;
auto viewportWidth = ASPECT_RATIO * viewportHeight;
auto focalLength = 1.0;
auto origin = point3(0, 0, 0);
auto horizontal = vec3(viewportWidth, 0, 0);
auto vertical = vec3(0, viewportHeight, 0);
auto lowerLeftCorner = origin - horizontal/2 - vertical/2 - vec3(0,0,focalLength);
std::cout << "P3\n" << WIDTH << ' ' << HEIGHT << "\n255\n";
for (int y = HEIGHT - 1; y >= 0; --y)
{
std::cerr << "\rScanlines remaining: " << y << ' ' << std::flush;
for (int x = 0; x < WIDTH; ++x)
{
colour pixelColour(double(x)/(WIDTH-1), double(y)/(HEIGHT-1), 0.25);
auto u = double(x) / (WIDTH-1);
auto v = double(y) / (HEIGHT-1);
ray r(origin, lowerLeftCorner + u*horizontal + v*vertical - origin);
colour pixelColour = rayColour(r);
writeColour(std::cout, pixelColour);
}
}

25
src/ray.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include "vec3.h"
class ray
{
public:
ray() {}
ray(const point3& origin, const vec3& direction) :
origin_(origin),
direction_(direction)
{}
point3 origin() const { return origin_; }
vec3 direction() const { return direction_; }
point3 at(double t) const
{
return origin_ + t * direction_;
}
private:
point3 origin_;
vec3 direction_;
};