add vec3 and colour

This commit is contained in:
K Tyl 2020-06-01 01:49:50 +01:00
parent 135d658dd3
commit 53326d6ff1
4 changed files with 132 additions and 10 deletions

View File

@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.10)
project(flark)
add_executable(flark main.cpp)
add_executable(flark main.cpp vec3.h colour.h)

13
colour.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include "vec3.h"
#include <iostream>
void writeColour(std::ostream &out, colour pixelColour)
{
// write the translated [0,255] value of each colour component.
out << static_cast<int>(255.999 * pixelColour.x()) << ' '
<< static_cast<int>(255.999 * pixelColour.y()) << ' '
<< static_cast<int>(255.999 * pixelColour.z()) << '\n';
}

View File

@ -1,3 +1,6 @@
#include "colour.h"
#include "vec3.h"
#include <iostream>
const int WIDTH = 256;
@ -14,15 +17,8 @@ int main()
for (int x = 0; x < WIDTH; ++x)
{
double r = double(x) / (WIDTH - 1);
double g = double(y) / (HEIGHT - 1);
double b = 0.25;
int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
std::cout << ir << ' ' << ig << ' ' << ib << std::endl;
colour pixelColour(double(x)/(WIDTH-1), double(y)/(HEIGHT-1), 0.25);
writeColour(std::cout, pixelColour);
}
}

113
vec3.h Normal file
View File

@ -0,0 +1,113 @@
#pragma once
#include <cmath>
#include <iostream>
class vec3
{
public:
vec3() : e{0,0,0} {}
vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
double x() const { return e[0]; }
double y() const { return e[1]; }
double z() const { return e[2]; }
vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
double operator[](int i) const { return e[i]; }
double& operator[](int i) { return e[i]; }
vec3& operator+=(const vec3 &v)
{
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
vec3& operator*=(const double t)
{
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}
vec3& operator/=(const double t)
{
return *this *= 1 / t;
}
double length() const
{
return std::sqrt(lengthSquared());
}
double lengthSquared() const
{
return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];
}
public:
double e[3];
};
// type aliases for vec3
using point3 = vec3; // 3D point
using colour = vec3; // RGB colour
// utility functions
inline std::ostream& operator<<(std::ostream &out, const vec3 &v)
{
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
}
inline vec3 operator+(const vec3 &u, const vec3 &v)
{
return vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
}
inline vec3 operator-(const vec3 &u, const vec3 &v)
{
return vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]);
}
inline vec3 operator*(const vec3 &u, const vec3 &v)
{
return vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
}
inline vec3 operator*(double t, const vec3 &v)
{
return vec3(t * v.e[0], t * v.e[1], t * v.e[2]);
}
inline vec3 operator*(const vec3 &v, double t)
{
return t * v;
}
inline vec3 operator/(vec3 v, double t)
{
return (1 / t) * v;
}
inline double dot(const vec3 &u, const vec3 &v)
{
return u.e[0] * v.e[0]
+ u.e[1] * v.e[1]
+ u.e[2] * v.e[2];
}
inline vec3 cross(const vec3 &u, const vec3 &v)
{
return vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
}
inline vec3 unitVector(vec3 v)
{
return v / v.length();
}