Second week goals achieved

This commit is contained in:
2024-10-02 22:43:26 +02:00
parent 4f83bb738c
commit f1a8d62b39
5 changed files with 115 additions and 116 deletions

View File

@@ -39,13 +39,10 @@ namespace dae
Matrix CalculateCameraToWorld() Matrix CalculateCameraToWorld()
{ {
right = Vector3::Cross(Vector3::UnitY, Camera::forward);
up = Vector3::Cross(Camera::forward, right);
Vector4 Right{ Vector3{}.Cross(Vector3::UnitY,forward).Normalized() ,0 }; return Matrix(right, up, Camera::forward, Camera::origin);
Vector4 Up{ Vector3{}.Cross(forward,Right).Normalized(),0 };
Vector4 TVec{ origin ,1 };
Matrix Onb{ Right,Up,forward, TVec };
return Onb;
} }
void Update(Timer* pTimer) void Update(Timer* pTimer)
@@ -61,22 +58,6 @@ namespace dae
SDL_Event event; SDL_Event event;
//if (SDL_PollEvent(&event))
//{
// if (SDL_MOUSEMOTION == event.type)
// {
// int x, y;
// SDL_GetMouseState(&x, &y);
// forward = Matrix::CreateRotation(x * rotSpeed * pTimer->GetElapsed(), y * rotSpeed * pTimer->GetElapsed(), 1).TransformVector(forward);
// }
//}
//
const float deltaTime = pTimer->GetElapsed(); const float deltaTime = pTimer->GetElapsed();
//Keyboard Input //Keyboard Input
@@ -86,50 +67,23 @@ namespace dae
int mouseX{}, mouseY{}; int mouseX{}, mouseY{};
const uint32_t mouseState = SDL_GetRelativeMouseState(&mouseX, &mouseY); const uint32_t mouseState = SDL_GetRelativeMouseState(&mouseX, &mouseY);
// Move Forward
if (pKeyboardState[SDL_SCANCODE_W])
{
Matrix forwardMovement = Matrix::CreateTranslation(0, 0, movSpeed);
origin = forwardMovement.TransformPoint(origin);
}
// Move Backward
if (pKeyboardState[SDL_SCANCODE_S])
{
Matrix backwardMovement = Matrix::CreateTranslation(0, 0, -movSpeed);
origin = backwardMovement.TransformPoint(origin);
}
// Strafe Left
if (pKeyboardState[SDL_SCANCODE_A])
{
Matrix leftMovement = Matrix::CreateTranslation(-movSpeed, 0, 0);
origin = leftMovement.TransformPoint(origin);
}
// Strafe Right
if (pKeyboardState[SDL_SCANCODE_D])
{
Matrix rightMovement = Matrix::CreateTranslation(movSpeed, 0, 0);
origin = rightMovement.TransformPoint(origin);
}
// Process mouse movements (relative to the last frame) // Process mouse movements (relative to the last frame)
if (mouseState & SDL_BUTTON(SDL_BUTTON_RIGHT)) if (mouseState & SDL_BUTTON(SDL_BUTTON_RIGHT))
{ {
if (mouseX != 0 || mouseY != 0) if (mouseX != 0 || mouseY != 0)
{ {
totalPitch = mouseY / rotSpeed * deltaTime;
totalYaw = mouseX / rotSpeed * deltaTime;
if (forward.z < 0) if (forward.z < 0)
{ {
forward = Matrix::CreateRotation(-(mouseY / rotSpeed) * deltaTime, (mouseX / rotSpeed) * deltaTime, 0).TransformVector(forward); forward = Matrix::CreateRotation(-totalPitch, totalYaw, 0).TransformVector(forward).Normalized();
} }
else else
{ {
forward = Matrix::CreateRotation((mouseY / rotSpeed) * deltaTime, (mouseX / rotSpeed) * deltaTime, 0).TransformVector(forward); forward = Matrix::CreateRotation(totalPitch, totalYaw, 0).TransformVector(forward).Normalized();
} }
} }
@@ -137,30 +91,58 @@ namespace dae
/* Vector3 moveVector = forward.Normalized() * movSpeed * deltaTime;; Vector3 forwardVec = forward.Normalized() * movSpeed * deltaTime;
Vector3 rightVector = Vector3{}.Cross(up , forward).Normalized() * movSpeed * deltaTime; Vector3 rightVector = Vector3{}.Cross(up , forward).Normalized() * movSpeed * deltaTime;
if (pKeyboardState[SDL_SCANCODE_W]) Vector3 upVector = Vector3{}.Cross(forward,right).Normalized() * movSpeed * deltaTime;
{
origin += moveVector;
}
if (pKeyboardState[SDL_SCANCODE_S]) if (pKeyboardState[SDL_SCANCODE_W])
{ {
origin -= moveVector; origin += forwardVec;
} }
if (pKeyboardState[SDL_SCANCODE_D]) if (pKeyboardState[SDL_SCANCODE_S])
{ {
origin += rightVector; origin -= forwardVec;
} }
if (pKeyboardState[SDL_SCANCODE_A]) if (pKeyboardState[SDL_SCANCODE_D])
{ {
origin -= rightVector; origin += rightVector;
}*/ }
if (pKeyboardState[SDL_SCANCODE_A])
{
origin -= rightVector;
}
if (pKeyboardState[SDL_SCANCODE_SPACE])
{
if (forward.z < 0)
{
origin -= upVector;
}
else
{
origin += upVector;
}
}
if (pKeyboardState[SDL_SCANCODE_LSHIFT])
{
if (forward.z < 0)
{
origin += upVector;
}
else
{
origin -= upVector;
}
}
} }
}; };

View File

@@ -39,36 +39,38 @@ void Renderer::Render(Scene* pScene) const
{ {
for (int py{}; py < m_Height; ++py) for (int py{}; py < m_Height; ++py)
{ {
//float gradient = px / static_cast<float>(m_Width);
//gradient += py / static_cast<float>(m_Width);
//gradient /= 2.0f;
//Sets screen to black
ColorRGB finalColor{ };
//creates a vector that holds a coordinate in 3D space dependent on which pixel the loop is on //creates a vector that holds a coordinate in 3D space dependent on which pixel the loop is on
Vector3 rayDirection{ float((2 * ((px + 0.5) / m_Width) - 1) * m_AspectRatio * camera.FOV),float((1 - 2 * ((py + 0.5) / m_Height)) * camera.FOV),1}; Vector3 rayDirection{ float((2 * ((px + 0.5) / m_Width) - 1) * m_AspectRatio * camera.FOV),float((1 - 2 * ((py + 0.5) / m_Height)) * camera.FOV),1};
rayDirection.Normalize(); rayDirection.Normalize();
rayDirection = cameraToWorld.TransformVector(rayDirection); rayDirection = cameraToWorld.TransformVector(rayDirection);
//Creates a Ray from origin to the point where the current pixel in loop is //Creates a Ray from origin to the point where the current pixel in loop is
Ray viewRay{ camera.origin,rayDirection }; Ray viewRay{ camera.origin,rayDirection };
//Sets screen to black
ColorRGB finalColor{ };
HitRecord closestHit{}; HitRecord closestHit{};
pScene->GetClosestHit(viewRay, closestHit); pScene->GetClosestHit(viewRay, closestHit);
////Makes a tempSphere
//Sphere testSphere{ {0.f,0.f,100.f},50.f,0 };
////Perform Sphere HitTest
//GeometryUtils::HitTest_Sphere(testSphere, viewRay, closestHit);
if (closestHit.didHit) if (closestHit.didHit)
{ {
finalColor = materials[closestHit.materialIndex]->Shade(); finalColor = materials[closestHit.materialIndex]->Shade();
for (int idx{ 0 }; idx < lights.size(); idx++)
{
Vector3 lightVec = LightUtils::GetDirectionToLight(lights[idx],closestHit.origin);
Ray shadowRay(closestHit.origin + closestHit.normal * 0.0001f, lightVec.Normalized(), 0.0001, lightVec.Magnitude());
if (pScene->DoesHit(shadowRay))
{
finalColor *= 0.5;
}
}
//const float scaled_t = (closestHit.t - 50.f) / 40.f; //shades spheres based on proximity //const float scaled_t = (closestHit.t - 50.f) / 40.f; //shades spheres based on proximity
//finalColor = { scaled_t,scaled_t,scaled_t }; //finalColor = { scaled_t,scaled_t,scaled_t };

View File

@@ -28,16 +28,18 @@ namespace dae {
void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const
{ {
HitRecord tempHit {};
//Checks through all the sphere //Checks through all the sphere
for (int idx{0};m_SphereGeometries.size() > idx;idx++) for (int idx{0};m_SphereGeometries.size() > idx;idx++)
{ {
HitRecord tempHit;
if (GeometryUtils::HitTest_Sphere(m_SphereGeometries[idx], ray, tempHit)) // Check if there's a hit from spheres if (GeometryUtils::HitTest_Sphere(m_SphereGeometries[idx], ray, tempHit)) // Check if there's a hit from spheres
{ {
if (tempHit.t < closestHit.t) if (tempHit.t < closestHit.t)
{ {
closestHit = tempHit; closestHit = tempHit;
} }
} }
@@ -46,8 +48,6 @@ namespace dae {
//Checks through all the planes //Checks through all the planes
for (int idx{ 0 }; m_PlaneGeometries.size() > idx; idx++) for (int idx{ 0 }; m_PlaneGeometries.size() > idx; idx++)
{ {
HitRecord tempHit;
if (GeometryUtils::HitTest_Plane(m_PlaneGeometries[idx],ray,tempHit)) // Check if there's a hit from planes if (GeometryUtils::HitTest_Plane(m_PlaneGeometries[idx],ray,tempHit)) // Check if there's a hit from planes
{ {
if (tempHit.t < closestHit.t) if (tempHit.t < closestHit.t)
@@ -56,12 +56,31 @@ namespace dae {
} }
} }
} }
tempHit = closestHit;
} }
bool Scene::DoesHit(const Ray& ray) const bool Scene::DoesHit(const Ray& ray) const
{ {
//todo W2 //Checks through all the sphere
throw std::runtime_error("Not Implemented Yet"); for (int idx{ 0 }; m_SphereGeometries.size() > idx; idx++)
{
if (GeometryUtils::HitTest_Sphere(m_SphereGeometries[idx], ray)) // Check if there's a hit from spheres
{
return true;
}
}
for (int idx{ 0 }; idx < m_PlaneGeometries.size(); ++idx)
{
if (GeometryUtils::HitTest_Plane(m_PlaneGeometries[idx], ray))
{
return true;
}
}
return false; return false;
} }
@@ -186,7 +205,7 @@ namespace dae {
AddSphere({ 0.f,5.f,0.f }, .75f, matId_Solid_Porple); /*AddSphere({ 0.f,5.f,0.f }, .75f, matId_Solid_Porple);*/
//Light //Light
AddPointLight({ 0.f,5.f,-5.f }, 70.f, colors::White); AddPointLight({ 0.f,5.f,-5.f }, 70.f, colors::White);

View File

@@ -53,6 +53,7 @@ namespace dae
Camera m_Camera{}; Camera m_Camera{};
Sphere* AddSphere(const Vector3& origin, float radius, unsigned char materialIndex = 0); Sphere* AddSphere(const Vector3& origin, float radius, unsigned char materialIndex = 0);
Plane* AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex = 0); Plane* AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex = 0);
TriangleMesh* AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex = 0); TriangleMesh* AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex = 0);

View File

@@ -28,30 +28,25 @@ namespace dae
float thc{ float(sqrt(sphere.radius*sphere.radius - od)) }; float thc{ float(sqrt(sphere.radius*sphere.radius - od)) };
float t0 = tca - thc; float t = tca - thc;
float t1 = tca + thc; if (t > ray.min and t < ray.max);
else
if (t0 >= ray.min && t0 < ray.max && t1 >= ray.min && t1 < ray.max)
{ {
Vector3 point{ ray.origin + t0 * ray.direction }; t = tca + thc;
Vector3 point2{ ray.origin + t1 * ray.direction }; if (t > ray.min and t < ray.max);
else return false;
if (Vector3(ray.direction, point).SqrMagnitude() > Vector3(ray.direction, point2).SqrMagnitude())
{
hitRecord.origin = Vector3(ray.direction, point2);
hitRecord.normal = Vector3(sphere.origin, Vector3(ray.direction, point2)).Normalized();
hitRecord.t = t1;
return true;
}
if (Vector3(ray.direction, point).SqrMagnitude() < Vector3(ray.direction, point2).SqrMagnitude())
{
hitRecord.origin = Vector3(ray.direction, point);
hitRecord.normal = Vector3(sphere.origin, Vector3(ray.direction, point)).Normalized();
hitRecord.t = t0;
return true;
}
} }
if (!ignoreHitRecord)
{
hitRecord.t = t;
hitRecord.didHit = true;
hitRecord.origin = ray.origin + hitRecord.t * ray.direction;
hitRecord.normal = (hitRecord.origin - sphere.origin).Normalized();
hitRecord.materialIndex = sphere.materialIndex;
}
return true;
} }
@@ -60,6 +55,7 @@ namespace dae
HitRecord temp{}; HitRecord temp{};
return HitTest_Sphere(sphere, ray, temp, true); return HitTest_Sphere(sphere, ray, temp, true);
} }
#pragma endregion #pragma endregion
#pragma region Plane HitTest #pragma region Plane HitTest
//PLANE HIT-TESTS //PLANE HIT-TESTS
@@ -126,9 +122,8 @@ namespace dae
//Direction from target to light //Direction from target to light
inline Vector3 GetDirectionToLight(const Light& light, const Vector3 origin) inline Vector3 GetDirectionToLight(const Light& light, const Vector3 origin)
{ {
//todo W3 Vector3 lightVec(origin, light.origin);
throw std::runtime_error("Not Implemented Yet"); return lightVec;
return {};
} }
inline ColorRGB GetRadiance(const Light& light, const Vector3& target) inline ColorRGB GetRadiance(const Light& light, const Vector3& target)