mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-07 23:12:00 +00:00
make area light
This commit is contained in:
parent
27bdd95fa6
commit
6b1f026427
3 changed files with 44 additions and 22 deletions
|
@ -21,8 +21,8 @@ add_compile_options(-DSTUDENT_SOLUTION)
|
||||||
|
|
||||||
# When testing large scenes the debug mode will be very slow
|
# When testing large scenes the debug mode will be very slow
|
||||||
# so switch to release
|
# so switch to release
|
||||||
set(CMAKE_BUILD_TYPE Debug)
|
#set(CMAKE_BUILD_TYPE Debug)
|
||||||
#set(CMAKE_BUILD_TYPE Release)
|
set(CMAKE_BUILD_TYPE Release)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
|
|
32
src/Light.cc
32
src/Light.cc
|
@ -1,6 +1,7 @@
|
||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
void Light::setTransform(const Matrix4f &transform) {
|
void Light::setTransform(const Matrix4f &transform) {
|
||||||
this->transform = transform;
|
this->transform = transform;
|
||||||
|
@ -10,6 +11,10 @@ void Light::setGridSize(unsigned int gridSize) { this->gridSize = gridSize; }
|
||||||
|
|
||||||
void Light::setUseCenter(bool useCenter) { this->useCenter = useCenter; }
|
void Light::setUseCenter(bool useCenter) { this->useCenter = useCenter; }
|
||||||
|
|
||||||
|
Vector3f Light::getDiffuse() const { return diffuse; }
|
||||||
|
|
||||||
|
Vector3f Light::getSpecular() const { return specular; }
|
||||||
|
|
||||||
Vector3f PointLight::illumination(const HitRecord &hit,
|
Vector3f PointLight::illumination(const HitRecord &hit,
|
||||||
const vector<Geometry *> &geometries) const {
|
const vector<Geometry *> &geometries) const {
|
||||||
Vector3f shadingPoint = hit.getPoint();
|
Vector3f shadingPoint = hit.getPoint();
|
||||||
|
@ -21,21 +26,34 @@ Vector3f PointLight::illumination(const HitRecord &hit,
|
||||||
if (g != geometry && g->intersect(shadowRay).hasValue())
|
if (g != geometry && g->intersect(shadowRay).hasValue())
|
||||||
return Vector3f::Zero();
|
return Vector3f::Zero();
|
||||||
|
|
||||||
float distance = (center - shadingPoint).norm();
|
|
||||||
float att = 1.0f / distance / distance;
|
|
||||||
|
|
||||||
Vector3f ambient_ = geometry->coefAmbient() * geometry->ambient();
|
Vector3f ambient_ = geometry->coefAmbient() * geometry->ambient();
|
||||||
Vector3f diffuse_ = att * geometry->coefDiffuse() * diffuse *
|
Vector3f diffuse_ = geometry->coefDiffuse() * diffuse.array() *
|
||||||
|
geometry->diffuse().array() *
|
||||||
std::max(0.0f, hit.normal().dot(rayDirection));
|
std::max(0.0f, hit.normal().dot(rayDirection));
|
||||||
|
|
||||||
Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized();
|
Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized();
|
||||||
Vector3f specular_ =
|
Vector3f specular_ =
|
||||||
att * geometry->coefSpecular() * specular *
|
geometry->coefSpecular() * specular.array() *
|
||||||
|
geometry->specular().array() *
|
||||||
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong());
|
pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong());
|
||||||
return ambient_ + diffuse_ + specular_;
|
return diffuse_ + specular_ + ambient_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3f AreaLight::illumination(const HitRecord &hit,
|
Vector3f AreaLight::illumination(const HitRecord &hit,
|
||||||
const vector<Geometry *> &geometries) const {
|
const vector<Geometry *> &geometries) const {
|
||||||
return Vector3f::Zero();
|
Vector3f u = p4 - p1;
|
||||||
|
Vector3f v = p2 - p1;
|
||||||
|
|
||||||
|
Vector3f color = Vector3f::Zero();
|
||||||
|
|
||||||
|
if (useCenter) {
|
||||||
|
color += PointLight(*this, (u + v) / 2).illumination(hit, geometries);
|
||||||
|
} else {
|
||||||
|
for (int y = 0; y < gridSize; ++y)
|
||||||
|
for (int x = 0; x < gridSize; ++x)
|
||||||
|
color += PointLight(*this, (u * x + v * y) / gridSize)
|
||||||
|
.illumination(hit, geometries);
|
||||||
|
}
|
||||||
|
|
||||||
|
return color / gridSize / gridSize;
|
||||||
}
|
}
|
||||||
|
|
30
src/Light.h
30
src/Light.h
|
@ -31,22 +31,11 @@ protected:
|
||||||
bool useCenter = false; // optional member `usecenter`
|
bool useCenter = false; // optional member `usecenter`
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// setters for optional members
|
|
||||||
void setTransform(const Matrix4f &);
|
void setTransform(const Matrix4f &);
|
||||||
void setGridSize(unsigned int);
|
void setGridSize(unsigned int);
|
||||||
void setUseCenter(bool);
|
void setUseCenter(bool);
|
||||||
};
|
Vector3f getDiffuse() const;
|
||||||
|
Vector3f getSpecular() const;
|
||||||
class PointLight : public Light {
|
|
||||||
public:
|
|
||||||
PointLight(const Vector3f &id, const Vector3f &is, Vector3f ¢er)
|
|
||||||
: Light(Type::Point, id, is), center(center) {}
|
|
||||||
|
|
||||||
virtual Vector3f illumination(const HitRecord &,
|
|
||||||
const vector<Geometry *> &) const override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Vector3f center;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class AreaLight : public Light {
|
class AreaLight : public Light {
|
||||||
|
@ -62,4 +51,19 @@ private:
|
||||||
Vector3f p1, p2, p3, p4;
|
Vector3f p1, p2, p3, p4;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PointLight : public Light {
|
||||||
|
public:
|
||||||
|
PointLight(const Vector3f &id, const Vector3f &is, const Vector3f ¢er)
|
||||||
|
: Light(Type::Point, id, is), center(center) {}
|
||||||
|
|
||||||
|
PointLight(const AreaLight &al, const Vector3f ¢er)
|
||||||
|
: PointLight(al.getDiffuse(), al.getSpecular(), center) {}
|
||||||
|
|
||||||
|
virtual Vector3f illumination(const HitRecord &,
|
||||||
|
const vector<Geometry *> &) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector3f center;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // !LIGHT_H_
|
#endif // !LIGHT_H_
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue