Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 1997.
  3. //
  4. // shadow.cpp
  5. //
  6. // Direct3D Reference Rasterizer - Shadow Mapping Methods
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. //-----------------------------------------------------------------------------
  12. //
  13. // Fast but adequate 16 bit linear congruential random number generators
  14. //
  15. // fRand returns 0.0 to 1.0, fRand2 returns -1.0 to 1.0
  16. //
  17. //-----------------------------------------------------------------------------
  18. static UINT16 _uRandDum = 123;
  19. static FLOAT fRand(void)
  20. {
  21. // Slower 32 bit LC random number generator
  22. // static long _uRandDum = 123;
  23. // idum = 1664525L*_uRandDum + 1013904223L;
  24. _uRandDum = 25173*_uRandDum + 13849;
  25. return ((FLOAT)_uRandDum/(FLOAT)0xffff);
  26. }
  27. //
  28. static FLOAT fRand2(void)
  29. {
  30. _uRandDum = 25173*_uRandDum + 13849;
  31. return ((FLOAT)_uRandDum/(FLOAT)0x8000) - 1.0F;
  32. }
  33. //-----------------------------------------------------------------------------
  34. //
  35. // DoShadow - Performs Shadow Z buffer Algorithm on a per-fragment basis.
  36. //
  37. //-----------------------------------------------------------------------------
  38. void RRTexture::DoShadow(INT32 iStage, FLOAT* pfCoord, RRColor& OutputColor)
  39. {
  40. #ifdef __SHADOWBUFFER
  41. FLOAT fW = pfCoord[3];
  42. // set output color to white in case there is no attenuation
  43. OutputColor = 0xffffffff;
  44. // don't shadow behind the light
  45. if (fW > 0.0F)
  46. {
  47. // these are already multiplied by fW
  48. FLOAT fU = pfCoord[0];
  49. FLOAT fV = pfCoord[1];
  50. FLOAT fZ = pfCoord[2];
  51. /////////////////////////////////////////////////
  52. // Do shadow filter
  53. /////////////////////////////////////////////////
  54. fZ -= m_pStageState[iStage].m_fVal[D3DTSS_SHADOWZBIASMIN];
  55. FLOAT fZRange = m_pStageState[iStage].m_fVal[D3DTSS_SHADOWZBIASMAX] -
  56. m_pStageState[iStage].m_fVal[D3DTSS_SHADOWZBIASMIN];
  57. if (fZ >= 0.0F)
  58. {
  59. FLOAT fShad;
  60. FLOAT fAtten = m_pStageState[iStage].m_fVal[D3DTSS_SHADOWATTENUATION];
  61. if (fZ > 1.0F)
  62. {
  63. // full shadow
  64. fShad = fAtten;
  65. }
  66. else
  67. {
  68. INT32 iFilterSize = m_pStageState[iStage].m_dwVal[D3DTSS_MAGFILTER] - D3DTFG_SHADOW_1 + 1;
  69. UINT32 uFilterArea = iFilterSize*iFilterSize;
  70. INT32 iMaskU = m_iWidth - 1;
  71. INT32 iMaskV = m_iHeight - 1;
  72. FLOAT fUCenter = (fU * m_iWidth/2) + m_iWidth/2;
  73. FLOAT fVCenter = (-fV * m_iHeight/2) + m_iHeight/2;
  74. INT32 u, v;
  75. UINT32 uShad = 0;
  76. for (v = -(iFilterSize-1)/2; v <= iFilterSize/2; v++)
  77. {
  78. for (u = -(iFilterSize-1)/2; u <= iFilterSize/2; u++)
  79. {
  80. // Now, do U, V jitter
  81. FLOAT fU = m_pStageState[iStage].m_fVal[D3DTSS_SHADOWSIZE]*fRand2();
  82. FLOAT fV = m_pStageState[iStage].m_fVal[D3DTSS_SHADOWSIZE]*fRand2();
  83. // add offset to center of sample
  84. fU += fUCenter;
  85. fV += fVCenter;
  86. INT32 iU = u + (INT32)fU;
  87. INT32 iV = v + (INT32)fV;
  88. if (((iU & ~iMaskU) == 0) && ((iV & ~iMaskV) == 0)) {
  89. FLOAT fZJit = fZRange*fRand();
  90. RRColor Texel;
  91. BOOL bColorKeyMatched; // ignore this for shadow mapping
  92. ReadColor( iU, iV, 0, Texel, bColorKeyMatched );
  93. if ( fZ > (FLOAT(Texel.G) + fZJit) ) {
  94. // in shadow
  95. uShad++;
  96. }
  97. }
  98. }
  99. }
  100. fShad = (FLOAT)(uFilterArea - uShad);
  101. fShad = (1.0F - fAtten)*fShad/(FLOAT)uFilterArea + fAtten;
  102. fShad = min(fShad, 1.0F);
  103. }
  104. if (fShad < 1.0F)
  105. {
  106. OutputColor.A = fShad;
  107. OutputColor.R = fShad;
  108. OutputColor.G = fShad;
  109. OutputColor.B = fShad;
  110. }
  111. }
  112. }
  113. #endif //__SHADOWBUFFER
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////
  116. // end