-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
84 lines (71 loc) · 3.22 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// -----------------------------------------------------------
// common.h
// 2004 - Jacco Bikker - [email protected] - www.bik5.com - <><
// -----------------------------------------------------------
#ifndef I_COMMON_H
#define I_COMMON_H
#include "math.h"
#include "stdlib.h"
typedef unsigned int Pixel;
inline float Rand( float a_Range ) { return ((float)rand() / RAND_MAX) * a_Range; }
namespace Raytracer {
#define DOT(A,B) (A.x*B.x+A.y*B.y+A.z*B.z)
#define NORMALIZE(A) {float l=1/sqrtf(A.x*A.x+A.y*A.y+A.z*A.z);A.x*=l;A.y*=l;A.z*=l;}
#define LENGTH(A) (sqrtf(A.x*A.x+A.y*A.y+A.z*A.z))
#define SQRLENGTH(A) (A.x*A.x+A.y*A.y+A.z*A.z)
#define SQRDISTANCE(A,B) ((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z))
#define EPSILON 0.001f
#define TRACEDEPTH 6
#define PI 3.141592653589793238462f
class vector3
{
public:
vector3() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {};
vector3( float a_X, float a_Y, float a_Z ) : x( a_X ), y( a_Y ), z( a_Z ) {};
void Set( float a_X, float a_Y, float a_Z ) { x = a_X; y = a_Y; z = a_Z; }
void Normalize() { float l = 1.0f / Length(); x *= l; y *= l; z *= l; }
float Length() { return (float)sqrt( x * x + y * y + z * z ); }
float SqrLength() { return x * x + y * y + z * z; }
float Dot( vector3 a_V ) { return x * a_V.x + y * a_V.y + z * a_V.z; }
vector3 Cross( vector3 b ) { return vector3( y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x ); }
void operator += ( vector3& a_V ) { x += a_V.x; y += a_V.y; z += a_V.z; }
void operator += ( vector3* a_V ) { x += a_V->x; y += a_V->y; z += a_V->z; }
void operator -= ( vector3& a_V ) { x -= a_V.x; y -= a_V.y; z -= a_V.z; }
void operator -= ( vector3* a_V ) { x -= a_V->x; y -= a_V->y; z -= a_V->z; }
void operator *= ( float f ) { x *= f; y *= f; z *= f; }
void operator *= ( vector3& a_V ) { x *= a_V.x; y *= a_V.y; z *= a_V.z; }
void operator *= ( vector3* a_V ) { x *= a_V->x; y *= a_V->y; z *= a_V->z; }
vector3 operator- () const { return vector3( -x, -y, -z ); }
friend vector3 operator + ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); }
friend vector3 operator - ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); }
friend vector3 operator + ( const vector3& v1, vector3* v2 ) { return vector3( v1.x + v2->x, v1.y + v2->y, v1.z + v2->z ); }
friend vector3 operator - ( const vector3& v1, vector3* v2 ) { return vector3( v1.x - v2->x, v1.y - v2->y, v1.z - v2->z ); }
friend vector3 operator * ( const vector3& v, float f ) { return vector3( v.x * f, v.y * f, v.z * f ); }
friend vector3 operator * ( const vector3& v1, vector3& v2 ) { return vector3( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); }
friend vector3 operator * ( float f, const vector3& v ) { return vector3( v.x * f, v.y * f, v.z * f ); }
friend bool operator > ( const vector3& v1, int f ) { return v1.x>f||v1.y>f||v1.z>f; }
union
{
struct { float x, y, z; };
struct { float r, g, b; };
struct { float cell[3]; };
};
};
class plane
{
public:
plane() : N( 0, 0, 0 ), D( 0 ) {};
plane( vector3 a_Normal, float a_D ) : N( a_Normal ), D( a_D ) {};
union
{
struct
{
vector3 N;
float D;
};
float cell[4];
};
};
typedef vector3 Color;
}; // namespace Raytracer
#endif