光线追踪2- Vec3 类

 Vec3.h 

#ifndef VEC3_H
#define VEC3_H
#include <cmath>
#include <iostream>
using std::sqrt;

class vec3 { 
	 public:    double e[3];    
	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*=(double t) {        
		e[0] *= t;        e[1] *= t;        e[2] *= t;        
		return *this;    
	}    
	vec3& operator/=(double t) {        
		return *this *= 1/t;    
	}    
	double length() const {        
		return sqrt(length_squared());    
	}    
	double length_squared() const {        
		return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];    
	}
};


// point3 is just an alias for vec3, but useful for geometric clarity in the code.
using point3 = vec3;

// Vector 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 unit_vector(vec3 v) {    
	return v / v.length();
}

#endif



3.1 Color Utility Functions

新建一个文件: color.h

#ifndef COLOR_H
#define COLOR_H

#include "vec3.h"
#include <iostream>

using color = vec3;
void write_color(std::ostream &out, color pixel_color) {
	// Write the translated [0,255] value of each color component.    
	out << static_cast<int>(255.999 * pixel_color.x()) << ' '
	<<static_cast<int>(255.999 * pixel_color.y()) << ' '
	<<static_cast<int>(255.999 * pixel_color.z()) << '\n';
}

#endif

修改主程序,以同时使用这两文件功能。
 

#include "color.h"
#include "vec3.h"

#include <iostream>

int main() {    
// Image    
int image_width = 256;    
int image_height = 256;

// Render    
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";   
 for (int j = 0; j < image_height; ++j) {        
	std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
	for (int i = 0; i < image_width; ++i) {            
		auto pixel_color = color(double(i)/(image_width-1),     		double(j)/(image_height-1), 0); 
		write_color(std::cout, pixel_color);
	}
    }
	std::clog << "\rDone.                 \n";
}






 

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值