chapter 2:三维向量类
文章翻译
几乎所有的图形学程序都有一些类用来存储几何向量和颜色。在很多的系统中,这些向量是4维的,对于我们来说,3个坐标就足够了。我们将使用同一个class vec3来存储颜色、坐标、方向、偏移量以及其他的东西。一些人不喜欢这样,因为这无法避免你做出一些愚蠢的操作,如把颜色添加给坐标这样的操作。他们持有一个好的观点,但是在没有明显错误的情况下,我们将总是向着"更少的代码"方向进行。
这里是我的class vec3的一部分,我在这里面使用float,但是项目中的某些地方我使用double,哪一个都对,符合你自己的风格就行。很多东西都包含在头文件里了,在文件的后面是很多关于向量的操作。
vec3.h
#pragma once
using namespace std;
#include<iostream>
class vec3
{
public:
vec3();
vec3(float e0, float e1, float e2);
inline float x() const;
inline float y() const;
inline float z() const;
inline float r() const;
inline float g() const;
inline float b() const;
inline const vec3& operator+() const;
inline vec3 operator-() const;
inline float operator[](int i) const;
inline float& operator[](int 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;
inline float squared_length() const;
inline void make_unit_vector();
public:
float e[3];
};
inline vec3 operator*(const vec3 &v, float t);
inline vec3 operator*(float t, const vec3 &v);
inline vec3 operator+(const vec3 &v1, const vec3 &v2);
inline vec3 operator-(const vec3 &v1, const vec3 &v2);
inline vec3 operator*(const vec3 &v1, const vec3 &v2);
inline vec3 operator/(const vec3 &v1, const vec3 &v2);
inline vec3 unit_vector(vec3 v);
inline float dot(const vec3 &v1, const vec3 &v2);
inline vec3 cross(const vec3 &v1, const vec3 &v2);
inline vec3 operator/(const vec3 &v1, float t);
double drand48(void);
vec3.cpp
#include "vec3.h"
#pragma once
#include <math.h>
#define mm 0x100000000LL
#define cc 0xB16
#define aa 0x5DEECE66DLL
static unsigned long long seed = 1;
vec3::vec3()
{
}
vec3::vec3(float e0, float e1, float e2)
{
e[0] = e0;
e[1] = e1;
e[2] = e2;
}
inline float vec3::x() const
{
return e[0];
}
inline float vec3::y() const
{
return e[1];
}
inline float vec3::z() const
{
return e[2];
}
inline float vec3::r() const
{
return e[0];
}
inline float vec3::g() const
{
return e[1];
}
inline float vec3::b() const
{
return e[2];
}
inline const vec3& vec3::operator+() const
{
return *this;
}
inline vec3 vec3::operator-() const
{
return vec3(-e[0], -e[1], -e[2]);
}
inline float vec3::operator[](int i) const
{
return e[i];
}
inline float& vec3::operator[](int i)
{
return e[i];
}
inline vec3& vec3::operator+=(const vec3 &v2)
{
e[0] += v2.e[0];
e[1] += v2.e[1];
e[2] += v2.e[2];
return *this;
}
inline vec3& vec3::operator-=(const vec3 &v2)
{
e[0] -= v2.e[0];
e[1] -= v2.e[1];
e[2] -= v2.e[2];
return *this;
}
inline vec3& vec3::operator*=(const vec3 &v2)
{
e[0] *= v2.e[0];
e[1] *= v2.e[1];
e[2] *= v2.e[2];
return *this;
}
inline vec3& vec3::operator/=(const vec3 &v2)
{
e[0] /= v2.e[0];
e[1] /= v2.e[1];
e[2] /= v2.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)
{
e[0] /= t;
e[1] /= t;
e[2] /= t;
return *this;
}
inline float vec3::length() const
{
return sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]);
}
inline float vec3::squared_length() const
{
return e[0] * e[0] + e[1] * e[1] + e[2] * e[2];
}
inline void vec3::make_unit_vector()
{
float k = 1 / this->length();
e[0] *= k;
e[1] *= k;
e[2] *= k;
}
//非成员函数,操作符重载
inline vec3 operator*(const vec3 &v, float t)
{
return vec3(v.e[0] * t, v.e[1] * t, v.e[2] * t);
}
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(v.e[0] * t, v.e[1] * t, v.e[2] * t);
}
inline vec3 unit_vector(vec3 v)
{
float k = 1 / v.length();
return vec3(v.e[0] * k, v.e[1] * k, v.e[2] * k);
}
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 operator/(const vec3 &v1, float t)
{
return vec3(v1.e[0] / t, v1.e[1] / t, v1.e[2] / t);
}
// 随机值函数
double drand48(void)
{
seed = (aa * seed + cc) & 0xFFFFFFFFFFFFLL;
unsigned int x = seed >> 16;
return ((double)x / (double)mm);
}
main.cpp
#include <stdlib.h>
#include <glut.h>
#include "vec3.cpp"
#include "vec3.h"
#define Width 1200
#define Height 600
unsigned char *Pixels;
void Draw(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Pixels);
glFlush();
}
int main(int argc, char* argv[])
{
Pixels = (unsigned char *)malloc(Width * Height * sizeof(unsigned char) * 4);
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
int offset = (Width * j + i) * 4;
vec3 col(float(i) / float(Width), float(j) / float(Height), 0.2);
Pixels[offset + 0] = 255.99 * col[0];
Pixels[offset + 1] = 255.99 * col[1];
Pixels[offset + 2] = 255.99 * col[2];
Pixels[offset + 3] = 0;
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(Width, Height);
glutInitWindowPosition(600, 400);
glutCreateWindow("Ray Tracing");
glutDisplayFunc(Draw);
glutMainLoop();
}
显示结果
总结
前两章就是教我们如何设置像素点,如何显示出一张图片,引入了向量进行颜色存储,方便之后进行光线衰减等相关操作。