Hey all. I'm trying to implement a sky box in DirectX. My current code lookes like this:
"Skybox.h"
//-----------------------------------------------------------------------------
// Class: Direct Graphics Sky Box
//
// Description: This is an implementation of a sky box in directX.
//
// Date: 15-05-2005
//-----------------------------------------------------------------------------
#include <d3dx9.h>
#include "Project.h"
#include "DirectGraphicsAdapter.h"
#include "DirectGraphicsSkyBox.h"
#define NUM_TRI 12
DirectGraphicsSkyBox::DirectGraphicsSkyBox( std::string inName ) : mTexture( 0 ),
mVB( 0 )
{
Init( inName );
}
DirectGraphicsSkyBox::~DirectGraphicsSkyBox()
{
Shutdown();
}
long DirectGraphicsSkyBox::Init( std::string inName )
{
mAdapter = ( DirectGraphicsAdapter * )gSystem->GetVisualSystem();
mDevice = mAdapter->GetDevice();
// Load cube texture.
D3DXCreateTextureFromFile( mDevice, inName.c_str(), &mTexture );
// Create our Cube.
Vertex theCube[] =
{
// Positive X.
{ D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), 1.0f, 0.0f },
// Negative X.
{ D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), 1.0f, 0.0f },
// Positive Y.
{ D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), 1.0f, 0.0f },
// Negative Y.
{ D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), 1.0f, 0.0f },
// Positive Z.
{ D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), 1.0f, 0.0f },
// Negative Z.
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), 0.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), 0.0f, 0.0f },
{ D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), 1.0f, 1.0f },
{ D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), 1.0f, 0.0f },
};
mDevice->CreateVertexBuffer( ( NUM_TRI * 3 ) * sizeof( Vertex ),
D3DUSAGE_WRITEONLY,
Vertex::FVF_Flags,
D3DPOOL_DEFAULT,
&mVB,
NULL );
// Lock the vertex buffer, and set up our cube vertices.
Vertex *theVBData;
mVB->Lock( 0,
( NUM_TRI * 3 ) * sizeof( Vertex ),
( void** )&theVBData,
D3DLOCK_DISCARD );
memcpy( theVBData, theCube, ( NUM_TRI * 3 ) * sizeof( Vertex ) );
mVB->Unlock();
return SYS_OK;
}
long DirectGraphicsSkyBox::Shutdown()
{
SAFE_RELEASE( mVB );
SAFE_RELEASE( mTexture );
return Clear();
}
long DirectGraphicsSkyBox::Clear()
{
mAdapter = 0;
mDevice = 0;
return SYS_OK;
}
long DirectGraphicsSkyBox::Render()
{
if( mAdapter && mDevice )
{
mDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
mDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
mDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
mDevice->SetRenderState( D3DRS_CLIPPING, FALSE );
mAdapter->ApplyMatrix();
mDevice->SetTexture( 0, mTexture );
mDevice->SetStreamSource( 0, mVB, 0, sizeof( Vertex ) );
mDevice->SetFVF( Vertex::FVF_Flags );
mDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, NUM_TRI );
mDevice->SetRenderState( D3DRS_CLIPPING, TRUE );
mDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
mDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
mDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
}
return SYS_OK;
}
It seemed perfect in theory, unfortunately when I go to rotate the camera (view matrix ), or the world it displays really wierdly. It seems to only draw one face at a time, and leaves a the background unrendered. Can anyone see what I'm doing wrong?