d3dcalls.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:11k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. //----------------------------------------------------------------
  2. //
  3. //  File: d3dcalls.c
  4. //
  5. //  Calls to Direct3D objects needed for rendering.  Part of D3DApp.
  6. //
  7. //----------------------------------------------------------------
  8. // Borland CBuilder3 doesn't support nameless unions...
  9. #include "stdafx.h"
  10. #include "XMudClient.h"
  11. #include "d3dappi.h"
  12. //**************************************************************************
  13. //                            Creation of D3D
  14. //*************************************************************************
  15. BOOL D3DAppICreateD3D(void)
  16. {
  17.   LastError = d3dappi.lpDD->QueryInterface(IID_IDirect3D2, (LPVOID*)&d3dappi.lpD3D);
  18.   if (LastError != DD_OK)
  19.   {
  20.     D3DAppISetErrorString("Creation of IDirect3D failed.n%s",
  21.       D3DAppErrorToString(LastError));
  22.     goto exit_with_error;
  23.   }
  24.   return TRUE;
  25. exit_with_error:
  26.   return FALSE;
  27. }
  28. /***************************************************************************/
  29. /*                           D3D Device Enumeration                        */
  30. /***************************************************************************/
  31. /*
  32. * enumDeviceFunc
  33. * Device enumeration callback.  Record information about the D3D device
  34. * reported by D3D.
  35. */
  36. static HRESULT
  37. WINAPI enumDeviceFunc(LPGUID lpGuid, LPSTR lpDeviceDescription,
  38.                       LPSTR lpDeviceName, LPD3DDEVICEDESC lpHWDesc,
  39.                       LPD3DDEVICEDESC lpHELDesc, LPVOID lpContext)
  40. {
  41.   lpContext = lpContext;
  42.   /*
  43.   * Don't accept any hardware D3D devices if emulation only option is set
  44.   */
  45.   if (lpHWDesc->dcmColorModel && d3dappi.bOnlyEmulation)
  46.     return D3DENUMRET_OK;
  47.     /*
  48.     * Record the D3D driver's inforamation
  49.   */
  50.   memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Guid, lpGuid, sizeof(GUID));
  51.   lstrcpy(d3dappi.Driver[d3dappi.NumDrivers].About, lpDeviceDescription);
  52.   lstrcpy(d3dappi.Driver[d3dappi.NumDrivers].Name, lpDeviceName);
  53.   /*
  54.   * Is this a hardware device or software emulation?  Checking the color
  55.   * model for a valid model works.
  56.   */
  57.   if (lpHWDesc->dcmColorModel) {
  58.     d3dappi.Driver[d3dappi.NumDrivers].bIsHardware = TRUE;
  59.     memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Desc, lpHWDesc,
  60.       sizeof(D3DDEVICEDESC));
  61.   } else {
  62.     d3dappi.Driver[d3dappi.NumDrivers].bIsHardware = FALSE;
  63.     memcpy(&d3dappi.Driver[d3dappi.NumDrivers].Desc, lpHELDesc,
  64.       sizeof(D3DDEVICEDESC));
  65.   }
  66.   /*
  67.   * Does this driver do texture mapping?
  68.   */
  69.   d3dappi.Driver[d3dappi.NumDrivers].bDoesTextures =
  70.     (d3dappi.Driver[d3dappi.NumDrivers].Desc.dpcTriCaps.dwTextureCaps &
  71.     D3DPTEXTURECAPS_PERSPECTIVE) ? TRUE : FALSE;
  72.     /*
  73.     * Can this driver use a z-buffer?
  74.   */
  75.   d3dappi.Driver[d3dappi.NumDrivers].bDoesZBuffer =
  76.     d3dappi.Driver[d3dappi.NumDrivers].Desc.dwDeviceZBufferBitDepth
  77.     ? TRUE : FALSE;
  78.     /*
  79.     * Can this driver render to the Windows display depth
  80.   */
  81.   d3dappi.Driver[d3dappi.NumDrivers].bCanDoWindow =
  82.     (d3dappi.Driver[d3dappi.NumDrivers].Desc.dwDeviceRenderBitDepth &
  83.     D3DAppIBPPToDDBD(d3dappi.WindowsDisplay.bpp)) ? TRUE : FALSE;
  84.   if (!d3dappi.bIsPrimary)
  85.     d3dappi.Driver[d3dappi.NumDrivers].bCanDoWindow = FALSE;
  86.   
  87.   d3dappi.NumDrivers++;
  88.   if (d3dappi.NumDrivers == D3DAPP_MAXD3DDRIVERS)
  89.     return (D3DENUMRET_CANCEL);
  90.   return (D3DENUMRET_OK);
  91. }
  92. /*
  93. * D3DAppIEnumDevices
  94. * Get the available drivers from Direct3D by enumeration.
  95. */
  96. BOOL D3DAppIEnumDevices(void)
  97. {
  98.   d3dappi.NumDrivers = 0;
  99.   LastError = d3dappi.lpD3D->EnumDevices(enumDeviceFunc, NULL);
  100.   if (LastError != DD_OK)
  101.   {
  102.     D3DAppISetErrorString("Enumeration of drivers failed.n%s",
  103.       D3DAppErrorToString(LastError));
  104.     return FALSE;
  105.   }
  106.   d3dappi.CurrDriver = 0;
  107.   return TRUE;
  108. }
  109. //***************************************************************************/
  110. //*                    Enumeration of texure format                         */
  111. //***************************************************************************/
  112. //
  113. // EnumTextureFormatsCallback
  114. // Record information about each texture format the current D3D driver can
  115. // support. Choose one as the default format (paletted formats are prefered)
  116. // and return it through lpContext.
  117. //
  118. static HRESULT CALLBACK EnumTextureFormatsCallback(LPDDSURFACEDESC lpDDSD, LPVOID lpContext)
  119. {
  120.   unsigned long m;
  121.   int r, g, b;
  122.   int *lpStartFormat = (int *)lpContext;
  123.   //
  124.   // Record the DDSURFACEDESC of this texture format
  125.   //
  126.   memset(&d3dappi.TextureFormat[d3dappi.NumTextureFormats], 0,
  127.     sizeof(D3DAppTextureFormat));
  128.   memcpy(&d3dappi.TextureFormat[d3dappi.NumTextureFormats].ddsd, lpDDSD,
  129.     sizeof(DDSURFACEDESC));
  130.   //
  131.   // Is this format palettized?  How many bits?  Otherwise, how many RGB
  132.   // bits?
  133.   //
  134.   if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8)
  135.   {
  136.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = TRUE;
  137.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 8;
  138.   } 
  139.   else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED4)
  140.   {
  141.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = TRUE;
  142.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 4;
  143.   }
  144.   else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS)
  145.   {
  146.     //
  147.     // The sample apps don't currently understand
  148.     // the alpha bit - just filter this format
  149.     // away for now.
  150.     //
  151.     return DDENUMRET_OK;
  152.   }
  153.   else
  154.   {
  155.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized = FALSE;
  156.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].IndexBPP = 0;
  157.     for (r = 0, m = lpDDSD->ddpfPixelFormat.dwRBitMask; !(m & 1);
  158.     r++, m >>= 1);
  159.     for (r = 0; m & 1; r++, m >>= 1);
  160.     for (g = 0, m = lpDDSD->ddpfPixelFormat.dwGBitMask; !(m & 1);
  161.     g++, m >>= 1);
  162.     for (g = 0; m & 1; g++, m >>= 1);
  163.     for (b = 0, m = lpDDSD->ddpfPixelFormat.dwBBitMask; !(m & 1);
  164.     b++, m >>= 1);
  165.     for (b = 0; m & 1; b++, m >>= 1);
  166.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].RedBPP = r;
  167.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].GreenBPP = g;
  168.     d3dappi.TextureFormat[d3dappi.NumTextureFormats].BlueBPP = b;
  169.   }
  170.   //
  171.   // If lpStarFormat is -1, this is the first format.  Select it.
  172.   //
  173.   if (*lpStartFormat == -1)
  174.     *lpStartFormat = d3dappi.NumTextureFormats;
  175.   //
  176.   // If this format is paletted, select it.
  177.   //
  178.   if (d3dappi.TextureFormat[d3dappi.NumTextureFormats].bPalettized)
  179.   {
  180.     *lpStartFormat = d3dappi.NumTextureFormats;
  181.   }
  182.   d3dappi.NumTextureFormats++;
  183.   return DDENUMRET_OK;
  184. }
  185. //
  186. // D3DAppIEnumTextureFormats
  187. // Get a list of available texture map formats from the Direct3D driver by
  188. // enumeration.  Choose a default format (paletted is prefered).
  189. //
  190. BOOL D3DAppIEnumTextureFormats(void)
  191. {
  192.   int StartFormat;
  193.   //
  194.   // Set the default format to -1 to let the callback know it's being
  195.   // called for the first time.
  196.   //
  197.   StartFormat = -1;
  198.   d3dappi.NumTextureFormats = 0;
  199.   LastError = d3dappi.lpD3DDevice->EnumTextureFormats(EnumTextureFormatsCallback, (LPVOID)&StartFormat);
  200.   if (LastError != DD_OK)
  201.   {
  202.     D3DAppISetErrorString("Enumeration of texture formats failed.n%s",
  203.       D3DAppErrorToString(LastError));
  204.     return FALSE;
  205.   }
  206.   memcpy(&d3dappi.ThisTextureFormat, &d3dappi.TextureFormat[StartFormat], sizeof(D3DAppTextureFormat));
  207.   d3dappi.CurrTextureFormat = StartFormat;
  208.   return TRUE;
  209. }
  210. /***************************************************************************/
  211. /*                               Device creation                           */
  212. /***************************************************************************/
  213. /*
  214. * D3DAppICreateDevice
  215. * Create the D3D device and enumerate the texture formats
  216. */
  217. BOOL D3DAppICreateDevice(int driver)
  218. {
  219.   RELEASE(d3dappi.lpD3DDevice);
  220.   
  221.   if (d3dappi.Driver[driver].bIsHardware && !d3dappi.bBackBufferInVideo) {
  222.     D3DAppISetErrorString("Could not fit the rendering surfaces in video memory for this hardware device.n");
  223.     goto exit_with_error;
  224.   }
  225.   
  226.   d3dappi.CurrDriver = driver;
  227.   memcpy(&d3dappi.ThisDriver, &d3dappi.Driver[driver], sizeof(D3DAppD3DDriver));
  228.   
  229.   LastError = d3dappi.lpD3D->CreateDevice(d3dappi.Driver[driver].Guid,
  230.     d3dappi.lpBackBuffer,
  231.     &d3dappi.lpD3DDevice);
  232.   if (LastError != DD_OK)
  233.   {
  234.     D3DAppISetErrorString("Create D3D device failed.n%s",
  235.       D3DAppErrorToString(LastError));
  236.     goto exit_with_error;
  237.   }
  238.   
  239.   d3dappi.CurrDriver = driver;
  240.   d3dappi.NumTextureFormats = 0;
  241.   if (d3dappi.Driver[driver].bDoesTextures)
  242.   {
  243.     if (!D3DAppIEnumTextureFormats())
  244.       goto exit_with_error;
  245.   }
  246.   
  247.   return TRUE;
  248. exit_with_error:
  249.   RELEASE(d3dappi.lpD3DDevice);
  250.   return FALSE;
  251. }
  252. /***************************************************************************/
  253. /*                      Setting the render state                           */
  254. /***************************************************************************/
  255. /*
  256. * D3DAppISetRenderState
  257. * Set the render state and light state for the current viewport.
  258. */
  259. BOOL D3DAppISetRenderState()
  260. {
  261. //  return TRUE;//[1999.9.1]
  262.   //
  263.   // Set render state
  264.   //
  265.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_SHADEMODE, d3dapprs.ShadeMode);
  266.   
  267.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREPERSPECTIVE, d3dapprs.bPerspCorrect);
  268.   
  269.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, d3dapprs.bZBufferOn && d3dappi.ThisDriver.bDoesZBuffer);
  270.   
  271.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, d3dapprs.bZBufferOn);
  272.   
  273.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_ZFUNC, D3DCMP_LESSEQUAL);
  274.   
  275.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREMAG, d3dapprs.TextureFilter);
  276.   
  277.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREMIN, d3dapprs.TextureFilter);
  278.   
  279. //  d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREMAPBLEND, d3dapprs.TextureBlend);
  280.   
  281.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FILLMODE, d3dapprs.FillMode);
  282.   
  283.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, d3dapprs.bDithering);
  284.   
  285.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, d3dapprs.bSpecular);
  286.   
  287.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_ANTIALIAS, d3dapprs.bAntialiasing);
  288.   
  289.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, d3dapprs.bFogEnabled);
  290.   
  291.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGCOLOR, d3dapprs.FogColor);
  292.   
  293.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
  294.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_BLENDENABLE, TRUE);
  295.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
  296.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
  297.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_TEXTUREMAPBLEND, D3DTBLEND_MODULATEALPHA);
  298.   /*
  299.   * Set light state
  300.   */
  301.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEMODE, d3dapprs.bFogEnabled ? d3dapprs.FogMode : D3DFOG_NONE);
  302.   
  303.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGTABLESTART, *(unsigned long*)&d3dapprs.FogStart);
  304.   
  305.   d3dappi.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGTABLEEND, *(unsigned long*)&d3dapprs.FogEnd);
  306.   return TRUE;
  307. }