Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML dynamic color tile-based lighting  (Read 20188 times)

0 Members and 1 Guest are viewing this topic.

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
SFML dynamic color tile-based lighting
« on: August 27, 2014, 11:12:48 am »
Some time ago i've posted info about the project I'm working on (F.I.R.E.D. http://en.sfml-dev.org/forums/index.php?topic=16025.0)

And now I've decided to make a simple demo and write an article about my lighting system.



Sources: https://github.com/achpile/sfml-lighting
Article: https://github.com/SFML/SFML/wiki/Tutorial:-Dynamic-colorful-tile-based-lighting

So, if you'll find something wrong or untold in the article - feel free to post your requests ;)

PS: to compile demo you have to install sfgui.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML dynamic color tile-based lighting
« Reply #1 on: August 27, 2014, 11:41:10 am »
Why does everybody post in General Discussions? This is rather something for the Wiki forum ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: SFML dynamic color tile-based lighting
« Reply #2 on: August 27, 2014, 11:46:58 am »
Why does everybody post in General Discussions? This is rather something for the Wiki forum ;)

Sorry :D Maybe because of Wiki is "Discussions about the wiki" and General is "For everything that is not a help request"  ;D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML dynamic color tile-based lighting
« Reply #3 on: August 27, 2014, 11:58:37 am »
To me, discussing about your wiki article fits quite well in "Discussions about the wiki" :P
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: SFML dynamic color tile-based lighting
« Reply #4 on: August 27, 2014, 12:06:38 pm »
To me, discussing about your wiki article fits quite well in "Discussions about the wiki" :P

Thanks for transfer and comment, Laurent ;)

adidas

  • Newbie
  • *
  • Posts: 1
  • Take my hands and follow me - i set you free
    • View Profile
Re: SFML dynamic color tile-based lighting
« Reply #5 on: June 16, 2018, 05:12:58 pm »
Help plz need simple examle Light SFML full
I can t build  simple project
#include <SFML/Graphics.hpp>
#include <ltbl/lighting/LightSystem.h>
using namespace sf;
int main()
{
    RenderWindow window;
    ContextSettings contextSettings;
    Event event;
    Texture backgroundImage;
    sf::RenderStates lightRenderStates;
    Sprite backgroundSprite(backgroundImage);
    sf::Sprite Lsprite;//&#1057;&#1087;&#1088;&#1072;&#1081;&#1090; &#1089;&#1074;&#1077;&#1090;&#1072;.
    Texture pointLightTexture,ConeLightTexture;
    Texture  penumbraTexture;
    Shader unshadowShader, lightOverShapeShader;
    ConvexShape shape1, shape2, shape3;
    ltbl::LightSystem ls;
    //ls._ambientColor(Color::Black);
    contextSettings.antialiasingLevel = 8;
    window.create(sf::VideoMode(1280, 720), "LTBL2", sf::Style::Default, contextSettings);
 
    penumbraTexture.loadFromFile("data/penumbraTexture.png");
    penumbraTexture.setSmooth(true);
 
    pointLightTexture.loadFromFile("data/pointLightTexture.png");
    pointLightTexture.setSmooth(true);
 
    ConeLightTexture.loadFromFile("data/spotLightTexture.png");
    ConeLightTexture.setSmooth(true);
   
    backgroundImage.loadFromFile("data/background.png");
    backgroundImage.setRepeated(true);
 
    backgroundSprite.setTextureRect(sf::IntRect(0, 0, 1280 * 2, 720 * 2));
    backgroundSprite.setPosition(-400.0f, -400.0f);
 
    unshadowShader.loadFromFile("data/unshadowShader.vert", "data/unshadowShader.frag");
    lightOverShapeShader.loadFromFile("data/lightOverShapeShader.vert", "data/lightOverShapeShader.frag");
 
    ls.create(ltbl::rectFromBounds(sf::Vector2f(-1000.0f, -1000.0f), sf::Vector2f(1000.0f, 1000.0f)), window.getSize(), penumbraTexture, unshadowShader, lightOverShapeShader);
   
 
    std::shared_ptr<ltbl::LightPointEmission> light = std::make_shared<ltbl::LightPointEmission>();//LightPointEmission->
    light->_emissionSprite.setOrigin(sf::Vector2f(pointLightTexture.getSize().x * 0.5f, pointLightTexture.getSize().y * 0.5f));
    light->_emissionSprite.setTexture(pointLightTexture);
    light->_emissionSprite.setScale(sf::Vector2f(10,10));
    light->_emissionSprite.setColor(sf::Color(255, 255, 255));
    light->_emissionSprite.setPosition(sf::Vector2f(50, 50));
    light->_sourceRadius = 10;
    light->_shadowOverExtendMultiplier =1
    ls.addLight(light);
    std::shared_ptr<ltbl::LightPointEmission> light2 = std::make_shared<ltbl::LightPointEmission>();
    light2->_emissionSprite.setOrigin(sf::Vector2f(ConeLightTexture.getSize().x * 0.5f, ConeLightTexture.getSize().y * 0.5f));
    light2->_emissionSprite.setTexture(ConeLightTexture);
    light2->_emissionSprite.setScale(sf::Vector2f(13, 10));
    light2->_emissionSprite.setColor(sf::Color(255, 255, 255));
    light2->_emissionSprite.setPosition(sf::Vector2f(440, 50));
    light2->_emissionSprite.setRotation(90);
    light2->_sourceRadius = 10;
    ls.addLight(light2);
 
    std::shared_ptr<ltbl::LightPointEmission> light1 = std::make_shared<ltbl::LightPointEmission>();
    light1->_emissionSprite.setOrigin(sf::Vector2f(pointLightTexture.getSize().x * 0.5f, pointLightTexture.getSize().y * 0.5f));
    light1->_emissionSprite.setTexture(pointLightTexture);
    light1->_emissionSprite.setScale(sf::Vector2f(5, 5));
    light1->_emissionSprite.setColor(sf::Color(255, 255, 255));
    light1->_sourceRadius = 10;
    ls.addLight(light1);
 
    std::shared_ptr<ltbl::LightDirectionEmission> light3 = std::make_shared<ltbl::LightDirectionEmission>();
    light3->_castDirection = ltbl::vectorNormalize(sf::Vector2f(-0.1f, 0.8f));
//  ls.addLight(light3);
   
    std::shared_ptr<ltbl::LightShape> lightShape = std::make_shared<ltbl::LightShape>();
    lightShape->_shape.setPointCount(4);
    lightShape->_shape.setPoint(0, Vector2f(0, 0));
    lightShape->_shape.setPoint(1, Vector2f(80, 0));
    lightShape->_shape.setPoint(2, Vector2f(80, 80));
    lightShape->_shape.setPoint(3, Vector2f(0, 80));
    lightShape->_renderLightOverShape = false;
    lightShape->_shape.setPosition(100,100);
    ls.addShape(lightShape);
 
    std::shared_ptr<ltbl::LightShape> lightShape1 = std::make_shared<ltbl::LightShape>();
    lightShape1->_shape.setPointCount(4);
 
    lightShape1->_shape.setPoint(0, Vector2f(0, 0));
    lightShape1->_shape.setPoint(1, Vector2f(200, 0));
    lightShape1->_shape.setPoint(2, Vector2f(200, 80));
    lightShape1->_shape.setPoint(3, Vector2f(0, 80));
    lightShape1->_renderLightOverShape = false;
    lightShape1->_shape.setPosition(300, 400);
    ls.addShape(lightShape1);
 
    std::shared_ptr<ltbl::LightShape> lightShape2 = std::make_shared<ltbl::LightShape>();
    lightShape2->_shape.setPointCount(4);
    lightShape2->_shape.setPoint(0, Vector2f(0, 0));
    lightShape2->_shape.setPoint(1, Vector2f(80, 0));
    lightShape2->_shape.setPoint(2, Vector2f(80, 80));
    lightShape2->_shape.setPoint(3, Vector2f(0, 80));
    lightShape2->_renderLightOverShape = false;
    lightShape2->_shape.setPosition(400, 200);
    ls.addShape(lightShape2);

    shape1.setPointCount(4);
    shape1.setPoint(0, Vector2f(0, 0));
    shape1.setPoint(1, Vector2f(80, 0));
    shape1.setPoint(2, Vector2f(80, 80));
    shape1.setPoint(3, Vector2f(0, 80));
    shape1.setPosition(100, 100);
    shape1.setFillColor(Color::Green);
 
    shape2.setPointCount(4);
    shape2.setPoint(0, Vector2f(0, 0));
    shape2.setPoint(1, Vector2f(80, 0));
    shape2.setPoint(2, Vector2f(80, 80));
    shape2.setPoint(3, Vector2f(0, 80));
    shape2.setPosition(400, 200);
    shape2.setFillColor(Color::Green);
 
    shape3.setPointCount(4);
    shape3.setPoint(0, Vector2f(0, 0));
    shape3.setPoint(1, Vector2f(200, 0));
    shape3.setPoint(2, Vector2f(200, 80));
    shape3.setPoint(3, Vector2f(0, 80));
    shape3.setPosition(300, 400);
    shape3.setFillColor(Color::Green);
 
    sf::View view = window.getDefaultView();
 
    view.setCenter(sf::Vector2f(1280 * 0.5f, 720 * 0.5f));
    while (window.isOpen())
    {
        Vector2i pixelPos = Mouse::getPosition(window);
        Vector2f pos = window.mapPixelToCoords(pixelPos);
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
 
        light1->_emissionSprite.setPosition(pos);
        window.clear(sf::Color::White);
 
        window.draw(backgroundSprite);
        window.draw(shape1);
        window.draw(shape2);
        window.draw(shape3);
//-----------------------------RENDER SUN-----------------------------------
        ls.render(view, unshadowShader,lightOverShapeShader);
        Lsprite.setTexture(ls.getLightingTexture());
        lightRenderStates.blendMode = sf::BlendMultiply;
        window.setView(window.getDefaultView());
        window.draw(Lsprite, lightRenderStates);
        window.setView(view);
// ---------------------------------------------------------------------------
 
        window.display();
    }
}
 
MY VS 2015 take it errors
>------ &#1057;&#1073;&#1086;&#1088;&#1082;&#1072; &#1085;&#1072;&#1095;&#1072;&#1090;&#1072;: &#1087;&#1088;&#1086;&#1077;&#1082;&#1090;: LIGHT777, &#1050;&#1086;&#1085;&#1092;&#1080;&#1075;&#1091;&#1088;&#1072;&#1094;&#1080;&#1103;: Debug Win32 ------
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "class sf::Rect<float> __cdecl ltbl::rectFromBounds(class sf::Vector2<float> const &,class sf::Vector2<float> const &)" (?rectFromBounds@ltbl@@YA?AV?$Rect@M@sf@@ABV?$Vector2@M@3@0@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "class sf::Vector2<float> __cdecl ltbl::vectorNormalize(class sf::Vector2<float> const &)" (?vectorNormalize@ltbl@@YA?AV?$Vector2@M@sf@@ABV23@@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "public: __thiscall ltbl::Quadtree::Quadtree(void)" (??0Quadtree@ltbl@@QAE@XZ) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; "public: __thiscall ltbl::DynamicQuadtree::DynamicQuadtree(void)" (??0DynamicQuadtree@ltbl@@QAE@XZ)
1>MAIN.obj : error LNK2001: &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; ""public: virtual void __thiscall ltbl::DynamicQuadtree::add(class ltbl::QuadtreeOccupant *)" (?add@DynamicQuadtree@ltbl@@UAEXPAVQuadtreeOccupant@2@@Z)"
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "public: void __thiscall ltbl::LightSystem::create(class sf::Rect<float> const &,class sf::Vector2<unsigned int> const &,class sf::Texture const &,class sf::Shader &,class sf::Shader &)" (?create@LightSystem@ltbl@@QAEXABV?$Rect@M@sf@@ABV?$Vector2@I@4@ABVTexture@4@AAVShader@4@3@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "public: void __thiscall ltbl::LightSystem::render(class sf::View const &,class sf::Shader &,class sf::Shader &)" (?render@LightSystem@ltbl@@QAEXABVView@sf@@AAVShader@4@1@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "public: void __thiscall ltbl::LightSystem::addShape(class std::shared_ptr<class ltbl::LightShape> const &)" (?addShape@LightSystem@ltbl@@QAEXABV?$shared_ptr@VLightShape@ltbl@@@std@@@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>MAIN.obj : error LNK2019: &#1089;&#1089;&#1099;&#1083;&#1082;&#1072; &#1085;&#1072; &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1081; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1081; &#1089;&#1080;&#1084;&#1074;&#1086;&#1083; "public: void __thiscall ltbl::LightSystem::addLight(class std::shared_ptr<class ltbl::LightPointEmission> const &)" (?addLight@LightSystem@ltbl@@QAEXABV?$shared_ptr@VLightPointEmission@ltbl@@@std@@@Z) &#1074; &#1092;&#1091;&#1085;&#1082;&#1094;&#1080;&#1080; _main
1>C:\000\LIGHT777\Debug\LIGHT777.exe : fatal error LNK1120: &#1085;&#1077;&#1088;&#1072;&#1079;&#1088;&#1077;&#1096;&#1077;&#1085;&#1085;&#1099;&#1093; &#1074;&#1085;&#1077;&#1096;&#1085;&#1080;&#1093; &#1101;&#1083;&#1077;&#1084;&#1077;&#1085;&#1090;&#1086;&#1074;: 8
========== &#1057;&#1073;&#1086;&#1088;&#1082;&#1072;: &#1091;&#1089;&#1087;&#1077;&#1096;&#1085;&#1086;: 0, &#1089; &#1086;&#1096;&#1080;&#1073;&#1082;&#1072;&#1084;&#1080;: 1, &#1073;&#1077;&#1079; &#1080;&#1079;&#1084;&#1077;&#1085;&#1077;&#1085;&#1080;&#1081;: 0, &#1087;&#1088;&#1086;&#1087;&#1091;&#1097;&#1077;&#1085;&#1086;: 0 ==========
 
« Last Edit: June 16, 2018, 05:21:08 pm by adidas »

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: SFML dynamic color tile-based lighting
« Reply #6 on: June 16, 2018, 05:35:57 pm »
I don't think that your message related to this topic

 

anything