home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / pplane / d3dutils.inl < prev    next >
Text File  |  1997-07-14  |  15KB  |  628 lines

  1. /*
  2. **-----------------------------------------------------------------------------
  3. ** Name:    D3DUtils.inl
  4. ** Purpose: Various D3D utility functions
  5. ** Notes:   This defines inline functions only
  6. **
  7. ** Copyright (c) 1995 - 1997 by Microsoft, all rights reserved
  8. **-----------------------------------------------------------------------------
  9. */
  10.  
  11. /*
  12. **-----------------------------------------------------------------------------
  13. **    Light Inline Methods
  14. **-----------------------------------------------------------------------------
  15. */
  16.  
  17. /*
  18. **-----------------------------------------------------------------------------
  19. **  Name:    Light::AddToViewport
  20. **-----------------------------------------------------------------------------
  21. */
  22.  
  23. inline HRESULT
  24. Light::AddToViewport (LPDIRECT3DVIEWPORT2 lpView)
  25. {
  26.     Set();
  27.     return lpView->AddLight(lpD3DLight);
  28. } // end Light::AddToViewport
  29.  
  30.  
  31. /*
  32. **-----------------------------------------------------------------------------
  33. **  Name:    Light::RemoveFromViewport
  34. **-----------------------------------------------------------------------------
  35. */
  36.  
  37. inline HRESULT
  38. Light::RemoveFromViewport (LPDIRECT3DVIEWPORT2 lpView)
  39. {
  40.     return lpView->DeleteLight(lpD3DLight);
  41. } // end Light::RemoveFromViewport
  42.  
  43.  
  44.   
  45. /*
  46. **-----------------------------------------------------------------------------
  47. **  Name:    Light::Set
  48. **-----------------------------------------------------------------------------
  49. */
  50.  
  51. inline HRESULT    
  52. Light::Set (void)
  53. {    
  54.     if (changed) {
  55.         changed = 0;
  56.         return lpD3DLight->SetLight((D3DLIGHT *)&light);
  57.     } 
  58.  
  59.     return D3D_OK;
  60. } // end Light::Set
  61.  
  62.  
  63. /*
  64. **-----------------------------------------------------------------------------
  65. **  Name:    Light::SetColor
  66. **-----------------------------------------------------------------------------
  67. */
  68.  
  69. inline void        
  70. Light::SetColor (const D3DVECTOR & color)
  71. {
  72.     light.dcvColor.r = color[0];
  73.     light.dcvColor.g = color[1];
  74.     light.dcvColor.b = color[2];
  75.     changed = 1;
  76. } // end Light::SetColor
  77.  
  78.  
  79.   
  80. /*
  81. **-----------------------------------------------------------------------------
  82. **  Name:    Light::GetColor
  83. **-----------------------------------------------------------------------------
  84. */
  85.  
  86. inline D3DVECTOR    
  87. Light::GetColor (void) const
  88. {
  89.     D3DVECTOR    ret;
  90.     ret[0] = light.dcvColor.r;
  91.     ret[1] = light.dcvColor.g;
  92.     ret[2] = light.dcvColor.b;
  93.     return ret;
  94. } // end Light::GetColor
  95.  
  96.  
  97.  
  98. /*
  99. **-----------------------------------------------------------------------------
  100. **  Name:    Light::SetPosition
  101. **-----------------------------------------------------------------------------
  102. */
  103.  
  104. inline void        
  105. Light::SetPosition (const D3DVECTOR & position)
  106.     light.dvPosition = position;
  107.     changed = 1;
  108. } // end Light::SetPosition
  109.  
  110.  
  111. /*
  112. **-----------------------------------------------------------------------------
  113. **  Name:    Light::GetPosition
  114. **-----------------------------------------------------------------------------
  115. */
  116.  
  117. inline D3DVECTOR    
  118. Light::GetPosition(void) const
  119.     return light.dvPosition;
  120. } // end Light::GetPosition
  121.  
  122.  
  123.   
  124. /*
  125. **-----------------------------------------------------------------------------
  126. **  Name:    Light::SetDirection
  127. **-----------------------------------------------------------------------------
  128. */
  129.  
  130. inline void        
  131. Light::SetDirection (const D3DVECTOR & direction)
  132.     light.dvDirection = direction;
  133.     changed = 1;
  134. }
  135.  
  136.  
  137.  
  138. /*
  139. **-----------------------------------------------------------------------------
  140. **  Name:    Light::GetDirection
  141. **-----------------------------------------------------------------------------
  142. */
  143.  
  144. inline D3DVECTOR    
  145. Light::GetDirection(void) const
  146.     return light.dvDirection;
  147. }
  148.  
  149.  
  150. /*
  151. **-----------------------------------------------------------------------------
  152. **  Name:    Light::SetAttenuation
  153. **-----------------------------------------------------------------------------
  154. */
  155.  
  156. inline void        
  157. Light::SetAttenuation (const D3DVECTOR & attenuation)
  158.     light.dvAttenuation0 = attenuation[0];
  159.     light.dvAttenuation1 = attenuation[1];
  160.     light.dvAttenuation2 = attenuation[2];
  161.     changed = 1;
  162. } // end Light::SetAttenuation
  163.  
  164.  
  165.  
  166. /*
  167. **-----------------------------------------------------------------------------
  168. **  Name:    Light::GetAttenuation
  169. **-----------------------------------------------------------------------------
  170. */
  171.  
  172. inline D3DVECTOR    
  173. Light::GetAttenuation(void) const
  174.     D3DVECTOR    ret;
  175.     ret[0] = light.dvAttenuation0;
  176.     ret[1] = light.dvAttenuation1;
  177.     ret[2] = light.dvAttenuation2;
  178.     return ret;
  179. } // end Light::GetAttenuation
  180.  
  181.  
  182.   
  183. /*
  184. **-----------------------------------------------------------------------------
  185. **  Name:    Light::SetRange
  186. **-----------------------------------------------------------------------------
  187. */
  188.  
  189. inline void        
  190. Light::SetRange(const float range)
  191. {
  192.     light.dvRange = range;
  193.     changed = 1;
  194. } // end Light::SetRange
  195.  
  196.  
  197. /*
  198. **-----------------------------------------------------------------------------
  199. **  Name:    Light::GetRange
  200. **-----------------------------------------------------------------------------
  201. */
  202.  
  203. inline float        
  204. Light::GetRange(void) const
  205. {
  206.     return light.dvRange;
  207. } // end Light::GetRange
  208.  
  209.  
  210.   
  211. /*
  212. **-----------------------------------------------------------------------------
  213. **  Name:    Light::SetFalloff
  214. **-----------------------------------------------------------------------------
  215. */
  216.  
  217. inline void        
  218. Light::SetFalloff(const float falloff)
  219. {
  220.     light.dvFalloff = falloff;
  221.     changed = 1;
  222. } // end Light::SetFalloff
  223.  
  224.  
  225.   
  226. /*
  227. **-----------------------------------------------------------------------------
  228. **  Name:    Light::GetFalloff
  229. **-----------------------------------------------------------------------------
  230. */
  231.  
  232. inline float        
  233. Light::GetFalloff(void) const
  234. {
  235.     return light.dvFalloff;
  236. } // end Light::GetFalloff
  237.  
  238.  
  239.  
  240. /*
  241. **-----------------------------------------------------------------------------
  242. **  Name:    Light::SetUmbra
  243. **-----------------------------------------------------------------------------
  244. */
  245.  
  246. inline void        
  247. Light::SetUmbra(const float umbra)
  248. {
  249.     light.dvTheta = umbra;
  250.     changed = 1;
  251. } // end Light::SetUmbra
  252.  
  253.  
  254.  
  255. /*
  256. **-----------------------------------------------------------------------------
  257. **  Name:    Light::GetUmbra
  258. **-----------------------------------------------------------------------------
  259. */
  260.  
  261. inline float        
  262. Light::GetUmbra(void) const
  263. {
  264.     return light.dvTheta;
  265. } // end Light::GetUmbra
  266.  
  267.  
  268.  
  269. /*
  270. **-----------------------------------------------------------------------------
  271. **  Name:    Light::SetPenumbra
  272. **-----------------------------------------------------------------------------
  273. */
  274.  
  275. inline void        
  276. Light::SetPenumbra(const float penumbra)
  277. {
  278.     light.dvPhi = penumbra;
  279.     changed = 1;
  280. } // end Light::SetPenumbra
  281.  
  282.  
  283.  
  284. /*
  285. **-----------------------------------------------------------------------------
  286. **  Name:    Light::GetPenumbra
  287. **-----------------------------------------------------------------------------
  288. */
  289.  
  290. inline float        
  291. Light::GetPenumbra(void) const
  292. {
  293.     return light.dvPhi;
  294. } // end Light::GetPenumbra
  295.  
  296.  
  297. inline void        
  298. Light::SetFlags(const DWORD flags)
  299. {
  300.     light.dwFlags = flags;
  301.     changed = 1;
  302. }    // end of Light::SetFlags()
  303.  
  304. inline DWORD        
  305. Light::GetFlags(void) const
  306. {
  307.     return light.dwFlags;
  308. }    // end of Light::GtFlags()
  309.  
  310. /*
  311. **-----------------------------------------------------------------------------
  312. **    Material inline methods
  313. **-----------------------------------------------------------------------------
  314. */
  315.  
  316. /*
  317. **-----------------------------------------------------------------------------
  318. **  Name:    Material::SetAsCurrent
  319. **-----------------------------------------------------------------------------
  320. */
  321.  
  322. inline HRESULT
  323. Material::SetAsCurrent(LPDIRECT3DDEVICE2 lpDev)
  324. {
  325.     HRESULT hResult;
  326.  
  327.     Set();
  328.  
  329.     hResult = lpDev->SetLightState(D3DLIGHTSTATE_MATERIAL, hMat);
  330.     if (hResult != D3D_OK)
  331.         return hResult;
  332.  
  333.     hResult = lpDev->SetRenderState(D3DRENDERSTATE_TEXTUREHANDLE, Mat.hTexture);
  334.     if (hResult != D3D_OK)
  335.         return hResult;
  336.  
  337.     // Success
  338.     return D3D_OK;
  339. } // end Material::SetAsCurrent
  340.  
  341.  
  342.   
  343. /*
  344. **-----------------------------------------------------------------------------
  345. **  Name:    Material::Set
  346. **-----------------------------------------------------------------------------
  347. */
  348.  
  349. inline HRESULT
  350. Material::Set(void)
  351. {
  352.     if (changed) {
  353.         changed = 0;
  354.         return lpMat->SetMaterial (&Mat);
  355.     } 
  356.  
  357.     return D3D_OK;
  358. } // end Material::Set
  359.  
  360.  
  361.  
  362. /*
  363. **-----------------------------------------------------------------------------
  364. **  Name:    Material::SetAsBackground
  365. **-----------------------------------------------------------------------------
  366. */
  367.  
  368. inline HRESULT
  369. Material::SetAsBackground (LPDIRECT3DVIEWPORT2 lpView)
  370. {
  371.     Mat.dwRampSize = 1;
  372.     changed = 1;
  373.     Set();
  374.     return lpView->SetBackground(hMat);
  375. } // end Material::SetAsBackground
  376.  
  377.  
  378.  
  379. /*
  380. **-----------------------------------------------------------------------------
  381. **  Name:    Material::SetDiffuse
  382. **-----------------------------------------------------------------------------
  383. */
  384.  
  385. inline void        
  386. Material::SetDiffuse(const D3DVECTOR& color)
  387. {
  388.     Mat.diffuse.r = color[0];
  389.     Mat.diffuse.g = color[1];
  390.     Mat.diffuse.b = color[2];
  391.     changed = 1;
  392. } // end Material::SetDiffuse
  393.  
  394.  
  395. /*
  396. **-----------------------------------------------------------------------------
  397. **  Name:    Material::GetDiffuse
  398. **-----------------------------------------------------------------------------
  399. */
  400.  
  401. inline D3DVECTOR    
  402. Material::GetDiffuse(void) const
  403. {
  404.     D3DVECTOR    ret;
  405.     ret[0] = Mat.diffuse.r;
  406.     ret[1] = Mat.diffuse.g;
  407.     ret[2] = Mat.diffuse.b;
  408.     return ret;
  409. } // end Material::GetDiffuse
  410.  
  411. inline void        
  412. Material::SetAlpha(const float& alpha)
  413. {
  414.     Mat.diffuse.a = alpha;
  415.     changed = 1;
  416. }    // end of Material::SetAlpha()
  417.  
  418. inline float    
  419. Material::GetAlpha(void) const
  420. {
  421.     return Mat.diffuse.a;
  422. }    // end of Material::GetAlpha()
  423.  
  424.  
  425.  
  426. /*
  427. **-----------------------------------------------------------------------------
  428. **  Name:    Material::SetAmbient
  429. **-----------------------------------------------------------------------------
  430. */
  431.  
  432. inline void        
  433. Material::SetAmbient(const D3DVECTOR& color)
  434. {
  435.     Mat.ambient.r = color[0];
  436.     Mat.ambient.g = color[1];
  437.     Mat.ambient.b = color[2];
  438.     changed = 1;
  439. } // end Material::SetAmbient
  440.  
  441.  
  442.   
  443. /*
  444. **-----------------------------------------------------------------------------
  445. **  Name:    Material::GetAmbient
  446. **-----------------------------------------------------------------------------
  447. */
  448.  
  449. inline D3DVECTOR    
  450. Material::GetAmbient(void) const
  451. {
  452.     D3DVECTOR    ret;
  453.     ret[0] = Mat.ambient.r;
  454.     ret[1] = Mat.ambient.g;
  455.     ret[2] = Mat.ambient.b;
  456.     return ret;
  457. } // end Material::GetAmbient
  458.  
  459.  
  460.  
  461. /*
  462. **-----------------------------------------------------------------------------
  463. **  Name:    Material::SetEmissive
  464. **-----------------------------------------------------------------------------
  465. */
  466.  
  467. inline void        
  468. Material::SetEmissive(const D3DVECTOR& color)
  469. {
  470.     Mat.emissive.r = color[0];
  471.     Mat.emissive.g = color[1];
  472.     Mat.emissive.b = color[2];
  473.     changed = 1;
  474. } // end Material::SetEmissive
  475.  
  476.  
  477.  
  478. /*
  479. **-----------------------------------------------------------------------------
  480. **  Name:    Material::GetEmissive
  481. **-----------------------------------------------------------------------------
  482. */
  483.  
  484. inline D3DVECTOR    
  485. Material::GetEmissive(void) const
  486. {
  487.     D3DVECTOR    ret;
  488.     ret[0] = Mat.emissive.r;
  489.     ret[1] = Mat.emissive.g;
  490.     ret[2] = Mat.emissive.b;
  491.     return ret;
  492. } // end Material::GetEmissive
  493.  
  494.  
  495.   
  496. /*
  497. **-----------------------------------------------------------------------------
  498. **  Name:    Material::SetSpecular
  499. **-----------------------------------------------------------------------------
  500. */
  501.  
  502. inline void        
  503. Material::SetSpecular(const D3DVECTOR& color)
  504. {
  505.     Mat.specular.r = color[0];
  506.     Mat.specular.g = color[1];
  507.     Mat.specular.b = color[2];
  508.     changed = 1;
  509. } // end Material::SetSpecular
  510.  
  511.  
  512.   
  513. /*
  514. **-----------------------------------------------------------------------------
  515. **  Name:    Material::GetSpecular
  516. **-----------------------------------------------------------------------------
  517. */
  518.  
  519. inline D3DVECTOR    
  520. Material::GetSpecular(void) const
  521. {
  522.     D3DVECTOR    ret;
  523.     ret[0] = Mat.specular.r;
  524.     ret[1] = Mat.specular.g;
  525.     ret[2] = Mat.specular.b;
  526.     return ret;
  527. } // end Material::GetSpecular
  528.  
  529.  
  530.  
  531. /*
  532. **-----------------------------------------------------------------------------
  533. **  Name:    Material::SetPower
  534. **-----------------------------------------------------------------------------
  535. */
  536.  
  537. inline void        
  538. Material::SetPower(const D3DVALUE& power)
  539. {
  540.     Mat.power = power;
  541.     changed = 1;
  542. } // end Material::SetPower
  543.  
  544.  
  545.   
  546. /*
  547. **-----------------------------------------------------------------------------
  548. **  Name:    Material::GetPower
  549. **-----------------------------------------------------------------------------
  550. */
  551.  
  552. inline D3DVALUE    
  553. Material::GetPower(void) const
  554. {
  555.     return Mat.power;
  556. } // end Material::GetPower
  557.  
  558.  
  559.   
  560. /*
  561. **-----------------------------------------------------------------------------
  562. **  Name:    Material::SetRampSize
  563. **-----------------------------------------------------------------------------
  564. */
  565.  
  566. inline void        
  567. Material::SetRampSize(const DWORD& ramp)
  568. {
  569.     Mat.dwRampSize = ramp;
  570.     changed = 1;
  571. } // end Material::SetRampSize
  572.  
  573.  
  574.   
  575. /*
  576. **-----------------------------------------------------------------------------
  577. **  Name:    Material::GetRampSize
  578. **-----------------------------------------------------------------------------
  579. */
  580.  
  581. inline DWORD        
  582. Material::GetRampSize(void) const
  583. {
  584.     return Mat.dwRampSize;
  585. } // end Material::GetRampSize
  586.  
  587.  
  588.  
  589. /*
  590. **-----------------------------------------------------------------------------
  591. **  Name:    Material::SetTextureHandle
  592. **-----------------------------------------------------------------------------
  593. */
  594.  
  595. inline void        
  596. Material::SetTextureHandle(const D3DTEXTUREHANDLE& hTexture)
  597. {
  598.     Mat.hTexture = hTexture;
  599.     changed = 1;
  600. } // end Material::SetTextureHandle
  601.  
  602.  
  603. /*
  604. **-----------------------------------------------------------------------------
  605. **  Name:    Material::GetTextureHandle
  606. **-----------------------------------------------------------------------------
  607. */
  608.  
  609. inline D3DTEXTUREHANDLE    
  610. Material::GetTextureHandle(void)
  611. {
  612.     Set();
  613.     return Mat.hTexture;
  614. } // end Material::GetTextureHandle
  615.  
  616.   
  617. /*
  618. **-----------------------------------------------------------------------------
  619. **  end of File
  620. **-----------------------------------------------------------------------------
  621. */
  622.