diff --git a/project/src/Camera.h b/project/src/Camera.h index 1c9239e..d18f436 100644 --- a/project/src/Camera.h +++ b/project/src/Camera.h @@ -39,13 +39,10 @@ namespace dae Matrix CalculateCameraToWorld() { + right = Vector3::Cross(Vector3::UnitY, Camera::forward); + up = Vector3::Cross(Camera::forward, right); - Vector4 Right{ Vector3{}.Cross(Vector3::UnitY,forward).Normalized() ,0 }; - Vector4 Up{ Vector3{}.Cross(forward,Right).Normalized(),0 }; - Vector4 TVec{ origin ,1 }; - Matrix Onb{ Right,Up,forward, TVec }; - - return Onb; + return Matrix(right, up, Camera::forward, Camera::origin); } void Update(Timer* pTimer) @@ -61,22 +58,6 @@ namespace dae 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(); //Keyboard Input @@ -86,50 +67,23 @@ namespace dae int 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) if (mouseState & SDL_BUTTON(SDL_BUTTON_RIGHT)) { if (mouseX != 0 || mouseY != 0) { + totalPitch = mouseY / rotSpeed * deltaTime; + totalYaw = mouseX / rotSpeed * deltaTime; + 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 { - 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]) - { - origin += moveVector; - } + Vector3 upVector = Vector3{}.Cross(forward,right).Normalized() * movSpeed * deltaTime; - if (pKeyboardState[SDL_SCANCODE_S]) - { - origin -= moveVector; - } + if (pKeyboardState[SDL_SCANCODE_W]) + { + origin += forwardVec; + } - if (pKeyboardState[SDL_SCANCODE_D]) - { - origin += rightVector; - } + if (pKeyboardState[SDL_SCANCODE_S]) + { + origin -= forwardVec; + } - if (pKeyboardState[SDL_SCANCODE_A]) - { - origin -= rightVector; - }*/ + if (pKeyboardState[SDL_SCANCODE_D]) + { + 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; + } + } } }; diff --git a/project/src/Renderer.cpp b/project/src/Renderer.cpp index db335a7..f544636 100644 --- a/project/src/Renderer.cpp +++ b/project/src/Renderer.cpp @@ -39,36 +39,38 @@ void Renderer::Render(Scene* pScene) const { for (int py{}; py < m_Height; ++py) { - //float gradient = px / static_cast(m_Width); - //gradient += py / static_cast(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 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 = cameraToWorld.TransformVector(rayDirection); - //Creates a Ray from origin to the point where the current pixel in loop is Ray viewRay{ camera.origin,rayDirection }; - //Sets screen to black - ColorRGB finalColor{ }; - HitRecord 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) { 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 //finalColor = { scaled_t,scaled_t,scaled_t }; diff --git a/project/src/Scene.cpp b/project/src/Scene.cpp index 7d5184c..b99a6a2 100644 --- a/project/src/Scene.cpp +++ b/project/src/Scene.cpp @@ -28,16 +28,18 @@ namespace dae { void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const { + HitRecord tempHit {}; + //Checks through all the sphere 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 (tempHit.t < closestHit.t) { - closestHit = tempHit; + closestHit = tempHit; + } } @@ -46,8 +48,6 @@ namespace dae { //Checks through all the planes 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 (tempHit.t < closestHit.t) @@ -56,12 +56,31 @@ namespace dae { } } } + + tempHit = closestHit; } bool Scene::DoesHit(const Ray& ray) const { - //todo W2 - throw std::runtime_error("Not Implemented Yet"); + //Checks through all the sphere + 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; } @@ -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 AddPointLight({ 0.f,5.f,-5.f }, 70.f, colors::White); diff --git a/project/src/Scene.h b/project/src/Scene.h index cd6a46c..752987a 100644 --- a/project/src/Scene.h +++ b/project/src/Scene.h @@ -53,6 +53,7 @@ namespace dae Camera m_Camera{}; + Sphere* AddSphere(const Vector3& origin, float radius, unsigned char materialIndex = 0); Plane* AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex = 0); TriangleMesh* AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex = 0); diff --git a/project/src/Utils.h b/project/src/Utils.h index 6ecda45..324ce3a 100644 --- a/project/src/Utils.h +++ b/project/src/Utils.h @@ -28,30 +28,25 @@ namespace dae float thc{ float(sqrt(sphere.radius*sphere.radius - od)) }; - float t0 = tca - thc; - float t1 = tca + thc; - - if (t0 >= ray.min && t0 < ray.max && t1 >= ray.min && t1 < ray.max) + float t = tca - thc; + if (t > ray.min and t < ray.max); + else { - Vector3 point{ ray.origin + t0 * ray.direction }; - Vector3 point2{ ray.origin + t1 * ray.direction }; - - 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; - } - + t = tca + thc; + if (t > ray.min and t < ray.max); + else return false; } + + 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{}; return HitTest_Sphere(sphere, ray, temp, true); } + #pragma endregion #pragma region Plane HitTest //PLANE HIT-TESTS @@ -126,9 +122,8 @@ namespace dae //Direction from target to light inline Vector3 GetDirectionToLight(const Light& light, const Vector3 origin) { - //todo W3 - throw std::runtime_error("Not Implemented Yet"); - return {}; + Vector3 lightVec(origin, light.origin); + return lightVec; } inline ColorRGB GetRadiance(const Light& light, const Vector3& target)