Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 11, 2024
1 parent b512f3f commit f48f5f9
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 34 deletions.
63 changes: 42 additions & 21 deletions examples/lights/point_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ namespace {
return plane;
}

auto addLights(Scene& scene) {
auto light1 = PointLight::create(Color::yellow);
light1->castShadow = true;
light1->shadow->bias = -0.005f;
light1->distance = 8;
light1->position.y = 4;

auto light2 = PointLight::create(Color::white);
light2->castShadow = true;
light2->shadow->bias = -0.005f;
light2->distance = 8;
light2->position.y = 4;

auto light3 = PointLight::create(Color::purple);
light3->castShadow = true;
light3->shadow->bias = -0.005f;
light3->distance = 10;
light3->position.y = 7;

auto lightHelper1 = PointLightHelper::create(*light1, 0.25f);
auto lightHelper2 = PointLightHelper::create(*light2, 0.25f);
auto lightHelper3 = PointLightHelper::create(*light3, 0.25f);

light1->name = "light1";
light2->name = "light2";
light3->name = "light3";

scene.add(light1);
scene.add(light2);
scene.add(light3);

scene.add(lightHelper1);
scene.add(lightHelper2);
scene.add(lightHelper3);
}

}// namespace

int main() {
Expand All @@ -49,25 +85,7 @@ int main() {

OrbitControls controls{*camera, canvas};

auto light1 = PointLight::create(Color::yellow);
light1->castShadow = true;
light1->shadow->bias = -0.005f;
light1->distance = 8;
light1->position.y = 4;
scene->add(light1);

auto lightHelper1 = PointLightHelper::create(*light1, 0.25f);
scene->add(lightHelper1);

auto light2 = PointLight::create(Color::white);
light2->castShadow = true;
light2->shadow->bias = -0.005f;
light2->distance = 8;
light2->position.y = 4;
scene->add(light2);

auto lightHelper2 = PointLightHelper::create(*light2, 0.25f);
scene->add(lightHelper2);
addLights(*scene);

auto knot = createTorusKnot();
scene->add(knot);
Expand All @@ -82,10 +100,13 @@ int main() {
renderer.setSize(size);
});

auto light1 = scene->getObjectByName("light1");
auto light2 = scene->getObjectByName("light2");

Clock clock;
canvas.animate([&]() {
float dt = clock.getDelta();
float t = clock.elapsedTime;
const float dt = clock.getDelta();
const float t = clock.elapsedTime;

knot->rotation.y += 0.5f * dt;

Expand Down
4 changes: 2 additions & 2 deletions include/threepp/core/Object3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ namespace threepp {
template<class T>
T* as() {

static_assert(std::is_base_of<T, typename std::remove_cv<typename std::remove_pointer<T>::type>::type>::value,
"T must be a base class of the current class");
static_assert(std::is_base_of<Object3D, typename std::remove_cv<typename std::remove_pointer<T>::type>::type>::value,
"T must be a base class of Object3D");

return dynamic_cast<T*>(this);
}
Expand Down
3 changes: 2 additions & 1 deletion include/threepp/lights/LightShadow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ namespace threepp {

LightShadow(LightShadow&&) = delete;
LightShadow(const LightShadow&) = delete;
LightShadow& operator=(LightShadow&&) = delete;
LightShadow& operator=(const LightShadow&) = delete;

[[nodiscard]] size_t getViewportCount() const;

[[nodiscard]] const Frustum& getFrustum() const;

virtual void updateMatrices(Light* light);
virtual void updateMatrices(Light& light);

Vector4& getViewport(size_t viewportIndex);

Expand Down
2 changes: 1 addition & 1 deletion include/threepp/lights/SpotLightShadow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace threepp {
public:
float focus = 1;

void updateMatrices(Light* light) override;
void updateMatrices(Light& light) override;

static std::shared_ptr<SpotLightShadow> create();

Expand Down
4 changes: 2 additions & 2 deletions include/threepp/materials/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ namespace threepp {
template<class T>
std::shared_ptr<T> as_shared() {

static_assert(std::is_base_of<T, typename std::remove_cv<typename std::remove_pointer<T>::type>::type>::value,
"T must be a base class of the current class");
static_assert(std::is_base_of<Material, typename std::remove_cv<typename std::remove_pointer<T>::type>::type>::value,
"T must be a base class of Material");

auto m = shared_from_this();
return std::dynamic_pointer_cast<T>(m);
Expand Down
6 changes: 3 additions & 3 deletions src/threepp/lights/LightShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const Frustum& LightShadow::getFrustum() const {
return this->_frustum;
}

void LightShadow::updateMatrices(Light* light) {
void LightShadow::updateMatrices(Light& light) {

auto& shadowCamera = this->camera;
auto& shadowMatrix = this->matrix;

_lightPositionWorld.setFromMatrixPosition(*light->matrixWorld);
_lightPositionWorld.setFromMatrixPosition(*light.matrixWorld);
shadowCamera->position.copy(_lightPositionWorld);

auto lightWithTarget = dynamic_cast<LightWithTarget*>(light);
auto lightWithTarget = dynamic_cast<LightWithTarget*>(&light);
_lookTarget.setFromMatrixPosition(*lightWithTarget->target().matrixWorld);
shadowCamera->lookAt(_lookTarget);
shadowCamera->updateMatrixWorld();
Expand Down
6 changes: 3 additions & 3 deletions src/threepp/lights/SpotLightShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using namespace threepp;

SpotLightShadow::SpotLightShadow()
: LightShadow(std::make_unique<PerspectiveCamera>(50, 1, 0.5f, 500)) {}
: LightShadow(std::make_unique<PerspectiveCamera>(50.f, 1.f, 0.5f, 500.f)) {}

void SpotLightShadow::updateMatrices(Light* _light) {
void SpotLightShadow::updateMatrices(Light& _light) {

auto light = _light->as<SpotLight>();
auto light = _light.as<SpotLight>();

const auto fov = math::RAD2DEG * 2 * light->angle * this->focus;
const auto aspect = this->mapSize.x / this->mapSize.y;
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/renderers/gl/GLShadowMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ struct GLShadowMap::Impl {
if (auto pointLightShadow = std::dynamic_pointer_cast<PointLightShadow>(shadow)) {
pointLightShadow->updateMatrices(light->as<PointLight>(), vp);
} else {
shadow->updateMatrices(light);
shadow->updateMatrices(*light);
}

_frustum = &shadow->getFrustum();
Expand Down

0 comments on commit f48f5f9

Please sign in to comment.