View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Spotlight

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1Medo3337  Members

Posted 23 July 2013 - 04:42 PM

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, 23 July 2013 - 04:44 PM.

#2Migi0027 (肉コーダ)  Members

Posted 23 July 2013 - 05:57 PM

Can you check if the IN.Normal is set?

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

#3Medo3337  Members

Posted 23 July 2013 - 06:55 PM

@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);

#4belfegor  Members

Posted 24 July 2013 - 02:33 AM

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, 24 July 2013 - 02:35 AM.

#5Medo3337  Members

Posted 24 July 2013 - 11:28 AM

@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:

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

// 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);

#6belfegor  Members

Posted 24 July 2013 - 12:07 PM

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;


#7Medo3337  Members

Posted 24 July 2013 - 12:26 PM

@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...

#8Medo3337  Members

Posted 25 July 2013 - 02:14 AM

Up, same problem, only works with bump map...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.