Sign in to follow this  
Medo Mex

Spotlight

Recommended Posts

Medo Mex    891

blink.png

 

The spot light is only working when I set a bump map, but when I don't set bump map it doesn't work.

 

The following doesn't work:

float3 n = normalize(IN.Normal);

The following works very well:

float3 n = normalize(tex2D(normalMap, IN.UV).rgb * 2.0f - 1.0f);

And here is what I do in Vertex Shader:

OUT.Normal = mul(IN.Normal, (float3x3)worldInverseTransposeMatrix);
Edited by Medo3337

Share this post


Link to post
Share on other sites
Medo Mex    891

@Migi0027: I think it's set, I'm using Visual Studio 2010

 

Shader VS_INPUT and VS_OUTPUT:

struct VS_INPUT
{
   float3 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
   float4 Pos : SV_POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
   float4 worldPos : TEXCOORD6;
}

C++ Mesh Clone:

D3DVERTEXELEMENT9 g_vertexElements[] =
{
    {0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
    {0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
    D3DDECL_END()
};

mesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_MANAGED, g_vertexElements, m_d3dDevice, &mesh);

Share this post


Link to post
Share on other sites
belfegor    2835

1.

How is this possible to even compile?

struct VS_OUTPUT
{
   float4 Pos : SV_POSITION; // sv_position dx10+ semantic
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
   float4 worldPos : TEXCOORD6;
}

Usually when you output from vertex shader you would use TEXCOORD[n] semantics, and set it in order:

struct VS_OUTPUT
{
   float4 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : TEXCOORD1;
   float4 worldPos : TEXCOORD2;
}

2.

mesh->CloneMesh(D3DXMESH_32BIT | D3DXMESH_MANAGED, g_vertexElements, m_d3dDevice, &mesh);

If mesh doesn't have normals and you cloned it with new vertex declaration like that, you just reserved space for normals but they doesn't have correct values, so you need to compute them and update mesh VB.

To check if mesh have normals (or any other vertex component) you would use code like this:

D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];

        hr = dxMesh->GetDeclaration(decl);
        if(FAILED(hr))
           ...error

        bool haveNormals    = false;

        for(UINT i = 0; i < MAX_FVF_DECL_SIZE; ++i)
        {
            if(decl[i].Stream == 0xFF || decl[i].Stream != 0)
            {
                break;
            }

            if(decl[i].Usage == D3DDECLUSAGE_NORMAL)
            {
                haveNormals      = true;
                break;
            }
        }
 
    if(haveNormal)
        MessageBox(0, "YES!", 0, 0);
    else
        MessageBox(0, "NO!", 0, 0);

3. As to why spotlight isn't working, you are not giving enough information to help you.

Edited by belfegor

Share this post


Link to post
Share on other sites
Medo Mex    891

@belfegor: I tested now, the mesh do have normals before I clone it.

 

Even the spot light that appear when I add a bump map is a little weird when I'm too close:

[attachment=16965:spotlight.png]

 

When I don't set a bump map, here is what I get:

[attachment=16966:spotlight.png]

 

Pixel Shader Spotlight:

// NOTICE: "lightDirection" is the actual direction of the spot light

float3 lightDir = (lightPosition - IN.worldPos) / lightRange;
float atten = saturate(1.0f - dot(lightDir, lightDir));

float2 cosAngles = cos(float2(lightSpotOuterCone, lightSpotInnerCone) * 0.5f);
float spotDot = dot(normalize(-lightDir), normalize(lightDirection));
float spotEffect = smoothstep(cosAngles[0], cosAngles[1], spotDot);
    
atten *= spotEffect;

float3 viewDir = IN.worldPos - cameraPos;
float3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));

float3 n = normalize(IN.Normal);
float3 h = normalize(halfVector);
float3 l = normalize(lightDir);
    
float nDotL = saturate(dot(n, l));
float nDotH = saturate(dot(n, h));
float power = pow(nDotH, materialPower); //(nDotL == 0.0f) ? 0.0f : pow(nDotH, materialPower);   

float4 FinalDiffuse = materialDiffuse * lightDiffuse;
float4 FinalSpecular = materialSpecular * lightSpecular;

float4 colorF = (materialAmbient + globalAmbient) +
                (FinalDiffuse * nDotL * atten) + (FinalSpecular * power * atten);

colorF.a = 1.0;
return colorF * tex2D(colorMap, IN.UV);

 

Share this post


Link to post
Share on other sites
belfegor    2835

If you use "bump map(ing)" then your must have/compute binormal/tangent vectors in mesh VB.

And i am pretty sure that "viewDir" should be "to camera vector" so:

//float3 viewDir = IN.worldPos - cameraPos;
float3 viewDir = cameraPos - IN.worldPos;

Share this post


Link to post
Share on other sites
Medo Mex    891

@belfegor: The following resolved the weird light with bump map, now the bump map works perfectly with spot light

//float3 viewDir = IN.worldPos - cameraPos;
float3 viewDir = cameraPos - IN.worldPos;

However, If I don't set bump map, I don't see the spot light anymore.

 

If you use "bump map(ing)" then your must have/compute binormal/tangent vectors in mesh VB

 

Not sure how and how that could affect the spot light...

Share this post


Link to post
Share on other sites

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