几乎所有的图形程序都有一些用于存储几何向量的类颜色。 在许多系统中,这些向量是4D(3D加上一个齐次坐标)几何图形和RGB加上颜色的Alpha透明度通道)。 为了我们的目的,三个坐标就足够了。 我们将使用相同的类vec3颜色,位置,方向,偏移量等等。 类vec3.h:

#ifndef VEC3H
#define VEC3H

#include <math.h>
#include <stdlib.h>
#include

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]);
}

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]);
}

//标量,向量之间的相互乘,除
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]);
}

//两向量点乘
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];
}

//两向量叉乘
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();
}

#endif

wjg.cpp中的main方法改为这样写:

#include
#include “vec3.h”
#include
using namespace std;

int main()
{
ofstream outfile;
outfile.open(“IMG.txt”);

int nx = 800, ny = 400;
outfile << "P3\\n" << nx << " " << ny << "\\n255\\n";
for (int j = ny - 1; j >= 0; j--)
{
    for (int i = 0; i < nx; i++)
    {
        vec3 vecTemp(float(i) / float(nx), float(j) / float(ny), 0.2);
        int iRed = int(255.99 \* vecTemp\[0\]);
        int iGreen = int(255.99 \* vecTemp\[1\]);
        int iBlue = int(255.99 \* vecTemp\[2\]);

outfile << iRed << " " << iGreen << " " << iBlue << "\\n";

}
}
outfile.close();
return 0;
}

修改txt文件为ppm,结果依然是: 参考书籍:《Ray Tracing in One Weekend》 RTIOW系列项目地址:GitHub RTIOW系列笔记: RTIOW-ch1:Output an image RTIOW-ch2:The vec3 class RTIOW-ch3:Rays, a simple camera, and background RTIOW-ch4:Adding a sphere RTIOW-ch5:Surface normals and multiple objects RTIOW-ch6:Antialiasing RTIOW-ch7:Diffuse Materials RTIOW-ch8:Metal RTIOW-ch9:Dielectrics RTIOW-ch10:Positionable camera RTIOW-ch11:Defocus Blur RTIOW-ch12:Where next