Browse Source

ray射线类

master
ubuntu20 4 years ago
parent
commit
303465315d
  1. 19
      Ray.cpp
  2. 12
      Ray.h
  3. 61
      Vector3.cpp
  4. 38
      Vector3.h
  5. 2
      raytracing.vcxproj
  6. 6
      raytracing.vcxproj.filters

19
Ray.cpp

@ -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;
}

12
Ray.h

@ -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);
};

61
Vector3.cpp

@ -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;
}

38
Vector3.h

@ -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);

2
raytracing.vcxproj

@ -129,10 +129,12 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="Scene.cpp" />
<ClCompile Include="util.cpp" />
<ClCompile Include="Vector3.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Scene.h" />
<ClInclude Include="util.h" />
<ClInclude Include="Vector3.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

6
raytracing.vcxproj.filters

@ -24,6 +24,9 @@
<ClCompile Include="util.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Vector3.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Scene.h">
@ -32,5 +35,8 @@
<ClInclude Include="util.h">
<Filter>源文件</Filter>
</ClInclude>
<ClInclude Include="Vector3.h">
<Filter>源文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading…
Cancel
Save