Batman
This commit is contained in:
156
project/src/Scene.cpp
Normal file
156
project/src/Scene.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "Scene.h"
|
||||
#include "Utils.h"
|
||||
#include "Material.h"
|
||||
|
||||
namespace dae {
|
||||
|
||||
#pragma region Base Scene
|
||||
//Initialize Scene with Default Solid Color Material (RED)
|
||||
Scene::Scene() :
|
||||
m_Materials({ new Material_SolidColor({1,0,0}) })
|
||||
{
|
||||
m_SphereGeometries.reserve(32);
|
||||
m_PlaneGeometries.reserve(32);
|
||||
m_TriangleMeshGeometries.reserve(32);
|
||||
m_Lights.reserve(32);
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
{
|
||||
for (auto& pMaterial : m_Materials)
|
||||
{
|
||||
delete pMaterial;
|
||||
pMaterial = nullptr;
|
||||
}
|
||||
|
||||
m_Materials.clear();
|
||||
}
|
||||
|
||||
void dae::Scene::GetClosestHit(const Ray& ray, HitRecord& closestHit) const
|
||||
{
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//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)
|
||||
{
|
||||
closestHit = tempHit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Scene::DoesHit(const Ray& ray) const
|
||||
{
|
||||
//todo W2
|
||||
throw std::runtime_error("Not Implemented Yet");
|
||||
return false;
|
||||
}
|
||||
|
||||
#pragma region Scene Helpers
|
||||
Sphere* Scene::AddSphere(const Vector3& origin, float radius, unsigned char materialIndex)
|
||||
{
|
||||
Sphere s;
|
||||
s.origin = origin;
|
||||
s.radius = radius;
|
||||
s.materialIndex = materialIndex;
|
||||
|
||||
m_SphereGeometries.emplace_back(s);
|
||||
return &m_SphereGeometries.back();
|
||||
}
|
||||
|
||||
Plane* Scene::AddPlane(const Vector3& origin, const Vector3& normal, unsigned char materialIndex)
|
||||
{
|
||||
Plane p;
|
||||
p.origin = origin;
|
||||
p.normal = normal;
|
||||
p.materialIndex = materialIndex;
|
||||
|
||||
m_PlaneGeometries.emplace_back(p);
|
||||
return &m_PlaneGeometries.back();
|
||||
}
|
||||
|
||||
TriangleMesh* Scene::AddTriangleMesh(TriangleCullMode cullMode, unsigned char materialIndex)
|
||||
{
|
||||
TriangleMesh m{};
|
||||
m.cullMode = cullMode;
|
||||
m.materialIndex = materialIndex;
|
||||
|
||||
m_TriangleMeshGeometries.emplace_back(m);
|
||||
return &m_TriangleMeshGeometries.back();
|
||||
}
|
||||
|
||||
Light* Scene::AddPointLight(const Vector3& origin, float intensity, const ColorRGB& color)
|
||||
{
|
||||
Light l;
|
||||
l.origin = origin;
|
||||
l.intensity = intensity;
|
||||
l.color = color;
|
||||
l.type = LightType::Point;
|
||||
|
||||
m_Lights.emplace_back(l);
|
||||
return &m_Lights.back();
|
||||
}
|
||||
|
||||
Light* Scene::AddDirectionalLight(const Vector3& direction, float intensity, const ColorRGB& color)
|
||||
{
|
||||
Light l;
|
||||
l.direction = direction;
|
||||
l.intensity = intensity;
|
||||
l.color = color;
|
||||
l.type = LightType::Directional;
|
||||
|
||||
m_Lights.emplace_back(l);
|
||||
return &m_Lights.back();
|
||||
}
|
||||
|
||||
unsigned char Scene::AddMaterial(Material* pMaterial)
|
||||
{
|
||||
m_Materials.push_back(pMaterial);
|
||||
return static_cast<unsigned char>(m_Materials.size() - 1);
|
||||
}
|
||||
#pragma endregion
|
||||
#pragma endregion
|
||||
|
||||
#pragma region SCENE W1
|
||||
void Scene_W1::Initialize()
|
||||
{
|
||||
//default: Material id0 >> SolidColor Material (RED)
|
||||
constexpr unsigned char matId_Solid_Red = 0;
|
||||
const unsigned char matId_Solid_Blue = AddMaterial(new Material_SolidColor{ colors::Blue });
|
||||
|
||||
const unsigned char matId_Solid_Yellow = AddMaterial(new Material_SolidColor{ colors::Yellow });
|
||||
const unsigned char matId_Solid_Green = AddMaterial(new Material_SolidColor{ colors::Green });
|
||||
const unsigned char matId_Solid_Magenta = AddMaterial(new Material_SolidColor{ colors::Magenta });
|
||||
|
||||
//Spheres
|
||||
AddSphere({ -25.f, 0.f, 100.f }, 50.f, matId_Solid_Red);
|
||||
AddSphere({ 25.f, 0.f, 100.f }, 50.f, matId_Solid_Blue);
|
||||
|
||||
//Plane
|
||||
AddPlane({ -75.f, 0.f, 0.f }, { 1.f, 0.f,0.f }, matId_Solid_Green);
|
||||
AddPlane({ 75.f, 0.f, 0.f }, { -1.f, 0.f,0.f }, matId_Solid_Green);
|
||||
AddPlane({ 0.f, -75.f, 0.f }, { 0.f, 1.f,0.f }, matId_Solid_Yellow);
|
||||
AddPlane({ 0.f, 75.f, 0.f }, { 0.f, -1.f,0.f }, matId_Solid_Yellow);
|
||||
AddPlane({ 0.f, 0.f, 125.f }, { 0.f, 0.f,-1.f }, matId_Solid_Magenta);
|
||||
}
|
||||
#pragma endregion
|
||||
}
|
||||
Reference in New Issue
Block a user