home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 July & August / PCWorld_2006-07-08_cd.bin / temacd / planearcade / planearcade.exe / Tank3.bmp / light.cpp < prev    next >
C/C++ Source or Header  |  2004-10-08  |  4KB  |  178 lines

  1.  
  2. #include "main.h"
  3.  
  4.  
  5. //---------------------------------------------------------------------
  6. //Globalne PremennΘ
  7. //---------------------------------------------------------------------
  8.  
  9. //Light Manager
  10. LIGHTMANAGER LightManager;
  11.  
  12. //------------------------------------------------------------------
  13. // Name: LIGHTMANAGER()
  14. // Desc: Konstruktor
  15. //------------------------------------------------------------------
  16. LIGHTMANAGER::LIGHTMANAGER()
  17. {
  18.     Light = NULL;
  19.     D3DLight = NULL;
  20.     NumLights = 0;
  21. }
  22.  
  23. //------------------------------------------------------------------
  24. // Name: LIGHTMANAGER()
  25. // Desc: Destruktor
  26. //------------------------------------------------------------------
  27. LIGHTMANAGER::~LIGHTMANAGER()
  28. {
  29.  
  30.     //
  31.     if (Light !=NULL)
  32.         delete [] Light;
  33.     Light = NULL;
  34.  
  35.     //
  36.     if (D3DLight !=NULL)
  37.         delete [] D3DLight;
  38.     D3DLight = NULL;
  39.  
  40.     
  41. }
  42.  
  43. //------------------------------------------------------------------
  44. // Name: Initialize()
  45. // Desc: Maximalny pocet svetiel
  46. //------------------------------------------------------------------
  47. void LIGHTMANAGER::Initialize(int NLights)
  48. {
  49.  
  50.     //
  51.     Light = new LIGHT[NLights];
  52.     D3DLight = new D3DLIGHT9[NLights];
  53.     NumLights = 0;
  54.  
  55.  
  56.  
  57.  
  58. }
  59.  
  60. //------------------------------------------------------------------
  61. // Name: CleanUp()
  62. // Desc: Zresetuj svetla
  63. //------------------------------------------------------------------
  64. void LIGHTMANAGER::CleanUp()
  65. {
  66.     if (Light !=NULL)
  67.         delete [] Light;
  68.     Light = NULL;
  69.  
  70.     NumLights = 0;
  71.  
  72. }
  73.  
  74.  
  75. //------------------------------------------------------------------
  76. // Name: AddLightPoint
  77. // Desc: Pridaj bodove svetlo
  78. //------------------------------------------------------------------
  79. void LIGHTMANAGER::AddLightPoint(VECTOR3D Pos,COLOR Diffuse,
  80.                                     COLOR Ambient,
  81.                                     COLOR Specular,
  82.                                     float Atten,float Range)
  83. {
  84.  
  85.     Light[NumLights].Pos = Pos;
  86.  
  87.     Light[NumLights].Type = D3DLIGHT_POINT;
  88.     
  89.     Light[NumLights].Ambient  = Ambient;
  90.     Light[NumLights].Diffuse  = Diffuse;
  91.     Light[NumLights].Specular = Specular;
  92.  
  93.     Light[NumLights].Atten0 = Atten;
  94.     Light[NumLights].Range = Range;
  95.     Light[NumLights].Active = true;
  96.  
  97.  
  98.     NumLights++;
  99. }
  100.  
  101.  
  102. //------------------------------------------------------------------
  103. // Name: AddLightDir
  104. // Desc: pridaj smerove svetlo (slnko)
  105. //------------------------------------------------------------------
  106. void LIGHTMANAGER::AddLightDir(VECTOR3D Dir,COLOR Diffuse,
  107.                               COLOR Ambient,
  108.                               COLOR Specular)
  109. {
  110.     Light[NumLights].Dir = Dir;
  111.  
  112.     Light[NumLights].Type = D3DLIGHT_DIRECTIONAL;
  113.     Light[NumLights].Range = 5.0f;
  114.     
  115.     Light[NumLights].Ambient  = Ambient;
  116.     Light[NumLights].Diffuse  = Diffuse;
  117.     Light[NumLights].Specular = Specular;
  118.  
  119.     Light[NumLights].Active = true;    
  120.  
  121.     NumLights++;
  122. }
  123.  
  124. //------------------------------------------------------------------
  125. // Name: RefreshLights()
  126. // Desc: obnovi svetla
  127. //------------------------------------------------------------------
  128. void LIGHTMANAGER::RefreshLights()
  129. {
  130.  
  131.     for (int i=0;i<NumLights;i++)
  132.     {
  133.     if (Light[i].Active == true)
  134.     {
  135.         ZeroMemory( &D3DLight[i], sizeof(D3DLIGHT9) );
  136.         //ambient
  137.         D3DLight[i].Ambient.a = Light[i].Ambient.A;
  138.         D3DLight[i].Ambient.r = Light[i].Ambient.R;
  139.         D3DLight[i].Ambient.g = Light[i].Ambient.G;
  140.         D3DLight[i].Ambient.b = Light[i].Ambient.B;
  141.  
  142.         //Diffuse
  143.         D3DLight[i].Diffuse.a = Light[i].Diffuse.A;
  144.         D3DLight[i].Diffuse.r = Light[i].Diffuse.R;
  145.         D3DLight[i].Diffuse.g = Light[i].Diffuse.G;
  146.         D3DLight[i].Diffuse.b = Light[i].Diffuse.B;
  147.  
  148.         //Specular
  149.         D3DLight[i].Specular.a = Light[i].Specular.A;
  150.         D3DLight[i].Specular.r = Light[i].Specular.R;
  151.         D3DLight[i].Specular.g = Light[i].Specular.G;
  152.         D3DLight[i].Specular.b = Light[i].Specular.B;
  153.  
  154.         //vlastnosti
  155.         D3DLight[i].Direction.x = Light[i].Dir.X  ;
  156.         D3DLight[i].Direction.y = Light[i].Dir.Y  ;
  157.         D3DLight[i].Direction.z = Light[i].Dir.Z  ;
  158.         
  159.         D3DLight[i].Attenuation0 = Light[i].Atten0 ;
  160.  
  161.         D3DLight[i].Position.x = Light[i].Pos.X; 
  162.         D3DLight[i].Position.y = Light[i].Pos.Y;
  163.         D3DLight[i].Position.z = Light[i].Pos.Z;
  164.  
  165.         D3DLight[i].Type  =  Light[i].Type;
  166.  
  167.         D3DLight[i].Range = Light[i].Range  ;
  168.  
  169.         g_pd3dDevice->SetLight( i, &D3DLight[i] );
  170.         g_pd3dDevice->LightEnable( i, TRUE );
  171.         
  172.     }
  173.     }
  174.  
  175.     
  176. }
  177.  
  178.