Fillwave  10.0.0
Engine.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <flw/Config.h>
25 
26 #include <flw/flf/management/TextureSystem.h>
27 
28 #include <flw/cmn/Aliases.h>
29 
30 #include <flw/flf/space/Scene.h>
31 
32 #include <flw/cmn/Macros.h>
33 
34 #include <flw/flf/actions/Event.h>
35 
36 #include <flw/flf/space/LightPoint.h>
37 #include <flw/flf/space/LightSpot.h>
38 #include <flw/flf/space/LightDirectional.h>
39 #include <flw/flf/hud/Text.h>
40 
41 #include <flw/flf/actions/EventHandler.h>
42 
43 #include <flw/cmn/resources/FileLoader.h>
44 #include <flw/flf/loaders/ModelLoader.h>
45 #include <flw/flf/loaders/ProgramLoader.h>
46 
47 #include <flw/flf/management/CacheBuffer.h>
48 #include <flw/flf/management/CacheProgram.h>
49 #include <flw/flf/management/Cachehader.h>
50 #include <flw/flf/management/CacheSampler.h>
51 
52 #include <flw/flf/management/LightSystem.h>
53 
54 #include <flw/flf/models/MeshOcclusion.h>
55 
56 #include <flw/flc/operations/PostProcessingPass.h>
57 
58 #include <flw/Debugger.h>
59 
60 #if defined(FILLWAVE_BACKEND_OPENGL_ES_20)
61 #else
62 #include <flw/flc/extended/buffers/PixelBuffer.h>
63 #include <flw/flc/extended/pipeline/Fence.h>
64 #endif
65 
66 namespace flw {
67 namespace flf {
68 class Focusable;
69 }
70 namespace flc {
71 class IndexBuffer;
72 struct VertexParticleGPU;
73 struct VertexPosition;
74 struct VertexFloat;
75 class VertexBufferParticles;
76 class VertexBufferParticlesGPU;
77 class VertexBufferFloat;
78 class VertexBufferPosition;
79 }
80 
85 class Engine {
86  public:
87  Engine(const std::string& runtimeBinaryFilePath, bool isEveryExtensionSuccessfullyLoaded);
88 
89  virtual ~Engine();
90 
91  /* Configuration */
92  void configDebugger(EDebuggerState state);
93  void configFileLogging(std::string fileName = "");
94  void configFPSCounter(std::string fontName = "", glm::vec2 position = glm::vec2(-0.95, 0.95), GLfloat size = 100.0);
95  void configBackgroundColor(glm::vec3 color);
96  void configTime(GLfloat timeFactor);
97 
98  /* Draw */
99  void draw();
100  void drawLines();
101  void drawPoints();
102  void drawTexture(flc::Texture2D* t, flc::Program* p);
103  void drawTexture(flc::Texture2D* t);
104 
105  /* Remove */
106  void detach(ps<flf::Text> text);
107  void detach(flf::LightSpot* light);
108  void detach(flf::LightDirectional* light);
109  void detach(flf::LightPoint* light);
110  void detachLights();
111  void detach(flf::Entity*);
112 
113  /* Assets */
114  void getPhysicalMeshBuffer(const std::string& path, flf::PhysicsMeshBuffer& buffer);
115  const std::string getGlobalPath(const std::string& localPath);
116 
117  /* Scene */
118  void setCurrentScene(pu<flf::Scene> &&scene);
119  pn<flf::Scene> getCurrentScene() const;
120 
121  /* Time */
122  GLuint getAndResetRenderedFramesCount();
123 
124  template <GLuint T> flc::Shader* storeShader(const std::string& shaderPath);
125  template <GLuint T> flc::Shader* storeShader(const std::string& shaderPath, const std::string& shaderSource);
126  flc::Program* storeProgram(const std::string &name, const vec<flc::Shader *> &shaders, bool isSkipLinking = false);
127 
128  /* Store textures */
129  flc::Texture2D* storeTexture(const std::string &path);
130 
131  flc::Texture2DRenderable* storeTextureRenderable();
132 
133  flc::Texture2DRenderableDynamic* storeTextureDynamic(const std::string &fragmentShaderPath);
134 
135  /* Store lights */
136  flf::LightSpot* storeLightSpot(glm::vec3 pos, glm::quat rot, glm::vec4 col, Moveable *followed = nullptr);
137  flf::LightPoint* storeLightPoint(glm::vec3 position, glm::vec4 color, Moveable *followed = nullptr);
138  flf::LightDirectional* storeLightDirectional(glm::vec3 pos, glm::quat rot, glm::vec4 col, Moveable* m = nullptr);
139 
140  /* Store text */
141  ps<flf::Text> storeText(
142  const std::string &text
143  , const std::string &font
144  , glm::vec2 pos
145  , GLfloat scale = 1.0f
146  , glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)
147  , ETextEffect effect = ETextEffect::none);
148 
149  /* Store vertex array objects */
150  flc::VertexArray* storeVAO(flf::IReloadable *user, flc::VertexArray *vao = nullptr);
151 
152  /* Store one buffer for one VAO */
153  template <class T, typename ...S>
154  T* storeBuffer(flc::VertexArray *vao, S ... p) {
155  return storeBufferInternal(vao, p...);
156  }
157 
158  /* Enables to store many buffers in one VAO */
159  template <class T, typename ...S>
160  T* storeBuffers(flc::VertexArray *vao, size_t idx, S ... p) {
161  return storeBuffersInternal(vao, idx, p...);
162  }
163 
164  /* Buffering */
165  void removeBuffer(flc::VertexArray* vao);
166  void removeBufferIndex(flc::VertexArray* vao);
167  void removeBufferIndexParticles(flc::VertexArray* vao);
168  void removeBufferBasic(flc::VertexArray* vao);
169  void removeBufferText(flc::VertexArray* vao);
170 
171  /* Screenshot */
172  void captureFramebufferToFile(const std::string &name);
173  void captureFramebufferToBuffer(GLubyte *buf, GLint *sizeInBytes, GLuint format = GL_RGBA, GLint bytesPerPixel = 4);
174 
175  /* Systems */
176  flf::LightSystem& getLightSystem() const;
177  flf::TextureSystem& getTextureSystem() const;
178 
179  /* Screen */
180  glm::ivec2 getScreenSize() const;
181  GLfloat getScreenAspectRatio() const;
182 
183  /* Log */
184  void log();
185 
186  /* Post processing */
187  void addPostProcess(const std::string &fragmentShaderPath, GLfloat lifeTime = flf::FILLWAVE_ENDLESS);
188  void reload();
189 
190  /* Screen */
191  GLuint mWindowWidth;
192  GLuint mWindowHeight;
193  GLfloat mWindowAspectRatio;
194 
195  /* Loaders */
196  flf::FontLoader mFontLoader;
197  flf::FileLoader mFileLoader;
198  flf::ProgramLoader mProgramLoader;
199  ModelLoader mModelLoader;
200 
201  /* Picking */
202  flc::Texture2DRenderable* mPickingRenderableTexture;
203 #if defined(FILLWAVE_BACKEND_OPENGL_ES_20)
204 #else
205  pu<flc::PixelBuffer> mPickingPixelBuffer;
206 #endif
207 
208  /* Resources */
209  flf::CacheShader mShaders;
210  flf::CacheProgram mPrograms;
211 #if defined(FILLWAVE_BACKEND_OPENGL_ES_20)
212 #else
213  flf::CacheSampler mSamplers;
214 #endif
215  flf::CacheBuffer mBuffers;
216  pu<flf::TextureSystem> mTextures;
217  pu<flf::LightSystem> mLights;
218  vec<flc::PostProcessingPass> mPostProcessingPasses;
219  flc::Program* mProgramTextureLookup;
220  vec<ps<Font>> mFontManager;
221  vec<ps<flf::Text>> mTextManager;
222 
223 #if defined(FILLWAVE_BACKEND_OPENGL_ES_20)
224 #else
225  pu<flc::Fence> mFence;
226 #endif
227 
228  /* OQ */
229  pu<flf::MeshOcclusion> mOcclusion;
230 
231  /* Input handlers */
232  vec<flf::EventHandler> mHandlers;
233 
234  pu<flf::Scene> mScene;
235 
236  /* Extras */
237  pu<flf::Debugger> mDebugger;
238  GLuint mFrameCounter;
239  GLfloat mTimeFactor;
240  ps<flf::Text> mFPSText;
241 
242  bool mIsOQ;
243 
244  glm::vec3 mBackgroundColor;
245 
246  public:
247  void onEvent(const flf::Event& event);
248  void onResizeScreen(GLuint width, GLuint height);
249  void onPick(GLuint xScreenSpace, GLuint yScreenSpace);
250  void attachHandler(std::function<void(const flf::Event&)>&& h, flf::EEventType type);
251  void detachHandlers();
252 
253  private:
254  void init();
255  void initContext();
256 
257 #if defined(FILLWAVE_BACKEND_OPENGL_ES_20)
258 #else
259  public:
260  flc::Texture3D* storeTexture3D(
261  const std::string &posX
262  , const std::string &negX
263  , const std::string &posY
264  , const std::string &negY
265  , const std::string &posZ
266  , const std::string &negZ);
267 
268  flc::Sampler* storeSO(GLint textureUnit);
269 
270  private:
271  void initPickingBuffer();
272 #endif
273 
274  void initPipelines();
275  void initUniforms();
276  void initManagement();
277  void initExtras();
278  void initOcclusionTest();
279 
280  void populateLights();
281  void populateDebugger();
282  void populateDynamicTextures(GLfloat timeExpiredInSeconds);
283 
284  void drawFront();
285  void drawOcclusionPass();
286 
287  /* IRenderer */
288  void drawClear();
289  void drawHUD();
290  void drawSceneStartup();
291  void drawScene();
292  void updatePostprocessing(GLfloat time);
293  void drawSceneCore();
294 
296  storeBufferInternal(
297  flc::VertexArray* vao
298  , std::function<float(float x, float y)> constructor
299  , GLint density
300  , GLfloat gap
301  , vec<GLuint> &indices);
302 
304  storeBufferInternal(
305  flc::VertexArray *vao
306  , vec<flc::VertexBasic> &data);
307 
309  storeBufferInternal(
310  flc::VertexArray *vao
311  , const vec<GLfloat> &data
312  , const vec<GLfloat> &coords);
313 
315  storeBuffersInternal(
316  flc::VertexArray *vao
317  , size_t idx
318  , vec<flc::VertexParticleGPU> &particles);
319 
321  storeBufferInternal(
322  flc::VertexArray *vao
323  , vec<GLfloat> &vel
324  , vec<GLfloat> &pos
325  , vec<GLfloat> &tim);
326 
327  flc::VertexBufferBasic* storeBufferInternal(
328  flc::VertexArray* vao
329  , const ModelLoader::ShapeType* shape
330  , ModelLoader::Animator* animator);
331 
332  flc::VertexBufferDebug* storeBufferInternal(flc::VertexArray *vao, float);
333  flc::VertexBufferFloat* storeBufferInternal(flc::VertexArray *vao, vec<flc::VertexFloat> &data);
334  flc::VertexBufferPosition* storeBufferInternal(flc::VertexArray *vao, vec<flc::VertexPosition> &data);
335  flc::IndexBuffer* storeBufferInternal(flc::VertexArray *vao, const ModelLoader::ShapeType* shape);
336  flc::IndexBuffer* storeBufferInternal(flc::VertexArray *vao, const vec<GLuint> &data);
337  flc::IndexBuffer* storeBufferInternal(flc::VertexArray *vao, GLuint elements);
338 
339  private:
340  /* Pick */
341  glm::ivec4 pickingBufferGetColor(GLubyte* data, GLuint x, GLuint y);
342  void reloadPickingBuffer();
343 
344 };
345 
346 } /* flw */
Definition: Aliases.h:30
Encapsulates reloadable objects.
Definition: IReloadable.h:35
Manager to handle TextureObject1D, TextureObject2D and TextureObject3D objects.
Definition: TextureSystem.h:38
Light implementing directional torch.
Definition: LightSpot.h:34
Single GLSL 2D Texture object.
Definition: Texture2D.h:36
Loads programs.
Definition: ProgramLoader.h:37
Vertex buffer specialized with VertexText data structure.
Definition: mVertexBufferText.h:42
Vertex buffer specialized with VertexParticle data structure.
Definition: mVertexBufferParticles.h:43
Not used. Vertex buffer specialized with VertexPosition data structure.
Definition: mVertexBufferPosition.h:41
Fillwave engine core.
Definition: Engine.h:85
Base for all Scene nodes.
Definition: Entity.h:48
Event.
Definition: Event.h:154
Dynamic texture will update its content during runtime according to specified fragment shader...
Definition: Texture2DRenderableDynamic.h:34
SamplerObject - SO.
Definition: Sampler.h:33
Basic manager of composites.
Definition: TCache.h:44
Loads files.
Definition: FileLoader.h:34
Vertex buffer specialized with VertexParticleGPU data structure.
Definition: VertexBufferParticlesGPU.h:49
VertexArrayObject - VAO.
Definition: VertexArray.h:33
IndexBufferObject - IBO.
Definition: IndexBuffer.h:35
Light with Orthographic projection.
Definition: LightDirectional.h:42
Physical mesh data.
Definition: PhysicsMeshBuffer.h:33
Base for every object which has a 3D position.
Definition: Moveable.h:43
Light system knows about all light related stuff.
Definition: LightSystem.h:40
Single GLSL 3D Texture object. It consists of six 2D images.
Definition: Texture3D.h:34
Loads fonts fromttf files.
Definition: FontLoader.h:33
Vertex buffer specialized with VertexDebug data structure.
Definition: mVertexBufferDebug.h:42
Single GLSL program object.
Definition: Program.h:37
Single GLSL shader object.
Definition: Shader.h:44
Vertex buffer specialized with VertexBasic data structure.
Definition: mVertexBufferBasic.h:61
Not used.
Definition: LightPoint.h:37
Connects VAO pointer and VAO&#39;s user pointer in single class.
Definition: CacheBuffer.h:39
One can render to this texture and use the rendered image as a 2D texture.
Definition: Texture2DRenderable.h:34
Vertex buffer specialized with GLfloat data structure.
Definition: mVertexBufferFloat.h:41