ubuntu20
4 years ago
6 changed files with 138 additions and 0 deletions
-
19Ray.cpp
-
12Ray.h
-
61Vector3.cpp
-
38Vector3.h
-
2raytracing.vcxproj
-
6raytracing.vcxproj.filters
@ -0,0 +1,19 @@ |
|||
#include "Ray.h"
|
|||
|
|||
Ray::Ray(const Vector3 & origin, const Vector3 & direction, Vector3 & attenuation) |
|||
{ |
|||
Set(origin, direction, attenuation); |
|||
} |
|||
|
|||
void Ray::Set(const Vector3 & origin, const Vector3 & direction, Vector3 & attenuation) |
|||
{ |
|||
mOrigin = origin; |
|||
mDirection = direction; |
|||
mDirection.Normalize(); |
|||
mLightAttenuation = attenuation; |
|||
} |
|||
|
|||
Vector3 Ray::PointAt(float k) |
|||
{ |
|||
return mOrigin + k * mDirection; |
|||
} |
@ -0,0 +1,12 @@ |
|||
#pragma once |
|||
#include "Vector3.h" |
|||
|
|||
class Ray { |
|||
public: |
|||
Vector3 mOrigin; |
|||
Vector3 mDirection; |
|||
Vector3 mLightAttenuation; |
|||
Ray(const Vector3& origin, const Vector3& direction, Vector3& attenuation); |
|||
void Set(const Vector3& origin, const Vector3& direction, Vector3& attenuation); |
|||
Vector3 PointAt(float k); |
|||
}; |
@ -0,0 +1,61 @@ |
|||
#include "Vector3.h"
|
|||
#include <math.h>
|
|||
|
|||
Vector3 Vector3::operator+(const Vector3 & v) const |
|||
{ |
|||
return Vector3(x+v.x, y+v.y, z+v.z); |
|||
} |
|||
|
|||
Vector3 Vector3::operator*(const Vector3 & v) const |
|||
{ |
|||
return Vector3(x*v.x, y*v.y, z*v.z); |
|||
} |
|||
|
|||
void Vector3::operator*=(float scale) |
|||
{ |
|||
x *= scale; |
|||
y *= scale; |
|||
z *= scale; |
|||
} |
|||
|
|||
Vector3 Vector3::operator/(float scale) const |
|||
{ |
|||
return Vector3(x/scale, y/scale, z/scale); |
|||
} |
|||
|
|||
void Vector3::operator/=(float scale) |
|||
{ |
|||
x /= scale; |
|||
y /= scale; |
|||
z /= scale; |
|||
} |
|||
|
|||
void Vector3::operator=(const Vector3 & v) |
|||
{ |
|||
x = v.x; |
|||
y = v.y; |
|||
z = v.z; |
|||
} |
|||
|
|||
void Vector3::Normalize() |
|||
{ |
|||
float magnitude = Magnitude(); |
|||
x /= magnitude; |
|||
y /= magnitude; |
|||
z /= magnitude; |
|||
} |
|||
|
|||
float Vector3::Magnitude() |
|||
{ |
|||
return sqrtf(x*x + y*y + z*z); |
|||
} |
|||
|
|||
Vector3 operator*(float f, const Vector3 & v) |
|||
{ |
|||
return Vector3(f*v.x, f*v.y, f*v.z); |
|||
} |
|||
|
|||
float Dot(const Vector3 & l, const Vector3 & r) |
|||
{ |
|||
return l.x * r.x + l.y * r.y + l.z * r.z; |
|||
} |
@ -0,0 +1,38 @@ |
|||
#pragma |
|||
class Vector3 { |
|||
public: |
|||
union { |
|||
float v[3]; |
|||
struct { |
|||
float x, y, z; |
|||
}; |
|||
}; |
|||
Vector3() { |
|||
x = 0.0f; |
|||
y = 0.0f; |
|||
z = 0.0f; |
|||
}; |
|||
Vector3(float x, float y, float z) { |
|||
this->x = x; |
|||
this->y = y; |
|||
this->z = z; |
|||
} |
|||
Vector3(const Vector3& v) { |
|||
x = v.x; |
|||
y = v.y; |
|||
z = v.z; |
|||
} |
|||
Vector3 operator+(const Vector3& v) const; |
|||
Vector3 operator*(const Vector3& v) const; |
|||
void operator*=(float scale); |
|||
Vector3 operator/(float scale) const; |
|||
void operator/=(float scale); |
|||
void operator=(const Vector3& v); |
|||
float operator[](int index) { |
|||
return v[index]; |
|||
} |
|||
void Normalize(); |
|||
float Magnitude(); |
|||
}; |
|||
Vector3 operator*(float f, const Vector3& v); |
|||
float Dot(const Vector3& l, const Vector3& r); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue