Source code of Windows XP (NT5)
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.

247 lines
8.1 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // rastattr.cpp
  5. //
  6. // Direct3D Reference Device -
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "pch.cpp"
  10. #pragma hdrstop
  11. //-----------------------------------------------------------------------------
  12. //
  13. //-----------------------------------------------------------------------------
  14. void RDAttribute::Init(
  15. RefRast* pRefRast, // RefRast with which this attrib is used
  16. UINT cDimensionality,
  17. BOOL bPerspective,
  18. BOOL bClamp )
  19. {
  20. m_pRR = pRefRast;
  21. m_cDimensionality = cDimensionality;
  22. m_bPerspective = bPerspective;
  23. m_bClamp = bClamp;
  24. m_cProjection = 0;
  25. m_dwWrapFlags = 0x0;
  26. m_bFlatShade = FALSE;
  27. }
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Sampling Routines
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. //-----------------------------------------------------------------------------
  34. //
  35. // Sample - Sample attribute at given location.
  36. //
  37. //-----------------------------------------------------------------------------
  38. void RDAttribute::Sample(
  39. FLOAT* pSample,
  40. FLOAT fX,
  41. FLOAT fY,
  42. BOOL bNoProjectionOverride, // disables projection if TRUE
  43. BOOL bClampOverride) // enables (forces) clamp if TRUE
  44. {
  45. FLOAT fPScale = 1.0F;
  46. if (m_cProjection && !m_bFlatShade && !bNoProjectionOverride)
  47. {
  48. // note that perspective is already incorporated into projective coord
  49. fPScale = 1.0F/( fX*m_fA[m_cProjection] + fY*m_fB[m_cProjection] + m_fC[m_cProjection] );
  50. }
  51. else if (m_bPerspective && !m_bFlatShade)
  52. {
  53. fPScale = m_pRR->m_fW[m_pRR->m_iPix];
  54. }
  55. for ( UINT i=0; i<m_cDimensionality; i++)
  56. {
  57. if (m_bFlatShade)
  58. {
  59. *(pSample+i) = m_fC[i];
  60. }
  61. else
  62. {
  63. *(pSample+i) =
  64. fPScale * ( fX*m_fA[i] + fY*m_fB[i] + m_fC[i] );
  65. }
  66. if (m_bClamp || bClampOverride)
  67. {
  68. *(pSample+i) = MIN( 1.F, MAX( 0.F, *(pSample+i) ) );
  69. }
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. //
  74. // Sample - Sample scalar attribute at given location. Assumes no perspective
  75. // or projection. (Used for W or Depth.)
  76. //
  77. //-----------------------------------------------------------------------------
  78. FLOAT RDAttribute::Sample(
  79. FLOAT fX,
  80. FLOAT fY)
  81. {
  82. return fX*m_fA[0] + fY*m_fB[0] + m_fC[0];
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. //
  86. // Setup Routines
  87. //
  88. ///////////////////////////////////////////////////////////////////////////////
  89. //-----------------------------------------------------------------------------
  90. //
  91. // WrapDiff - returns the difference (B-A) as defined under the D3D WRAPU/V
  92. // rules which is the shortest path between the two assuming a coincident
  93. // position at 1. and 0. The fA and fB input range is 0. to 1.
  94. //
  95. //-----------------------------------------------------------------------------
  96. static FLOAT
  97. WrapDiff( FLOAT fB, FLOAT fA )
  98. {
  99. // compute straight distance
  100. FLOAT fDist1 = fB - fA;
  101. // compute distance 'warping' between 0. and 1.
  102. FLOAT fDist2 = ( fDist1 < 0 ) ? ( fDist1+1 ) : ( fDist1-1 );
  103. // return minimum of these
  104. return ( fabs( fDist1) < fabs( fDist2) ) ? ( fDist1) : ( fDist2 );
  105. }
  106. //-----------------------------------------------------------------------------
  107. //
  108. //-----------------------------------------------------------------------------
  109. void RDAttribute::Setup(
  110. const FLOAT* pVtx0, const FLOAT* pVtx1, const FLOAT* pVtx2)
  111. {
  112. if (m_pRR->m_bIsLine)
  113. {
  114. LineSetup( pVtx0, pVtx1, pVtx2 );
  115. return;
  116. }
  117. for ( UINT i=0; i<m_cDimensionality; i++)
  118. {
  119. FLOAT fVal0 = (pVtx0) ? (*(pVtx0+i)) : (0.);
  120. FLOAT fVal1 = (pVtx1) ? (*(pVtx1+i)) : (0.);
  121. FLOAT fVal2 = (pVtx2) ? (*(pVtx2+i)) : (0.);
  122. if (m_bFlatShade)
  123. {
  124. m_fA[i] = m_fB[i] = 0.F;
  125. switch ( m_pRR->m_iFlatVtx )
  126. {
  127. default:
  128. case 0: m_fC[i] = fVal0; break;
  129. case 1: m_fC[i] = fVal1; break;
  130. case 2: m_fC[i] = fVal2; break;
  131. }
  132. continue;
  133. }
  134. // extract wrap flag for this dimension
  135. BOOL bWrap = m_dwWrapFlags & (1<<i);
  136. // compute adjusted values for vertices 1,2 based on wrap flag
  137. FLOAT fVal1P = bWrap ? ( fVal0 + WrapDiff(fVal1,fVal0) ) : (fVal1);
  138. FLOAT fVal2P = bWrap ? ( fVal0 + WrapDiff(fVal2,fVal0) ) : (fVal2);
  139. // compute (maybe) perspective corrected linear deltas along two edges
  140. FLOAT fRHW0 = (m_bPerspective) ? (m_pRR->m_fRHW0) : (1.0F);
  141. FLOAT fRHW1 = (m_bPerspective) ? (m_pRR->m_fRHW1) : (1.0F);
  142. FLOAT fRHW2 = (m_bPerspective) ? (m_pRR->m_fRHW2) : (1.0F);
  143. FLOAT fDelAttrib10 = ( fVal1P * fRHW1 ) - ( fVal0 * fRHW0 );
  144. FLOAT fDelAttrib20 = ( fVal2P * fRHW2 ) - ( fVal0 * fRHW0 );
  145. // compute A & B terms (dVdX and dVdY)
  146. m_fA[i] = m_pRR->m_fTriOODet *
  147. ( fDelAttrib10 * m_pRR->m_fDelY20 + fDelAttrib20 * m_pRR->m_fDelY01 );
  148. m_fB[i] = m_pRR->m_fTriOODet *
  149. ( fDelAttrib20 * m_pRR->m_fDelX10 + fDelAttrib10 * m_pRR->m_fDelX02 );
  150. // compute C term (Fv = A*Xv + B*Yv + C => C = Fv - A*Xv - B*Yv)
  151. m_fC[i] = ( fVal0 * fRHW0 )
  152. - ( m_fA[i] * m_pRR->m_fX0 ) - ( m_fB[i] * m_pRR->m_fY0 );
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. //
  157. //-----------------------------------------------------------------------------
  158. void RDAttribute::LineSetup(
  159. const FLOAT* pVtx0, const FLOAT* pVtx1, const FLOAT* pVtxFlat)
  160. {
  161. for ( UINT i=0; i<m_cDimensionality; i++)
  162. {
  163. FLOAT fVal0 = (pVtx0) ? (*(pVtx0+i)) : (0.);
  164. FLOAT fVal1 = (pVtx1) ? (*(pVtx1+i)) : (0.);
  165. if (m_bFlatShade)
  166. {
  167. m_fA[i] = m_fB[i] = 0.F;
  168. m_fC[i] = (pVtxFlat) ? (*(pVtxFlat+i)) : fVal0;
  169. continue;
  170. }
  171. // extract wrap flag for this dimension
  172. BOOL bWrap = m_dwWrapFlags & (1<<i);
  173. // compute adjusted values for vertices 1,2 based on wrap flag
  174. FLOAT fVal1P = bWrap ? ( fVal0 + WrapDiff(fVal1,fVal0) ) : (fVal1);
  175. // compute (maybe) perspective corrected linear deltas along two edges
  176. FLOAT fRHW0 = (m_bPerspective) ? (m_pRR->m_fRHW0) : (1.0F);
  177. FLOAT fRHW1 = (m_bPerspective) ? (m_pRR->m_fRHW1) : (1.0F);
  178. FLOAT fDelta = ( fVal1P*fRHW1 - fVal0*fRHW0) / m_pRR->m_fLineMajorLength;
  179. m_fA[i] = ( m_pRR->m_bLineXMajor ) ? ( fDelta ) : ( 0. );
  180. m_fB[i] = ( m_pRR->m_bLineXMajor ) ? ( 0. ) : ( fDelta );
  181. // compute C term (Fv = A*Xv + B*Yv + C => C = Fv - A*Xv - B*Yv)
  182. m_fC[i] = ( fVal0* fRHW0)
  183. - ( m_fA[i] * m_pRR->m_fX0 ) - ( m_fB[i] * m_pRR->m_fY0 );
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. //
  188. // Setup attribute given packed DWORD color. Color format is that of the
  189. // colors in the FVF vertex, which corresponds to D3DFMT_A8R8G8B8 (and is
  190. // the same as D3DCOLOR).
  191. //
  192. //-----------------------------------------------------------------------------
  193. void RDAttribute::Setup(
  194. DWORD dwVtx0, DWORD dwVtx1, DWORD dwVtx2)
  195. {
  196. FLOAT fVtx0[4];
  197. FLOAT fVtx1[4];
  198. FLOAT fVtx2[4];
  199. fVtx0[0] = RGBA_GETRED( dwVtx0 ) * (1./255.);
  200. fVtx0[1] = RGBA_GETGREEN( dwVtx0 ) * (1./255.);
  201. fVtx0[2] = RGBA_GETBLUE( dwVtx0 ) * (1./255.);
  202. fVtx0[3] = RGBA_GETALPHA( dwVtx0 ) * (1./255.);
  203. fVtx1[0] = RGBA_GETRED( dwVtx1 ) * (1./255.);
  204. fVtx1[1] = RGBA_GETGREEN( dwVtx1 ) * (1./255.);
  205. fVtx1[2] = RGBA_GETBLUE( dwVtx1 ) * (1./255.);
  206. fVtx1[3] = RGBA_GETALPHA( dwVtx1 ) * (1./255.);
  207. fVtx2[0] = RGBA_GETRED( dwVtx2 ) * (1./255.);
  208. fVtx2[1] = RGBA_GETGREEN( dwVtx2 ) * (1./255.);
  209. fVtx2[2] = RGBA_GETBLUE( dwVtx2 ) * (1./255.);
  210. fVtx2[3] = RGBA_GETALPHA( dwVtx2 ) * (1./255.);
  211. Setup( fVtx0, fVtx1, fVtx2);
  212. }
  213. ///////////////////////////////////////////////////////////////////////////////
  214. // end