snoopy/src/main.cpp

52 lines
1.3 KiB
C++
Raw Normal View History

2020-06-01 02:49:50 +02:00
#include "colour.h"
#include "vec3.h"
2020-06-03 02:56:59 +02:00
#include "ray.h"
2020-06-01 02:49:50 +02:00
2020-06-01 00:52:34 +02:00
#include <iostream>
2020-06-03 02:56:59 +02:00
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);
2020-06-04 01:47:33 +02:00
auto a = colour(1.0, 1.0, 1.0);
auto b = colour(0.0, 0.0, 0.0);
return lerp(a, b, t);
2020-06-03 02:56:59 +02:00
}
2020-06-01 00:52:34 +02:00
int main()
{
2020-06-03 02:56:59 +02:00
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";
2020-06-01 00:52:34 +02:00
for (int y = HEIGHT - 1; y >= 0; --y)
{
std::cerr << "\rScanlines remaining: " << y << ' ' << std::flush;
for (int x = 0; x < WIDTH; ++x)
{
2020-06-03 02:56:59 +02:00
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);
2020-06-01 02:49:50 +02:00
writeColour(std::cout, pixelColour);
2020-06-01 00:52:34 +02:00
}
}
std::cerr << "\nDone." << std::endl;
}