Light.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /******************************************************************************
  2. Use 'LightDir' to add a directional light onto the scene
  3. Use 'LightPoint' to add a point light onto the scene
  4. Use 'LightSqr' to add a point light onto the scene (with small range)
  5. Use 'LightCone' to add a cone light onto the scene
  6. Access 'CurrentLight' to gain informations about the current light which is being applied.
  7. /******************************************************************************/
  8. enum LIGHT_TYPE : Byte // Light Type
  9. {
  10. LIGHT_NONE , // none
  11. LIGHT_DIR , // directional
  12. LIGHT_POINT, // point
  13. LIGHT_SQR , // point with small range
  14. LIGHT_CONE , // cone
  15. };
  16. /******************************************************************************/
  17. struct LightDir // Directional Light
  18. {
  19. Vec dir , // direction , normalized vector
  20. color ; // color , (0,0,0) .. (1,1,1)
  21. Flt vol , // volumetric amount , (0..1 )
  22. vol_exponent, // volumetric exponent, (0..Inf)
  23. vol_steam ; // volumetric steam , (0..1 )
  24. void add(Bool shadow=true, CPtr light_src=null); // add light to scene, this needs to be called only in RM_PREPARE mode, 'shadow'=if shadowing enabled, 'light_src'=custom pointer to light source (which can be later accessed from "CurrentLight.src")
  25. void set( ); // use only outside Renderer rendering, before drawing any shade'able meshes
  26. LightDir() {}
  27. LightDir(C Vec &dir, C Vec &color=Vec(1, 1, 1), Flt vol=0, Flt vol_exponent=1, Flt vol_steam=0.5f) {T.dir=dir; T.color=color; T.vol=vol; T.vol_exponent=vol_exponent; T.vol_steam=vol_steam;}
  28. #if EE_PRIVATE
  29. Bool toScreenRect(Rect &rect)C {rect=D.viewRect(); return true;}
  30. #endif
  31. };
  32. /******************************************************************************/
  33. struct LightPoint // Point Light
  34. {
  35. Flt power , // power , (0..Inf), determines light range
  36. vol , // volumetric amount , (0..Inf)
  37. vol_max; // volumetric maximum, (0..1 )
  38. VecD pos ; // position ,
  39. Vec color ; // color , (0,0,0) .. (1,1,1)
  40. Flt range( )C; // get affected range according to light's 'power'
  41. void add (Flt shadow_opacity=1.0f, CPtr light_src=null) ; // add light to scene, this needs to be called only in RM_PREPARE mode, 'shadow_opacity'=opacity of shadows (0..1) where value 0 disables shadowing, value 1 sets full shadows, and values between allow for manual blending the shadows, 'light_src'=custom pointer to light source (which can be later accessed from "CurrentLight.src")
  42. LightPoint() {}
  43. LightPoint(Flt power, C VecD &pos, C Vec &color=Vec(1, 1, 1), Flt vol=0, Flt vol_max=0.5f) {T.power=power; T.pos=pos; T.color=color; T.vol=vol; T.vol_max=vol_max;}
  44. #if EE_PRIVATE
  45. void set(Flt shadow_opacity);
  46. BallM asBall()C {return BallM(range(), pos);}
  47. Bool toScreenRect(Rect &rect)C {return ToFullScreenRect(asBall(), rect);}
  48. #endif
  49. };
  50. /******************************************************************************/
  51. struct LightSqr // Point Light with small range
  52. {
  53. Flt range , // range , (0..Inf)
  54. vol , // volumetric amount , (0..Inf)
  55. vol_max; // volumetric maximum, (0..1 )
  56. VecD pos ; // position ,
  57. Vec color ; // color , (0,0,0) .. (1,1,1)
  58. void add(Flt shadow_opacity=1.0f, CPtr light_src=null); // add light to scene, this needs to be called only in RM_PREPARE mode, 'shadow_opacity'=opacity of shadows (0..1) where value 0 disables shadowing, value 1 sets full shadows, and values between allow for manual blending the shadows, 'light_src'=custom pointer to light source (which can be later accessed from "CurrentLight.src")
  59. LightSqr() {}
  60. LightSqr(Flt range, C VecD &pos, C Vec &color=Vec(1, 1, 1), Flt vol=0, Flt vol_max=0.5f) {T.range=range; T.pos=pos; T.color=color; T.vol=vol; T.vol_max=vol_max;}
  61. #if EE_PRIVATE
  62. void set(Flt shadow_opacity);
  63. BallM asBall()C {return BallM(range, pos);}
  64. Bool toScreenRect(Rect &rect)C {return ToFullScreenRect(asBall(), rect);}
  65. #endif
  66. };
  67. /******************************************************************************/
  68. struct LightCone // Cone Light
  69. {
  70. Flt falloff, // light falloff , (0..1 ), default=0.5
  71. vol , // volumetric amount , (0..Inf)
  72. vol_max; // volumetric maximum, (0..1 )
  73. Vec color ; // color , (0,0,0) .. (1,1,1)
  74. PyramidM pyramid; // pyramid , determines orientation of the light
  75. void add(Flt shadow_opacity=1.0f, CPtr light_src=null, Image *image=null, Flt image_scale=1, C Color &image_add=TRANSPARENT, Flt image_specular=0); // add light to scene, this needs to be called only in RM_PREPARE mode, 'shadow_opacity'=opacity of shadows (0..1) where value 0 disables shadowing, value 1 sets full shadows, and values between allow for manual blending the shadows, 'light_src'=custom pointer to light source (which can be later accessed from "CurrentLight.src"), 'image'=dynamic lightmap, 'image_add'=add color to dynamic lightmap, 'image_scale'=scale dynamic lightmap, 'image_specular'=specular of dynamic lightmap
  76. LightCone() {}
  77. LightCone(Flt length, C VecD &pos, C Vec &dir, C Vec &color=Vec(1, 1, 1), Flt vol=0, Flt vol_max=0.5f);
  78. #if EE_PRIVATE
  79. void set(Flt shadow_opacity);
  80. Bool toScreenRect(Rect &rect)C {return ToFullScreenRect(pyramid, rect);}
  81. #endif
  82. };
  83. /******************************************************************************/
  84. struct Light
  85. {
  86. LIGHT_TYPE type ; // light type
  87. Bool shadow ; // if shadowing enabled
  88. Flt shadow_opacity; // opacity of shadows
  89. Color image_add ; // dynamic lightmap color add
  90. Flt image_specular, // dynamic lightmap specular
  91. image_scale ; // dynamic lightmap scale
  92. Rect rect ; // on screen rectangle affected by light
  93. CPtr src ; // custom pointer to light source
  94. Image *image ; // dynamic lightmap
  95. union
  96. {
  97. LightDir dir ; // directional light, valid when "type==LIGHT_DIR"
  98. LightPoint point; // point light, valid when "type==LIGHT_POINT"
  99. LightSqr sqr ; // point light, valid when "type==LIGHT_SQR"
  100. LightCone cone ; // cone light, valid when "type==LIGHT_CONE"
  101. };
  102. // get / set
  103. Flt range()C; // get light range (this is equal to 0 for directional lights)
  104. Flt vol ()C; // get light volumetric amount
  105. VecD pos ()C; // get light position (this is equal to (0,0,0) for directional lights)
  106. Light() {} // needed because of union
  107. #if EE_PRIVATE
  108. Bool toScreenRect(Rect &rect)C;
  109. void scalePower(Flt scale);
  110. void set();
  111. void set(LightDir &light, Bool shadow , CPtr light_src);
  112. void set(LightPoint &light, C Rect &rect, Flt shadow_opacity, CPtr light_src);
  113. void set(LightSqr &light, C Rect &rect, Flt shadow_opacity, CPtr light_src);
  114. void set(LightCone &light, C Rect &rect, Flt shadow_opacity, CPtr light_src);
  115. void draw ();
  116. void drawForward(Image *dest, ALPHA_MODE alpha);
  117. #endif
  118. }extern
  119. CurrentLight; // this contains information about the light which is currently rendered
  120. /******************************************************************************/
  121. #if EE_PRIVATE
  122. #define SHADOW_MAP_DIR_RANGE_MUL 8
  123. extern Memc<Light> Lights;
  124. void ShutLight ();
  125. void InitLight ();
  126. void LimitLights();
  127. void SortLights();
  128. void DrawLights();
  129. #endif
  130. /******************************************************************************/