nier_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <irrlicht/irrlicht.h>
  2. #include <cstdint>
  3. #include <iostream>
  4. #include <wallpaperengine/config.h>
  5. #include "common.h"
  6. #include "wallpaperengine/shaders/compiler.h"
  7. #include "wallpaperengine/fs/fileResolver.h"
  8. #include "wallpaperengine/project.h"
  9. #include "wallpaperengine/irrlicht.h"
  10. irr::io::path _example_base_folder = "../res/";
  11. irr::f32 g_AnimationSpeed = 0.1f;
  12. irr::f32 g_Scale = 2.5f;
  13. irr::f32 g_ScrollSpeed = 0.0f;
  14. irr::f32 g_Direction = 0.0f;
  15. irr::f32 g_Strength = 0.07f;
  16. irr::f32 g_SpecularPower = 1.0f;
  17. irr::f32 g_SpecularStrength = 1.0f;
  18. irr::f32 g_SpecularColor [3] = {1.0f, 1.0f, 1.0f};
  19. irr::f32 g_Texture1Resolution [4] = {1.0f, 1.0f, 1.0f, 1.0f};
  20. irr::f32 g_Texture0 = 0;
  21. irr::f32 g_Texture1 = 1;
  22. irr::f32 g_Texture2 = 2;
  23. irr::f32 g_Time = 0;
  24. irr::core::vector3df _map_size = irr::core::vector3df (21, 11.5f, 16);
  25. class MyShaderCallback : public irr::video::IShaderConstantSetCallBack
  26. {
  27. virtual void OnSetConstants (irr::video::IMaterialRendererServices* services, int32_t userData)
  28. {
  29. irr::video::IVideoDriver* driver = services->getVideoDriver ();
  30. irr::core::matrix4 worldViewProj;
  31. worldViewProj = driver->getTransform(irr::video::ETS_PROJECTION);
  32. worldViewProj *= driver->getTransform(irr::video::ETS_VIEW);
  33. worldViewProj *= driver->getTransform(irr::video::ETS_WORLD);
  34. services->setVertexShaderConstant ("g_AnimationSpeed", &g_AnimationSpeed, 1);
  35. services->setVertexShaderConstant ("g_Scale", &g_Scale, 1);
  36. services->setVertexShaderConstant ("g_ScrollSpeed", &g_ScrollSpeed, 1);
  37. services->setVertexShaderConstant ("g_Direction", &g_Direction, 1);
  38. services->setVertexShaderConstant ("g_Time", &g_Time, 1);
  39. services->setVertexShaderConstant ("g_ModelViewProjectionMatrix", worldViewProj.pointer(), 16);
  40. services->setVertexShaderConstant ("g_Texture0Resolution", g_Texture1Resolution, 4);
  41. services->setVertexShaderConstant ("g_Texture1Resolution", g_Texture1Resolution, 4);
  42. services->setVertexShaderConstant ("g_Texture2Resolution", g_Texture1Resolution, 4);
  43. // TODO: Support up to 7 materials (as wallpaper engine)
  44. services->setPixelShaderConstant ("g_Strength", &g_Strength, 1);
  45. services->setPixelShaderConstant ("g_SpecularPower", &g_SpecularPower, 1);
  46. services->setPixelShaderConstant ("g_SpecularStrength", &g_SpecularStrength, 1);
  47. services->setPixelShaderConstant ("g_SpecularColor", g_SpecularColor, 3);
  48. services->setPixelShaderConstant ("g_Texture0", &g_Texture0, 1);
  49. services->setPixelShaderConstant ("g_Texture1", &g_Texture1, 1);
  50. services->setPixelShaderConstant ("g_Texture2", &g_Texture2, 1);
  51. }
  52. };
  53. class QuadSceneNode : public irr::scene::ISceneNode
  54. {
  55. irr::core::aabbox3d <irr::f32> m_box;
  56. irr::video::S3DVertex m_vertices [4];
  57. irr::video::SMaterial m_material;
  58. public:
  59. QuadSceneNode (irr::scene::ISceneNode* parent, irr::scene::ISceneManager* sceneManager, int32_t id)
  60. : irr::scene::ISceneNode (parent, sceneManager, id)
  61. {
  62. m_material.Wireframe = false;
  63. m_material.Lighting = false;
  64. /*m_vertices[0].Pos = irr::core::vector3df ( _map_size.X, _map_size.Y, 0.0f); // bottom right
  65. m_vertices[1].Pos = irr::core::vector3df ( _map_size.X, -_map_size.Y, 0.0f); // top right
  66. m_vertices[2].Pos = irr::core::vector3df (-_map_size.X, -_map_size.Y, 0.0f); // top left
  67. m_vertices[3].Pos = irr::core::vector3df (-_map_size.X, _map_size.Y, 0.0f); // bottom left
  68. m_vertices[0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  69. m_vertices[1].TCoords = irr::core::vector2df (1.0f, 1.0f);
  70. m_vertices[2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  71. m_vertices[3].TCoords = irr::core::vector2df (0.0f, 0.0f);
  72. m_vertices[0].Color = irr::video::SColor (255, 255, 255, 255);
  73. m_vertices[1].Color = irr::video::SColor (255, 255, 255, 255);
  74. m_vertices[2].Color = irr::video::SColor (255, 255, 255, 255);
  75. m_vertices[3].Color = irr::video::SColor (255, 255, 255, 255);
  76. m_box.reset (m_vertices[0].Pos);
  77. for (int32_t i = 1; i < 4; i ++)
  78. {
  79. m_box.addInternalPoint (m_vertices [i].Pos);
  80. }*/
  81. irr::video::SColor color; color.set (255, 255, 255, 255);
  82. irr::f32 xright = 1920.0f / 2.0f;
  83. irr::f32 xleft = -xright;
  84. irr::f32 ztop = 1080.0f / 2.0f;
  85. irr::f32 zbottom = -xright;
  86. this->m_vertices [0] = irr::video::S3DVertex (xleft, ztop,0, 0, 0,0, color,1,0);
  87. this->m_vertices [1] = irr::video::S3DVertex(xright, ztop,0, 0, 0,0, color,0,0);
  88. this->m_vertices [2] = irr::video::S3DVertex(xright,zbottom,0, 0, 0,0, color,0,1);
  89. this->m_vertices [3] = irr::video::S3DVertex(xleft, zbottom,0, 0, 0,0, color,1,1);
  90. }
  91. virtual void OnRegisterSceneNode ()
  92. {
  93. if (IsVisible)
  94. SceneManager->registerNodeForRendering (this);
  95. ISceneNode::OnRegisterSceneNode ();
  96. }
  97. virtual void render ()
  98. {
  99. /*uint16_t indices[] = {
  100. 0, 1, 2, 3
  101. };*/
  102. uint16_t indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
  103. irr::video::IVideoDriver* driver = SceneManager->getVideoDriver ();
  104. driver->setMaterial (m_material);
  105. driver->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
  106. // driver->drawVertexPrimitiveList (m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  107. driver->drawVertexPrimitiveList(&this->m_vertices[0], 4, &indices[0], 4, irr::video::EVT_STANDARD, irr::scene::EPT_TRIANGLES, irr::video::EIT_16BIT);
  108. }
  109. virtual const irr::core::aabbox3d <irr::f32>& getBoundingBox () const
  110. {
  111. return m_box;
  112. }
  113. virtual uint32_t getMaterialCount () const
  114. {
  115. return 1;
  116. }
  117. virtual irr::video::SMaterial& getMaterial (uint32_t i)
  118. {
  119. return m_material;
  120. }
  121. };
  122. int nier_test ()
  123. {
  124. irr::io::path _wp_engine_folder = "/home/almamu/Downloads/nier__automata_-_become_as_gods_edition/";
  125. // set our working directory
  126. wp::fs::resolver.changeWorkingDirectory (_wp_engine_folder);
  127. wp::project* wp_project = new wp::project ();
  128. wp::fs::resolver.changeWorkingDirectory (wp::config::path::resources);
  129. irr::io::path _water_example = wp::fs::resolver.resolve ("materials/water-intact.png");
  130. irr::io::path _mud_example = wp::fs::resolver.resolve ("materials/plant-on-water.png");
  131. irr::io::path _background_example = wp::fs::resolver.resolve ("materials/top-part.png");
  132. irr::io::path _waterripple_normal = wp::fs::resolver.resolve ("materials/effects/waterripplenormal.png");
  133. irr::io::path _waterripple_frag_shader = wp::fs::resolver.resolve ("shaders/effects/waterripple_opengl.frag");
  134. irr::io::path _waterripple_vert_shader = wp::fs::resolver.resolve ("shaders/effects/waterripple_opengl.vert");
  135. irr::io::path _white = wp::fs::resolver.resolve ("materials/white.png");
  136. // check for ps and vs support
  137. if (wp::irrlicht::driver->queryFeature (irr::video::EVDF_PIXEL_SHADER_1_1) == false && wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_FRAGMENT_PROGRAM_1) == false)
  138. {
  139. wp::irrlicht::device->getLogger ()->log ("WARNING: Pixel shaders disabled because of missing driver/hardware support");
  140. _waterripple_frag_shader = "";
  141. }
  142. if (wp::irrlicht::driver->queryFeature (irr::video::EVDF_VERTEX_SHADER_1_1) == false && wp::irrlicht::driver->queryFeature (irr::video::EVDF_ARB_VERTEX_PROGRAM_1) == false)
  143. {
  144. wp::irrlicht::device->getLogger ()->log ("WARNING: Vertex shaders disabled because of missing driver/hardware support");
  145. _waterripple_vert_shader = "";
  146. }
  147. irr::video::IGPUProgrammingServices* gpuProgrammingServices = wp::irrlicht::driver->getGPUProgrammingServices ();
  148. int32_t materialType1 = 0;
  149. if (gpuProgrammingServices)
  150. {
  151. MyShaderCallback* shader = new MyShaderCallback ();
  152. wp::shaders::compiler _vert(_waterripple_vert_shader, wp::shaders::compiler::Type::Type_Vertex);
  153. wp::shaders::compiler _frag(_waterripple_frag_shader, wp::shaders::compiler::Type::Type_Pixel);
  154. materialType1 = gpuProgrammingServices->addHighLevelShaderMaterial(
  155. _vert.precompile ().c_str (), "vertexMain", irr::video::EVST_VS_2_0,
  156. _frag.precompile ().c_str (), "pixelMain", irr::video::EPST_PS_2_0,
  157. shader, irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL, 0, irr::video::EGSL_DEFAULT
  158. );
  159. shader->drop ();
  160. }
  161. // load some basic textures
  162. irr::video::ITexture* waterTexture = wp::irrlicht::driver->getTexture (_water_example.c_str ());
  163. irr::video::ITexture* mudTexture = wp::irrlicht::driver->getTexture (_mud_example.c_str ());
  164. irr::video::ITexture* backgroundExample = wp::irrlicht::driver->getTexture (_background_example.c_str ());
  165. irr::video::ITexture* waterRippleNormalTexture = wp::irrlicht::driver->getTexture (_waterripple_normal.c_str ());
  166. irr::video::ITexture* whiteTexture = wp::irrlicht::driver->getTexture (_white.c_str ());
  167. // get scene manager
  168. irr::scene::ISceneManager* sceneManager = wp::irrlicht::device->getSceneManager ();
  169. _map_size = wp_project->getScene ()->getCamera ()->getCenter ();
  170. sceneManager->addCameraSceneNode (0, wp_project->getScene ()->getCamera ()->getCenter (), wp_project->getScene ()->getCamera ()->getEye ());
  171. // sceneManager->addCameraSceneNode (0, irr::core::vector3df (0.0f, 0.0f, -_map_size.Z), irr::core::vector3df (0.0f, 0.0f, _map_size.Z));
  172. QuadSceneNode* backgroundNode = new QuadSceneNode (sceneManager->getRootSceneNode (), sceneManager, 666);
  173. QuadSceneNode* waterNode = new QuadSceneNode (sceneManager->getRootSceneNode (), sceneManager, 667);
  174. QuadSceneNode* mudNode = new QuadSceneNode (sceneManager->getRootSceneNode (), sceneManager, 668);
  175. backgroundNode->getMaterial (0).setTexture (0, backgroundExample);
  176. backgroundNode->setMaterialType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  177. mudNode->getMaterial (0).setTexture (0, mudTexture);
  178. mudNode->setMaterialType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  179. waterNode->setMaterialFlag(irr::video::EMF_LIGHTING, false);
  180. waterNode->setMaterialFlag(irr::video::EMF_BLEND_OPERATION, true);
  181. waterNode->getMaterial (0).setTexture (0, waterTexture);
  182. waterNode->getMaterial (0).setTexture (1, waterRippleNormalTexture);
  183. waterNode->getMaterial (0).setTexture (2, whiteTexture);
  184. waterNode->setMaterialType ( (irr::video::E_MATERIAL_TYPE) materialType1);
  185. /*irr::core::matrix4 identity; identity.makeIdentity ();
  186. irr::core::matrix4 orthoProjection; orthoProjection.buildProjectionMatrixOrthoLH (wp_project->getScene ()->getCamera ()->getEye ().X, wp_project->getScene ()->getCamera ()->getEye ().Y, wp_project->getScene ()->getCamera ()->getEye ().Z, wp_project->getScene ()->getCamera ()->getCenter ().Z);
  187. wp::irrlicht::driver->setTransform (irr::video::ETS_PROJECTION, orthoProjection);
  188. wp::irrlicht::driver->setTransform (irr::video::ETS_VIEW, identity);
  189. wp::irrlicht::driver->setTransform (irr::video::ETS_WORLD, identity);*/
  190. int32_t lastTime = 0;
  191. int32_t minimumTime = 1000 / 90;
  192. int32_t currentTime = 0;
  193. while (wp::irrlicht::device->run () && wp::irrlicht::driver)
  194. {
  195. // if (device->isWindowActive ())
  196. {
  197. currentTime = wp::irrlicht::device->getTimer ()->getTime ();
  198. g_Time = currentTime / 1000.0f;
  199. if (currentTime - lastTime > minimumTime)
  200. {
  201. wp::irrlicht::driver->beginScene (true, true, irr::video::SColor(0, 0, 0, 0));
  202. sceneManager->drawAll ();
  203. wp::irrlicht::driver->endScene ();
  204. lastTime = currentTime;
  205. }
  206. else
  207. {
  208. wp::irrlicht::device->sleep (1, false);
  209. }
  210. }
  211. }
  212. return 0;
  213. }