Blend Light.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /******************************************************************************/
  2. #include "!Header.h"
  3. #include "Ambient Occlusion.h"
  4. #include "Vertex Fog.h"
  5. #include "Fog.h"
  6. #include "Sky.h"
  7. /******************************************************************************/
  8. #define ALPHA_CLIP 0.5f
  9. /******************************************************************************/
  10. #define PARAMS \
  11. uniform Bool skin ,\
  12. uniform Bool color ,\
  13. uniform Int textures ,\
  14. uniform Int bump_mode ,\
  15. uniform Bool alpha_test ,\
  16. uniform Bool alpha ,\
  17. uniform Bool light_map ,\
  18. uniform Bool rflct ,\
  19. uniform Int fx ,\
  20. uniform Bool per_pixel ,\
  21. uniform Int shadow_maps
  22. // when adding "Bool tesselate" here, then remove "Bool tesselate=false;" below
  23. #define use_vel alpha_test
  24. /******************************************************************************/
  25. struct VS_PS
  26. {
  27. Vec pos :TEXCOORD0;
  28. Vec2 tex :TEXCOORD1;
  29. MatrixH3 mtrx :TEXCOORD2; // !! may not be Normalized !!
  30. Vec2 tex_l :TEXCOORD5;
  31. Vec rfl :TEXCOORD6;
  32. Vec vel :TEXCOORD7;
  33. VecH4 col :COLOR0 ;
  34. VecH col_add:COLOR1 ;
  35. };
  36. /******************************************************************************/
  37. // VS
  38. /******************************************************************************/
  39. void VS
  40. (
  41. VtxInput vtx,
  42. out VS_PS O,
  43. out Vec4 O_vtx:POSITION,
  44. PARAMS
  45. )
  46. {
  47. Bool heightmap=false, tesselate=false;
  48. Vec pos=vtx.pos();
  49. VecH nrm, tan; if(bump_mode>=SBUMP_FLAT)nrm=vtx.nrm(); if(bump_mode>SBUMP_FLAT)tan=vtx.tan(nrm, heightmap);
  50. if(textures )O.tex =vtx.tex (heightmap);
  51. if(light_map )O.tex_l=vtx.tex1();
  52. O.col =MaterialColor();
  53. if(color )O.col *=vtx.color();
  54. if(fx==FX_LEAF)
  55. {
  56. if(bump_mode> SBUMP_FLAT)BendLeaf(vtx.hlp(), pos, nrm, tan);else
  57. if(bump_mode==SBUMP_FLAT)BendLeaf(vtx.hlp(), pos, nrm );else
  58. BendLeaf(vtx.hlp(), pos );
  59. }
  60. if(fx==FX_LEAFS)
  61. {
  62. if(bump_mode> SBUMP_FLAT)BendLeafs(vtx.hlp(), vtx.size(), pos, nrm, tan);else
  63. if(bump_mode==SBUMP_FLAT)BendLeafs(vtx.hlp(), vtx.size(), pos, nrm );else
  64. BendLeafs(vtx.hlp(), vtx.size(), pos );
  65. }
  66. if(!skin)
  67. {
  68. #if MODEL>=SM_4 || MODEL==SM_GL
  69. if(true) // instance
  70. {
  71. O.pos=TransformPos(pos, vtx.instance());
  72. if(bump_mode>=SBUMP_FLAT)O.mtrx[2]=TransformDir(nrm, vtx.instance());
  73. if(bump_mode> SBUMP_FLAT)O.mtrx[0]=TransformDir(tan, vtx.instance());
  74. if(fx==FX_GRASS)
  75. {
  76. BendGrass(pos, O.pos, vtx.instance());
  77. O.col.a*=1-GrassFadeOut(vtx.instance());
  78. }
  79. if(use_vel){O.vel=ObjVel[vtx.instance()]; UpdateVelocities_VS(O.vel, pos, O.pos, vtx.instance());} // #PER_INSTANCE_VEL
  80. }else
  81. #endif
  82. {
  83. O.pos=TransformPos(pos);
  84. if(bump_mode>=SBUMP_FLAT)O.mtrx[2]=TransformDir(nrm);
  85. if(bump_mode> SBUMP_FLAT)O.mtrx[0]=TransformDir(tan);
  86. if(fx==FX_GRASS)
  87. {
  88. BendGrass(pos, O.pos);
  89. O.col.a*=1-GrassFadeOut();
  90. }
  91. if(use_vel){O.vel=ObjVel[0]; UpdateVelocities_VS(O.vel, pos, O.pos);}
  92. }
  93. }else
  94. {
  95. VecI bone=vtx.bone();
  96. O.pos=TransformPos(pos, bone, vtx.weight());
  97. if(bump_mode>=SBUMP_FLAT)O.mtrx[2]=TransformDir(nrm, bone, vtx.weight());
  98. if(bump_mode> SBUMP_FLAT)O.mtrx[0]=TransformDir(tan, bone, vtx.weight());
  99. if(use_vel){O.vel=GetBoneVel(bone, vtx.weight()); UpdateVelocities_VS(O.vel, pos, O.pos);}
  100. }
  101. // normalize (have to do all at the same time, so all have the same lengths)
  102. if(bump_mode>SBUMP_FLAT // calculating binormal (this also covers the case when we have tangent from heightmap which is not Normalized)
  103. || rflct && !(per_pixel && bump_mode>SBUMP_FLAT) // per-vertex reflection
  104. || !per_pixel && bump_mode>=SBUMP_FLAT // per-vertex lighting
  105. || tesselate) // needed for tesselation
  106. {
  107. O.mtrx[2]=Normalize(O.mtrx[2]);
  108. if(bump_mode>SBUMP_FLAT)O.mtrx[0]=Normalize(O.mtrx[0]);
  109. }
  110. if(bump_mode>SBUMP_FLAT)O.mtrx[1]=vtx.bin(O.mtrx[2], O.mtrx[0], heightmap);
  111. if(rflct && !(per_pixel && bump_mode>SBUMP_FLAT))O.rfl=Transform3(reflect(Normalize(O.pos), O.mtrx[2]), CamMatrix);
  112. if(per_pixel)
  113. {
  114. Flt d=Length(O.pos);
  115. O.col.a*=Sat(d*SkyFracMulAdd.x + SkyFracMulAdd.y);
  116. Half fog_rev= VisibleOpacity(FogDensity(), d); // fog_rev=1-fog
  117. O.col.rgb*= fog_rev ; // *=1-fog
  118. O.col_add =Lerp(FogColor(), Highlight.rgb, fog_rev); // 1-fog
  119. }else
  120. {
  121. // lighting
  122. if(bump_mode>=SBUMP_FLAT)
  123. {
  124. Half d =Sat(Dot(O.mtrx[2], Light_dir.dir));
  125. VecH lum=Light_dir.color.rgb*d + AmbColor;
  126. O.col.rgb*=lum;
  127. }
  128. // fog
  129. Half fog_rev=Sat(Length2(O.pos)*VertexFogMulAdd.x+VertexFogMulAdd.y); // fog_rev=1-fog
  130. O.col.rgb*= fog_rev ; // *=1-fog
  131. O.col_add =Lerp(VertexFogColor.rgb, Highlight.rgb, fog_rev); // 1-fog
  132. }
  133. O_vtx=Project(O.pos);
  134. }
  135. /******************************************************************************/
  136. // PS
  137. /******************************************************************************/
  138. void PS
  139. (
  140. VS_PS I,
  141. //PIXEL,
  142. IF_IS_FRONT
  143. out Vec4 outCol:COLOR0,
  144. out Vec4 outVel:COLOR1, // #BlendRT
  145. PARAMS
  146. )
  147. {
  148. VecH nrm;
  149. Half specular=MaterialSpecular();
  150. VecH4 tex_nrm; // #MaterialTextureChannelOrder
  151. if(textures==0)
  152. {
  153. if(per_pixel && bump_mode>=SBUMP_FLAT)nrm=Normalize(I.mtrx[2]);
  154. }else
  155. if(textures==1)
  156. {
  157. VecH4 tex_col=Tex(Col, I.tex); if(alpha_test)clip(tex_col.a-ALPHA_CLIP);
  158. if(alpha)I.col*=tex_col;else I.col.rgb*=tex_col.rgb;
  159. if(per_pixel && bump_mode>=SBUMP_FLAT)nrm=Normalize(I.mtrx[2]);
  160. }else
  161. if(textures==2)
  162. {
  163. tex_nrm=Tex(Nrm, I.tex); if(alpha_test)clip(tex_nrm.a-ALPHA_CLIP);
  164. specular *=tex_nrm.z;
  165. if(alpha)I.col.a *=tex_nrm.a;
  166. I.col.rgb*=Tex(Col, I.tex).rgb;
  167. if(per_pixel)
  168. {
  169. if(bump_mode==SBUMP_FLAT)nrm=Normalize(I.mtrx[2]);else
  170. if(bump_mode> SBUMP_FLAT)
  171. {
  172. nrm.xy=(tex_nrm.xy*2-1)*MaterialRough();
  173. nrm.z =CalcZ(nrm.xy);
  174. nrm =Normalize(Transform(nrm, I.mtrx));
  175. }
  176. }
  177. }
  178. if(light_map)I.col.rgb*=Tex(Lum, I.tex_l).rgb;
  179. // reflection
  180. if(rflct)
  181. {
  182. if(per_pixel && bump_mode>SBUMP_FLAT)I.rfl=Transform3(reflect(I.pos, nrm), CamMatrix);
  183. I.col.rgb+=TexCube(Rfl, I.rfl).rgb * ((textures==2) ? MaterialReflect()*tex_nrm.z : MaterialReflect());
  184. }
  185. // calculate lighting
  186. if(per_pixel && bump_mode>=SBUMP_FLAT)
  187. {
  188. VecH total_lum=AmbColor;//+AmbMaterial*MaterialAmbient();
  189. //VecH total_specular=0;
  190. if(fx!=FX_GRASS && fx!=FX_LEAF && fx!=FX_LEAFS)BackFlip(nrm, front);
  191. // directional light
  192. {
  193. // shadow
  194. Half shd; if(shadow_maps)shd=Sat(ShadowDirValue(I.pos, 0, false, shadow_maps, false)); // for improved performance don't use shadow jittering because it's not much needed for blended objects
  195. // diffuse
  196. VecH light_dir=Light_dir.dir;
  197. Half lum =LightDiffuse(nrm, light_dir); if(shadow_maps)lum*=shd;
  198. // specular
  199. /*#if MODEL>=SM_4 // don't use specular at all
  200. BRANCH if(lum*specular>EPS_COL)
  201. #endif
  202. {
  203. Vec eye_dir=Normalize (-I.pos);
  204. Flt spec =LightSpecular( nrm, specular, light_dir, eye_dir); if(shadow_maps)spec*=shd;
  205. total_specular+=Light_dir.color.rgb*spec;
  206. }*/total_lum +=Light_dir.color.rgb*lum ;
  207. }
  208. //UpdateColorBySss(col, nrm, MaterialSss()); // don't use sub-surface scattering at all
  209. // perform lighting
  210. I.col.rgb=I.col.rgb*total_lum;// + total_specular;
  211. }
  212. I.col.rgb+=I.col_add;
  213. outCol=I.col;
  214. if(use_vel){UpdateVelocities_PS(I.vel, I.pos); outVel.xyz=I.vel; outVel.w=I.col.a;} // alpha needed because of blending
  215. }
  216. /******************************************************************************/
  217. // TECHNIQUES
  218. /******************************************************************************/
  219. CUSTOM_TECHNIQUE
  220. /******************************************************************************/
  221. #if 0
  222. @GROUP "Main"
  223. @SHARED
  224. #include "Glsl.h"
  225. #include "Glsl Light.h"
  226. #include "Glsl Material.h"
  227. #define ALPHA_CLIP 0.5
  228. #define use_vel alpha_test
  229. VAR LP Vec4 IO_col;
  230. VAR LP Vec IO_col_add;
  231. #if textures>=1
  232. VAR HP Vec2 IO_tex;
  233. #endif
  234. #if light_map==1
  235. VAR HP Vec2 IO_tex_l;
  236. #endif
  237. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  238. VAR MP Vec IO_nrm;
  239. #endif
  240. #if rflct!=0
  241. VAR MP Vec IO_rfl;
  242. #endif
  243. #if use_vel!=0
  244. VAR HP Vec IO_pos;
  245. VAR MP Vec IO_vel;
  246. #endif
  247. @SHARED_END
  248. @VS
  249. #include "Glsl VS.h"
  250. #include "Glsl VS 3D.h"
  251. void main()
  252. {
  253. HP Vec pos=vtx_pos();
  254. MP Vec nrm=vtx_nrm();
  255. #if use_vel==0
  256. HP Vec IO_pos;
  257. #endif
  258. #if textures>=1
  259. IO_tex=vtx_tex();
  260. #endif
  261. #if light_map==1
  262. IO_tex_l=vtx_tex1();
  263. #endif
  264. IO_col =MaterialColor();
  265. if(COLOR!=0)IO_col*= vtx_color();
  266. #if fx==FX_GRASS
  267. IO_col.a*=1.0-GrassFadeOut();
  268. #elif fx==FX_LEAF
  269. pos=BendLeaf(vtx_hlp(), pos);
  270. #elif fx==FX_LEAFS
  271. pos=BendLeafs(vtx_hlp(), vtx_size(), pos);
  272. #endif
  273. #if skin==0
  274. {
  275. IO_pos= TransformPos(pos, gl_InstanceID) ; if(fx==FX_GRASS)IO_pos+=BendGrass(pos);
  276. if(bump_mode>=SBUMP_FLAT)nrm=Normalize(TransformDir(nrm, gl_InstanceID));
  277. O_vtx= Project (IO_pos);
  278. #if use_vel!=0
  279. IO_vel=ObjVel[gl_InstanceID]; // #PER_INSTANCE_VEL
  280. #endif
  281. }
  282. #else
  283. {
  284. MP VecI bone =vtx_bone ();
  285. MP Vec weight=vtx_weight();
  286. IO_pos= TransformPos(pos, bone, weight) ;
  287. if(bump_mode>=SBUMP_FLAT)nrm=Normalize(TransformDir(nrm, bone, weight));
  288. O_vtx= Project (IO_pos);
  289. #if use_vel!=0
  290. IO_vel=GetBoneVel(bone, weight);
  291. #endif
  292. }
  293. #endif
  294. #if per_pixel==0
  295. {
  296. if(bump_mode>=SBUMP_FLAT)
  297. {
  298. LP Flt d =Max(Dot(nrm, Light_dir.dir), 0.0);
  299. LP Vec lum=Light_dir.color.rgb*d + AmbColor;
  300. IO_col.rgb*=lum;
  301. }
  302. LP Flt fog_rev=Sat(Length2(IO_pos)*VertexFogMulAdd.x+VertexFogMulAdd.y);
  303. IO_col.rgb*= fog_rev ;
  304. IO_col_add =Lerp(VertexFogColor.rgb, Highlight.rgb, fog_rev);
  305. }
  306. #else
  307. {
  308. #if bump_mode>=SBUMP_FLAT
  309. IO_nrm=nrm;
  310. #endif
  311. MP Vec mp_pos =IO_pos;
  312. MP Flt d =Length(mp_pos);
  313. LP Flt opacity=Sat(d*SkyFracMulAdd.x + SkyFracMulAdd.y);
  314. IO_col.a*=opacity;
  315. LP Flt fog_rev= VisibleOpacity(FogDensity(), d);
  316. IO_col.rgb*= fog_rev ;
  317. IO_col_add =Lerp(FogColor(), Highlight.rgb, fog_rev);
  318. }
  319. #endif
  320. #if rflct!=0
  321. {
  322. IO_rfl=Normalize(IO_pos); // convert to MP
  323. #if !(per_pixel!=0 && bump_mode>=SBUMP_FLAT)
  324. IO_rfl=Transform3(reflect(IO_rfl, nrm), CamMatrix);
  325. #endif
  326. }
  327. #endif
  328. #if use_vel!=0
  329. UpdateVelocities_VS(IO_vel, pos, IO_pos);
  330. #endif
  331. }
  332. @VS_END
  333. @PS
  334. #include "Glsl PS.h"
  335. #include "Glsl 3D.h"
  336. void main()
  337. {
  338. MP Vec nrm;
  339. LP Vec4 col=IO_col;
  340. #if textures==0
  341. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  342. nrm=Normalize(IO_nrm);
  343. #endif
  344. #elif textures==1
  345. LP Vec4 tex_col=Tex(Col, IO_tex);
  346. #if alpha_test
  347. if(tex_col.a<ALPHA_CLIP)discard;
  348. #endif
  349. #if ALPHA
  350. col*=tex_col;
  351. #else
  352. col.rgb*=tex_col.rgb;
  353. #endif
  354. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  355. nrm=Normalize(IO_nrm);
  356. #endif
  357. #elif textures==2
  358. LP Vec4 tex_nrm=Tex(Nrm, IO_tex); // #MaterialTextureChannelOrder
  359. #if alpha_test
  360. if(tex_nrm.a<ALPHA_CLIP)discard;
  361. #endif
  362. col.rgb*=Tex(Col, IO_tex).rgb;
  363. #if ALPHA
  364. col.a *=tex_nrm.a;
  365. #endif
  366. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  367. nrm=Normalize(IO_nrm);
  368. #endif
  369. #endif
  370. #if light_map==1
  371. col.rgb*=Tex(Lum, IO_tex_l).rgb;
  372. #endif
  373. #if rflct!=0
  374. MP Vec rfl=IO_rfl;
  375. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  376. rfl=Transform3(reflect(rfl, nrm), CamMatrix);
  377. #endif
  378. #if textures==2
  379. col.rgb+=TexCube(Rfl, rfl).rgb*(MaterialReflect()*tex_nrm.z);
  380. #else
  381. col.rgb+=TexCube(Rfl, rfl).rgb*MaterialReflect();
  382. #endif
  383. #endif
  384. #if per_pixel!=0 && bump_mode>=SBUMP_FLAT
  385. {
  386. LP Vec total_lum=AmbColor;
  387. if(fx!=FX_GRASS && fx!=FX_LEAF && fx!=FX_LEAFS)BackFlip(nrm);
  388. // directional light
  389. LP Flt d =Max(Dot(nrm, Light_dir.dir), 0.0);
  390. total_lum+=Light_dir.color.rgb*d;
  391. col.rgb*=total_lum;
  392. }
  393. #endif
  394. col.rgb+=IO_col_add;
  395. gl_FragData[0]=col;
  396. #if use_vel!=0
  397. gl_FragData[1].xyz=UpdateVelocities_PS(IO_vel, IO_pos);
  398. gl_FragData[1].w =col.a; // alpha needed because of blending
  399. #endif
  400. }
  401. @PS_END
  402. @GROUP_END
  403. #endif
  404. /******************************************************************************/