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

  1.  
  2. #include "main.h"
  3.  
  4. //------------------------------------------------------------------
  5. // Name: SKYDOME()
  6. // Desc: konÜtruktor
  7. //------------------------------------------------------------------
  8. SKYDOME::SKYDOME()
  9. {
  10.     g_pVB = NULL;
  11.     g_pTexture = NULL;
  12.     Rotation = 0.0f;
  13.     Sca = Get3D(1.0f,1.0f,1.0f);
  14.     Pos = Get3D(0.0f,0.0f,0.0f);
  15.     NumVer = 0;
  16. }
  17.  
  18.  
  19. //------------------------------------------------------------------
  20. // Name: ~SKYDOME()
  21. // Desc: deÜtruktor
  22. //------------------------------------------------------------------
  23. SKYDOME::~SKYDOME()
  24. {
  25.     //vertex buffer
  26.     if (g_pVB != NULL)
  27.         g_pVB->Release();
  28.     g_pVB = NULL;
  29.  
  30.     //textura
  31.     if (g_pTexture != NULL)
  32.         g_pTexture->Release();
  33.     g_pTexture = NULL;
  34. }
  35.  
  36.  
  37. //------------------------------------------------------------------
  38. // Name: CreateSkyDome()
  39. // Desc: Vygeneruje SkyDome
  40. //------------------------------------------------------------------
  41. void SKYDOME::CreateSkyDome(float radius, float dtheta, float dphi, float hTile, float vTile)
  42. {
  43. #define PI 3.1415926535897f
  44. #define DTOR (PI/180.0f)
  45. #define SQR(x) (x*x)
  46.  
  47.     //zistenie poΦtu vertexov
  48.     NumVer = (int)((360/dtheta)*(90/dphi)*4);
  49.  
  50.     //log
  51.     char cBuf[80];
  52.     LogPrint("Vytvaram SkyDome");
  53.     sprintf(cBuf,"  Pocet Vertexov: %d",NumVer);
  54.     LogPrint(cBuf);
  55.  
  56.     //vytvorenie VB
  57.     if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumVer*sizeof(CUSTOMVERTEXSKYDOME),
  58.                                    D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEXSKYDOME,
  59.                                    D3DPOOL_DEFAULT, &g_pVB, NULL )))
  60.       {
  61.         LogPrint("  Chyba pri vytvarani VB");
  62.     }
  63.     else
  64.     {
  65.         LogPrint("  Vertex Buffer vytvoreny");
  66.     }
  67.  
  68.     //naplnanie VB
  69.     CUSTOMVERTEXSKYDOME *Vertices;
  70.     g_pVB->Lock(0, 0, (void**)&Vertices, 0 ) ;
  71.  
  72.     // pomocne premenne
  73.     float vx, vy, vz, mag;
  74.     int theta, phi;
  75.  
  76.     // generovanie skydomu
  77.     int n = 0;
  78.     for (phi=0; phi <= 90 - dphi; phi += (int)dphi)
  79.     {
  80.         for (theta=0; theta <= 360 - dtheta; theta += (int)dtheta)
  81.         {
  82.             // Calculate the vertex at phi, theta
  83.             Vertices[n].x = radius * sinf(phi*DTOR) * cosf(DTOR*theta);
  84.             Vertices[n].y = radius * sinf(phi*DTOR) * sinf(DTOR*theta);
  85.             Vertices[n].z = radius * cosf(phi*DTOR);
  86.  
  87.             // Create a vector from the origin to this vertex
  88.             vx = Vertices[n].x;
  89.             vy = Vertices[n].y;
  90.             vz = Vertices[n].z;
  91.  
  92.             // Normalize the vector
  93.             mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
  94.             vx /= mag;
  95.             vy /= mag;
  96.             vz /= mag;
  97.  
  98.             // Calculate the spherical texture coordinates
  99.             Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
  100.             Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;        
  101.             n++;
  102.  
  103.             // Calculate the vertex at phi+dphi, theta
  104.             Vertices[n].x = radius * sinf((phi+dphi)*DTOR) * cosf(theta*DTOR);
  105.             Vertices[n].y = radius * sinf((phi+dphi)*DTOR) * sinf(theta*DTOR);
  106.             Vertices[n].z = radius * cosf((phi+dphi)*DTOR);
  107.             
  108.             // Calculate the texture coordinates
  109.             vx = Vertices[n].x;
  110.             vy = Vertices[n].y;
  111.             vz = Vertices[n].z;
  112.  
  113.             mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
  114.             vx /= mag;
  115.             vy /= mag;
  116.             vz /= mag;
  117.  
  118.             Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
  119.             Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;        
  120.             n++;
  121.  
  122.             // Calculate the vertex at phi, theta+dtheta
  123.             Vertices[n].x = radius * sinf(DTOR*phi) * cosf(DTOR*(theta+dtheta));
  124.             Vertices[n].y = radius * sinf(DTOR*phi) * sinf(DTOR*(theta+dtheta));
  125.             Vertices[n].z = radius * cosf(DTOR*phi);
  126.             
  127.             // Calculate the texture coordinates
  128.             vx = Vertices[n].x;
  129.             vy = Vertices[n].y;
  130.             vz = Vertices[n].z;
  131.  
  132.             mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
  133.             vx /= mag;
  134.             vy /= mag;
  135.             vz /= mag;
  136.  
  137.             Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
  138.             Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;        
  139.             n++;
  140.  
  141.             if (phi > -90 && phi < 90)
  142.             {
  143.                 // Calculate the vertex at phi+dphi, theta+dtheta
  144.                 Vertices[n].x = radius * sinf((phi+dphi)*DTOR) * cosf(DTOR*(theta+dtheta));
  145.                 Vertices[n].y = radius * sinf((phi+dphi)*DTOR) * sinf(DTOR*(theta+dtheta));
  146.                 Vertices[n].z = radius * cosf((phi+dphi)*DTOR);
  147.                 
  148.                 // Calculate the texture coordinates
  149.                 vx = Vertices[n].x;
  150.                 vy = Vertices[n].y;
  151.                 vz = Vertices[n].z;
  152.  
  153.                 mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
  154.                 vx /= mag;
  155.                 vy /= mag;
  156.                 vz /= mag;
  157.  
  158.                 Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
  159.                 Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;        
  160.                 n++;
  161.             }
  162.  
  163.             
  164.         }
  165.     }
  166.  
  167.  
  168.     //
  169.     //upravi na DirectX
  170.     //
  171.     float Sur; //pomacna premenna pri prehodeni
  172.     for(int i=0;i<NumVer;i++)
  173.     {
  174.         //prehod suradnicu y a z
  175.         Sur = Vertices[i].y;
  176.         Vertices[i].y = Vertices[i].z;
  177.         Vertices[i].z = Sur;
  178.  
  179.         //box mapping
  180.         Vertices[i].v = ((Vertices[i].x+(radius/1.0f))/(radius*2.0f))*vTile;
  181.         Vertices[i].u = ((Vertices[i].z+(radius/1.0f))/(radius*2.0f))*hTile;
  182.     }
  183.  
  184.     //zatvor VB
  185.     g_pVB->Unlock();
  186.  
  187. }
  188.  
  189. //------------------------------------------------------------------
  190. // Name: LoadTexture()
  191. // Desc: Loadne texturu
  192. //------------------------------------------------------------------
  193. void SKYDOME::LoadTexture(char *FileName)
  194. {
  195.  
  196.     char cBuf[80];
  197.  
  198.     sprintf(cBuf,"  Textura: %s",FileName);
  199.     LogPrint(cBuf);
  200.     
  201.     if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice, 
  202.                                   FileName,    
  203.                                   D3DX_DEFAULT, 
  204.                                   D3DX_DEFAULT, 
  205.                                   Engine.MipMapLevels,   //MipLevels
  206.                                   0,            
  207.                                   Engine.TextureFormat, 
  208.                                   Engine.TextureCreatingFlag,
  209.                                   D3DX_DEFAULT, //Filter
  210.                                   D3DX_DEFAULT, //MipFilter
  211.                                   0,   //ColourKey
  212.                                   NULL,         
  213.                                   NULL,         
  214.                                   &g_pTexture)))  
  215.     {
  216.         sprintf(cBuf,"  chyba pri vytvarani textury: %s",FileName);
  217.         LogPrint(cBuf);
  218.     }
  219.     else
  220.     {
  221.         LogPrint("  Textura vytvorena");
  222.     }    
  223.  
  224. }
  225.  
  226. //------------------------------------------------------------------
  227. // Name: Render()
  228. // Desc: Vykresli skydome
  229. //------------------------------------------------------------------
  230. void SKYDOME::Render()
  231. {
  232.  
  233.     //nastav maticu
  234.     D3DXMATRIXA16 Matica;
  235.     Matica = GetMatrix(Pos,Get3D(0.0f,Rotation,0.0f),Sca);
  236.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &Matica);
  237.  
  238.     g_pd3dDevice->SetTexture( 0, g_pTexture );
  239.  
  240.         g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXSKYDOME));
  241.         g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXSKYDOME);
  242.         g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, NumVer-2);
  243.  
  244.     //reset nastaveni
  245.     Engine.ResetToDefault();
  246.  
  247. }