Simplistic wielded tool lighting, added setMeshVerticesColor to utility.h and refacto...
authorKahrl <kahrl@gmx.net>
Wed, 21 Sep 2011 16:37:29 +0000 (18:37 +0200)
committerKahrl <kahrl@gmx.net>
Wed, 21 Sep 2011 16:37:29 +0000 (18:37 +0200)
src/camera.cpp
src/camera.h
src/content_cao.cpp
src/environment.cpp
src/mapblockobject.h
src/player.h
src/utility.cpp
src/utility.h

index c14d0e8bf4c4a2e8e79a6b9586bec02cfb6fdd15..a615321438ed0019a9d05212df2e6c48e895b2d3 100644 (file)
@@ -249,6 +249,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, v2u32 screensize)
        }
        m_wieldnode->setPosition(wield_position);
        m_wieldnode->setRotation(wield_rotation);
+       m_wieldnode->updateLight(player->light);
 
        // Render distance feedback loop
        updateViewingRange(frametime);
@@ -472,6 +473,7 @@ ExtrudedSpriteSceneNode::ExtrudedSpriteSceneNode(
        m_thickness = 0.1;
        m_cubemesh = NULL;
        m_is_cube = false;
+       m_light = LIGHT_MAX;
 }
 
 ExtrudedSpriteSceneNode::~ExtrudedSpriteSceneNode()
@@ -519,6 +521,7 @@ void ExtrudedSpriteSceneNode::setSprite(video::ITexture* texture)
        m_meshnode->getMaterial(0).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
        m_meshnode->setVisible(true);
        m_is_cube = false;
+       updateLight(m_light);
 }
 
 void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
@@ -546,6 +549,16 @@ void ExtrudedSpriteSceneNode::setCube(const TileSpec tiles[6])
        }
        m_meshnode->setVisible(true);
        m_is_cube = true;
+       updateLight(m_light);
+}
+
+void ExtrudedSpriteSceneNode::updateLight(u8 light)
+{
+       m_light = light;
+
+       u8 li = decode_light(light);
+       video::SColor color(255,li,li,li);
+       setMeshVerticesColor(m_meshnode->getMesh(), color);
 }
 
 void ExtrudedSpriteSceneNode::removeSpriteFromCache(video::ITexture* texture)
index fbee4a3786af523b9a78779f2d8ea395c3a627b8..66825e372a58646ad28b44a56b7f5ee88b6e3962 100644 (file)
@@ -205,6 +205,8 @@ public:
        f32 getSpriteThickness() const { return m_thickness; }
        void setSpriteThickness(f32 thickness);
 
+       void updateLight(u8 light);
+
        void removeSpriteFromCache(video::ITexture* texture);
 
        virtual const core::aabbox3d<f32>& getBoundingBox() const;
@@ -216,6 +218,7 @@ private:
        f32 m_thickness;
        scene::IMesh* m_cubemesh;
        bool m_is_cube;
+       u8 m_light;
 
        // internal extrusion helper methods
        io::path getExtrudedName(video::ITexture* texture);
index dfeaea85ab09babf8d62f7a07b22a058046ea353..b6543061aea6b38c612820d9dd2cffdf8238bbdb 100644 (file)
@@ -224,22 +224,7 @@ void ItemCAO::updateLight(u8 light_at_pos)
 
        u8 li = decode_light(light_at_pos);
        video::SColor color(255,li,li,li);
-
-       scene::IMesh *mesh = m_node->getMesh();
-       if(mesh == NULL)
-               return;
-       
-       u16 mc = mesh->getMeshBufferCount();
-       for(u16 j=0; j<mc; j++)
-       {
-               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-               u16 vc = buf->getVertexCount();
-               for(u16 i=0; i<vc; i++)
-               {
-                       vertices[i].Color = color;
-               }
-       }
+       setMeshVerticesColor(m_node->getMesh(), color);
 }
 
 v3s16 ItemCAO::getLightPosition()
@@ -430,22 +415,7 @@ void RatCAO::updateLight(u8 light_at_pos)
 
        u8 li = decode_light(light_at_pos);
        video::SColor color(255,li,li,li);
-
-       scene::IMesh *mesh = m_node->getMesh();
-       if(mesh == NULL)
-               return;
-       
-       u16 mc = mesh->getMeshBufferCount();
-       for(u16 j=0; j<mc; j++)
-       {
-               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-               u16 vc = buf->getVertexCount();
-               for(u16 i=0; i<vc; i++)
-               {
-                       vertices[i].Color = color;
-               }
-       }
+       setMeshVerticesColor(m_node->getMesh(), color);
 }
 
 v3s16 RatCAO::getLightPosition()
@@ -601,22 +571,7 @@ void Oerkki1CAO::updateLight(u8 light_at_pos)
 
        u8 li = decode_light(light_at_pos);
        video::SColor color(255,li,li,li);
-
-       scene::IMesh *mesh = m_node->getMesh();
-       if(mesh == NULL)
-               return;
-       
-       u16 mc = mesh->getMeshBufferCount();
-       for(u16 j=0; j<mc; j++)
-       {
-               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-               u16 vc = buf->getVertexCount();
-               for(u16 i=0; i<vc; i++)
-               {
-                       vertices[i].Color = color;
-               }
-       }
+       setMeshVerticesColor(m_node->getMesh(), color);
 }
 
 v3s16 Oerkki1CAO::getLightPosition()
@@ -833,22 +788,7 @@ void FireflyCAO::updateLight(u8 light_at_pos)
 
        u8 li = 255;
        video::SColor color(255,li,li,li);
-
-       scene::IMesh *mesh = m_node->getMesh();
-       if(mesh == NULL)
-               return;
-       
-       u16 mc = mesh->getMeshBufferCount();
-       for(u16 j=0; j<mc; j++)
-       {
-               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-               u16 vc = buf->getVertexCount();
-               for(u16 i=0; i<vc; i++)
-               {
-                       vertices[i].Color = color;
-               }
-       }
+       setMeshVerticesColor(m_node->getMesh(), color);
 }
 
 v3s16 FireflyCAO::getLightPosition()
index 80f41f9fd44905dfc6673b00c1d82853e64f30ed..ea33274df86b1f82ed0c2fac4c55428558048f85 100644 (file)
@@ -1686,18 +1686,19 @@ void ClientEnvironment::step(float dtime)
                        // Move
                        player->move(dtime, *m_map, 100*BS);
 
-                       // Update lighting on remote players on client
-                       u8 light = LIGHT_MAX;
-                       try{
-                               // Get node at head
-                               v3s16 p = player->getLightPosition();
-                               MapNode n = m_map->getNode(p);
-                               light = n.getLightBlend(getDayNightRatio());
-                       }
-                       catch(InvalidPositionException &e) {}
-                       player->updateLight(light);
                }
                
+               // Update lighting on all players on client
+               u8 light = LIGHT_MAX;
+               try{
+                       // Get node at head
+                       v3s16 p = player->getLightPosition();
+                       MapNode n = m_map->getNode(p);
+                       light = n.getLightBlend(getDayNightRatio());
+               }
+               catch(InvalidPositionException &e) {}
+               player->updateLight(light);
+
                /*
                        Add footsteps to grass
                */
index 804494715608312699518e90d77ee65e9c205ed9..b852812e2da9f2eba8ccd718783dc04ed6a70f28 100644 (file)
@@ -484,20 +484,7 @@ public:
 
                u8 li = decode_light(light_at_pos);
                video::SColor color(255,li,li,li);
-
-               scene::IMesh *mesh = m_node->getMesh();
-               
-               u16 mc = mesh->getMeshBufferCount();
-               for(u16 j=0; j<mc; j++)
-               {
-                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-                       u16 vc = buf->getVertexCount();
-                       for(u16 i=0; i<vc; i++)
-                       {
-                               vertices[i].Color = color;
-                       }
-               }
+               setMeshVerticesColor(m_node->getMesh(), color);
        }
 #endif
 
@@ -676,20 +663,7 @@ public:
 
                u8 li = decode_light(light_at_pos);
                video::SColor color(255,li,li,li);
-
-               scene::IMesh *mesh = m_node->getMesh();
-               
-               u16 mc = mesh->getMeshBufferCount();
-               for(u16 j=0; j<mc; j++)
-               {
-                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-                       u16 vc = buf->getVertexCount();
-                       for(u16 i=0; i<vc; i++)
-                       {
-                               vertices[i].Color = color;
-                       }
-               }
+               setMeshVerticesColor(m_node->getMesh(), color);
        }
        
 #endif
@@ -836,20 +810,7 @@ public:
 
                u8 li = decode_light(light_at_pos);
                video::SColor color(255,li,li,li);
-
-               scene::IMesh *mesh = m_node->getMesh();
-               
-               u16 mc = mesh->getMeshBufferCount();
-               for(u16 j=0; j<mc; j++)
-               {
-                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-                       u16 vc = buf->getVertexCount();
-                       for(u16 i=0; i<vc; i++)
-                       {
-                               vertices[i].Color = color;
-                       }
-               }
+               setMeshVerticesColor(m_node->getMesh(), color);
        }
 #endif
 
@@ -982,20 +943,7 @@ public:
 
                u8 li = decode_light(light_at_pos);
                video::SColor color(255,li,li,li);
-
-               scene::IMesh *mesh = m_node->getMesh();
-               
-               u16 mc = mesh->getMeshBufferCount();
-               for(u16 j=0; j<mc; j++)
-               {
-                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-                       u16 vc = buf->getVertexCount();
-                       for(u16 i=0; i<vc; i++)
-                       {
-                               vertices[i].Color = color;
-                       }
-               }
+               setMeshVerticesColor(m_node->getMesh(), color);
        }
        
 #endif
index 350b85e1ba5ba46a547729b417713a219d05200f..ce1001992fda971a02e2e565a7253f7a229d6978 100644 (file)
@@ -126,7 +126,10 @@ public:
 
        virtual bool isLocal() const = 0;
 
-       virtual void updateLight(u8 light_at_pos) {};
+       virtual void updateLight(u8 light_at_pos)
+       {
+               light = light_at_pos;
+       }
        
        // NOTE: Use peer_id == 0 for disconnected
        /*virtual bool isClientConnected() { return false; }
@@ -149,6 +152,8 @@ public:
        bool swimming_up;
        bool is_frozen;
        
+       u8 light;
+
        Inventory inventory;
        // Actual inventory is backed up here when creative mode is used
        Inventory *inventory_backup;
@@ -266,25 +271,14 @@ public:
 
        virtual void updateLight(u8 light_at_pos)
        {
+               Player::updateLight(light_at_pos);
+
                if(m_node == NULL)
                        return;
 
                u8 li = decode_light(light_at_pos);
                video::SColor color(255,li,li,li);
-
-               scene::IMesh *mesh = m_node->getMesh();
-               
-               u16 mc = mesh->getMeshBufferCount();
-               for(u16 j=0; j<mc; j++)
-               {
-                       scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
-                       video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
-                       u16 vc = buf->getVertexCount();
-                       for(u16 i=0; i<vc; i++)
-                       {
-                               vertices[i].Color = color;
-                       }
-               }
+               setMeshVerticesColor(m_node->getMesh(), color);
        }
        
        void move(f32 dtime, Map &map, f32 pos_max_d);
index 9c1edc8a95fa6c5ee3ce268718b56f2e7229c5f7..3c6c2f286ea7f05800f976d9c5a8ef0a5142b786 100644 (file)
@@ -156,6 +156,25 @@ void mysrand(unsigned seed)
    next = seed;
 }
 
+// Sets the color of all vertices in the mesh
+void setMeshVerticesColor(scene::IMesh* mesh, video::SColor& color)
+{
+       if(mesh == NULL)
+               return;
+       
+       u16 mc = mesh->getMeshBufferCount();
+       for(u16 j=0; j<mc; j++)
+       {
+               scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
+               video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
+               u16 vc = buf->getVertexCount();
+               for(u16 i=0; i<vc; i++)
+               {
+                       vertices[i].Color = color;
+               }
+       }
+}
+
 /*
        blockpos: position of block in block coordinates
        camera_pos: position of camera in nodes
index d331bafbb2896020b90d49e39e1c36dcf57729e8..2fefd86678ff243d5eec5848edd10260c598478f 100644 (file)
@@ -553,6 +553,9 @@ private:
        u32 *m_result;
 };
 
+// Sets the color of all vertices in the mesh
+void setMeshVerticesColor(scene::IMesh* mesh, video::SColor& color);
+
 // Calculates the borders of a "d-radius" cube
 inline void getFacePositions(core::list<v3s16> &list, u16 d)
 {