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.

152 lines
5.1 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: light.h
  6. * Content: Direct3D lighting include file
  7. *
  8. ***************************************************************************/
  9. #ifndef __LIGHT_H__
  10. #define __LIGHT_H__
  11. //-----------------------------------------------------------------------
  12. inline void LIGHT_VERTEX(LPD3DFE_PROCESSVERTICES pv, void *in)
  13. {
  14. D3DFE_LIGHTING &ldrv = pv->lighting;
  15. D3DI_LIGHT *light;
  16. ldrv.specularComputed = FALSE;
  17. ldrv.diffuse = ldrv.diffuse0;
  18. ldrv.specular.r = D3DVAL(0);
  19. ldrv.specular.g = D3DVAL(0);
  20. ldrv.specular.b = D3DVAL(0);
  21. light = ldrv.activeLights;
  22. while (light)
  23. {
  24. (*light->lightVertexFunc)(pv, light, (D3DLIGHTINGELEMENT*)in);
  25. light = light->next;
  26. }
  27. {
  28. int r = FTOI(ldrv.diffuse.r);
  29. int g = FTOI(ldrv.diffuse.g);
  30. int b = FTOI(ldrv.diffuse.b);
  31. if (r < 0) r = 0; else if (r > 255) r = 255;
  32. if (g < 0) g = 0; else if (g > 255) g = 255;
  33. if (b < 0) b = 0; else if (b > 255) b = 255;
  34. ldrv.outDiffuse = ldrv.alpha + (r<<16) + (g<<8) + b;
  35. if (ldrv.specularComputed)
  36. {
  37. r = FTOI(ldrv.specular.r);
  38. g = FTOI(ldrv.specular.g);
  39. b = FTOI(ldrv.specular.b);
  40. if (r < 0) r = 0; else if (r > 255) r = 255;
  41. if (g < 0) g = 0; else if (g > 255) g = 255;
  42. if (b < 0) b = 0; else if (b > 255) b = 255;
  43. if (!(pv->dwDeviceFlags & D3DDEV_PREDX6DEVICE))
  44. ldrv.outSpecular = (r<<16) + (g<<8) + b;
  45. else
  46. // DX5 used to copy diffuse alpha to the specular alpha
  47. // Nobody knows why, but we have to preserve the behavior
  48. ldrv.outSpecular = (r<<16) + (g<<8) + b + ldrv.alpha;
  49. }
  50. else
  51. {
  52. if (!(pv->dwDeviceFlags & D3DDEV_PREDX6DEVICE))
  53. ldrv.outSpecular = 0;
  54. else
  55. ldrv.outSpecular = ldrv.alpha;
  56. }
  57. }
  58. }
  59. //-----------------------------------------------------------------------
  60. inline void LIGHT_VERTEX_RAMP(LPD3DFE_PROCESSVERTICES pv, void *in)
  61. {
  62. D3DFE_LIGHTING &ldrv = pv->lighting;
  63. D3DI_LIGHT *light;
  64. ldrv.specularComputed = FALSE;
  65. ldrv.diffuse.r = D3DVAL(0);
  66. ldrv.specular.r = D3DVAL(0);
  67. light = ldrv.activeLights;
  68. while (light)
  69. {
  70. (*light->lightVertexFunc)(pv, light, (D3DLIGHTINGELEMENT*)in);
  71. light = light->next;
  72. }
  73. }
  74. //--------------------------------------------------------------------------
  75. #define D3DFE_SET_ALPHA(color, a) ((char*)&color)[3] = (unsigned char)a;
  76. //--------------------------------------------------------------------------
  77. inline void FOG_VERTEX(D3DFE_LIGHTING *ldrv, D3DVALUE z)
  78. {
  79. int f;
  80. if (z < ldrv->fog_start)
  81. D3DFE_SET_ALPHA(ldrv->outSpecular, 255)
  82. else
  83. if (z >= ldrv->fog_end)
  84. D3DFE_SET_ALPHA(ldrv->outSpecular, 0)
  85. else
  86. {
  87. D3DVALUE v = (ldrv->fog_end - z) * ldrv->fog_factor;
  88. f = FTOI(v);
  89. D3DFE_SET_ALPHA(ldrv->outSpecular, f)
  90. }
  91. }
  92. //--------------------------------------------------------------------------
  93. inline void FOG_VERTEX_RAMP(D3DFE_LIGHTING *ldrv, D3DVALUE z)
  94. {
  95. int f;
  96. if (z > ldrv->fog_start)
  97. {
  98. if (z >= ldrv->fog_end)
  99. {
  100. ldrv->specular.r = D3DVAL(0);
  101. ldrv->diffuse.r = D3DVAL(0);
  102. }
  103. else
  104. {
  105. D3DVALUE v = (ldrv->fog_end - z) * ldrv->fog_factor_ramp;
  106. ldrv->specular.r *= v;
  107. ldrv->diffuse.r *= v;
  108. }
  109. }
  110. }
  111. //--------------------------------------------------------------------------
  112. inline void MAKE_VERTEX_COLOR_RAMP(D3DFE_PROCESSVERTICES *pv, D3DTLVERTEX *vertex)
  113. {
  114. D3DVALUE v;
  115. if (pv->lighting.diffuse.r > 1.0f)
  116. pv->lighting.diffuse.r = 1.0f;
  117. else
  118. if (FLOAT_LEZ(pv->lighting.diffuse.r))
  119. pv->lighting.diffuse.r = 0.0f;
  120. if (pv->dwFlags & D3DPV_RAMPSPECULAR)
  121. {
  122. if (pv->lighting.specular.r > 1.0f)
  123. pv->lighting.specular.r = 1.0f;
  124. else
  125. if (FLOAT_LEZ(pv->lighting.specular.r))
  126. pv->lighting.specular.r = 0.0f;
  127. v = 0.75f * pv->lighting.diffuse.r * (1.0f - pv->lighting.specular.r) +
  128. pv->lighting.specular.r;
  129. }
  130. else
  131. v = pv->lighting.diffuse.r;
  132. // Should be consistent with CI_MAKE macro
  133. vertex->color = pv->lighting.alpha + (((DWORD)(v * pv->dvRampScale) +
  134. pv->dwRampBase) << 8);
  135. vertex->specular = PtrToUlong(pv->lpvRampTexture);
  136. }
  137. //---------------------------------------------------------------------
  138. inline void MakeColor(D3DFE_COLOR *out, DWORD inputColor)
  139. {
  140. out->r = (D3DVALUE)RGB_GETRED(inputColor);
  141. out->g = (D3DVALUE)RGB_GETGREEN(inputColor);
  142. out->b = (D3DVALUE)RGB_GETBLUE(inputColor);
  143. }
  144. extern void D3DFE_UpdateLights(LPDIRECT3DDEVICEI);
  145. #endif /* __LIGHT_H__ */