add ray
This commit is contained in:
parent
c330c8b760
commit
0a30234b26
32
src/main.cpp
32
src/main.cpp
|
@ -1,23 +1,43 @@
|
||||||
#include "colour.h"
|
#include "colour.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
#include "ray.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
const int WIDTH = 256;
|
const double ASPECT_RATIO = 16.0 / 9.0;
|
||||||
const int HEIGHT = 256;
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "P3" << std::endl;
|
auto viewportHeight = 2.0;
|
||||||
std::cout << WIDTH << ' ' << HEIGHT << "\n255\n";
|
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)
|
for (int y = HEIGHT - 1; y >= 0; --y)
|
||||||
{
|
{
|
||||||
std::cerr << "\rScanlines remaining: " << y << ' ' << std::flush;
|
std::cerr << "\rScanlines remaining: " << y << ' ' << std::flush;
|
||||||
|
|
||||||
for (int x = 0; x < WIDTH; ++x)
|
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);
|
writeColour(std::cout, pixelColour);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_;
|
||||||
|
};
|
Loading…
Reference in New Issue