Gamedev Framework (gf)  0.20.0
A C++17 framework for 2D games
LightSystem.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_LIGHT_SYSTEM_H
22 #define GF_LIGHT_SYSTEM_H
23 
24 #include <cstdint>
25 #include <vector>
26 
27 #include "Drawable.h"
28 #include "GraphicsApi.h"
29 #include "LightPointEmission.h"
30 #include "LightShape.h"
31 #include "Polygon.h"
32 #include "Ref.h"
33 #include "RenderTexture.h"
34 #include "Shader.h"
35 #include "Shapes.h"
36 #include "Texture.h"
37 #include "Vector.h"
38 #include "View.h"
39 
40 namespace gf {
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 inline namespace v1 {
43 #endif
44  struct RenderStates;
45  class Texture;
46 
53  enum class LightType : uint32_t {
54  Shape,
55  Point,
56  Direction,
57  };
58 
65  struct GF_GRAPHICS_API LightId {
67  uint32_t index;
68  };
69 
74  class GF_GRAPHICS_API LightSystem : public Drawable {
75  public:
76  LightSystem(Vector2i size);
77 
78  void setAmbientColor(const Color4f& color) {
79  m_ambientColor = color;
80  }
81 
82  LightId addLightShape(LightShape& shape);
83  LightId addLightPoint(LightPointEmission& light);
84 
85 // LightId addLightDirection(LightDirectionEmission& light);
86 
87  void removeLight(LightId id);
88 
89  void draw(RenderTarget& target, const RenderStates& states) override;
90 
91  void dump();
92 
93  private:
94 
95  struct LightDirection {
96 
97  bool active = true;
98  };
99 
100  private:
101  void resize(Vector2i size);
102 
103  void renderLightPointEmission(LightPointEmission& light, const View& view);
104 
105  void dumpTextures();
106 
107  private:
108  std::vector<Ref<LightShape>> m_shapes;
109  std::vector<Ref<LightPointEmission>> m_points;
110  std::vector<Ref<LightDirection>> m_directions;
111 
112  Color4f m_ambientColor;
113 
114  Texture m_penumbraTexture;
115  Shader m_unshadowShader;
116  Shader m_lightOverShapeShader;
117 
118  Vector2i m_size;
119  View m_view;
120  RenderTexture m_lightTexture;
121  RenderTexture m_emissionTexture;
122  RenderTexture m_antumbraTexture;
123  RenderTexture m_compositionTexture;
124 
125  bool m_dump = false;
126  };
127 
128 #ifndef DOXYGEN_SHOULD_SKIP_THIS
129 }
130 #endif
131 }
132 
133 #endif // GF_LIGHT_SYSTEM_H
2D camera that defines what region is shown on framebuffer
Definition: View.h:94
Target for off-screen 2D rendering into a texture.
Definition: RenderTexture.h:98
Light shape.
Definition: LightShape.h:54
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setAmbientColor(const Color4f &color)
Definition: LightSystem.h:78
A light system.
Definition: LightSystem.h:74
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:57
uint32_t index
Definition: LightSystem.h:67
An OpenGL vertex and/or fragment shader.
Definition: Shader.h:119
LightType
The type of light.
Definition: LightSystem.h:53
A texture for colored images.
Definition: Texture.h:313
The id of a light.
Definition: LightSystem.h:65
Light point emission.
Definition: LightPointEmission.h:39
The namespace for gf classes.
Definition: Action.h:35
A 4D vector.
Definition: Vector.h:852
LightType type
Definition: LightSystem.h:66