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 >
Wrap
C/C++ Source or Header
|
2004-10-03
|
7KB
|
247 lines
#include "main.h"
//------------------------------------------------------------------
// Name: SKYDOME()
// Desc: konÜtruktor
//------------------------------------------------------------------
SKYDOME::SKYDOME()
{
g_pVB = NULL;
g_pTexture = NULL;
Rotation = 0.0f;
Sca = Get3D(1.0f,1.0f,1.0f);
Pos = Get3D(0.0f,0.0f,0.0f);
NumVer = 0;
}
//------------------------------------------------------------------
// Name: ~SKYDOME()
// Desc: deÜtruktor
//------------------------------------------------------------------
SKYDOME::~SKYDOME()
{
//vertex buffer
if (g_pVB != NULL)
g_pVB->Release();
g_pVB = NULL;
//textura
if (g_pTexture != NULL)
g_pTexture->Release();
g_pTexture = NULL;
}
//------------------------------------------------------------------
// Name: CreateSkyDome()
// Desc: Vygeneruje SkyDome
//------------------------------------------------------------------
void SKYDOME::CreateSkyDome(float radius, float dtheta, float dphi, float hTile, float vTile)
{
#define PI 3.1415926535897f
#define DTOR (PI/180.0f)
#define SQR(x) (x*x)
//zistenie poΦtu vertexov
NumVer = (int)((360/dtheta)*(90/dphi)*4);
//log
char cBuf[80];
LogPrint("Vytvaram SkyDome");
sprintf(cBuf," Pocet Vertexov: %d",NumVer);
LogPrint(cBuf);
//vytvorenie VB
if (FAILED(g_pd3dDevice->CreateVertexBuffer(NumVer*sizeof(CUSTOMVERTEXSKYDOME),
D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEXSKYDOME,
D3DPOOL_DEFAULT, &g_pVB, NULL )))
{
LogPrint(" Chyba pri vytvarani VB");
}
else
{
LogPrint(" Vertex Buffer vytvoreny");
}
//naplnanie VB
CUSTOMVERTEXSKYDOME *Vertices;
g_pVB->Lock(0, 0, (void**)&Vertices, 0 ) ;
// pomocne premenne
float vx, vy, vz, mag;
int theta, phi;
// generovanie skydomu
int n = 0;
for (phi=0; phi <= 90 - dphi; phi += (int)dphi)
{
for (theta=0; theta <= 360 - dtheta; theta += (int)dtheta)
{
// Calculate the vertex at phi, theta
Vertices[n].x = radius * sinf(phi*DTOR) * cosf(DTOR*theta);
Vertices[n].y = radius * sinf(phi*DTOR) * sinf(DTOR*theta);
Vertices[n].z = radius * cosf(phi*DTOR);
// Create a vector from the origin to this vertex
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
// Normalize the vector
mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
vx /= mag;
vy /= mag;
vz /= mag;
// Calculate the spherical texture coordinates
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;
n++;
// Calculate the vertex at phi+dphi, theta
Vertices[n].x = radius * sinf((phi+dphi)*DTOR) * cosf(theta*DTOR);
Vertices[n].y = radius * sinf((phi+dphi)*DTOR) * sinf(theta*DTOR);
Vertices[n].z = radius * cosf((phi+dphi)*DTOR);
// Calculate the texture coordinates
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;
n++;
// Calculate the vertex at phi, theta+dtheta
Vertices[n].x = radius * sinf(DTOR*phi) * cosf(DTOR*(theta+dtheta));
Vertices[n].y = radius * sinf(DTOR*phi) * sinf(DTOR*(theta+dtheta));
Vertices[n].z = radius * cosf(DTOR*phi);
// Calculate the texture coordinates
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;
n++;
if (phi > -90 && phi < 90)
{
// Calculate the vertex at phi+dphi, theta+dtheta
Vertices[n].x = radius * sinf((phi+dphi)*DTOR) * cosf(DTOR*(theta+dtheta));
Vertices[n].y = radius * sinf((phi+dphi)*DTOR) * sinf(DTOR*(theta+dtheta));
Vertices[n].z = radius * cosf((phi+dphi)*DTOR);
// Calculate the texture coordinates
vx = Vertices[n].x;
vy = Vertices[n].y;
vz = Vertices[n].z;
mag = (float)sqrt(SQR(vx)+SQR(vy)+SQR(vz));
vx /= mag;
vy /= mag;
vz /= mag;
Vertices[n].u = hTile * (float)(atan2(vx, vz)/(PI*2)) + 0.5f;
Vertices[n].v = vTile * (float)(asinf(vy) / PI) + 0.5f;
n++;
}
}
}
//
//upravi na DirectX
//
float Sur; //pomacna premenna pri prehodeni
for(int i=0;i<NumVer;i++)
{
//prehod suradnicu y a z
Sur = Vertices[i].y;
Vertices[i].y = Vertices[i].z;
Vertices[i].z = Sur;
//box mapping
Vertices[i].v = ((Vertices[i].x+(radius/1.0f))/(radius*2.0f))*vTile;
Vertices[i].u = ((Vertices[i].z+(radius/1.0f))/(radius*2.0f))*hTile;
}
//zatvor VB
g_pVB->Unlock();
}
//------------------------------------------------------------------
// Name: LoadTexture()
// Desc: Loadne texturu
//------------------------------------------------------------------
void SKYDOME::LoadTexture(char *FileName)
{
char cBuf[80];
sprintf(cBuf," Textura: %s",FileName);
LogPrint(cBuf);
if (FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,
FileName,
D3DX_DEFAULT,
D3DX_DEFAULT,
Engine.MipMapLevels, //MipLevels
0,
Engine.TextureFormat,
Engine.TextureCreatingFlag,
D3DX_DEFAULT, //Filter
D3DX_DEFAULT, //MipFilter
0, //ColourKey
NULL,
NULL,
&g_pTexture)))
{
sprintf(cBuf," chyba pri vytvarani textury: %s",FileName);
LogPrint(cBuf);
}
else
{
LogPrint(" Textura vytvorena");
}
}
//------------------------------------------------------------------
// Name: Render()
// Desc: Vykresli skydome
//------------------------------------------------------------------
void SKYDOME::Render()
{
//nastav maticu
D3DXMATRIXA16 Matica;
Matica = GetMatrix(Pos,Get3D(0.0f,Rotation,0.0f),Sca);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &Matica);
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEXSKYDOME));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXSKYDOME);
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, NumVer-2);
//reset nastaveni
Engine.ResetToDefault();
}