第二章 写Vec3类
作者在第二章让我们设计一个vec3类,用来存储颜色、位置、方向等等。以下是我们设计的vec3类。
vec3类的这些代码就全部放在Vec3.h中
Vec3.h的代码如下:
//#ifdef VEC3H
//#define VEC3H
//#endif
#include <math.h>
#include <stdlib.h>
#include <iostream>
class Vec3 {
public:
Vec3(){}
Vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }
inline float x() const { return e[0]; }
inline float y() const { return e[1]; }
inline float z() const { return e[2]; }
inline float r() const { return e[0]; }
inline float g() const { return e[1]; }
inline float b() const { return e[2]; }
inline const Vec3& operator+() const { return *this; }
inline Vec3 operator-() const { return Vec3(-e[0], -e[1], -e[2]); }
inline float operator[](int i) const { return e[i]; }
inline float& operator[](int i) { return e[i]; };
inline Vec3& operator+=(const Vec3 &v2);
inline Vec3& operator-=(const Vec3 &v2);
inline Vec3& operator*=(const Vec3 &v2);
inline Vec3& operator/=(const Vec3 &v2);
inline Vec3& operator*=(const float t);
inline Vec3& operator/=(const float t);
inline float length() const { return sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]); }
inline float squared_length() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; }
inline void make_unit_vector();
float e[3];
};
//输入流和格式
inline std::istream& operator>>(std::istream &is, Vec3 &t) {
is >> t.e[0] >> t.e[1] >> t.e[2];
return is;
}
//输出流和格式
inline std::ostream& operator<<(std::ostream &os, const Vec3 &t) {
os << t.e[0] << " " << t.e[1] << " " << t.e[2];
return os;
}
//生成单位向量 向量/向量的模
inline void Vec3::make_unit_vector() {
float k = 1.0 / sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
e[0] *= k; e[1] *= k; e[2] *= k;
}
//两向量相加
inline Vec3 operator+(const Vec3 &v1, const Vec3 &v2) {
return Vec3(v1.e[0] + v2.e[0], v1.e[1] + v2.e[1], v1.e[2] + v2.e[2]);
}
//两向量相减
inline Vec3 operator-(const Vec3 &v1, const Vec3 &v2) {
return Vec3(v1.e[0] - v2.e[0], v1.e[1] - v2.e[1], v1.e[2] - v2.e[2]);
}
//两向量相乘(for colors)
inline Vec3 operator*(const Vec3 &v1, const Vec3 &v2) {
return Vec3(v1.e[0] * v2.e[0], v1.e[1] * v2.e[1], v1.e[2] * v2.e[2]);
}
//两向量相除(for colors)
inline Vec3 operator/(const Vec3 &v1, const Vec3 &v2) {
return Vec3(v1.e[0] / v2.e[0], v1.e[1] / v2.e[1], v1.e[2] / v2.e[2]);
}
//一个标量乘一个向量
inline Vec3 operator*(float t, const Vec3 &v) {
return Vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
}
//一个向量除以一个标量
inline Vec3 operator/(Vec3 v, float t) {
return Vec3(v.e[0] / t, v.e[1] / t, v.e[2] / t);
}
//一个向量乘一个标量
inline Vec3 operator*(const Vec3 &v, float t) {
return Vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
}
//两向量点乘(求内积,得一个常数)(for locations)
inline float dot(const Vec3 &v1, const Vec3 &v2) {
return v1.e[0] * v2.e[0] + v1.e[1] * v2.e[1] + v1.e[2] * v2.e[2];
}
//两向量叉乘(求外积,得一个向量)(for locations)
inline Vec3 cross(const Vec3 &v1, const Vec3 &v2) {
return Vec3((v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1]),
(-(v1.e[0] * v2.e[2] - v1.e[2] * v2.e[0])),
(v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]));
}
inline Vec3& Vec3::operator+=(const Vec3 &v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
inline Vec3& Vec3::operator*=(const Vec3 &v) {
e[0] *= v.e[0];
e[1] *= v.e[1];
e[2] *= v.e[2];
return *this;
}
inline Vec3& Vec3::operator/=(const Vec3 &v) {
e[0] /= v.e[0];
e[1] /= v.e[1];
e[2] /= v.e[2];
return *this;
}
inline Vec3& Vec3::operator-=(const Vec3& v) {
e[0] -= v.e[0];
e[1] -= v.e[1];
e[2] -= v.e[2];
return *this;
}
inline Vec3& Vec3::operator*=(const float t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}
inline Vec3& Vec3::operator/=(const float t) {
float k = 1.0 / t;
e[0] *= k;
e[1] *= k;
e[2] *= k;
return *this;
}
//归一化向量
inline Vec3 unit_vector(Vec3 v) {
return v / v.length();
}
然后再第一章中我们画的第一张图片就可以使用这个类了,输出结果一致
#include <iostream>
#include <fstream>
#include "Vec3.h"
using namespace std;
int main()
{
ofstream outfile;
outfile.open("firstpicture.txt");
int nx = 200;
int ny = 100;
outfile << "P3\n" << nx << " " << ny << "\n255\n";
for (int j = ny - 1; j >= 0; j--)
{
for (int i = 0; i < nx; i++)
{
Vec3 col(float(i) / float(nx), float(j) / float(ny), 0.2);
int ir = int(255.99 * col[0]);
int ig = int(255.99 * col[1]);
int ib = int(255.99 * col[2]);
outfile << ir << " " << ig << " " << ib << "\n";
}
}
outfile.close();
return 0;
}