PostProcess.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "PostProcess.h"
  2. CREATE_SERVICE(PostProcessService, /*100*/120)
  3. static IConsole *console = null;
  4. static ShaderId PostPolygon_id;
  5. struct Vertex
  6. {
  7. float x,y; dword c;
  8. };
  9. PostProcessService:: PostProcessService() : nodes(_FL_,16)
  10. {
  11. pRS = null;
  12. count = 0;
  13. empty = true;
  14. trace = false;
  15. key = false;
  16. dflag = true;
  17. enabled = true;
  18. }
  19. PostProcessService::~PostProcessService()
  20. {
  21. }
  22. bool PostProcessService::Init()
  23. {
  24. pRS = (IRender *)api->GetService("DX9Render");
  25. //Added: by Joker
  26. IFileService * fs = (IFileService *)api->GetService("FileService");
  27. Assert(fs);
  28. IIniFile * pEngineIni = fs->SystemIni();
  29. bool bPostProcessEnabled = false;
  30. if (pEngineIni)
  31. {
  32. bPostProcessEnabled = pEngineIni->GetLong("Render", "PostProcess", 1) != 0;
  33. RELEASE(pEngineIni);
  34. }
  35. /*
  36. #ifdef _XBOX
  37. //JOKER - пока фильтры не пофикшенны я их отключил
  38. //т.к. версия для X360 непоказабельная...
  39. bPostProcessEnabled = false;
  40. #endif
  41. */
  42. if( !bPostProcessEnabled )
  43. {
  44. return true;
  45. }
  46. console = (IConsole *)api->GetService("Console");
  47. //filter_motion.Init();
  48. //filter_motion.UserInit();
  49. //pRS->AddRenderFilter(&filter_motion,1.0f);
  50. //filter_motion.Enable(false);
  51. //filter_effect.Init();
  52. //filter_effect.UserInit();
  53. //pRS->AddRenderFilter(&filter_effect,1.0f);
  54. //filter_effect.Enable(false);
  55. //filter_blur.Init();
  56. //filter_blur.UserInit();
  57. //pRS->AddRenderFilter(&filter_blur,1.0f);
  58. //filter_blur.Enable(false);
  59. //filter_bw.Init();
  60. //filter_bw.UserInit();
  61. //pRS->AddRenderFilter(&filter_bw,1.0f);
  62. //filter_bw.Enable(false);
  63. filter_splash.Init();
  64. filter_splash.UserInit();
  65. pRS->AddRenderFilter(&filter_splash,1.0f);
  66. filter_splash.Enable(true);
  67. api->SetStartFrameLevel(this,Core_DefaultExecuteLevel);
  68. api->SetEndFrameLevel(this,Core_DefaultExecuteLevel);
  69. pRS->GetShaderId("PostPolygon",PostPolygon_id);
  70. if( console )
  71. {
  72. console->RegisterCommand("PostDraw","Draw posteffects." ,this,(CONSOLE_COMMAND)&PostProcessService::Console_EnableDraw);
  73. console->RegisterCommand("PostList","Draw posteffects list.",this,(CONSOLE_COMMAND)&PostProcessService::Console_EnableList);
  74. }
  75. return true;
  76. }
  77. void PostProcessService::StartFrame(float dltTime)
  78. {
  79. if( !empty )
  80. {
  81. for( int i = 0 ; i < nodes ; i++ )
  82. {
  83. nodes[i].reset();
  84. }
  85. weight = 0.0f;
  86. count = 0;
  87. empty = true;
  88. }
  89. filter_splash.Update(dltTime);
  90. }
  91. void PostProcessService::EndFrame(float dltTime)
  92. {
  93. // #ifndef _XBOX
  94. // if( !console || console->IsHided())
  95. {
  96. #ifndef _XBOX
  97. bool pressed = api->DebugKeyState('P',VK_CONTROL);
  98. if( pressed && !key )
  99. {
  100. trace = !trace;
  101. }
  102. key = pressed;
  103. #endif
  104. if( pRS && !empty && trace && enabled )
  105. {
  106. /* float y = 10.0f;
  107. for( int i = 0 ; i < nodes ; i++, y += 15.0f )
  108. {
  109. const Node &node = nodes[i];
  110. if( !!node )
  111. {
  112. pRS->Print(10.0f,y,-1,"%s by \"%s\"",node.effect,node.object);
  113. }
  114. }*/
  115. const RENDERVIEWPORT &wp = pRS->GetFullScreenViewPort_3D();
  116. float cx = (float)wp.Width;
  117. float cy = (float)wp.Height;
  118. const float ch = 17.0f;
  119. float up = 1.0f - (20.0f + count*ch)/cy;
  120. // const dword c = 0x80000000;
  121. const dword c = 0x70000000;
  122. Vertex v[] = {
  123. 0.0f, up,c,
  124. 0.0f,1.0f,c,
  125. 0.5f, up,c,
  126. 0.5f,1.0f,c};
  127. pRS->DrawPrimitiveUP(PostPolygon_id,PT_TRIANGLESTRIP,2,v,sizeof(Vertex));
  128. // const float max_w = 53.0f;
  129. // const float sum_w = 116.0f;
  130. const float max_w = 27.0f;
  131. const float sum_w = 63.0f;
  132. float k = weight/sum_w;
  133. if( k > 1.0f )
  134. k = 1.0f;
  135. float y = cy - 10.0f - ch;
  136. for( int i = 0 ; i < nodes ; i++, y -= ch )
  137. {
  138. const Node &node = nodes[i];
  139. if( !!node )
  140. {
  141. // pRS->Print(10.0f,y,0xff00ff00,"%s by \"%s\"",node.effect,node.object);
  142. float w = node.weight/max_w*k;
  143. if( w > 1.0f )
  144. w = 1.0f;
  145. Color c(0xff000000);
  146. if( w < 0.5f )
  147. {
  148. c.g = 1.0f; c.r = w*2.0f;
  149. }
  150. else
  151. {
  152. c.r = 1.0f; c.g = 1.0f - (w - 0.5f)*2.0f;
  153. }
  154. pRS->Print(10.0f,y,c.GetDword(),"%1.2f %s by \"%s\"",node.k,node.effect,node.object.c_str());
  155. }
  156. }
  157. }
  158. }
  159. // #endif
  160. enabled = dflag;
  161. }
  162. void PostProcessService::FilterNotify(const char *effect, const char *object, float weight, float k)
  163. {
  164. for( int i = 0 ; i < nodes ; i++ )
  165. {
  166. Node &cur = nodes[i];
  167. if( cur.weight == weight )
  168. break;
  169. }
  170. if( i >= nodes )
  171. this->weight += weight;
  172. Node *node = null;
  173. for( int i = 0 ; i < nodes ; i++ )
  174. {
  175. Node &cur = nodes[i];
  176. if( !cur )
  177. {
  178. node = &cur;
  179. break;
  180. }
  181. }
  182. if( !node )
  183. node = &nodes[nodes.Add()];
  184. node->set(effect,object,weight,k);
  185. count++;
  186. empty = false;
  187. }
  188. /*IBlur *PostProcessService::GetBlurFilter()
  189. {
  190. return (IBlur *)&filter_blur;
  191. }
  192. IMotionBlur *PostProcessService::GetMotionBlurFilter()
  193. {
  194. return (IMotionBlur *)&filter_motion;
  195. }
  196. BlackAndWhite *PostProcessService::GetBlackFilter()
  197. {
  198. return &filter_bw;
  199. }
  200. MotionEffect *PostProcessService::GetMotionEffectFilter()
  201. {
  202. return &filter_effect;
  203. }*/
  204. Splash *PostProcessService::GetSplashFilter()
  205. {
  206. return &filter_splash;
  207. }
  208. void _cdecl PostProcessService::Console_EnableDraw(const ConsoleStack &params)
  209. {
  210. dflag = !dflag;
  211. }
  212. void _cdecl PostProcessService::Console_EnableList(const ConsoleStack &params)
  213. {
  214. trace = !trace;
  215. }