CPass.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #include <sstream>
  2. #include "CPass.h"
  3. #include "WallpaperEngine/Render/CFBO.h"
  4. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h"
  5. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableFloat.h"
  6. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableInteger.h"
  7. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector2.h"
  8. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector3.h"
  9. #include "WallpaperEngine/Render/Shaders/Variables/CShaderVariableVector4.h"
  10. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h"
  11. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantFloat.h"
  12. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantInteger.h"
  13. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantVector4.h"
  14. using namespace WallpaperEngine::Core::Objects::Effects::Constants;
  15. using namespace WallpaperEngine::Render::Shaders::Variables;
  16. using namespace WallpaperEngine::Render::Objects::Effects;
  17. extern float g_Time;
  18. CPass::CPass (CMaterial* material, Core::Objects::Images::Materials::CPass* pass) :
  19. m_material (material),
  20. m_pass (pass)
  21. {
  22. this->setupTextures ();
  23. this->setupShaders ();
  24. this->setupShaderVariables ();
  25. }
  26. ITexture* CPass::resolveTexture (ITexture* expected, int index, ITexture* previous)
  27. {
  28. if (expected == nullptr)
  29. {
  30. auto it = this->m_fbos.find (index);
  31. if (it == this->m_fbos.end ())
  32. return nullptr;
  33. expected = (*it).second;
  34. }
  35. // first check in the binds and replace it if necessary
  36. auto it = this->m_material->getMaterial ()->getTextureBinds ().find (index);
  37. if (it == this->m_material->getMaterial ()->getTextureBinds ().end ())
  38. return expected;
  39. // a bind named "previous" is just another way of telling it to use whatever texture there was already
  40. if ((*it).second->getName () == "previous")
  41. if (previous == nullptr)
  42. return expected;
  43. else
  44. return previous;
  45. // the bind actually has a name, search the FBO in the effect and return it
  46. auto fbo = this->m_material->m_effect->findFBO ((*it).second->getName ());
  47. if (fbo == nullptr)
  48. return nullptr;
  49. return fbo;
  50. }
  51. void CPass::render (CFBO* drawTo, ITexture* input, GLuint position, GLuint texcoord, glm::mat4 projection)
  52. {
  53. // set the framebuffer we're drawing to
  54. glBindFramebuffer (GL_FRAMEBUFFER, drawTo->getFramebuffer ());
  55. // set proper viewport based on what we're drawing to
  56. glViewport (0, 0, drawTo->getRealWidth (), drawTo->getRealHeight ());
  57. if (drawTo != this->m_material->getImage ()->getScene ()->getFBO())
  58. glClear (GL_COLOR_BUFFER_BIT);
  59. // set texture blending
  60. if (this->m_pass->getBlendingMode () == "translucent")
  61. {
  62. glEnable (GL_BLEND);
  63. glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  64. }
  65. else if (this->m_pass->getBlendingMode () == "additive")
  66. {
  67. glEnable (GL_BLEND);
  68. glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
  69. }
  70. else if (this->m_pass->getBlendingMode () == "normal")
  71. {
  72. glEnable (GL_BLEND);
  73. glBlendFuncSeparate (GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);
  74. }
  75. else
  76. {
  77. glDisable (GL_BLEND);
  78. }
  79. // set depth testing
  80. if (this->m_pass->getDepthTest () == "disabled")
  81. {
  82. glDisable (GL_DEPTH_TEST);
  83. }
  84. else
  85. {
  86. glEnable (GL_DEPTH_TEST);
  87. }
  88. if (this->m_pass->getCullingMode () == "nocull")
  89. {
  90. glDisable (GL_CULL_FACE);
  91. }
  92. else
  93. {
  94. glEnable (GL_CULL_FACE);
  95. }
  96. if (this->m_pass->getDepthWrite () == "disabled")
  97. {
  98. glDepthMask (false);
  99. }
  100. else
  101. {
  102. glDepthMask (true);
  103. }
  104. // update variables used in the render process (like g_ModelViewProjectionMatrix)
  105. this->m_modelViewProjectionMatrix = projection;
  106. // update a_TexCoord and a_Position based on what to draw to
  107. // this should not be required once we do some prediction on rendering things
  108. // but for now should be enough
  109. this->a_TexCoord = texcoord;
  110. this->a_Position = position;
  111. // use the shader we have registered
  112. glUseProgram (this->m_programID);
  113. ITexture* texture = this->resolveTexture (input, 0, input);
  114. glActiveTexture (GL_TEXTURE0);
  115. glBindTexture (GL_TEXTURE_2D, texture->getTextureID ());
  116. int lastTextureIndex = 0;
  117. // first bind the textures to their sampler place
  118. {
  119. // set texture slots for the shader
  120. auto cur = this->m_textures.begin ();
  121. auto end = this->m_textures.end ();
  122. for (int index = 1; cur != end; cur ++, index ++)
  123. {
  124. texture = this->resolveTexture ((*cur), index, input);
  125. glActiveTexture (GL_TEXTURE0 + index);
  126. glBindTexture (GL_TEXTURE_2D, texture->getTextureID ());
  127. // increase the number of textures counter
  128. lastTextureIndex ++;
  129. }
  130. }
  131. {
  132. // load the extra textures needed (if any) from the shader
  133. auto cur = this->m_fragShader->getTextures ().begin ();
  134. auto end = this->m_fragShader->getTextures ().end ();
  135. for (; cur != end; cur ++)
  136. {
  137. if ((*cur).first <= lastTextureIndex)
  138. continue;
  139. texture = this->resolveTexture ((*cur).second, (*cur).first);
  140. // set the active texture index
  141. glActiveTexture (GL_TEXTURE0 + (*cur).first);
  142. // bind the correct texture here
  143. glBindTexture(GL_TEXTURE_2D, texture->getTextureID ());
  144. }
  145. }
  146. {
  147. // load the extra textures needed (if any) from the shader
  148. auto cur = this->m_vertShader->getTextures ().begin ();
  149. auto end = this->m_vertShader->getTextures ().end ();
  150. for (; cur != end; cur ++)
  151. {
  152. if ((*cur).first <= lastTextureIndex)
  153. continue;
  154. texture = this->resolveTexture ((*cur).second, (*cur).first);
  155. // set the active texture index
  156. glActiveTexture (GL_TEXTURE0 + (*cur).first);
  157. // bind the correct texture here
  158. glBindTexture(GL_TEXTURE_2D, texture->getTextureID ());
  159. }
  160. }
  161. // add uniforms
  162. {
  163. auto cur = this->m_uniforms.begin ();
  164. auto end = this->m_uniforms.end ();
  165. for (; cur != end; cur ++)
  166. {
  167. UniformEntry* entry = (*cur).second;
  168. switch (entry->type)
  169. {
  170. case Double:
  171. glUniform1d (entry->id, *reinterpret_cast <const double*> (entry->value));
  172. break;
  173. case Float:
  174. glUniform1f (entry->id, *reinterpret_cast <const float*> (entry->value));
  175. break;
  176. case Integer:
  177. glUniform1i (entry->id, *reinterpret_cast <const int*> (entry->value));
  178. break;
  179. case Vector4:
  180. glUniform4fv (entry->id, 1, glm::value_ptr (*reinterpret_cast <const glm::vec4*> (entry->value)));
  181. break;
  182. case Vector3:
  183. glUniform3fv (entry->id, 1, glm::value_ptr (*reinterpret_cast <const glm::vec3*> (entry->value)));
  184. break;
  185. case Vector2:
  186. glUniform2fv (entry->id, 1, glm::value_ptr (*reinterpret_cast <const glm::vec2*> (entry->value)));
  187. break;
  188. case Matrix4:
  189. glUniformMatrix4fv (entry->id, 1, GL_FALSE, glm::value_ptr (*reinterpret_cast <const glm::mat4*> (entry->value)));
  190. break;
  191. }
  192. }
  193. }
  194. if (this->g_Texture0Rotation != -1)
  195. {
  196. glUniform4f (this->g_Texture0Rotation, 1.0f, 0.0f, 1.0f, 0.0f);
  197. }
  198. if (this->g_Texture0Translation != -1)
  199. {
  200. glUniform2f (this->g_Texture0Translation, 0.0f, 0.0f);
  201. }
  202. {
  203. auto cur = this->m_attribs.begin ();
  204. auto end = this->m_attribs.end ();
  205. for (; cur != end; cur ++)
  206. {
  207. glEnableVertexAttribArray ((*cur)->id);
  208. glBindBuffer (GL_ARRAY_BUFFER, *(*cur)->value);
  209. glVertexAttribPointer ((*cur)->id, (*cur)->elements, (*cur)->type, GL_FALSE, 0, nullptr);
  210. }
  211. }
  212. // start actual rendering now
  213. glBindBuffer (GL_ARRAY_BUFFER, position);
  214. glDrawArrays (GL_TRIANGLES, 0, 6);
  215. // disable vertex attribs array
  216. {
  217. auto cur = this->m_attribs.begin ();
  218. auto end = this->m_attribs.end ();
  219. for (; cur != end; cur ++)
  220. glDisableVertexAttribArray ((*cur)->id);
  221. }
  222. }
  223. GLuint CPass::compileShader (Render::Shaders::Compiler* shader, GLuint type)
  224. {
  225. // reserve shaders in OpenGL
  226. GLuint shaderID = glCreateShader (type);
  227. // give shader's source code to OpenGL to be compiled
  228. const char* sourcePointer = shader->getCompiled ().c_str ();
  229. glShaderSource (shaderID, 1, &sourcePointer, nullptr);
  230. glCompileShader (shaderID);
  231. GLint result = GL_FALSE;
  232. int infoLogLength = 0;
  233. // ensure the vertex shader was correctly compiled
  234. glGetShaderiv (shaderID, GL_COMPILE_STATUS, &result);
  235. glGetShaderiv (shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  236. if (infoLogLength > 0)
  237. {
  238. char* logBuffer = new char [infoLogLength + 1];
  239. // ensure logBuffer ends with a \0
  240. memset (logBuffer, 0, infoLogLength + 1);
  241. // get information about the error
  242. glGetShaderInfoLog (shaderID, infoLogLength, nullptr, logBuffer);
  243. // throw an exception about the issue
  244. std::string message = logBuffer;
  245. // free the buffer
  246. delete[] logBuffer;
  247. // throw an exception
  248. throw std::runtime_error (message);
  249. }
  250. return shaderID;
  251. }
  252. void CPass::setupShaders ()
  253. {
  254. // ensure the constants are defined
  255. const ITexture* texture0 = this->m_material->getImage ()->getTexture ();
  256. // TODO: THE VALUES ARE THE SAME AS THE ENUMERATION, SO MAYBE IT HAS TO BE SPECIFIED FOR THE TEXTURE 0 OF ALL ELEMENTS?
  257. if (texture0 != nullptr)
  258. {
  259. if (texture0->getFormat () == ITexture::TextureFormat::RG88)
  260. {
  261. this->m_pass->insertCombo ("TEX0FORMAT", 8);
  262. }
  263. else if (texture0->getFormat () == ITexture::TextureFormat::R8)
  264. {
  265. this->m_pass->insertCombo ("TEX0FORMAT", 9);
  266. }
  267. }
  268. // prepare the shaders
  269. this->m_fragShader = new Render::Shaders::Compiler (
  270. this->m_material->getImage ()->getContainer (),
  271. this->m_pass->getShader (),
  272. Shaders::Compiler::Type_Pixel,
  273. this->m_pass->getCombos (),
  274. this->m_pass->getConstants ()
  275. );
  276. this->m_fragShader->precompile ();
  277. this->m_vertShader = new Render::Shaders::Compiler (
  278. this->m_material->getImage ()->getContainer (),
  279. this->m_pass->getShader (),
  280. Shaders::Compiler::Type_Vertex,
  281. this->m_pass->getCombos (),
  282. this->m_pass->getConstants ()
  283. );
  284. this->m_vertShader->precompile ();
  285. this->m_fragShader->precompile ();
  286. this->m_vertShader->precompile ();
  287. // compile the shaders
  288. GLuint vertexShaderID = compileShader (this->m_vertShader, GL_VERTEX_SHADER);
  289. GLuint fragmentShaderID = compileShader (this->m_fragShader, GL_FRAGMENT_SHADER);
  290. // create the final program
  291. this->m_programID = glCreateProgram ();
  292. // link the shaders together
  293. glAttachShader (this->m_programID, vertexShaderID);
  294. glAttachShader (this->m_programID, fragmentShaderID);
  295. glLinkProgram (this->m_programID);
  296. // check that the shader was properly linked
  297. GLint result = GL_FALSE;
  298. int infoLogLength = 0;
  299. glGetProgramiv (this->m_programID, GL_LINK_STATUS, &result);
  300. glGetProgramiv (this->m_programID, GL_INFO_LOG_LENGTH, &infoLogLength);
  301. if (infoLogLength > 0)
  302. {
  303. char* logBuffer = new char [infoLogLength + 1];
  304. // ensure logBuffer ends with a \0
  305. memset (logBuffer, 0, infoLogLength + 1);
  306. // get information about the error
  307. glGetProgramInfoLog (this->m_programID, infoLogLength, nullptr, logBuffer);
  308. // throw an exception about the issue
  309. std::string message = logBuffer;
  310. // free the buffer
  311. delete[] logBuffer;
  312. // throw an exception
  313. throw std::runtime_error (message);
  314. }
  315. // after being liked shaders can be dettached and deleted
  316. glDetachShader (this->m_programID, vertexShaderID);
  317. glDetachShader (this->m_programID, fragmentShaderID);
  318. glDeleteShader (vertexShaderID);
  319. glDeleteShader (fragmentShaderID);
  320. // setup uniforms
  321. this->setupUniforms ();
  322. // setup attributes too
  323. this->setupAttributes ();
  324. // get information from the program, like uniforms, etc
  325. // support three textures for now
  326. this->g_Texture0Rotation = glGetUniformLocation (this->m_programID, "g_Texture0Rotation");
  327. this->g_Texture0Translation = glGetUniformLocation (this->m_programID, "g_Texture0Translation");
  328. }
  329. void CPass::setupAttributes ()
  330. {
  331. this->addAttribute ("a_TexCoord", GL_FLOAT, 2, &this->a_TexCoord);
  332. this->addAttribute ("a_Position", GL_FLOAT, 3, &this->a_Position);
  333. }
  334. void CPass::setupUniforms ()
  335. {
  336. // resolve the main texture
  337. ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0);
  338. // register all the texture uniforms with correct values
  339. this->addUniform ("g_Texture0", 0);
  340. this->addUniform ("g_Texture1", 1);
  341. this->addUniform ("g_Texture2", 2);
  342. this->addUniform ("g_Texture3", 3);
  343. this->addUniform ("g_Texture4", 4);
  344. this->addUniform ("g_Texture5", 5);
  345. this->addUniform ("g_Texture6", 6);
  346. this->addUniform ("g_Texture7", 7);
  347. this->addUniform ("g_Texture0Resolution", texture->getResolution ());
  348. int lastTextureIndex = 0;
  349. // register the extra texture resolutions
  350. {
  351. auto cur = this->m_textures.begin ();
  352. auto end = this->m_textures.end ();
  353. for (int index = 1; cur != end; cur ++, index ++)
  354. {
  355. std::ostringstream namestream;
  356. namestream << "g_Texture" << index << "Resolution";
  357. texture = this->resolveTexture ((*cur), index, texture);
  358. this->addUniform (namestream.str (), texture->getResolution ());
  359. lastTextureIndex ++;
  360. }
  361. }
  362. // registers the extra texture resolutions from the shader
  363. {
  364. auto cur = this->m_fragShader->getTextures ().begin ();
  365. auto end = this->m_fragShader->getTextures ().end ();
  366. for (; cur != end; cur ++)
  367. {
  368. if ((*cur).first <= lastTextureIndex)
  369. continue;
  370. std::ostringstream namestream;
  371. namestream << "g_Texture" << (*cur).first << "Resolution";
  372. texture = this->resolveTexture ((*cur).second, (*cur).first, texture);
  373. this->addUniform (namestream.str (), texture->getResolution ());
  374. }
  375. }
  376. // registers the extra texture resolutions from the shader
  377. {
  378. auto cur = this->m_vertShader->getTextures ().begin ();
  379. auto end = this->m_vertShader->getTextures ().end ();
  380. for (; cur != end; cur ++)
  381. {
  382. if ((*cur).first <= lastTextureIndex)
  383. continue;
  384. std::ostringstream namestream;
  385. namestream << "g_Texture" << (*cur).first << "Resolution";
  386. texture = this->resolveTexture ((*cur).second, (*cur).first, texture);
  387. this->addUniform (namestream.str (), texture->getResolution ());
  388. }
  389. }
  390. // register variables like brightness and alpha with some default value
  391. this->addUniform ("g_Brightness", this->m_material->getImage ()->getImage ()->getBrightness ());
  392. this->addUniform ("g_UserAlpha", this->m_material->getImage ()->getImage ()->getAlpha ());
  393. this->addUniform ("g_Alpha", this->m_material->getImage ()->getImage ()->getAlpha ());
  394. this->addUniform ("g_Color", this->m_material->getImage ()->getImage ()->getColor ());
  395. // add some external variables
  396. this->addUniform ("g_Time", &g_Time);
  397. // add model-view-projection matrix
  398. this->addUniform ("g_ModelViewProjectionMatrix", &this->m_modelViewProjectionMatrix);
  399. this->addUniform ("g_PointerPosition", this->m_material->getImage ()->getScene ()->getMousePosition ());
  400. this->addUniform ("g_EffectTextureProjectionMatrix", glm::mat4(1.0));
  401. this->addUniform ("g_EffectTextureProjectionMatrixInverse", glm::mat4(1.0));
  402. }
  403. void CPass::addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value)
  404. {
  405. GLint id = glGetAttribLocation (this->m_programID, name.c_str ());
  406. if (id == -1)
  407. return;
  408. this->m_attribs.emplace_back (
  409. new AttribEntry (id, name, type, elements, value)
  410. );
  411. }
  412. template <typename T>
  413. void CPass::addUniform (const std::string& name, UniformType type, T value)
  414. {
  415. GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
  416. // parameter not found, can be ignored
  417. if (id == -1)
  418. return;
  419. // build a copy of the value and allocate it somewhere
  420. T* newValue = new T (value);
  421. // uniform found, add it to the list
  422. this->m_uniforms.insert (
  423. std::make_pair (name, new UniformEntry (id, name, type, newValue))
  424. );
  425. }
  426. template <typename T>
  427. void CPass::addUniform (const std::string& name, UniformType type, T* value)
  428. {
  429. // this version is used to reference to system variables so things like g_Time works fine
  430. GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
  431. // parameter not found, can be ignored
  432. if (id == -1)
  433. return;
  434. // uniform found, add it to the list
  435. this->m_uniforms.insert (
  436. std::make_pair (name, new UniformEntry (id, name, type, value))
  437. );
  438. }
  439. void CPass::setupTextures ()
  440. {
  441. auto cur = this->m_pass->getTextures ().begin ();
  442. auto end = this->m_pass->getTextures ().end ();
  443. for (int index = 0; cur != end; cur ++, index ++)
  444. {
  445. // ignore first texture as that'll be the input of the last pass/image (unless the image is an FBO)
  446. if (index == 0)
  447. continue;
  448. if ((*cur).find ("_rt_") == 0)
  449. {
  450. CFBO* fbo = this->m_material->m_effect->findFBO ((*cur));
  451. if (fbo == nullptr)
  452. fbo = this->m_material->getImage ()->getScene ()->findFBO ((*cur));
  453. if (fbo != nullptr)
  454. {
  455. this->m_fbos.insert (std::make_pair (index, fbo));
  456. this->m_textures.emplace_back (
  457. nullptr
  458. );
  459. }
  460. // _rt_texture
  461. }
  462. else
  463. {
  464. this->m_textures.emplace_back (
  465. this->m_material->getImage ()->getContainer ()->readTexture ((*cur))
  466. );
  467. }
  468. }
  469. }
  470. void CPass::setupShaderVariables ()
  471. {
  472. {
  473. // find variables in the shaders and set the value with the constants if possible
  474. auto cur = this->m_pass->getConstants ().begin ();
  475. auto end = this->m_pass->getConstants ().end ();
  476. for (; cur != end; cur ++)
  477. {
  478. CShaderVariable* vertexVar = this->m_vertShader->findParameter ((*cur).first);
  479. CShaderVariable* pixelVar = this->m_fragShader->findParameter ((*cur).first);
  480. // variable not found, can be ignored
  481. if (vertexVar == nullptr && pixelVar == nullptr)
  482. continue;
  483. // if both can be found, ensure they're the correct type
  484. /*if (vertexVar != nullptr && pixelVar != nullptr)
  485. {
  486. if (vertexVar->getType () != pixelVar->getType ())
  487. throw std::runtime_error ("Pixel and vertex shader variable types do not match");
  488. }*/
  489. // get one instance of it
  490. CShaderVariable* var = vertexVar == nullptr ? pixelVar : vertexVar;
  491. // ensure the shader's and the constant are of the same type
  492. if ((*cur).second->getType () != var->getType ())
  493. {
  494. // there's situations where this type mismatch is actually expected
  495. // integers and floats are equivalent, this could be detected at load time
  496. // but that'd mean to compile the shader in the load, and not on the render stage
  497. // so take into account these conversions here
  498. if ((*cur).second->is <CShaderConstantFloat> () == true && var->is <CShaderVariableInteger> () == true)
  499. {
  500. // create an integer value from a float
  501. this->addUniform (var->getName (), static_cast <int> (*(*cur).second->as <CShaderConstantFloat> ()->getValue ()));
  502. }
  503. else if ((*cur).second->is <CShaderConstantInteger> () == true && var->is <CShaderVariableFloat> () == true)
  504. {
  505. // create a float value from an integer
  506. this->addUniform (var->getName (), static_cast <float> (*(*cur).second->as <CShaderConstantInteger> ()->getValue ()));
  507. }
  508. else if ((*cur).second->is <CShaderConstantVector4> () == true && var->is <CShaderVariableVector2> () == true)
  509. {
  510. CShaderConstantVector4* val = (*cur).second->as <CShaderConstantVector4> ();
  511. // create a new vector2 with the first two values
  512. this->addUniform (var->getName (), {val->getValue ()->x, val->getValue ()->y});
  513. }
  514. else if ((*cur).second->is <CShaderConstantVector4> () == true && var->is <CShaderVariableVector3> () == true)
  515. {
  516. CShaderConstantVector4* val = (*cur).second->as <CShaderConstantVector4> ();
  517. this->addUniform (var->getName (), {val->getValue ()->x, val->getValue ()->y, val->getValue ()->z});
  518. }
  519. else
  520. {
  521. throw std::runtime_error ("Constant and pixel/vertex variable are not of the same type");
  522. }
  523. }
  524. else
  525. {
  526. // now determine the constant's type and register the correct uniform for it
  527. if ((*cur).second->is <CShaderConstantFloat> ())
  528. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantFloat> ()->getValue ());
  529. else if ((*cur).second->is <CShaderConstantInteger> ())
  530. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantInteger> ()->getValue ());
  531. else if ((*cur).second->is <CShaderConstantVector4> ())
  532. this->addUniform (var->getName (), (*cur).second->as <CShaderConstantVector4> ()->getValue ());
  533. }
  534. }
  535. }
  536. {
  537. auto cur = this->m_vertShader->getParameters ().begin ();
  538. auto end = this->m_vertShader->getParameters ().end ();
  539. for (; cur != end; cur ++)
  540. {
  541. if (this->m_uniforms.find ((*cur)->getName ()) != this->m_uniforms.end ())
  542. continue;
  543. if ((*cur)->is <CShaderVariableFloat> ())
  544. this->addUniform ((*cur)->getName (), const_cast <float*> (reinterpret_cast <const float*> ((*cur)->as <CShaderVariableFloat> ()->getValue ())));
  545. else if ((*cur)->is <CShaderVariableInteger> ())
  546. this->addUniform ((*cur)->getName (), const_cast <int*> (reinterpret_cast <const int*> ((*cur)->as <CShaderVariableInteger> ()->getValue ())));
  547. else if ((*cur)->is <CShaderVariableVector2> ())
  548. this->addUniform ((*cur)->getName (), const_cast <glm::vec2*> (reinterpret_cast <const glm::vec2*> ((*cur)->as <CShaderVariableVector2> ()->getValue ())));
  549. else if ((*cur)->is <CShaderVariableVector3> ())
  550. this->addUniform ((*cur)->getName (), const_cast <glm::vec3*> (reinterpret_cast <const glm::vec3*> ((*cur)->as <CShaderVariableVector3> ()->getValue ())));
  551. else if ((*cur)->is <CShaderVariableVector4> ())
  552. this->addUniform ((*cur)->getName (), const_cast <glm::vec4*> (reinterpret_cast <const glm::vec4*> ((*cur)->as <CShaderVariableVector4> ()->getValue ())));
  553. }
  554. }
  555. {
  556. auto cur = this->m_fragShader->getParameters ().begin ();
  557. auto end = this->m_fragShader->getParameters ().end ();
  558. for (; cur != end; cur ++)
  559. {
  560. if (this->m_uniforms.find ((*cur)->getName ()) != this->m_uniforms.end ())
  561. continue;
  562. if ((*cur)->is <CShaderVariableFloat> ())
  563. this->addUniform ((*cur)->getName (), const_cast <float*> (reinterpret_cast <const float*> ((*cur)->as <CShaderVariableFloat> ()->getValue ())));
  564. else if ((*cur)->is <CShaderVariableInteger> ())
  565. this->addUniform ((*cur)->getName (), const_cast <int*> (reinterpret_cast <const int*> ((*cur)->as <CShaderVariableInteger> ()->getValue ())));
  566. else if ((*cur)->is <CShaderVariableVector2> ())
  567. this->addUniform ((*cur)->getName (), const_cast <glm::vec2*> (reinterpret_cast <const glm::vec2*> ((*cur)->as <CShaderVariableVector2> ()->getValue ())));
  568. else if ((*cur)->is <CShaderVariableVector3> ())
  569. this->addUniform ((*cur)->getName (), const_cast <glm::vec3*> (reinterpret_cast <const glm::vec3*> ((*cur)->as <CShaderVariableVector3> ()->getValue ())));
  570. else if ((*cur)->is <CShaderVariableVector4> ())
  571. this->addUniform ((*cur)->getName (), const_cast <glm::vec4*> (reinterpret_cast <const glm::vec4*> ((*cur)->as <CShaderVariableVector4> ()->getValue ())));
  572. }
  573. }
  574. }
  575. // define some basic methods for the template
  576. /*template double Core::jsonFindDefault (nlohmann::json& data, const char *key, double defaultValue);*/
  577. void CPass::addUniform (const std::string& name, int value)
  578. {
  579. this->addUniform (name, UniformType::Integer, value);
  580. }
  581. void CPass::addUniform (const std::string& name, const int* value)
  582. {
  583. this->addUniform (name, UniformType::Integer, value);
  584. }
  585. void CPass::addUniform (const std::string& name, double value)
  586. {
  587. this->addUniform (name, UniformType::Double, value);
  588. }
  589. void CPass::addUniform (const std::string& name, const double* value)
  590. {
  591. this->addUniform (name, UniformType::Double, value);
  592. }
  593. void CPass::addUniform (const std::string& name, float value)
  594. {
  595. this->addUniform (name, UniformType::Float, value);
  596. }
  597. void CPass::addUniform (const std::string& name, const float* value)
  598. {
  599. this->addUniform (name, UniformType::Float, value);
  600. }
  601. void CPass::addUniform (const std::string& name, glm::vec2 value)
  602. {
  603. this->addUniform (name, UniformType::Vector2, value);
  604. }
  605. void CPass::addUniform (const std::string& name, const glm::vec2* value)
  606. {
  607. this->addUniform (name, UniformType::Vector2, value);
  608. }
  609. void CPass::addUniform (const std::string& name, glm::vec3 value)
  610. {
  611. this->addUniform (name, UniformType::Vector3, value);
  612. }
  613. void CPass::addUniform (const std::string& name, const glm::vec3* value)
  614. {
  615. this->addUniform (name, UniformType::Vector3, value);
  616. }
  617. void CPass::addUniform (const std::string& name, glm::vec4 value)
  618. {
  619. this->addUniform (name, UniformType::Vector4, value);
  620. }
  621. void CPass::addUniform (const std::string& name, const glm::vec4* value)
  622. {
  623. this->addUniform (name, UniformType::Vector4, value);
  624. }
  625. void CPass::addUniform (const std::string& name, glm::mat4 value)
  626. {
  627. this->addUniform (name, UniformType::Matrix4, value);
  628. }
  629. void CPass::addUniform (const std::string& name, const glm::mat4* value)
  630. {
  631. this->addUniform (name, UniformType::Matrix4, value);
  632. }