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

Author Topic: Let There Be Light 2  (Read 147221 times)

0 Members and 2 Guests are viewing this topic.

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: Let There Be Light 2
« Reply #60 on: July 15, 2015, 03:57:51 pm »
Hey lolz123, nice demo! I would like to use this for my game, I was able to compile and add to my project but I'm having some troubles:

This is my code for the creation of a light (to test):

Quote
   // lights setup
   sf::Shader unshadowShader;
   sf::Shader lightOverShapeShader;
   unshadowShader.loadFromFile("resources/unshadowShader.vert", "resources/unshadowShader.frag");
   lightOverShapeShader.loadFromFile("resources/lightOverShapeShader.vert", "resources/lightOverShapeShader.frag");
   sf::Texture penumbraTexture;
   penumbraTexture.loadFromFile("resources/penumbraTexture.png");
   penumbraTexture.setSmooth(true);

   ltbl::LightSystem ls;
   ls.create(sf::FloatRect(-1000.0f, -1000.0f, 1000.0f, 1000.0f), Game::GetWindow().getSize(), penumbraTexture, unshadowShader, lightOverShapeShader);

   std::shared_ptr<ltbl::LightDirectionEmission> light = std::make_shared<ltbl::LightDirectionEmission>();

   light->_emissionSprite.setTexture(penumbraTexture);
   light->_castDirection = sf::Vector2f(25.f, 25.f);

   ls.addLight(light);

That's all I do and the light won't show, am I missing something? Maybe a call to draw every frame to show the light?

I followed your quick start (https://github.com/222464/LTBL2), I don't understand which texture I have to use in this line "light->_emissionSprite.setTexture(penumbraTexture);" one of your textures?

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Let There Be Light 2
« Reply #61 on: July 16, 2015, 02:42:52 pm »
The textures are also in the github repository, where you found the quick start guide.

https://github.com/222464/LTBL2/tree/master/LTBL2/resources
« Last Edit: July 21, 2015, 08:49:12 am by Ghosa »

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: Let There Be Light 2
« Reply #62 on: July 21, 2015, 04:22:42 am »
Thanks Glosa, I'm using those textures but still not showing any light.

Lolz, it would be nice to see the source code of the demo so we can see how exactly to use this. I'm creating light and then drawing it with Nrender but nothing happens  :-\

Cmake generated without errors, the sln compiled without problems and this is my code:

Sector.h:
Code: [Select]
...

#include <ltbl/lighting/LightSystem.h>

class Sector
{
public:
Sector(int mapNumber, int sectorNumber);
~Sector();
        ...

private:
...

ltbl::LightSystem ls;
sf::Shader unshadowShader;
sf::Shader lightOverShapeShader;
};

Sector.cpp (Constructor):
Code: [Select]
Sector::Sector(int mapNumber, int sectorNumber)
: static_objects_counter(0), enemies_counter(0), pickup_counter(0), _name(NumberToStr(sectorNumber))
{
belongsToMapNumber = mapNumber;

enemies.clear();
enemiesToRemove.clear();

unshadowShader.loadFromFile("resources/unshadowShader.vert", "resources/unshadowShader.frag");
lightOverShapeShader.loadFromFile("resources/lightOverShapeShader.vert", "resources/lightOverShapeShader.frag");

sf::Texture penumbraTexture;
penumbraTexture.loadFromFile("resources/penumbraTexture.png");
penumbraTexture.setSmooth(true);

ls.create(sf::FloatRect(-1000.0f, -1000.0f, 1000.0f, 1000.0f), Game::GetWindow().getSize(), penumbraTexture, unshadowShader, lightOverShapeShader);

// Below is an example for creating one point light and one directional light:
std::shared_ptr<ltbl::LightPointEmission> plight = std::make_shared<ltbl::LightPointEmission>();

sf::Texture pointLightTexture;
pointLightTexture.loadFromFile("resources/pointLightTexture.png");
pointLightTexture.setSmooth(true);

plight->_emissionSprite.setOrigin(sf::Vector2f(0.f, 0.f));
plight->_emissionSprite.setTexture(pointLightTexture);
plight->_emissionSprite.setColor(sf::Color::Blue);
plight->_emissionSprite.setPosition(sf::Vector2f(100, 100));
plight->_localCastCenter = sf::Vector2f(0.f, 0.f); // This is where the shadows emanate from relative to the sprite

ls.addLight(plight);

std::shared_ptr<ltbl::LightDirectionEmission> dlight = std::make_shared<ltbl::LightDirectionEmission>();

sf::Texture directionLightTexture;
directionLightTexture.loadFromFile("resources/directionLightTexture.png");
directionLightTexture.setSmooth(true);

dlight->_emissionSprite.setTexture(directionLightTexture);
dlight->_castDirection = sf::Vector2f(10.f, 10.f);

ls.addLight(dlight);
}

Sector.cpp (update):
Code: [Select]
void Sector::update(sf::Time timeElapsedSinceLastUpdate)
{
        ...

ls.render(Game::GetWindow().getView(), unshadowShader, lightOverShapeShader);
}

Textures and shaders load fine (I checked), update is called, render is called, I'm not drawing the map over the lights, so I don't know what is the problem, any ideas?
« Last Edit: July 22, 2015, 01:09:51 am by Guido Bisocoli »

Sayuri

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Let There Be Light 2
« Reply #63 on: July 28, 2015, 05:03:53 am »
Thank you sooooo much for this project, it's really great!

I have a few questions, though:
To keep track of which lights and occluders belong to which object i not only use the "lightsystem.addLight(<light>)" to put a shared_ptr into the lightsystem but i also put shared_ptr of that same light in certain objects. For example for the flashlight of my player i currently have it like that:

Code: [Select]
class Player {
    std::shared_ptr<ltbl::LightPointEmission> flashlight;
}


And when creating the light i put a shared_ptr in both the LTBL2 lightsystem object aswell as my player flashlight pointer, like that:

Code: [Select]
Player::Player() {
    this->flashlight = std::make_shared<ltbl::LightPointEmission>();
    ...
    this->game->getLightsystem().addLight(light);
}

Effectively both the player and the light system have now each a shared_ptr that point to the light object.
The problem is now: how do i actually delete/deactivate/turn off the lights (for example turn off a flashlight or turn off a light in a room)? For example if i want to switch the flashlight off with a toggleFlashlight method, how would i do that?
This doesn't work (in Player.cpp):

Code: [Select]
void Player::toggleFlashlight() {
    this->game->getLightsystem().removeLight(this->flashlight);
}

because the shared_ptr from the player and the shared_ptr in the lightsystem are different, and the "removeLight()" method, as far as i can understand it, only checks for equality of the pointer (which isn't true, since the flashlight pointer of the player and the flashlight pointer in the light system have different adresses), not the actual light object that these pointers point to.

Do you have any suggestions how one could make lights that can be toggled on/off, and suggestions how to make object keep track of lights which belong to them while also being able to remove them at any time?
The only quick, but probably not smart, solution i found right now while writing this would be to set the scale to "0 ,0"
Code: [Select]
this->flashlight->_emissionSprite.setScale(0,0);This causes the light sprite to be too small to be rendered, virtually making it invisible and thus "turn off", and then, when "turning the light back on", i would turn the scale back to what it was.
But that seems kinda silly, since then i'd have to keep track of the original sprite scale and still have many lights that still need to be calculated, just with a (0,0) scale.

Another wierd alternative would be, instead of turning the scale to (0,0), to set the sprite color to sf::Color::Transparent, in order to make the sprite invisible, and then sf::Color::White to make the light visible again.

Do you have any suggestions which one to choose, or even alternatives for turning lights on/off?


In addition to that, may i suggest an "setActive(bool)" method, similar to Box2Ds system, for lights and occluders?
In Box2d you can set bodies to active or inactive, and if they're set to "setActive(false)" then these bodies won't be used for almost any calculation.
Could maybe something similar be done with LTBL2, where we can toggle lights and occluders on and off via "setActive(bool)" method, and inactive lights won't be drawn/inactive occluders won't make any shadows?
This could give us more control over the lights while also possibly saving performance by ignoring inactive lights/occluders in the render/quadtree calculations?

This is a really great project.
Again, thank you so much for creating it! I hope you will continue working on it.

Targa

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Let There Be Light 2
« Reply #64 on: July 29, 2015, 12:41:41 am »
Dear,

First of all, thank you so much for sharing your code and hard work!
I would like to ask a question though, which has been asked before.

I do everything stated in the documentation, but no light seems to appear when I run my application.

I have an easy test project setup so I'll post the code I'm using at the moment.

This is the initialization of everything.
(click to show/hide)
The lights should be rendered here:
(click to show/hide)

I am looking forward to your answer!
Thanks in advance.

lolz123

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
Re: Let There Be Light 2
« Reply #65 on: August 01, 2015, 02:33:39 pm »
Hello,

Calling ls.render only renders the lights to an internal texture. You must then render the internal texture after calling ls.render. You can do this with ls.getLightingTexture. Make sure that the blending is set to multiplicative before you draw it on top of your scene!

Have you heard about the new Cray super computer?  It’s so fast, it executes an infinite loop in 6 seconds.

DJuego

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Let There Be Light 2
« Reply #66 on: August 01, 2015, 09:53:38 pm »
lolz123 Happy to read you this way again!

Your work has been VERY useful for me. SFML, Thor and LTBL2 are my Holy Trinity  ;D

Frankly, I am eager about new things in the LTBL2 project. ;D Please! please! please!.

A humble and very briefly explained tribute:

A multimedia application: Virtual ants in a virtual environment expel a pheromone trail in order that robotic ants follow it in a physic environment.

The virtual environment (tcp/ip-and-bluetooth server view) is a representation of physic environment  The multiple ants are controlled by human "players" in a local network. LTBL2 plays an important role here. Thanks again!



The physic environment (global view). The robotic ants request info (bluetooth) to server about the amount of pheromone in a ground cell. Pheromone weakens with time. The pheromone only is visible in the virtual environment. In fact it was implemented with the particle system of Thor (Thanks Nexus;D).



The physic environment with the ant hole in foreground. Multiple robotic ants transport food to the anthill simultaneously  (stigmergic behavior).



Sorry for my English!

[And of course MANY thanks to the SFML Team  :-* but this was a tribute for lolz123  :P]

DJuego

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: Let There Be Light 2
« Reply #67 on: August 05, 2015, 12:07:38 am »
Hello,

Calling ls.render only renders the lights to an internal texture. You must then render the internal texture after calling ls.render. You can do this with ls.getLightingTexture. Make sure that the blending is set to multiplicative before you draw it on top of your scene!

Hey Lolz, thanks for the answer, I've been able to render lights like this:

Code: [Select]
lightSystem.render(Game::GetWindow().getView(), unshadowShader, lightOverShapeShader);

lightsSprite.setTexture(lightSystem.getLightingTexture());
Game::GetWindow().draw(lightsSprite, sf::BlendMultiply);

and works good except it doesn't update the view when I move my player (even when I'm passing it to the render()), you can see this on the right of the screen capture attached (don't mind the crappy graphics, they are temporary  :P).

Am I missing something?

mashedtatoes

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Let There Be Light 2
« Reply #68 on: August 15, 2015, 02:36:33 am »
Am I missing something?

You need to set the position of lightSprite when you move the camera.
lightSprite.setPosition(view.getCenter());
You will also need to set the sprite's origin so that the center of the sprite matches the center of he view.
lightSprite.setOrigin(view.getSize().x / 2.0f, view.getSize().y / 2.0f);
Do this before running the game.
« Last Edit: August 15, 2015, 02:39:02 am by mashedtatoes »

Cupcake26699

  • Guest
Re: Let There Be Light 2
« Reply #69 on: August 15, 2015, 11:56:25 am »
Hey can anyone help me figure out why my programs don't compile even after successfully building with CMake on Visual Studio 2013?

I do the whole CMake thing, it compiles just fine with these settings:

Where is the source code: C:/Users/Me/Desktop/Main/LTBL2-master/LTBL2
Where to build the binaries: C:/Users/Me/Desktop/Main/LTBL2-master/LTBL2
I add the SFML_ROOT entry and point it to C:/SFML/SFML-2.2.

I specify the generator as Visual Studio 12 2013, after configuring I hit generate and it completes with no errors at all.

Then I go into a Visual Studio project that I know works with SFML 2.2 as I test opening a window.

I add these to the properties:

> C/C++
    >General

            > Additional Include Directories: C:\Users\Me\Desktop\Main\LTBL2-master\LTBL2\source\

> Linker
    >General

            > Additional Library Directories: C:\Users\Me\Desktop\Main\LTBL2-master\LTBL2\source\

After all that I finally make paste this simple piece of code into the Main.cpp file:

#include <SFML/Graphics.hpp>
#include <ltbl/lighting/LightSystem.h>

int main()
{

        sf::RenderWindow window(sf::VideoMode(600, 600), "SFML works!");

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();
                window.display();
        }

        return 0;
}

And this code compiles just fine. However, if I add any type of ltbl object (such as ltbl::LightSystem) anywhere in the code, the compiler just gives me three LNK2001 errors.

How do I fix this?

« Last Edit: August 15, 2015, 11:59:03 am by Cupcake26699 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Let There Be Light 2
« Reply #70 on: August 15, 2015, 12:17:00 pm »
You set an additional library directory to the linker, so that it will know where to find the library, but you don't add the library itself (Linker > Input > Additional libraries).
Laurent Gomila - SFML developer

Cupcake26699

  • Guest
Re: Let There Be Light 2
« Reply #71 on: August 15, 2015, 12:41:09 pm »
Thanks. I think I understand what you mean Laurent, however I don't see how I'm meant to actually include the library itself. There isn't a "Additional Libraries" section in the that section of the properties either.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Let There Be Light 2
« Reply #72 on: August 15, 2015, 01:51:53 pm »
It was "Additional dependencies".
Laurent Gomila - SFML developer

Cupcake26699

  • Guest
Re: Let There Be Light 2
« Reply #73 on: August 15, 2015, 03:58:40 pm »
I still don't know what to do to fix the bug, cant find any files that I believe would make sense going into additional dependencies.

DJuego

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Let There Be Light 2
« Reply #74 on: August 15, 2015, 05:29:22 pm »
Quote
If you are using Visual Studio, you may have to set your startup project to the ERL project, and you may have to add the source files to the project.

Have you added the LTBL2 .cpp files to your project? Unfortunately there are not .lib files for LTBL2 in Visual Studio( :-[)

DJuego