Fur.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. #define FUR_LAYERS 48 // this was the smallest value that produced good results when zoomed to a fur object
  6. #define DRAW_LAYERS_NUM (DEBUG && 0)
  7. #define SUPPORT_FUR_CREATE (DEBUG && 0)
  8. /******************************************************************************/
  9. static ImagePtr FurCol;
  10. static ShaderParam *FurStep;
  11. static ShaderImage *FurLight;
  12. /******************************************************************************/
  13. #if SUPPORT_FUR_CREATE
  14. #pragma message("!! Warning: Use this only for debugging !!")
  15. static void SetupFur(Int res=256)
  16. {
  17. Randomizer random(UIDZero);
  18. static Image img; img.create2D(res, res, IMAGE_R8, 0, false); if(img.lock(LOCK_WRITE))
  19. {
  20. FREPD(y, img.h())
  21. FREPD(x, img.w())img.pixB(x, y)=random(256);
  22. img.unlock().updateMipMaps();
  23. if(1)img.save("C:/Esenthel/Data/Img/fur.img");
  24. }
  25. GetShaderImage("FurCol")->set(img);
  26. }
  27. #endif
  28. /******************************************************************************/
  29. void InitFur()
  30. {
  31. if(D.canDraw() && FurCol.get("Img/fur.img"))GetShaderImage("FurCol")->set(FurCol());
  32. FurStep =GetShaderParam("FurStep");
  33. FurLight=GetShaderImage("FurLight");
  34. }
  35. void PrepareFur()
  36. {
  37. FurLight->set(Renderer._lum_1s);
  38. #if SUPPORT_FUR_CREATE
  39. if(Kb.bp(KB_1))SetupFur(8);
  40. if(Kb.bp(KB_2))SetupFur(16);
  41. if(Kb.bp(KB_3))SetupFur(32);
  42. if(Kb.bp(KB_4))SetupFur(64);
  43. if(Kb.bp(KB_5))SetupFur(128);
  44. if(Kb.bp(KB_6))SetupFur(256);
  45. if(Kb.bp(KB_0))SetupFur(); // default
  46. #endif
  47. }
  48. /******************************************************************************/
  49. #define FUR_VEL_FIX (WINDOWS && GL) // Fur Shader does not ouput velocity to second RT, however for some reason it will set the same value as the source color, #BlendRT TODO: check again in the future (was tested and works OK on Android)
  50. void DrawFur(C MeshRender &mshr, Shader &shader, Flt scale)
  51. {
  52. #if FUR_VEL_FIX
  53. D.colWrite(0, 1); // disable writing
  54. #endif
  55. scale=((scale>0) ? Max(1, 0.5f/scale) : 1); // !! scale can be <=0 if behind camera !!
  56. #if DRAW_LAYERS_NUM
  57. Int layers=0;
  58. #endif
  59. for(Flt f=scale; f<=FUR_LAYERS-0.5f; f+=scale) // when f==FUR_LAYERS then that layer will be completely transparent, so stop 0.5 before
  60. {
  61. #if DRAW_LAYERS_NUM
  62. layers++;
  63. #endif
  64. Vec2 s; s.x=f/FUR_LAYERS; s.y=s.x+1; FurStep->set(s);
  65. shader.commit ();
  66. mshr .drawFur();
  67. }
  68. #if FUR_VEL_FIX
  69. D.colWrite(COL_WRITE_RGBA, 1); // restore writing
  70. #endif
  71. #if DRAW_LAYERS_NUM
  72. D.text(0, D.h()*-0.9f, S+"Fur layers:"+layers);
  73. #endif
  74. }
  75. /******************************************************************************/
  76. }
  77. /******************************************************************************/