【Ray Tracing in One Weekend】(ch2)世界的基石?向量

来源:互联网 发布:javaweb高级编程 编辑:程序博客网 时间:2024/06/05 07:54

Chapter 2: The vec3 class

老话说得好:要想星际打得好,农民伯伯不能少。图形学也应如是。

大部分图形程序中都有着自己的向量类,用来存储几何向量或颜色向量。其中大部分是四维的。

几何向量( geometric vectors):x、y、z,加上一个其次坐标(homogeneous coordinate)。

颜色向量(colors):r、g、b,加上一个alpha值,表示透明度。

在本书中三维足以使用,来表示位置、颜色、偏移、方向等。

向量类(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();}

现在,把 Main.cpp 中的 main 方法修改如下,亦可生成与第一节中相同的图片:

#include <iostream>#include <fstream>#include "Vec3.h"using namespace std;int main(){    ofstream outfile;    outfile.open("firstImage.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;}
阅读全文
0 0
原创粉丝点击