【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
- 【Ray Tracing in One Weekend】(ch2)世界的基石?向量
- 【Ray Tracing in One Weekend】(ch5)法向量的可视化与多个球的出现
- 【Ray Tracing in One Weekend】(ch6)Ray Tracer 里的反走样
- 【Ray Tracing in One Weekend】(ch3)射线类
- 【Ray Tracing in One Weekend】(ch7)漫反射材质
- 【Ray Tracing in One Weekend】(ch8)Metal&Lambertian
- 【Ray Tracing in One Weekend】(ch9)Dielectrics
- 【Ray Tracing in One Weekend】(ch10)Positionable camera
- 总结《Ray Tracing in One Weekend》
- 【Ray Tracing in One Weekend】(ch0~1)c++生成的第一张图片
- 问题三十:《Ray Tracing In One Weekend》封面图形生成
- 《Ray Tracing in One Weekend》——Chapter 0: Overview
- 《Ray Tracing in One Weekend》——Chapter 6: Antialiasing
- 《Ray Tracing in One Weekend》——Chapter 8: Metal
- 《Ray Tracing in One Weekend》——Chapter 9: Dielectrics
- 【Ray Tracing in One Weekend】(ch4)场景中第一个物体,球体
- 《Ray Tracing in One Weekend》——Chapter 1: Output an image
- 《Ray Tracing in One Weekend》——Chapter 2: The vec3 class
- nginx的安装环境
- Javase—其他常用类
- 工业4.0时代,最可怕的是什么?
- CCF 1024 因子个数
- TensorFlow中.eval()函数理解
- 【Ray Tracing in One Weekend】(ch2)世界的基石?向量
- stm32芯片启动文件类型的选择
- Kubernetes网络隔离NetworkPolicy实验
- 关于python2与python3共存问题
- linux下使用 du查看某个文件或目录占用磁盘空间的大小
- 旋转数组
- 读后感---5w
- HTML中 用CSS样式实现 文字两边添加水平线
- ExtentReports实现