9 changed files with 92 additions and 21 deletions
-
37Camera.cpp
-
11Camera.h
-
16Ray.cpp
-
11Ray.h
-
6Scene.cpp
-
10Vector3.cpp
-
6Vector3.h
-
4raytracing.vcxproj
-
12raytracing.vcxproj.filters
@ -0,0 +1,37 @@ |
|||
#include"Camera.h"
|
|||
#include"Ray.h"
|
|||
#include<math.h>
|
|||
Camera::Camera(float fov, float aspect) { |
|||
mFOV = fov; |
|||
mAspect = aspect; |
|||
mNearPlaneWidth = 0.0f; |
|||
mNearPlaneHeight = 0.0f; |
|||
} |
|||
void Camera::LookAt(const Vector3& position, const Vector3& center, const Vector3& up) { |
|||
Vector3 camera_to_view_point = center - position; |
|||
float distance = camera_to_view_point.Magnitude(); |
|||
float half_fov = mFOV / 2.0f; |
|||
float radian_half_fov = half_fov * 3.14159f / 180.0f; |
|||
float half_height = atanf(radian_half_fov)*distance; |
|||
float half_width = half_height * mAspect; |
|||
mNearPlaneWidth = half_width * 2.0f; |
|||
mNearPlaneHeight = half_height * 2.0f; |
|||
camera_to_view_point.Normalize(); |
|||
Vector3 right_direction = Cross(camera_to_view_point, up); |
|||
right_direction.Normalize(); |
|||
Vector3 up_direction = Cross(right_direction, camera_to_view_point); |
|||
up_direction.Normalize(); |
|||
mUVector = right_direction; |
|||
mVVector = up_direction; |
|||
mUSpanVector = mNearPlaneWidth * mUVector; |
|||
mVSpanVector = mNearPlaneHeight * mUVector; |
|||
mCenter = center; |
|||
mPosition = position; |
|||
mUp = up; |
|||
} |
|||
Ray Camera::GetRay(float u, float v) { |
|||
u = u * 2.0f - 1.0f; |
|||
v = v * 2.0f - 1.0f; |
|||
Vector3 point_on_near_plane = mCenter + 0.5f*u*mUSpanVector + 0.5f*v*mVSpanVector; |
|||
return Ray(mPosition, point_on_near_plane - mPosition, Vector3(1.0f, 1.0f, 1.0f)); |
|||
} |
@ -0,0 +1,11 @@ |
|||
#pragma once |
|||
#include"Vector3.h" |
|||
class Ray; |
|||
class Camera { |
|||
public: |
|||
Vector3 mPosition, mCenter, mUp, mUVector, mVVector, mUSpanVector, mVSpanVector; |
|||
float mFOV, mAspect, mNearPlaneWidth, mNearPlaneHeight; |
|||
Camera(float fov, float aspect); |
|||
void LookAt(const Vector3& position, const Vector3& center, const Vector3& up); |
|||
Ray GetRay(float u, float v); |
|||
}; |
@ -1,19 +1,13 @@ |
|||
#include "Ray.h"
|
|||
|
|||
Ray::Ray(const Vector3 & origin, const Vector3 & direction, Vector3 & attenuation) |
|||
{ |
|||
#include"Ray.h"
|
|||
Ray::Ray(const Vector3& origin, const Vector3& direction, const Vector3& attenuation) { |
|||
Set(origin, direction, attenuation); |
|||
} |
|||
|
|||
void Ray::Set(const Vector3 & origin, const Vector3 & direction, Vector3 & attenuation) |
|||
{ |
|||
void Ray::Set(const Vector3& origin, const Vector3& direction, const Vector3& attenuation) { |
|||
mOrigin = origin; |
|||
mDirection = direction; |
|||
mDirection.Normalize(); |
|||
mLightAttenuation = attenuation; |
|||
} |
|||
|
|||
Vector3 Ray::PointAt(float k) |
|||
{ |
|||
Vector3 Ray::PointAt(float k) { |
|||
return mOrigin + k * mDirection; |
|||
} |
|||
} |
@ -1,12 +1,9 @@ |
|||
#pragma once |
|||
#include "Vector3.h" |
|||
|
|||
#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 mOrigin, mDirection, mLightAttenuation; |
|||
Ray(const Vector3& origin, const Vector3& direction, const Vector3& attenuation); |
|||
void Set(const Vector3& origin, const Vector3& direction, const Vector3& attenuation); |
|||
Vector3 PointAt(float k); |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue