SuperTuxKart
sp_base.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2018 SuperTuxKart-Team
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 #ifndef HEADER_SP_BASE_HPP
19 #define HEADER_SP_BASE_HPP
20 
21 #include "graphics/gl_headers.hpp"
22 #include "utils/constants.hpp"
23 #include "utils/no_copy.hpp"
24 
25 #include "irrMath.h"
26 #include "vector3d.h"
27 
28 #include <array>
29 #include <atomic>
30 #include <cmath>
31 #include <functional>
32 #include <ostream>
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 namespace irr
38 {
39  namespace scene { class ICameraSceneNode; class IMesh; }
40  namespace video { class SColor; }
41 }
42 
44 
45 namespace SP
46 {
47 class SPMesh;
48 
49 enum DrawCallType: unsigned int
50 {
51  DCT_NORMAL = 0,
52  DCT_SHADOW1,
53  DCT_SHADOW2,
54  DCT_SHADOW3,
55  DCT_SHADOW4,
56  DCT_TRANSPARENT,
57  DCT_FOR_VAO
58 };
59 
60 inline std::ostream& operator<<(std::ostream& os, const DrawCallType& dct)
61 {
62  switch (dct)
63  {
64  case DCT_NORMAL:
65  return os << "normal";
66  case DCT_TRANSPARENT:
67  return os << "transparent";
68  case DCT_SHADOW1:
69  return os << "shadow cam 1";
70  case DCT_SHADOW2:
71  return os << "shadow cam 2";
72  case DCT_SHADOW3:
73  return os << "shadow cam 3";
74  case DCT_SHADOW4:
75  return os << "shadow cam 4";
76  default:
77  return os;
78  }
79 }
80 
81 enum SamplerType: unsigned int;
82 enum RenderPass: unsigned int;
83 class SPDynamicDrawCall;
84 class SPMaterial;
85 class SPMeshNode;
86 class SPShader;
87 class SPMeshBuffer;
88 
89 extern GLuint sp_mat_ubo[MAX_PLAYER_COUNT][3];
90 extern GLuint sp_fog_ubo;
91 extern std::array<GLuint, 1> sp_prefilled_tex;
92 extern std::atomic<uint32_t> sp_max_texture_size;
93 extern unsigned sp_solid_poly_count;
94 extern unsigned sp_shadow_poly_count;
95 extern int sp_cur_shadow_cascade;
96 extern bool sp_culling;
97 extern bool sp_debug_view;
98 extern bool sp_apitrace;
99 extern unsigned sp_cur_player;
100 extern unsigned sp_cur_buf_id[MAX_PLAYER_COUNT];
101 extern irr::core::vector3df sp_wind_dir;
102 // ----------------------------------------------------------------------------
103 void init();
104 // ----------------------------------------------------------------------------
105 void destroy();
106 // ----------------------------------------------------------------------------
107 GLuint getSampler(SamplerType);
108 // ----------------------------------------------------------------------------
109 SPShader* getNormalVisualizer();
110 // ----------------------------------------------------------------------------
111 SPShader* getGlowShader();
112 // ----------------------------------------------------------------------------
113 bool skinningUseTBO();
114 // ----------------------------------------------------------------------------
115 void prepareDrawCalls();
116 // ----------------------------------------------------------------------------
117 void draw(RenderPass, DrawCallType dct = DCT_NORMAL);
118 // ----------------------------------------------------------------------------
119 void drawGlow();
120 // ----------------------------------------------------------------------------
121 void drawSPDebugView();
122 // ----------------------------------------------------------------------------
123 void addObject(SPMeshNode*);
124 // ----------------------------------------------------------------------------
125 void initSTKRenderer(ShaderBasedRenderer*);
126 // ----------------------------------------------------------------------------
127 void initSamplers();
128 // ----------------------------------------------------------------------------
129 void prepareScene();
130 // ----------------------------------------------------------------------------
131 void handleDynamicDrawCall();
132 // ----------------------------------------------------------------------------
133 void addDynamicDrawCall(std::shared_ptr<SPDynamicDrawCall>);
134 // ----------------------------------------------------------------------------
135 void updateModelMatrix();
136 // ----------------------------------------------------------------------------
137 void uploadAll();
138 // ----------------------------------------------------------------------------
139 void resetEmptyFogColor();
140 // ----------------------------------------------------------------------------
141 void drawBoundingBoxes();
142 // ----------------------------------------------------------------------------
143 void loadShaders();
144 // ----------------------------------------------------------------------------
145 SPMesh* convertEVTStandard(irr::scene::IMesh* mesh,
146  const irr::video::SColor* color = NULL);
147 // ----------------------------------------------------------------------------
148 void uploadSPM(irr::scene::IMesh* mesh);
149 // ----------------------------------------------------------------------------
150 #ifdef SERVER_ONLY
151 inline void setMaxTextureSize() {}
152 #else
153 void setMaxTextureSize();
154 #endif
155 // ----------------------------------------------------------------------------
156 inline void unsetMaxTextureSize() { sp_max_texture_size.store(2048); }
157 // ----------------------------------------------------------------------------
158 inline uint8_t srgbToLinear(float color_srgb)
159 {
160  int ret;
161  if (color_srgb <= 0.04045f)
162  {
163  ret = (int)(255.0f * (color_srgb / 12.92f));
164  }
165  else
166  {
167  ret = (int)(255.0f * (powf((color_srgb + 0.055f) / 1.055f, 2.4f)));
168  }
169  return uint8_t(irr::core::clamp(ret, 0, 255));
170 }
171 // ----------------------------------------------------------------------------
172 inline uint8_t linearToSrgb(float color_linear)
173 {
174  if (color_linear <= 0.0031308f)
175  {
176  color_linear = color_linear * 12.92f;
177  }
178  else
179  {
180  color_linear = 1.055f * powf(color_linear, 1.0f / 2.4f) - 0.055f;
181  }
182  return uint8_t(irr::core::clamp(int(color_linear * 255.0f), 0, 255));
183 }
184 // ----------------------------------------------------------------------------
185 ShaderBasedRenderer* getRenderer();
186 }
187 
188 
189 #endif
Definition: shader_based_renderer.hpp:49