Sign in to follow this  

Sky Box

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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);

// update
sky.setPosition(camera.getPosition());

Share this post


Link to post
Share on other sites

This topic is 4591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this