Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having some issue changing a basic lighting shader to take a normal texture and a diffuse texture for a model and then using the normal texture for lighting. The code is very simple and i can't see where i have gone wrong at all, am i not allowed to sample two textures with the same sampler? This is the output i am getting:

Final Output

Using the two textures:

Normal Texture Diffuse Map

THe basic shader code is:

Texture2D shaderTexture : register(t0);
Texture2D heightNormalTexture : register(t1);

    float4 PS(VOUT pIn) : SV_Target
    float4 diffuseColour = shaderTexture.Sample(SampleType, pIn.texCoord);
    float4 normal = heightNormalTexture.Sample(SampleType, pIn.texCoord);

    float3 litColour = float3(0.0f, 0.0f, 0.0f);

    for(int i = 0; i < 3; i++)
        SurfaceInfo sInfo = {pIn.posW,, diffuseColour, pIn.spec};
        [branch] switch(gLight[i].type)
            case 0: //Parallel
                litColour += ParallelLight(sInfo, gLight[i], gEyePosW);//
            case 1: //Point
                litColour += PointLight(sInfo, gLight[i], gEyePosW);
            case 2: //Spotlight
                //litColour += SpotLight(sInfo, gLight[i], gEyePosW);
    return float4(litColour, diffuseColour.a);

The lighting code seems to work just fine when i pass in the normals as part of the vertex buffer, but goes this odd mash-up of diffuse and normal when i try to use the normals from a texture. From what i can tell i am setting the texture right in C++.

    ID3D11ShaderResourceView *pixelShaderTextures[2];
pixelShaderTextures[0] = m_textureManager.GetTextureResourceView(std::string("TestHeightMapNormal"));
pixelShaderTextures[1] = m_textureManager.GetTextureResourceView(std::string("TestTexture"));

context->PSSetShaderResources(0, 2, pixelShaderTextures);

If you guys have any help/advice for me to resolve this, that would be great.

share|improve this question
up vote 4 down vote accepted

I've not touched DX11, but in the shader you seem to expect sampler 0 to be the diffuse, with sampler 1 to be the normal map. When you set the textures on the C++ side, you seem to have the slots reversed from what I can see.

share|improve this answer
That may be it :| My light isn't working still but is that because i have to multiply the normals by the world matrix? Like what is usually done in the vertex shader – Craig Jan 1 '12 at 5:59
You'll need to have the light, eye and normals in the same "space". Without transforming the normals by anything they'll be in "object" space while the light(s) and eye will be in world space. You could transform the normals into world space in the pixel shader, but it may be more efficient to pre-transform the eye and lights back into object space either in the vertex shader or outside of the draw call for efficiency, so you don't need to transform at all. Depending on where rendering bottlenecks occur it might not make much difference whatever you do if the pixel shader isn't overly complex. – Roger Perkins Jan 2 '12 at 7:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.