Sky Box

Started by
2 comments, last by eFoDay 18 years, 11 months ago
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
//-----------------------------------------------------------------------------

#ifndef DIRECT_GRAPHICS_SKYBOX_IMPL_INCLUDED
#define DIRECT_GRAPHICS_SKYBOX_IMPL_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif

#include "GraphicsObject.h"

class DirectGraphicsSkyBox : public GraphicsObject
{
public:
	DirectGraphicsSkyBox( std::string inName );
	~DirectGraphicsSkyBox();

protected:
	long Init( std::string inName );
	long Shutdown();
	long Clear();

	long Render();

	unsigned int GetType() { return Resource::RES_SKYBOX; }

	struct Vertex
	{
		D3DXVECTOR3 S;
		float u, v;

		enum FVF
		{
			FVF_Flags = D3DFVF_XYZ | D3DFVF_TEX1
		};
	};

	DirectGraphicsAdapter	*mAdapter;

	IDirect3DDevice9		*mDevice;
	IDirect3DTexture9		*mTexture;
	IDirect3DVertexBuffer9	*mVB;
};

#endif
"Skybox.cpp"
//-----------------------------------------------------------------------------
//	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?
---Anything is possible. Yep, even that.---
Advertisement
Have you tried:

1. storing the current view matrix
2. positioning the camera around the location of your player (or origin depending on your game)
3. rendering the skybox
4. restoring the original view matrix

You're already disabling depth buffer writes which is good

hth,
Learn about game programming!Games Programming in C++: Start to Finish
Yeah, that's what I do in my rendering function:

	if( mSkyBox )	{		gSystem->GetVisualSystem()->PushMatrix();		gSystem->GetVisualSystem()->LoadIdentity();		gSystem->GetVisualSystem()->Translate( mCamera->Position() );		mSkyBox->Render();		gSystem->GetVisualSystem()->PopMatrix();	}


It still draws weird when I rotate the camera...
---Anything is possible. Yep, even that.---
you may want to only disable z writing not the entire z buffer
not sure if this could cause problems

heres how I draw a skybox

// draw (before everything else)gfx.getDevice()->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);sky.setWorldMatrix();sky.draw();gfx.getDevice()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); // updatesky.setPosition(camera.getPosition());

This topic is closed to new replies.

Advertisement