#ifndef PLANET_H
#define PLANET_H
#include "Actor.h"
#include "PlanetStruct.h"
class Planet: public Actor
{
public:
~Planet();
Planet();
Planet( IDirect3DDevice9 **D3DDevice, char* name, float diameter );
Planet( char* name, float diameter, float circumference );
void setID( char* id );
char* getID();
void createMesh( IDirect3DDevice9 **D3DDevice );
ID3DXMesh* getMesh();
ID3DXMesh* getMesh_2();
ID3DXMesh* CreateMappedSphere( IDirect3DDevice9 **D3DDevice, float fRad, UINT slices, UINT stacks);
protected:
ID3DXMesh* meshPlanet;
ID3DXMesh* meshPlanet_2;
float diameter, circumference;
PlanetVertex* planetVertex;
};
#endif
#include "Planet.h"
Planet::~Planet()
{
// Code omitted...
}
Planet::Planet()
{
// Code omitted...
}
Planet::Planet( IDirect3DDevice9 **D3DDevice, char* name, float diameter )
{
id = name;
this->diameter = diameter;
position = new Vector3();
meshPlanet_2 = CreateMappedSphere( &( *D3DDevice ), 3, 20, 20 );
}
Planet::Planet( char* name, float diameter, float circumference )
{
// Code omitted...
}
void Planet::setID( char* id )
{
// Code omitted...
}
char* Planet::getID()
{
// Code omitted...
}
void Planet::createMesh( IDirect3DDevice9 **D3DDevice )
{
// Code omitted...
}
ID3DXMesh* Planet::CreateMappedSphere( IDirect3DDevice9 **D3DDevice, float fRad, UINT slices, UINT stacks)
{
// CREDIT GOES TO "http://www.mvps.org/directx/articles/spheremap.htm"
// create the sphere
ID3DXMesh* mesh;
if( FAILED( D3DXCreateSphere( *D3DDevice, fRad, slices, stacks, &mesh, NULL )))
return NULL;
// create a copy of the mesh with texture coordinates,
// since the D3DX function doesn't include them
ID3DXMesh* texMesh;
if( FAILED( mesh->CloneMeshFVF( D3DXMESH_SYSTEMMEM, VERTEX_FVF, *D3DDevice, &texMesh )))
{ // failed, return un-textured mesh
return mesh;
}
// finished with the original mesh, release it
mesh->Release();
// lock the vertex buffer
//LPVERTEX pVerts;
if( SUCCEEDED( texMesh->LockVertexBuffer( 0, ( void ** ) &planetVertex )))
{
// get vertex count
int numVerts = texMesh->GetNumVertices();
// loop through the vertices
planetVertex = new PlanetVertex[ numVerts ];
for( int i = 0; i < numVerts; i++ )
{
// calculate texture coordinates
planetVertex.tu = asinf( planetVertex.norm.x ) / D3DX_PI + <span class="cpp-number">0</span>.5f;
planetVertex.tv = asinf( planetVertex.norm.y ) / D3DX_PI + <span class="cpp-number">0</span>.5f;
}
}
<span class="cpp-comment">// unlock the vertex buffer</span>
texMesh->UnlockVertexBuffer();
<span class="cpp-comment">// return pointer to caller</span>
<span class="cpp-keyword">return</span> texMesh;
}
ID3DXMesh* Planet::getMesh()
{
<span class="cpp-keyword">return</span> meshPlanet;
}
ID3DXMesh* Planet::getMesh_2()
{
<span class="cpp-keyword">return</span> meshPlanet_2;
}
</pre></div><!–ENDSCRIPT–>
PLANETSTRUCT HEADER
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">struct</span> PlanetVertex
{
D3DXVECTOR3 pos; <span class="cpp-comment">// vertex position</span>
D3DXVECTOR3 norm; <span class="cpp-comment">// vertex normal</span>
<span class="cpp-keyword">float</span> tu; <span class="cpp-comment">// texture coordinates</span>
<span class="cpp-keyword">float</span> tv;
};
<span class="cpp-directive">#define</span> VERTEX_FVF (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
</pre></div><!–ENDSCRIPT–>
MAIN APPLICATION
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">// Comments omitted…</span>
<span class="cpp-comment">// Code omitted…</span>
IDirect3DDevice9 *D3DDevice = NULL;
Direct3DUtility *direct3DUtility = NULL;
IDirect3DTexture9 *TxtureCube = NULL;
<span class="cpp-comment">// Code omitted…</span>
Planet **planet = <span class="cpp-keyword">new</span> Planet *[ <span class="cpp-number">8</span> ];
<span class="cpp-keyword">struct</span> CustomVertex
{
D3DXVECTOR3 position; <span class="cpp-comment">// Vertex Position</span>
D3DVECTOR normal; <span class="cpp-comment">// Vertex Normal</span>
<span class="cpp-keyword">float</span> u, v; <span class="cpp-comment">// Texture Coordinate</span>
};
<span class="cpp-directive">#define</span> D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
<span class="cpp-keyword">void</span> RandomNumGen::seedRandom();
<span class="cpp-comment">//— Function Prototypes —//</span>
<span class="cpp-keyword">void</span> cleanUp();
<span class="cpp-keyword">void</span> getInput();
<span class="cpp-keyword">void</span> initStars();
<span class="cpp-keyword">void</span> renderGame();
<span class="cpp-keyword">void</span> setupGame();
<span class="cpp-keyword">void</span> updateGame();
<span class="cpp-keyword">int</span> WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, <span class="cpp-keyword">int</span> nShowCmd )
{
<span class="cpp-comment">// Code omitted…</span>
setupGame();
<span class="cpp-comment">//Code omitted…</span>
MSG msg;
ZeroMemory( &msg, <span class="cpp-keyword">sizeof</span>( MSG ));
<span class="cpp-keyword">while</span>( msg.message != WM_QUIT )
{
<span class="cpp-keyword">if</span>( PeekMessage( &msg, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, PM_REMOVE ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
<span class="cpp-keyword">else</span>
{
updateGame();
renderGame();
Yield();
}
}
UnregisterClass( baseWindow->getClassName(), hInstance );
<span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;
}
<span class="cpp-keyword">void</span> getInput()
{
<span class="cpp-comment">// Code omitted…</span>
}
<span class="cpp-keyword">void</span> setupGame()
{
<span class="cpp-comment">// Code omitted…</span>
planet[ <span class="cpp-number">0</span> ] = <span class="cpp-keyword">new</span> Planet( &D3DDevice, <span class="cpp-literal">"Sun"</span>, <span class="cpp-number">20</span>.0f );
}
<span class="cpp-keyword">void</span> renderGame()
{
D3DXMATRIX matTranslate;
D3DDevice->Clear( <span class="cpp-number">0</span>, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span> ), <span class="cpp-number">1</span>.0f, <span class="cpp-number">0</span> );
getInput();
D3DDevice->BeginScene();
<span class="cpp-comment">// Sun </span>
D3DXMatrixTranslation( &matTranslate, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span> );
D3DDevice->SetTransform (D3DTS_WORLD, &matTranslate);
direct3DUtility->setMaterialColor( direct3DUtility->getColorYellow(), <span class="cpp-number">0</span>.1f, <span class="cpp-number">0</span>.2f, <span class="cpp-number">0</span>.3f, <span class="cpp-number">0</span>.5f );
D3DDevice->SetMaterial( &( direct3DUtility->getMaterial() ));
D3DDevice->SetTexture( <span class="cpp-number">0</span>, TxtureCube );
planet[ <span class="cpp-number">0</span> ]->getMesh_2()->DrawSubset( <span class="cpp-number">0</span> );
D3DDevice->EndScene();
D3DDevice->Present( NULL, NULL, NULL, NULL );
}
<span class="cpp-keyword">void</span> updateGame()
{
<span class="cpp-comment">// Code omitted… </span>
}
<span class="cpp-keyword">void</span> initStars()
{
<span class="cpp-comment">// Code omitted…</span>
<span class="cpp-comment">// Texture Section</span>
D3DXCreateTextureFromFile( D3DDevice, <span class="cpp-literal">"earthmap.jpg"</span>, &TxtureCube );
D3DDevice->SetSamplerState( <span class="cpp-number">0</span>, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
D3DDevice->SetSamplerState( <span class="cpp-number">0</span>, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
D3DDevice->SetSamplerState( <span class="cpp-number">0</span>, D3DSAMP_MIPFILTER, D3DTEXF_POINT );
}
<span class="cpp-keyword">void</span> cleanUp()
{
<span class="cpp-comment">// Code omitted…</span>
}
</pre></div><!–ENDSCRIPT–>
So I have left out alot of code that I think is not needed, and only included the important sections.
If any section is unclear or further clarification is needed please don't hesitate to ask and I will explain or add more code.
So in case you have forgotten my problem, I can't seem to get the texture to show when I ran the application.
<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - tenpoundbear on April 14, 2010 9:46:08 AM]<!–EDIT–></span><!–/EDIT–>