# Skybox Problem

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

## Recommended Posts

I have been trying to generate a skybox by reading several articles. However, all I get is just black. I think it may be the way I am rendering it because of the position and texture coordinates to begin with. Does anyone see why? Skybox.cpp
#include "..\Camera.h"
#include "..\Managers\EffectManager.h"
#include "Skybox.h"
#define   FVF_FLAGS			D3DFVF_XYZ | D3DFVF_TEX1

struct TVertex
{
float x, y, z, w;		// 3D world position
float t1, t2, t3, t4;		// Texture coordinates
};

// 24 vertices = 6 faces (cube) * 4 vertices per face
//
// Example diagram of "front" quad
// The numbers are vertices
//
// 2  __________ 4
//   |\         |
//	 |  \       |
//   |    \     |
//   |      \   |
// 1 |        \ | 3
//	  ----------

TVertex g_SkyboxMesh[24] =
{
{-10.0f, -10.0f,  10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
{-10.0f,  10.0f,  10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{ 10.0f, -10.0f,  10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f},
{ 10.0f,  10.0f,  10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f},

{ 10.0f, -10.0f, -10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ 10.0f,  10.0f, -10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{-10.0f, -10.0f, -10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{-10.0f,  10.0f, -10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f },

{-10.0f, -10.0f, -10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{-10.0f,  10.0f, -10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{-10.0f, -10.0f,  10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{-10.0f,  10.0f,  10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f },

{ 10.0f, -10.0f,  10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ 10.0f,  10.0f,  10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ 10.0f, -10.0f, -10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 10.0f,  10.0f, -10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f },

{-10.0f,  10.0f,  10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{-10.0f,  10.0f, -10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ 10.0f,  10.0f,  10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 10.0f,  10.0f, -10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f },

{-10.0f, -10.0f, -10.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{-10.0f, -10.0f,  10.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ 10.0f, -10.0f, -10.0f,  0.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 10.0f, -10.0f,  10.0f,  0.0f, 1.0f, 0.0f, 0.0f, 0.0f }
};

CSkybox::CSkybox()
{
m_pVertexBuffer	= NULL;
m_tCubeTexture = 0;
}
CSkybox::~CSkybox()
{

}

bool CSkybox::BuildSkybox(LPDIRECT3DDEVICE9 lpD3Device)
{
HRESULT hRet;

D3DVERTEXELEMENT9 dvVertexElements[] =
{
{0,  0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
lpD3Device->CreateVertexDeclaration(dvVertexElements, &m_VD);

hRet = lpD3Device->CreateVertexBuffer( sizeof(TVertex) * 24,
0,
FVF_FLAGS,
D3DPOOL_MANAGED,
&m_pVertexBuffer,
NULL );

void *pVertices = NULL;

m_pVertexBuffer->Lock( 0, sizeof(TVertex) * 24, (void**)&pVertices, 0 );
memcpy( pVertices, g_SkyboxMesh, sizeof(TVertex) * 24 );
m_pVertexBuffer->Unlock();

D3DXCreateCubeTextureFromFile(lpD3Device, "Textures/Skybox/Skybox.dds", &m_tCubeTexture);
}
void CSkybox::RenderSkybox(LPDIRECT3DDEVICE9 lpD3Device)
{
CCamera *pCam = CCamera::GetInstance();
CEffectsManager *pEM = CEffectsManager::GetInstance();
CEffectBase *eSkyboxEffect = pEM->GetEffectByIndex(3);

eSkyboxEffect->BeginEffect("BasicCubeMap");
eSkyboxEffect->BeginPass(0);

lpD3Device->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(TVertex));
lpD3Device->SetVertexDeclaration(m_VD);
D3DXMATRIX mViewProj = pCam->GetViewMatrix() * pCam->GetProjectionMatrix();
eSkyboxEffect->SetMatrix("mWorldViewProj", mViewProj);
eSkyboxEffect->CommitChanges();

for ( ULONG i = 0; i < 6; ++i )
{
lpD3Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, i * 4, 2 );
}

eSkyboxEffect->EndPass();
eSkyboxEffect->EndEffect();
}
void CSkybox::ReleaseSkyboxMemory()
{
if(m_tCubeTexture)
{
m_tCubeTexture->Release();
m_tCubeTexture = NULL;
}

if(m_VD)
{
m_VD->Release();
m_VD = NULL;
}
}


float4x4 mWorldViewProj: WORLDVIEWPROJECTION;
texture tex0;

struct VS_INPUT
{
float4	Pos	: POSITION;
};

struct VS_OUTPUT
{
float4 Pos  : POSITION;
float4 vTex0: TEXCOORD0;
};

VS_OUTPUT VS(const VS_INPUT v)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float4 pos = mul(v.Pos, mWorldViewProj);
Out.Pos = pos.xyww;
Out.vTex0 = v.Pos.yzxw;

return Out;
}

sampler LinearSamp0 =
sampler_state
{
texture = <tex0>;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

float4 CubeMap(VS_OUTPUT In) : COLOR
{
// return the cube map texture
return tex3D(LinearSamp0, In.vTex0 );
}

technique BasicCubeMap
{
pass P0
{

CULLMODE = NONE;

AlphaBlendEnable = false;
AlphaTestEnable = false;

ZENABLE = TRUE;
ZWRITEENABLE = TRUE;
ZFUNC = always;

StencilEnable = true;
StencilFunc = always;
StencilPass = replace;
StencilRef = 0;

}
}



##### Share on other sites
I would get PIX on the case here. Capture a few relevant frames and then get the pixel history up and see if you can get some insight into what it really does - you've got a load of swizzles and remappings in there that it'd be all too easy to create something similar to the classic "off by 1" bug... [smile]

That said, do you really need to be using an actual cube of geometry? It's been quite a while since I last played with this, but I'm sure I did it using a single screen-aligned quad and a cubemap texture...

hth
Jack

##### Share on other sites
I do actually need more then a single quad. I have done a skybox before without a shader using all fixed function pipeline. Then when I tried to apply that knowledge along with others things I have read I come up with this problem. I have tried to take several debugging measures, however, I am just really unclear on what is wrong. I believe it is the way I set up the verticies.

##### Share on other sites
It doesn't appear that you set the texture anywhere.

##### Share on other sites
You were right buckeye, I am not sure how I overlooked that one. However, even with it set it does not look right, it just looks really zoomed in. I still think it may be somethign with the verticies but I am not figure out how to fix it. I set the texture like this

"eSkyboxEffect->m_pEffect->SetTexture("tex0", m_tCubeTexture);"

##### Share on other sites
I think the problem might be in your shader. Im not very experienced with shaders but i'll try to help :D
Why dont you have your vertex input structure take in a texcoord? You have one specified in your vertex declaration?

struct VS_INPUT{    float4 Pos  : POSITION;    float4 vTex0: TEXCOORD0;};

then you would change this line

Out.vTex0 = v.Pos.yzxw;

to

Out.vTex0 = v.vTex0;

Hope that halps

[Edited by - fitfool on August 3, 2008 1:46:03 PM]

##### Share on other sites
I'm not much of an HLSL person, so I don't claim to understand your position swizzles for the position, and for the tex coord generation. As Jack suggested, it looks like you're going to have to resort to PIX to see if your swizzle algorithms are what you intended.

Observation: You declare a vertex with tex coords, but never use them in the shader.

##### Share on other sites
Do you think it could be the fact that I set the pos.w in every vert to 0.0f? I know in the shader I am using the w at times. I think it may be that but I am not sure why .w should be.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×