rate up
1
rate down
DirectX 11 - Cube Mapping (Skybox)
So, long story short I tried implementing a Skybox with a cube (not a sphere as shown in Tutorial) and I encountered a problem. +[http://www.braynzarsoft.net/image/100288][Problem] Here is the HLSL code: cbuffer cbPerObj { float4x4 WVP; float4x4 World; }; struct SkymapVSOutput { float4 Position : SV_Position; float3 Texture : TEXCOORD; }; TextureCube SkyMap; SamplerState Sampler; SkymapVSOutput SkymapVShader(float3 inPos : POSITION, float3 inNor : NORMAL, float2 inTex : TEXCOORD) { SkymapVSOutput output; output.Position = mul(float4(inPos,1.0f), WVP).xyww; output.Texture = inPos; return output; } float4 SkymapPShader(SkymapVSOutput input) : SV_Target { return SkyMap.Sample(Sampler, input.Texture); } Here is the class with the skybox class CSkymap { public: // Because i'm too lazy to implement a Render() method // Texture details ID3D11SamplerState *m_pTextureSampler; ID3D11ShaderResourceView *m_pTextureView; // Buffers ID3D11Buffer *m_pVertexBuffer; ID3D11Buffer *m_pIndexBuffer; // Shaders ID3D11VertexShader *m_pVertexShader; ID3D11PixelShader *m_pPixelShader; ID3D10Blob *m_pVSBuffer; ID3D10Blob *m_pPSBuffer; public: CSkymap(); ~CSkymap(); bool Initialize(ID3D11Device *Device); void Shutdown(); private: bool InitializeBuffers(ID3D11Device *Device); bool InitializeShaders(ID3D11Device *Device); bool InitializeTexture(ID3D11Device *Device); }; And here are the important methods inside CSkymap(InitializeBuffers(), InitializeShaders() and InitializeTexture()) Constructor just initialize everything with nullptr, Destructor is empty, Shutdown() releases evertything and Initialize() calls these methods. bool CSkymap::InitializeBuffers(ID3D11Device *Device) { HRESULT hr; // Vertices and indices Vertex vertices[] = { // Front Face Vertex(-1.0f, -1.0f, -1.0f), Vertex(-1.0f, 1.0f, -1.0f), Vertex(1.0f, 1.0f, -1.0f), Vertex(1.0f, -1.0f, -1.0f), // Back Face Vertex(-1.0f, -1.0f, 1.0f), Vertex(1.0f, -1.0f, 1.0f), Vertex(1.0f, 1.0f, 1.0f), Vertex(-1.0f, 1.0f, 1.0f), // Top Face Vertex(-1.0f, 1.0f, -1.0f), Vertex(-1.0f, 1.0f, 1.0f), Vertex(1.0f, 1.0f, 1.0f), Vertex(1.0f, 1.0f, -1.0f), // Bottom Face Vertex(-1.0f, -1.0f, -1.0f), Vertex(1.0f, -1.0f, -1.0f), Vertex(1.0f, -1.0f, 1.0f), Vertex(-1.0f, -1.0f, 1.0f), // Left Face Vertex(-1.0f, -1.0f, 1.0f), Vertex(-1.0f, 1.0f, 1.0f), Vertex(-1.0f, 1.0f, -1.0f), Vertex(-1.0f, -1.0f, -1.0f), // Right Face Vertex(1.0f, -1.0f, -1.0f), Vertex(1.0f, 1.0f, -1.0f), Vertex(1.0f, 1.0f, 1.0f), Vertex(1.0f, -1.0f, 1.0f), }; DWORD Indices[]= { // Front Face 0, 1, 2, 0, 2, 3, // Back Face 4, 5, 6, 4, 6, 7, // Top Face 8, 9, 10, 8, 10, 11, // Bottom Face 12, 13, 14, 12, 14, 15, // Left Face 16, 17, 18, 16, 18, 19, // Right Face 20, 21, 22, 20, 22, 23 }; // Describe and create buffers D3D11_BUFFER_DESC buffDesc = { 0 }; buffDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_VERTEX_BUFFER; buffDesc.ByteWidth = sizeof(vertices); buffDesc.Usage = D3D11_USAGE::D3D11_USAGE_DEFAULT; D3D11_SUBRESOURCE_DATA buffData = { 0 }; buffData.pSysMem = vertices; hr = Device->CreateBuffer(&buffDesc, &buffData, &m_pVertexBuffer); if (FAILED(hr)) return false; buffDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_INDEX_BUFFER; buffDesc.ByteWidth = sizeof(Indices); buffData.pSysMem = Indices; hr = Device->CreateBuffer(&buffDesc, &buffData, &m_pIndexBuffer); if (FAILED(hr)) return false; return true; } bool CSkymap::InitializeShaders(ID3D11Device *Device) { HRESULT hr; // Compile and create shaders hr = D3DX11CompileFromFile(L"SkymapShaders.fx", NULL, NULL, "SkymapVShader", "vs_4_0", NULL, NULL, NULL, &m_pVSBuffer, NULL, NULL); if (FAILED(hr)) return false; hr = D3DX11CompileFromFile(L"SkymapShaders.fx", NULL, NULL, "SkymapPShader", "ps_4_0", NULL, NULL, NULL, &m_pPSBuffer, NULL, NULL); if (FAILED(hr)) return false; hr = Device->CreateVertexShader(m_pVSBuffer->GetBufferPointer(), m_pVSBuffer->GetBufferSize(), NULL, &m_pVertexShader); if (FAILED(hr)) return false; hr = Device->CreatePixelShader(m_pPSBuffer->GetBufferPointer(), m_pPSBuffer->GetBufferSize(), NULL, &m_pPixelShader); if (FAILED(hr)) return false; return true; } bool CSkymap::InitializeTexture(ID3D11Device *Device) { HRESULT hr; D3DX11_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D11_RESOURCE_MISC_FLAG::D3D11_RESOURCE_MISC_TEXTURECUBE; ID3D11Texture2D *SphereTexture = nullptr; hr = D3DX11CreateTextureFromFile(Device, L"Skymap.dds", &loadInfo, NULL, (ID3D11Resource**)&SphereTexture, NULL); if (FAILED(hr)) return false; D3D11_TEXTURE2D_DESC texDesc = { 0 }; SphereTexture->GetDesc(&texDesc); D3D11_SHADER_RESOURCE_VIEW_DESC shadDesc; shadDesc.Format = texDesc.Format; shadDesc.ViewDimension = D3D11_SRV_DIMENSION::D3D11_SRV_DIMENSION_TEXTURECUBE; shadDesc.TextureCube.MipLevels = texDesc.MipLevels; shadDesc.TextureCube.MostDetailedMip = 0; hr = Device->CreateShaderResourceView(SphereTexture, &shadDesc, &m_pTextureView); SphereTexture->Release(); if (FAILED(hr)) return false; return true; } Is a Skybox with a cube possible? Thanks in advance!
Comments
where are your texture coordinates? try making the cube not move with the player, so you can move around the cube to make sure the cube looks right
on Aug 22 `16
iedoc
Have you used Visual Studio Graphics Diagnostics to check if your buffer's data is as expected? It's really good tool to debug with, you should try it out.
on Aug 22 `16
IamU4
I am so sorry for wasting your time ... Before trying to implement the cube mapping I changed the values of primitive topology and forget it set to D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, I know I'm very stupid ... I feel so bad for wasting your time, I AM SO SORRY! Thanks a lot for trying to help me! I AM SO SORRY!
on Aug 22 `16
Kavarna
That's ok, good that you fixed your problem :) Create a question again if you have a problem :)
on Aug 25 `16
IamU4
Sign in to answer!