Skip to content

Commit

Permalink
Use multiple light positions for CAO lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jun 11, 2020
1 parent f897941 commit 3f0cbbc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/client/clientobject.h
Expand Up @@ -43,7 +43,6 @@ class ClientActiveObject : public ActiveObject
virtual void removeFromScene(bool permanent) {}

virtual void updateLight(u32 day_night_ratio) {}
virtual v3s16 getLightPosition() { return v3s16(0, 0, 0); }

virtual bool getCollisionBox(aabb3f *toset) const { return false; }
virtual bool getSelectionBox(aabb3f *toset) const { return false; }
Expand Down
42 changes: 24 additions & 18 deletions src/client/content_cao.cpp
Expand Up @@ -182,7 +182,6 @@ class TestCAO : public ClientActiveObject
void addToScene(ITextureSource *tsrc);
void removeFromScene(bool permanent);
void updateLight(u32 day_night_ratio);
v3s16 getLightPosition();
void updateNodePos();

void step(float dtime, ClientEnvironment *env);
Expand Down Expand Up @@ -258,11 +257,6 @@ void TestCAO::updateLight(u32 day_night_ratio)
{
}

v3s16 TestCAO::getLightPosition()
{
return floatToInt(m_position, BS);
}

void TestCAO::updateNodePos()
{
if (!m_node)
Expand Down Expand Up @@ -799,13 +793,20 @@ void GenericCAO::updateLight(u32 day_night_ratio)
return;

u8 light_at_pos = 0;
bool pos_ok;

v3s16 p = getLightPosition();
MapNode n = m_env->getMap().getNode(p, &pos_ok);
if (pos_ok)
light_at_pos = n.getLightBlend(day_night_ratio, m_client->ndef());
else
bool pos_ok = false;

v3s16 pos[3];
u16 npos = getLightPosition(pos);
for (u16 i = 0; i < npos; i++) {
bool this_ok;
MapNode n = m_env->getMap().getNode(pos[i], &this_ok);
if (this_ok) {
u8 this_light = n.getLightBlend(day_night_ratio, m_client->ndef());
light_at_pos = MYMAX(light_at_pos, this_light);
pos_ok = true;
}
}
if (!pos_ok)
light_at_pos = blend_light(day_night_ratio, LIGHT_SUN, 0);

u8 light = decode_light(light_at_pos + m_glow);
Expand Down Expand Up @@ -856,12 +857,17 @@ void GenericCAO::setNodeLight(u8 light)
}
}

v3s16 GenericCAO::getLightPosition()
u16 GenericCAO::getLightPosition(v3s16 *pos)
{
if (m_is_player)
return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);

return floatToInt(m_position, BS);
const auto &box = m_prop.collisionbox;
pos[0] = floatToInt(m_position + box.MinEdge * BS, BS);
pos[1] = floatToInt(m_position + box.MaxEdge * BS, BS);

// Skip center pos if it falls into the same node as Min or MaxEdge
if ((box.MaxEdge - box.MinEdge).getLengthSQ() < 3.0f)
return 2;
pos[2] = floatToInt(m_position + box.getCenter() * BS, BS);
return 3;
}

void GenericCAO::updateNametag()
Expand Down
5 changes: 4 additions & 1 deletion src/client/content_cao.h
Expand Up @@ -240,7 +240,10 @@ class GenericCAO : public ClientActiveObject

void setNodeLight(u8 light);

v3s16 getLightPosition();
/* Get light position(s).
* returns number of positions written into pos[], which must have space
* for at least 3 vectors. */
u16 getLightPosition(v3s16 *pos);

void updateNametag();

Expand Down

0 comments on commit 3f0cbbc

Please sign in to comment.