I will also post my planet code to see if you guys can help me fix this Z fighting problem.
CSphere.h
Code:
#ifndef CSPHERE
#define CSPHERE
#include "d3dx9.h"
#include "CQuickCom.h"
#include "CFileLog.h"
#include "d3dx9mesh.h"
#define DEGTORAD(x) (x*D3DX_PI/180.0f)
struct CSphereVertex
{
D3DXVECTOR3 pos;
D3DXVECTOR3 normal;
float u,v;
static const DWORD FVF;
CSphereVertex(D3DXVECTOR3 tpos,D3DXVECTOR3 tnormal,float tu,float tv):
pos(tpos),normal(tnormal),u(tu),v(tv){}
};
class CSphere
{
protected:
CFileLog SphereLog;
IDirect3DDevice9 *Device;
IDirect3DVertexBuffer9 *VB;
IDirect3DIndexBuffer9 *IB;
ID3DXMesh *SphereMesh;
unsigned int NumTris;
unsigned int NumFaces;
unsigned int NumVertices;
unsigned int NumVertsPerRow;
unsigned int NumVertsPerCol;
float Radius;
public:
CSphere(void) {Device=NULL,VB=NULL,IB=NULL,NumTris=0,NumFaces=0,
NumVertices=0,NumVertsPerRow=0,NumVertsPerCol=0,Radius=0.0f;};
~CSphere(void)
{
SAFE_RELEASE(VB);
SAFE_RELEASE(IB);
SAFE_RELEASE(SphereMesh);
}
//Old - not used
//void CreateSphere(IDirect3DDevice9 *_device,
// float _radius,
// unsigned int _faces,
// float _texmag,
// D3DXVECTOR3 &pos);
void CreateMeshSphere(IDirect3DDevice9 *_device,
float _radius,
unsigned int _faces);
//Old - not used
//IDirect3DVertexBuffer9 *GetVB(void) {return VB;};
//IDirect3DIndexBuffer9 *GetIB(void) {return IB;};
ID3DXMesh *GetMesh(void) {return SphereMesh;};
unsigned int GetNumTris(void) {return NumTris;};
unsigned int GetNumVertices(void) {return NumVertices;};
};
#endif
CSphere.cpp
Code:
#include "CSphere.h"
const DWORD CSphereVertex::FVF=D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
void CSphere::CreateMeshSphere(IDirect3DDevice9 *_device,float _radius,unsigned int _faces)
{
Device=_device;
Radius=_radius;
NumFaces=_faces;
LPD3DXMESH temp;
if (FAILED(D3DXCreateSphere(Device,Radius,NumFaces,NumFaces,&temp,NULL)))
{
::MessageBox(0,"Failed to create sphere",0,0);
}
if (FAILED(temp->CloneMeshFVF(D3DXMESH_SYSTEMMEM,CSphereVertex::FVF,Device,&SphereMesh)))
{
::MessageBox(0,"Failed to clone mesh",0,0);
}
CSphereVertex *Vertices;
SphereMesh->LockVertexBuffer(0,(void **)&Vertices);
float angleinc_alpha=360.0f/_faces;
float angleinc_beta=180.0f/_faces;
float alpha=0.0f;
float beta=-180.0f;
float u=0.0f,v=0.0f;
unsigned int vertex_count;
float PI2=D3DX_PI*2.0f;
for (unsigned int i=0;i<SphereMesh->GetNumVertices();i++)
{
u=(DEGTORAD(alpha)+D3DX_PI)/(PI2);
v=DEGTORAD(beta)/D3DX_PI;
Vertices[i].u=u;
Vertices[i].v=v;
alpha+=angleinc_alpha;
vertex_count++;
if (vertex_count>=(_faces))
{
alpha=0.0f;
beta+=angleinc_beta;
//Vertices[i+1].v=0.0f;
//Vertices[i+1].u=0.0f;
vertex_count=0;
}
}
SphereMesh->UnlockVertexBuffer();
temp->Release();
}
CPlanet.h
Code:
#ifndef CPLANET
#define CPLANET
#include "CPrimitives.h"
#include "d3dx9.h"
#include <String>
#include "CQuickCom.h"
#include "CSphere.h"
class CPlanet
{
IDirect3DDevice9 *Device;
CSphere Planet;
CSphere Atmosphere;
D3DXVECTOR3 Pos;
D3DXVECTOR3 Rotation;
D3DXVECTOR3 RotationVelocity;
IDirect3DTexture9 *Texture;
D3DMATERIAL9 Material;
D3DMATERIAL9 AMaterial;
float Radius;
float ARadius;
int Faces;
bool AtmosphereFlag;
public:
CPlanet(void);
~CPlanet(void);
void SetTexture(std::string PlanetFile);
void SetPosition(D3DXVECTOR3 *_newpos) {Pos=*_newpos;};
void GetPosition(D3DXVECTOR3 *_curpos) {*_curpos=Pos;};
void SetRotationParams(D3DXVECTOR3 *_rotation,D3DXVECTOR3 *_rotation_velocity)
{
Rotation=*_rotation;
RotationVelocity=*_rotation_velocity;
}
void GetRotationParams(D3DXVECTOR3 *_rotation,D3DXVECTOR3 *_rotation_velocity)
{
*_rotation=Rotation;
*_rotation_velocity=RotationVelocity;
}
void CreateModel(IDirect3DDevice9 *_Device,float _Radius,int _Faces,D3DXVECTOR3 &pos);
void TestCreateModel(IDirect3DDevice9 *_device,float _radius,unsigned int _faces)
{
Device=_device;
Radius=_radius;
Faces=_faces;
Planet.CreateMeshSphere(_device,_radius,_faces);
D3DXComputeNormals(Planet.GetMesh(),NULL);
}
void SetMaterial(D3DMATERIAL9 *_material) {Material=*_material;};
void GetMaterial(D3DMATERIAL9 *_material) {*_material=Material;};
float GetRadius(void) {return Radius;};
float GetAtmosphereRadius(void) {return ARadius;};
void SetAtmosphere(D3DMATERIAL9 *_material,float _radius);
void Render(float timeDelta);
void TestRender(float timeDelta);
};
#endif
CPlanet.cpp
Code:
#include "CPlanet.h"
CPlanet::CPlanet(void)
{
Device=NULL;
Pos=D3DXVECTOR3(0.0f,0.0f,0.0f);
Rotation=D3DXVECTOR3(0.0f,0.0f,0.0f);
RotationVelocity=D3DXVECTOR3(0.0f,0.0f,0.0f);
Texture=NULL;
Radius=0.0f;
}
CPlanet::~CPlanet(void)
{
if (Texture) Texture->Release();
}
void CPlanet::SetTexture(std::string PlanetFile)
{
if (FAILED(D3DXCreateTextureFromFile(Device,PlanetFile.c_str(),&Texture)))
{
::MessageBox(0,"Failed to load planet texture",0,0);
return;
}
}
void CPlanet::CreateModel(IDirect3DDevice9 *_Device,float _Radius,int _Faces,D3DXVECTOR3 &pos)
{
Device=_Device;
Radius=_Radius;
Faces=_Faces;
SetPosition(&pos);
Planet.CreateMeshSphere(Device,Radius,_Faces);
}
void CPlanet::SetAtmosphere(D3DMATERIAL9 *_material,float _radius)
{
AMaterial=*_material;
ARadius=_radius;
AtmosphereFlag=true;
Atmosphere.CreateMeshSphere(Device,ARadius,Faces);
}
void CPlanet::Render(float timeDelta)
{
Device->SetRenderState(D3DRS_WRAP0,true);
Device->SetRenderState(D3DRS_WRAP1,true);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
Rotation.x+=(RotationVelocity.x*timeDelta);
Rotation.y+=(RotationVelocity.y*timeDelta);
Rotation.z+=(RotationVelocity.z*timeDelta);
D3DXMATRIX rot;
D3DXMatrixRotationYawPitchRoll(&rot,Rotation.x,Rotation.y,Rotation.z);
rot._41=Pos.x;
rot._42=Pos.y;
rot._43=Pos.z;
Device->SetTransform(D3DTS_WORLD,&rot);
Device->SetMaterial(&Material);
Device->SetTexture(0,Texture);
Planet.GetMesh()->DrawSubset(0);
if (AtmosphereFlag)
{
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
Device->SetTexture(0,NULL);
Device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTA_DIFFUSE);
Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTOP_SELECTARG1);
Device->SetMaterial(&AMaterial);
Atmosphere.GetMesh()->DrawSubset(0);
Device->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
}
Device->SetRenderState(D3DRS_WRAP0,false);
Device->SetRenderState(D3DRS_WRAP1,false);
}
Sorry, the board screws up my formatting all the time. All my indents were removed when I edited the post. I'm not going to go and manually re-indent everything. Could we please, please, PLEASE, get this bug fixed!!!!!!!!!!!!!
There are a lot of old functions and variables in the code right now and it needs cleaned up, but during dev I didn't want to erase any functions or variables until I made sure I didn't want to use that method or algo. I used to create the sphere's mathematically but now I just clone one from D3D.
As you can see this project is quickly becoming very large. I'm going to need help from some of you later on down the road. It's impossible to do all of it myself.