cube map generation with DDS texture Loader

Started by
1 comment, last by pcmaster 6 years ago

Hi, I'm implementing a simple 3D engine based on DirectX11. I'm trying to render a skybox with a cubemap on it and to do so I'm using DDS Texture Loader from DirectXTex library. I use texassemble to generate the cubemap (texture array of 6 textures) into a DDS file that I load at runtime. I generated a cube "dome" and sample the texture using the position vector of the vertex as the sample coordinates (so far so good), but I always get the same face of the cubemap mapped on the sky. As I look around I always get the same face (and it wobbles a bit if I move the camera). My code:   


 //Texture.cpp:

        Texture::Texture(const wchar_t *textureFilePath, const std::string &textureType) : mType(textureType)
        {
            //CreateDDSTextureFromFile(Game::GetInstance()->GetDevice(), Game::GetInstance()->GetDeviceContext(), textureFilePath, &mResource, &mShaderResourceView);
            CreateDDSTextureFromFileEx(Game::GetInstance()->GetDevice(), Game::GetInstance()->GetDeviceContext(), textureFilePath, 0, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, D3D11_RESOURCE_MISC_TEXTURECUBE, false, &mResource, &mShaderResourceView);
        }


    // SkyBox.cpp:
    
    void SkyBox::Draw()
    {
        // set cube map
        ID3D11ShaderResourceView *resource = mTexture.GetResource();
        Game::GetInstance()->GetDeviceContext()->PSSetShaderResources(0, 1, &resource);
    
        // set primitive topology
        Game::GetInstance()->GetDeviceContext()->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    
        mMesh.Bind();
        mMesh.Draw();
    }


    // Vertex Shader:

    cbuffer Transform : register(b0)
    {
        float4x4 viewProjectionMatrix;
    };
    
    float4 main(inout float3 pos : POSITION) : SV_POSITION
    {
        return mul(float4(pos, 1.0f), viewProjectionMatrix);
    }


    // Pixel Shader:

    SamplerState cubeSampler;
    TextureCube cubeMap;
    
    float4 main(in float3 pos : POSITION) : SV_TARGET
    {
        float4 color = cubeMap.Sample(cubeSampler, pos.xyz);
        return color;
    }

I tried both functions grom DDS loader but I keep getting the same result. All results I found on the web are about the old SDK toolkits, but I'm using the new DirectXTex lib.

Advertisement

In your code, I'd transform the vertex position with the (model+)view matrix only (instead of view+projection). If you sample at direction normalize(float3(1,1,1)), you will get the texel from the right-top-far corner (1,1,1) of your cubemap, which really represents a cube aligned with world. The direction (parameter to cubeMap.Sample) needs to be in the space of the cubemap, which can be the world-space, for example.

Maybe a better/cheaper idea: Why not render a full-screen-aligned quad (after all other geometry, in order to not rasterise pixels that will be covered by other geometry later), reconstruct the vector from camera to each pixel in WORLD SPACE COORDINATES, then use that vector as the cubemap coordinate?

This topic is closed to new replies.

Advertisement